@@ -3,54 +3,72 @@ package testing
33import (
44 "testing"
55
6- "github.com/stretchr/testify/assert "
6+ "github.com/stretchr/testify/suite "
77
88 contractstesting "github.com/goravel/framework/contracts/testing"
99)
1010
11- func TestNewAssertableJSON (t * testing.T ) {
11+ type AssertableJsonTestSuite struct {
12+ suite.Suite
13+ }
14+
15+ func TestAssertableJsonTestSuite (t * testing.T ) {
16+ json = & testJson {}
17+
18+ suite .Run (t , new (AssertableJsonTestSuite ))
19+ }
20+
21+ func (s * AssertableJsonTestSuite ) SetupTest () {
22+
23+ }
24+
25+ func (s * AssertableJsonTestSuite ) TearDownSuite () {
26+ json = nil
27+ }
28+
29+ func (s * AssertableJsonTestSuite ) TestNewAssertableJSON () {
1230 validJSON := `{"key1": "value1", "key2": [1, 2, 3]}`
1331 invalidJSON := `{"key1": "value1", "key2": [1, 2, 3]`
1432
15- assertable , err := NewAssertableJSON (t , validJSON )
16- assert .NoError (t , err )
17- assert .NotNil (t , assertable )
33+ assertable , err := NewAssertableJSON (s . T () , validJSON )
34+ s .NoError (err )
35+ s .NotNil (assertable )
1836
19- assertable , err = NewAssertableJSON (t , invalidJSON )
20- assert .Error (t , err )
21- assert .Nil (t , assertable )
37+ assertable , err = NewAssertableJSON (s . T () , invalidJSON )
38+ s .Error (err )
39+ s .Nil (assertable )
2240}
2341
24- func TestCount ( t * testing. T ) {
42+ func ( s * AssertableJsonTestSuite ) TestCount ( ) {
2543 jsonStr := `{"items": [1, 2, 3], "otherKey": "value"}`
26- assertable , err := NewAssertableJSON (t , jsonStr )
27- assert .NoError (t , err )
44+ assertable , err := NewAssertableJSON (s . T () , jsonStr )
45+ s .NoError (err )
2846
2947 assertable .Count ("items" , 3 )
3048
3149 //assertable.Count("items", 4)
3250}
3351
34- func TestHas ( t * testing. T ) {
52+ func ( s * AssertableJsonTestSuite ) TestHas ( ) {
3553 jsonStr := `{
3654 "key1": "value1",
3755 "key2": [1, 2, 3],
3856 "nested": {"deep": "value"},
3957 "nullKey": null
4058 }`
41- assertable , err := NewAssertableJSON (t , jsonStr )
42- assert .NoError (t , err )
59+ assertable , err := NewAssertableJSON (s . T () , jsonStr )
60+ s .NoError (err )
4361
4462 assertable .Has ("key1" )
4563 assertable .Has ("nullKey" )
4664
4765 //assertable.Has("nonExistingKey")
4866}
4967
50- func TestHasAll ( t * testing. T ) {
68+ func ( s * AssertableJsonTestSuite ) TestHasAll ( ) {
5169 jsonStr := `{"key1": "value1", "key2": [1, 2, 3]}`
52- assertable , err := NewAssertableJSON (t , jsonStr )
53- assert .NoError (t , err )
70+ assertable , err := NewAssertableJSON (s . T () , jsonStr )
71+ s .NoError (err )
5472
5573 // Test all keys exist
5674 assertable .HasAll ([]string {"key1" , "key2" })
@@ -59,10 +77,10 @@ func TestHasAll(t *testing.T) {
5977 //assertable.HasAll([]string{"key1", "nonExistingKey"})
6078}
6179
62- func TestHasAny ( t * testing. T ) {
80+ func ( s * AssertableJsonTestSuite ) TestHasAny ( ) {
6381 jsonStr := `{"key1": "value1", "key2": [1, 2, 3]}`
64- assertable , err := NewAssertableJSON (t , jsonStr )
65- assert .NoError (t , err )
82+ assertable , err := NewAssertableJSON (s . T () , jsonStr )
83+ s .NoError (err )
6684
6785 // Test at least one key exists
6886 assertable .HasAny ([]string {"key1" , "key2" })
@@ -71,10 +89,10 @@ func TestHasAny(t *testing.T) {
7189 //assertable.HasAny([]string{"nonExistingKey1", "nonExistingKey2"})
7290}
7391
74- func TestMissing ( t * testing. T ) {
92+ func ( s * AssertableJsonTestSuite ) TestMissing ( ) {
7593 jsonStr := `{"key1": "value1"}`
76- assertable , err := NewAssertableJSON (t , jsonStr )
77- assert .NoError (t , err )
94+ assertable , err := NewAssertableJSON (s . T () , jsonStr )
95+ s .NoError (err )
7896
7997 // Test key is missing
8098 assertable .Missing ("nonExistingKey" )
@@ -83,10 +101,10 @@ func TestMissing(t *testing.T) {
83101 //assertable.Missing("key1")
84102}
85103
86- func TestMissingAll ( t * testing. T ) {
104+ func ( s * AssertableJsonTestSuite ) TestMissingAll ( ) {
87105 jsonStr := `{"key1": "value1"}`
88- assertable , err := NewAssertableJSON (t , jsonStr )
89- assert .NoError (t , err )
106+ assertable , err := NewAssertableJSON (s . T () , jsonStr )
107+ s .NoError (err )
90108
91109 // Test all keys are missing
92110 assertable .MissingAll ([]string {"nonExistingKey1" , "nonExistingKey2" })
@@ -95,7 +113,7 @@ func TestMissingAll(t *testing.T) {
95113 //assertable.MissingAll([]string{"key1"})
96114}
97115
98- func TestWhere ( t * testing. T ) {
116+ func ( s * AssertableJsonTestSuite ) TestWhere ( ) {
99117 jsonStr := `{
100118 "key1": "value1",
101119 "intKey": 42,
@@ -104,8 +122,8 @@ func TestWhere(t *testing.T) {
104122 "objKey": {"nested": "value"},
105123 "arrayKey": [1, 2, 3]
106124 }`
107- assertable , err := NewAssertableJSON (t , jsonStr )
108- assert .NoError (t , err )
125+ assertable , err := NewAssertableJSON (s . T () , jsonStr )
126+ s .NoError (err )
109127
110128 // Test correct value
111129 assertable .Where ("key1" , "value1" )
@@ -123,10 +141,10 @@ func TestWhere(t *testing.T) {
123141 //assertable.Where("key1", "wrongValue")
124142}
125143
126- func TestWhereNot ( t * testing. T ) {
144+ func ( s * AssertableJsonTestSuite ) TestWhereNot ( ) {
127145 jsonStr := `{"key1": "value1", "key2": [1, 2, 3]}`
128- assertable , err := NewAssertableJSON (t , jsonStr )
129- assert .NoError (t , err )
146+ assertable , err := NewAssertableJSON (s . T () , jsonStr )
147+ s .NoError (err )
130148
131149 // Test value is not as expected
132150 assertable .WhereNot ("key1" , "wrongValue" )
@@ -135,11 +153,11 @@ func TestWhereNot(t *testing.T) {
135153 //assertable.WhereNot("key1", "value1")
136154}
137155
138- func TestFirst ( t * testing. T ) {
156+ func ( s * AssertableJsonTestSuite ) TestFirst ( ) {
139157 jsonStr := `{"items": [{"id": 1}, {"id": 2}]}`
140158
141- assertable , err := NewAssertableJSON (t , jsonStr )
142- assert .NoError (t , err )
159+ assertable , err := NewAssertableJSON (s . T () , jsonStr )
160+ s .NoError (err )
143161
144162 // Test fetching the first item
145163 assertable .First ("items" , func (item contractstesting.AssertableJSON ) {
@@ -155,11 +173,11 @@ func TestFirst(t *testing.T) {
155173 //emptyAssertable.First("items", func(item contractstesting.AssertableJSON) {})
156174}
157175
158- func TestHasWithScope ( t * testing. T ) {
176+ func ( s * AssertableJsonTestSuite ) TestHasWithScope ( ) {
159177 jsonStr := `{"items": [{"id": 1}, {"id": 2}]}`
160178
161- assertable , err := NewAssertableJSON (t , jsonStr )
162- assert .NoError (t , err )
179+ assertable , err := NewAssertableJSON (s . T () , jsonStr )
180+ s .NoError (err )
163181
164182 // Test has with correct length
165183 assertable .HasWithScope ("items" , 2 , func (item contractstesting.AssertableJSON ) {
@@ -173,34 +191,35 @@ func TestHasWithScope(t *testing.T) {
173191 //assertable.HasWithScope("nonExistingKey", 0, func(item contractstesting.AssertableJSON) {})
174192}
175193
176- func TestEach ( t * testing. T ) {
194+ func ( s * AssertableJsonTestSuite ) TestEach ( ) {
177195 jsonStr := `{
178196 "items": [{"id": 1}, {"id": 2}],
179197 "mixedTypes": [42, "string", {"key": "value"}],
180198 "nonArray": "value"
181199 }`
182200
183- assertable , err := NewAssertableJSON (t , jsonStr )
184- assert .NoError (t , err )
201+ assertable , err := NewAssertableJSON (s . T () , jsonStr )
202+ s .NoError (err )
185203
186204 // Test iterating over each item
187205 callCount := 0
188206 assertable .Each ("items" , func (item contractstesting.AssertableJSON ) {
189207 item .Where ("id" , float64 (callCount + 1 ))
190208 callCount ++
191209 })
192- assert .Equal (t , 2 , callCount )
210+ s .Equal (2 , callCount )
193211
194212 // Test with a non-existing key
195213 //assertable.Each("nonExistingKey", func(item contractstesting.AssertableJSON) {})
196214
197215 // Test with an empty array
198216 emptyJsonStr := `{"items": []}`
199- emptyAssertable , err := NewAssertableJSON (t , emptyJsonStr )
200- assert .NoError (t , err )
217+ emptyAssertable , err := NewAssertableJSON (s .T (), emptyJsonStr )
218+ s .NoError (err )
219+
201220 emptyCallCount := 0
202221 emptyAssertable .Each ("items" , func (item contractstesting.AssertableJSON ) {
203222 emptyCallCount ++
204223 })
205- assert .Equal (t , 0 , emptyCallCount )
224+ s .Equal (0 , emptyCallCount )
206225}
0 commit comments