Debian Codegraph @

common/types/null_test.go

raw symbols SCIP
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package types
16
17 import (
18 "errors"
19 "fmt"
20 "reflect"
21 "testing"
22
23 "google.golang.org/protobuf/proto"
24
25 proto3pb "github.com/google/cel-go/test/proto3pb"
26 dynamicpb "google.golang.org/protobuf/types/dynamicpb"
27 anypb "google.golang.org/protobuf/types/known/anypb"
28 structpb "google.golang.org/protobuf/types/known/structpb"
29 )
30
31 func TestNullConvertToNative(t *testing.T) {
32 tests := []struct {
33 goType reflect.Type
34 out any
35 err error
36 }{
37 {
38 goType: JSONValueType,
39 out: structpb.NewNullValue(),
40 },
41 {
42 goType: JSONNullType,
43 out: structpb.NullValue_NULL_VALUE,
44 },
45 {
46 goType: anyValueType,
47 out: testPackAny(t, structpb.NewNullValue()),
48 },
49 {
50 goType: reflect.TypeOf(NullValue),
51 out: NullValue,
52 },
53 {goType: boolWrapperType},
54 {goType: byteWrapperType},
55 {goType: doubleWrapperType},
56 {goType: floatWrapperType},
57 {goType: int32WrapperType},
58 {goType: int64WrapperType},
59 {goType: stringWrapperType},
60 {goType: uint32WrapperType},
61 {goType: uint64WrapperType},
62 {goType: durationValueType},
63 {goType: timestampValueType},
64 {goType: reflect.TypeOf((*dynamicpb.Message)(nil))},
65 {goType: reflect.TypeOf(&proto3pb.TestAllTypes{})},
66 {goType: reflect.TypeOf((*proto3pb.TestAllTypes)(nil))},
67 {
68 goType: reflect.TypeOf(1),
69 err: errors.New("type conversion error from 'null_type' to 'int'"),
70 },
71 {
72 goType: JSONListType,
73 err: errors.New("type conversion error from 'null_type' to '*structpb.ListValue'"),
74 },
75 {
76 goType: JSONStructType,
77 err: errors.New("type conversion error from 'null_type' to '*structpb.Struct'"),
78 },
79 }
80
81 for i, tst := range tests {
82 tc := tst
83 t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) {
84 out, err := NullValue.ConvertToNative(tc.goType)
85 if err != nil {
86 if tc.err == nil {
87 t.Fatalf("NullValue.ConvertToType(%v) failed: %v", tc.goType, err)
88 }
89 if tc.err.Error() != err.Error() {
90 t.Errorf("NullValue.ConvertToType(%v) got error %v, wanted error %v", tc.goType, err, tc.err)
91 }
92 return
93 }
94 pbMsg, isPB := out.(proto.Message)
95 if (isPB && !proto.Equal(pbMsg, tc.out.(proto.Message))) || (!isPB && out != tc.out) {
96 t.Errorf("NullValue.ConvertToNative(%v) got %v, wanted %v", tc.goType, pbMsg, tc.out)
97 }
98 })
99 }
100 }
101
102 func TestNullConvertToType(t *testing.T) {
103 if !NullValue.ConvertToType(NullType).Equal(NullValue).(Bool) {
104 t.Error("Failed to get NullType of NullValue.")
105 }
106
107 if !NullValue.ConvertToType(StringType).Equal(String("null")).(Bool) {
108 t.Error("Failed to get StringType of NullValue.")
109 }
110 if !NullValue.ConvertToType(TypeType).Equal(NullType).(Bool) {
111 t.Error("Failed to convert NullValue to type.")
112 }
113 }
114
115 func TestNullEqual(t *testing.T) {
116 if !NullValue.Equal(NullValue).(Bool) {
117 t.Error("NullValue does not equal to itself.")
118 }
119 }
120
121 func TestNullIsZeroValue(t *testing.T) {
122 if !NullValue.IsZeroValue() {
123 t.Error("NullValue.IsZeroValue() returned false, wanted true")
124 }
125 }
126
127 func TestNullType(t *testing.T) {
128 if NullValue.Type() != NullType {
129 t.Error("NullValue gets incorrect type.")
130 }
131 }
132
133 func TestNullValue(t *testing.T) {
134 if NullValue.Value() != structpb.NullValue_NULL_VALUE {
135 t.Error("NullValue gets incorrect value.")
136 }
137 }
138
139 func testPackAny(t *testing.T, val proto.Message) *anypb.Any {
140 t.Helper()
141 out, err := anypb.New(val)
142 if err != nil {
143 t.Fatalf("anypb.New(%v) failed: %v", val, err)
144 }
145 return out
146 }