@@ -2,7 +2,6 @@ package sentinel_test
22
33import (
44 "github.com/babelcloud/gbox-sdk-go/internal/encoding/json/sentinel"
5- "github.com/babelcloud/gbox-sdk-go/packages/param"
65 "reflect"
76 "slices"
87 "testing"
@@ -16,19 +15,25 @@ type Pair struct {
1615func TestNullSlice (t * testing.T ) {
1716 var nilSlice []int = nil
1817 var nonNilSlice []int = []int {1 , 2 , 3 }
19- var nullSlice []int = param .NullSlice [[] int ]()
18+ var nullSlice []int = sentinel .NullSlice [int ]()
2019
2120 cases := map [string ]Pair {
22- "nilSlice" : {sentinel .IsNull (nilSlice ), false },
23- "nullSlice" : {sentinel .IsNull (nullSlice ), true },
24- "newNullSlice" : {sentinel .IsNull ( param .NullSlice [[] int ]()), true },
21+ "nilSlice" : {sentinel .IsNullSlice (nilSlice ), false },
22+ "nullSlice" : {sentinel .IsNullSlice (nullSlice ), true },
23+ "newNullSlice" : {sentinel .IsNullSlice ( sentinel .NullSlice [int ]()), true },
2524 "lenNullSlice" : {len (nullSlice ) == 0 , true },
26- "nilSliceValue" : {sentinel .IsValueNull (reflect .ValueOf (nilSlice )), false },
27- "nullSliceValue" : {sentinel .IsValueNull (reflect .ValueOf (nullSlice )), true },
25+ "nilSliceValue" : {sentinel .IsValueNullSlice (reflect .ValueOf (nilSlice )), false },
26+ "nullSliceValue" : {sentinel .IsValueNullSlice (reflect .ValueOf (nullSlice )), true },
2827 "compareSlices" : {slices .Compare (nilSlice , nullSlice ) == 0 , true },
2928 "compareNonNilSlices" : {slices .Compare (nonNilSlice , nullSlice ) == 0 , false },
3029 }
3130
31+ nilSlice = append (nullSlice , 12 )
32+ cases ["append_result" ] = Pair {sentinel .IsNullSlice (nilSlice ), false }
33+ cases ["mutated_result" ] = Pair {sentinel .IsNullSlice (nullSlice ), true }
34+ cases ["append_result_len" ] = Pair {len (nilSlice ) == 1 , true }
35+ cases ["append_null_slice_len" ] = Pair {len (nullSlice ) == 0 , true }
36+
3237 for name , c := range cases {
3338 t .Run (name , func (t * testing.T ) {
3439 got , want := c .got , c .want
@@ -39,20 +44,37 @@ func TestNullSlice(t *testing.T) {
3944 }
4045}
4146
42- func TestNullMap (t * testing.T ) {
43- var nilMap map [string ]int = nil
44- var nonNilMap map [string ]int = map [string ]int {"a" : 1 , "b" : 2 }
45- var nullMap map [string ]int = param .NullMap [map [string ]int ]()
47+ func TestNullPtr (t * testing.T ) {
48+ var s * string = nil
49+ var i * int = nil
50+ var slice * []int = nil
51+
52+ var nullptrStr * string = sentinel .NullPtr [string ]()
53+ var nullptrInt * int = sentinel .NullPtr [int ]()
54+ var nullptrSlice * []int = sentinel .NullPtr [[]int ]()
55+
56+ if * nullptrStr != "" {
57+ t .Errorf ("Failed to safely deref" )
58+ }
59+ if * nullptrInt != 0 {
60+ t .Errorf ("Failed to safely deref" )
61+ }
62+ if len (* nullptrSlice ) != 0 {
63+ t .Errorf ("Failed to safely deref" )
64+ }
4665
4766 cases := map [string ]Pair {
48- "nilMap" : {sentinel .IsNull (nilMap ), false },
49- "nullMap" : {sentinel .IsNull (nullMap ), true },
50- "newNullMap" : {sentinel .IsNull (param .NullMap [map [string ]int ]()), true },
51- "lenNullMap" : {len (nullMap ) == 0 , true },
52- "nilMapValue" : {sentinel .IsValueNull (reflect .ValueOf (nilMap )), false },
53- "nullMapValue" : {sentinel .IsValueNull (reflect .ValueOf (nullMap )), true },
54- "compareMaps" : {reflect .DeepEqual (nilMap , nullMap ), false },
55- "compareNonNilMaps" : {reflect .DeepEqual (nonNilMap , nullMap ), false },
67+ "nilStr" : {sentinel .IsNullPtr (s ), false },
68+ "nullStr" : {sentinel .IsNullPtr (nullptrStr ), true },
69+
70+ "nilInt" : {sentinel .IsNullPtr (i ), false },
71+ "nullInt" : {sentinel .IsNullPtr (nullptrInt ), true },
72+
73+ "nilSlice" : {sentinel .IsNullPtr (slice ), false },
74+ "nullSlice" : {sentinel .IsNullPtr (nullptrSlice ), true },
75+
76+ "nilValuePtr" : {sentinel .IsValueNullPtr (reflect .ValueOf (i )), false },
77+ "nullValuePtr" : {sentinel .IsValueNullPtr (reflect .ValueOf (nullptrInt )), true },
5678 }
5779
5880 for name , test := range cases {
@@ -64,28 +86,3 @@ func TestNullMap(t *testing.T) {
6486 })
6587 }
6688}
67-
68- func TestIsNullRepeated (t * testing.T ) {
69- // Test for slices
70- nullSlice1 := param .NullSlice [[]int ]()
71- nullSlice2 := param .NullSlice [[]int ]()
72- if ! sentinel .IsNull (nullSlice1 ) {
73- t .Errorf ("IsNull(nullSlice1) = false, want true" )
74- }
75- if ! sentinel .IsNull (nullSlice2 ) {
76- t .Errorf ("IsNull(nullSlice2) = false, want true" )
77- }
78- if ! sentinel .IsNull (nullSlice1 ) || ! sentinel .IsNull (nullSlice2 ) {
79- t .Errorf ("IsNull should return true for all NullSlice instances" )
80- }
81-
82- // Test for maps
83- nullMap1 := param .NullMap [map [string ]int ]()
84- nullMap2 := param .NullMap [map [string ]int ]()
85- if ! sentinel .IsNull (nullMap1 ) {
86- t .Errorf ("IsNull(nullMap1) = false, want true" )
87- }
88- if ! sentinel .IsNull (nullMap2 ) {
89- t .Errorf ("IsNull(nullMap2) = false, want true" )
90- }
91- }
0 commit comments