Debian Codegraph @

common/types/bool_test.go

raw
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 "reflect"
19 "testing"
20
21 "google.golang.org/protobuf/proto"
22
23 anypb "google.golang.org/protobuf/types/known/anypb"
24 structpb "google.golang.org/protobuf/types/known/structpb"
25 wrapperspb "google.golang.org/protobuf/types/known/wrapperspb"
26 )
27
28 func TestBoolCompare(t *testing.T) {
29 if False.Compare(True).(Int) != IntNegOne {
30 t.Error("False was not less than true")
31 }
32 if True.Compare(False).(Int) != IntOne {
33 t.Error("True was not greater than false")
34 }
35 if True.Compare(True).(Int) != IntZero {
36 t.Error("True was not equal to true")
37 }
38 if False.Compare(False).(Int) != IntZero {
39 t.Error("False was not equal to false")
40 }
41 if !IsError(True.Compare(Uint(0))) {
42 t.Error("Was able to compare uncomparable types.")
43 }
44 }
45
46 func TestBoolConvertToNative_Any(t *testing.T) {
47 val, err := True.ConvertToNative(anyValueType)
48 if err != nil {
49 t.Error(err)
50 }
51 pbVal, err := anypb.New(wrapperspb.Bool(true))
52 if err != nil {
53 t.Error(err)
54 }
55 if !proto.Equal(val.(proto.Message), pbVal) {
56 t.Error("Error during conversion to protobuf.Any", val)
57 }
58 }
59
60 func TestBoolConvertToNative_Bool(t *testing.T) {
61 refType := reflect.TypeOf(true)
62 val, err := True.ConvertToNative(refType)
63 if err != nil {
64 t.Error(err)
65 } else if !val.(bool) {
66 t.Error("Error during conversion to bool", val)
67 }
68 }
69
70 func TestBoolConvertToNative_Error(t *testing.T) {
71 refType := reflect.TypeOf("")
72 val, err := True.ConvertToNative(refType)
73 if err == nil {
74 t.Errorf("Got '%v', expected error", val)
75 }
76 }
77
78 func TestBoolConvertToNative_Json(t *testing.T) {
79 val, err := True.ConvertToNative(JSONValueType)
80 pbVal := &structpb.Value{Kind: &structpb.Value_BoolValue{BoolValue: true}}
81 if err != nil {
82 t.Error(err)
83 } else if !proto.Equal(val.(proto.Message), pbVal) {
84 t.Error("Error during conversion to json Value type", val)
85 }
86 }
87
88 func TestBoolConvertToNative_Ptr(t *testing.T) {
89 ptrType := true
90 refType := reflect.TypeOf(&ptrType)
91 val, err := True.ConvertToNative(refType)
92 if err != nil {
93 t.Error(err)
94 } else if !*val.(*bool) {
95 t.Error("Error during conversion to *bool", val)
96 }
97 }
98
99 func TestBoolConvertToNative_Wrapper(t *testing.T) {
100 val, err := True.ConvertToNative(boolWrapperType)
101 pbVal := wrapperspb.Bool(true)
102 if err != nil {
103 t.Error(err)
104 } else if !proto.Equal(val.(proto.Message), pbVal) {
105 t.Error("Error during conversion to wrapper value type", val)
106 }
107 }
108
109 func TestBoolConvertToType(t *testing.T) {
110 if !True.ConvertToType(StringType).Equal(String("true")).(Bool) {
111 t.Error("Boolean could not be converted to string")
112 }
113 if True.ConvertToType(BoolType) != True {
114 t.Error("Boolean could not be converted to a boolean.")
115 }
116 if True.ConvertToType(TypeType) != BoolType {
117 t.Error("Boolean could not be converted to a type.")
118 }
119 if !IsError(True.ConvertToType(TimestampType)) {
120 t.Error("Got value, expected error.")
121 }
122 }
123
124 func TestBoolEqual(t *testing.T) {
125 if !True.Equal(True).(Bool) {
126 t.Error("True was not equal to true")
127 }
128 if False.Equal(True).(Bool) {
129 t.Error("False was equal to true")
130 }
131 if Double(0.0).Equal(False) != False {
132 t.Error("Cross-type equality yielded error value.")
133 }
134 }
135
136 func TestBoolIsZeroValue(t *testing.T) {
137 if True.IsZeroValue() {
138 t.Error("True.IsZeroValue() returned true, wanted false.")
139 }
140 if !False.IsZeroValue() {
141 t.Error("False.IsZeroValue() returned false, wanted true")
142 }
143 }
144
145 func TestBoolNegate(t *testing.T) {
146 if True.Negate() != False {
147 t.Error("True did not negate to false.")
148 }
149 if False.Negate() != True {
150 t.Error("False did not negate to true")
151 }
152 }
153
154 func TestIsBool(t *testing.T) {
155 if !IsBool(True) || !IsBool(False) {
156 t.Error("Boolean values did not test as boolean.")
157 }
158 if IsBool(String("true")) {
159 t.Error("Non-boolean value tested as boolean.")
160 }
161 }
162
163 func BenchmarkIsBool(b *testing.B) {
164 for i := 0; i < b.N; i++ {
165 if !(IsBool(True) && !IsBool(IntNegOne)) {
166 b.Fatal("IsBool() failed to return proper result")
167 }
168 }
169 }