Skip to content

Commit 94e8c2d

Browse files
Copilotziflex
andcommitted
Add comprehensive tests for casting and binary operations
Co-authored-by: ziflex <1607148+ziflex@users.noreply.github.com>
1 parent 3dd1e8d commit 94e8c2d

2 files changed

Lines changed: 427 additions & 0 deletions

File tree

pkg/runtime/binary_test.go

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package runtime_test
2+
3+
import (
4+
"context"
5+
"errors"
6+
"strings"
7+
"testing"
8+
9+
. "github.com/smartystreets/goconvey/convey"
10+
11+
"github.com/MontFerret/ferret/pkg/runtime"
12+
)
13+
14+
func TestBinary(t *testing.T) {
15+
ctx := context.Background()
16+
17+
Convey("Binary Operations", t, func() {
18+
19+
Convey("NewBinary", func() {
20+
Convey("Should create Binary from byte slice", func() {
21+
data := []byte("hello world")
22+
binary := runtime.NewBinary(data)
23+
So(binary, ShouldNotBeNil)
24+
So(binary.Unwrap(), ShouldResemble, data)
25+
})
26+
27+
Convey("Should create empty Binary", func() {
28+
binary := runtime.NewBinary([]byte{})
29+
So(binary, ShouldNotBeNil)
30+
So(binary.Unwrap(), ShouldResemble, []byte{})
31+
})
32+
})
33+
34+
Convey("NewBinaryFrom", func() {
35+
Convey("Should create Binary from reader", func() {
36+
data := "hello world"
37+
reader := strings.NewReader(data)
38+
39+
binary, err := runtime.NewBinaryFrom(reader)
40+
So(err, ShouldBeNil)
41+
So(binary, ShouldNotBeNil)
42+
So(binary.Unwrap(), ShouldResemble, []byte(data))
43+
})
44+
45+
Convey("Should create empty Binary from empty reader", func() {
46+
reader := strings.NewReader("")
47+
48+
binary, err := runtime.NewBinaryFrom(reader)
49+
So(err, ShouldBeNil)
50+
So(binary, ShouldNotBeNil)
51+
So(binary.Unwrap(), ShouldResemble, []byte{})
52+
})
53+
54+
Convey("Should handle reader errors", func() {
55+
// Use a buffer and close it to simulate an error
56+
reader := &ErrorReader{}
57+
58+
_, err := runtime.NewBinaryFrom(reader)
59+
So(err, ShouldNotBeNil)
60+
})
61+
})
62+
63+
Convey(".MarshalJSON", func() {
64+
Convey("Should marshal to base64 encoded JSON string", func() {
65+
data := []byte("hello")
66+
binary := runtime.NewBinary(data)
67+
68+
marshaled, err := binary.MarshalJSON()
69+
So(err, ShouldBeNil)
70+
So(string(marshaled), ShouldContainSubstring, "aGVsbG8=") // base64 for "hello"
71+
})
72+
73+
Convey("Should marshal empty binary", func() {
74+
binary := runtime.NewBinary([]byte{})
75+
76+
marshaled, err := binary.MarshalJSON()
77+
So(err, ShouldBeNil)
78+
So(string(marshaled), ShouldNotBeEmpty)
79+
})
80+
})
81+
82+
Convey(".String", func() {
83+
Convey("Should return string representation", func() {
84+
data := []byte("hello")
85+
binary := runtime.NewBinary(data)
86+
87+
str := binary.String()
88+
So(str, ShouldNotBeEmpty)
89+
})
90+
})
91+
92+
Convey(".Hash", func() {
93+
Convey("Should calculate hash consistently", func() {
94+
data := []byte("hello")
95+
binary1 := runtime.NewBinary(data)
96+
binary2 := runtime.NewBinary(data)
97+
98+
hash1 := binary1.Hash()
99+
hash2 := binary2.Hash()
100+
So(hash1, ShouldEqual, hash2)
101+
})
102+
103+
Convey("Should calculate different hash for different data", func() {
104+
binary1 := runtime.NewBinary([]byte("hello"))
105+
binary2 := runtime.NewBinary([]byte("world"))
106+
107+
hash1 := binary1.Hash()
108+
hash2 := binary2.Hash()
109+
So(hash1, ShouldNotEqual, hash2)
110+
})
111+
})
112+
113+
Convey(".Copy", func() {
114+
Convey("Should create independent copy", func() {
115+
data := []byte("hello")
116+
original := runtime.NewBinary(data)
117+
118+
copied := original.Copy()
119+
copyBinary := copied.(runtime.Binary)
120+
121+
So(copyBinary.Unwrap(), ShouldResemble, original.Unwrap())
122+
123+
// Modifying original data shouldn't affect copy since Copy should create new slice
124+
originalBytes := original.Unwrap().([]byte)
125+
if len(originalBytes) > 0 {
126+
originalBytes[0] = 'x' // This should not affect the copy
127+
}
128+
})
129+
})
130+
131+
Convey(".Length", func() {
132+
Convey("Should return correct length", func() {
133+
data := []byte("hello")
134+
binary := runtime.NewBinary(data)
135+
136+
length, err := binary.Length(ctx)
137+
So(err, ShouldBeNil)
138+
So(length, ShouldEqual, runtime.NewInt(5))
139+
})
140+
141+
Convey("Should return zero for empty binary", func() {
142+
binary := runtime.NewBinary([]byte{})
143+
144+
length, err := binary.Length(ctx)
145+
So(err, ShouldBeNil)
146+
So(length, ShouldEqual, runtime.NewInt(0))
147+
})
148+
})
149+
150+
Convey(".Compare", func() {
151+
Convey("Should return 0 for equal binaries", func() {
152+
data := []byte("hello")
153+
binary1 := runtime.NewBinary(data)
154+
binary2 := runtime.NewBinary(data)
155+
156+
result := binary1.Compare(binary2)
157+
So(result, ShouldEqual, 0)
158+
})
159+
160+
Convey("Should return non-zero for different binaries", func() {
161+
binary1 := runtime.NewBinary([]byte("hello"))
162+
binary2 := runtime.NewBinary([]byte("world"))
163+
164+
result := binary1.Compare(binary2)
165+
So(result, ShouldNotEqual, 0)
166+
})
167+
168+
Convey("Should handle comparison with non-binary types", func() {
169+
binary := runtime.NewBinary([]byte("hello"))
170+
171+
result := binary.Compare(runtime.NewString("hello"))
172+
So(result, ShouldNotEqual, 0) // Different types
173+
})
174+
})
175+
})
176+
}
177+
178+
// ErrorReader is a helper for testing error conditions
179+
type ErrorReader struct{}
180+
181+
func (r *ErrorReader) Read(p []byte) (n int, err error) {
182+
return 0, errors.New("simulated read error")
183+
}

0 commit comments

Comments
 (0)