github.com/anthropics/anthropic-sdk-go@v1.70.1/internal/encoding/json/sentinel/sentinel_test.go (about) 1 package sentinel_test 2 3 import ( 4 "github.com/anthropics/anthropic-sdk-go/internal/encoding/json/sentinel" 5 "github.com/anthropics/anthropic-sdk-go/packages/param" 6 "reflect" 7 "slices" 8 "testing" 9 ) 10 11 type Pair struct { 12 got bool 13 want bool 14 } 15 16 func TestNullSlice(t *testing.T) { 17 var nilSlice []int = nil 18 var nonNilSlice []int = []int{1, 2, 3} 19 var nullSlice []int = param.NullSlice[[]int]() 20 21 cases := map[string]Pair{ 22 "nilSlice": {sentinel.IsNull(nilSlice), false}, 23 "nullSlice": {sentinel.IsNull(nullSlice), true}, 24 "newNullSlice": {sentinel.IsNull(param.NullSlice[[]int]()), true}, 25 "lenNullSlice": {len(nullSlice) == 0, true}, 26 "nilSliceValue": {sentinel.IsValueNull(reflect.ValueOf(nilSlice)), false}, 27 "nullSliceValue": {sentinel.IsValueNull(reflect.ValueOf(nullSlice)), true}, 28 "compareSlices": {slices.Compare(nilSlice, nullSlice) == 0, true}, 29 "compareNonNilSlices": {slices.Compare(nonNilSlice, nullSlice) == 0, false}, 30 } 31 32 for name, c := range cases { 33 t.Run(name, func(t *testing.T) { 34 got, want := c.got, c.want 35 if got != want { 36 t.Errorf("got %v, want %v", got, want) 37 } 38 }) 39 } 40 } 41 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]() 46 47 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}, 56 } 57 58 for name, test := range cases { 59 t.Run(name, func(t *testing.T) { 60 got, want := test.got, test.want 61 if got != want { 62 t.Errorf("got %v, want %v", got, want) 63 } 64 }) 65 } 66 } 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 }