| 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 | "time" |
| 21 | |
| 22 | "google.golang.org/protobuf/proto" |
| 23 | |
| 24 | "github.com/google/cel-go/common/overloads" |
| 25 | "github.com/google/cel-go/common/types/ref" |
| 26 | |
| 27 | anypb "google.golang.org/protobuf/types/known/anypb" |
| 28 | structpb "google.golang.org/protobuf/types/known/structpb" |
| 29 | wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" |
| 30 | ) |
| 31 | |
| 32 | func TestStringAdd(t *testing.T) { |
| 33 | if String("hello").Add(String(" world")) != String("hello world") { |
| 34 | t.Error("Adding two strings did not produce a concatenated value.") |
| 35 | } |
| 36 | if !IsError(String("goodbye").Add(Int(1))) { |
| 37 | t.Error("Adding a string an int did not cause an error") |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestStringCompare(t *testing.T) { |
| 42 | a := String("a") |
| 43 | b := String("bbbb") |
| 44 | c := String("c") |
| 45 | if a.Compare(b) != IntNegOne { |
| 46 | t.Error("'a' was not less than 'bbbb'") |
| 47 | } |
| 48 | if a.Compare(a) != IntZero { |
| 49 | t.Error("'a' was not equal to 'a'") |
| 50 | } |
| 51 | if c.Compare(b) != IntOne { |
| 52 | t.Error("'c' was not greater than 'bbbb'") |
| 53 | } |
| 54 | if !IsError(a.Compare(True)) { |
| 55 | t.Error("Comparison to a non-string type did not generate an error.") |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestStringConvertToNative_Any(t *testing.T) { |
| 60 | val, err := String("hello").ConvertToNative(anyValueType) |
| 61 | if err != nil { |
| 62 | t.Error(err) |
| 63 | } |
| 64 | want, err := anypb.New(wrapperspb.String("hello")) |
| 65 | if err != nil { |
| 66 | t.Error(err) |
| 67 | } |
| 68 | if !proto.Equal(val.(proto.Message), want) { |
| 69 | t.Errorf("Got '%v', expected '%v'", val, want) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestStringConvertToNative_Error(t *testing.T) { |
| 74 | val, err := String("hello").ConvertToNative(reflect.TypeOf(0)) |
| 75 | if err == nil { |
| 76 | t.Errorf("Got '%v', expected error", val) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func TestStringConvertToNative_Json(t *testing.T) { |
| 81 | val, err := String("hello").ConvertToNative(JSONValueType) |
| 82 | pbVal := structpb.NewStringValue("hello") |
| 83 | if err != nil { |
| 84 | t.Error(err) |
| 85 | } else if !proto.Equal(val.(proto.Message), pbVal) { |
| 86 | t.Errorf("Got '%v', expected json Value type", val) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestStringConvertToNative_Ptr(t *testing.T) { |
| 91 | ptrType := "" |
| 92 | val, err := String("hello").ConvertToNative(reflect.TypeOf(&ptrType)) |
| 93 | if err != nil { |
| 94 | t.Error(err) |
| 95 | } else if *val.(*string) != "hello" { |
| 96 | t.Errorf("Got '%v', expected 'hello'", val) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestStringConvertToNative_String(t *testing.T) { |
| 101 | val, err := String("hello").ConvertToNative(reflect.TypeOf("")) |
| 102 | if err != nil { |
| 103 | t.Error(err) |
| 104 | } else if val.(string) != "hello" { |
| 105 | t.Errorf("Got '%v', expected 'hello'", val) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | type customString string |
| 110 | |
| 111 | func TestStringConvertToNative_CustomString(t *testing.T) { |
| 112 | val, err := String("hello").ConvertToNative(reflect.TypeOf(customString(""))) |
| 113 | if err != nil { |
| 114 | t.Error(err) |
| 115 | } else if v, ok := val.(customString); !ok || v != "hello" { |
| 116 | t.Errorf("Got %T with val '%v', expected %T with val 'hello'", val, v, customString("")) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func TestStringConvertToNative_Wrapper(t *testing.T) { |
| 121 | val, err := String("hello").ConvertToNative(stringWrapperType) |
| 122 | if err != nil { |
| 123 | t.Error(err) |
| 124 | } |
| 125 | want := wrapperspb.String("hello") |
| 126 | if !proto.Equal(val.(proto.Message), want) { |
| 127 | t.Errorf("Got '%v', expected '%v'", val, want) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func TestStringConvertToType(t *testing.T) { |
| 132 | if !String("-1").ConvertToType(IntType).Equal(IntNegOne).(Bool) { |
| 133 | t.Error("String could not be converted to int") |
| 134 | } |
| 135 | if !String("false").ConvertToType(BoolType).Equal(False).(Bool) { |
| 136 | t.Error("String could not be converted to bool") |
| 137 | } |
| 138 | if !String("1").ConvertToType(UintType).Equal(Uint(1)).(Bool) { |
| 139 | t.Error("String could not be converted to uint") |
| 140 | } |
| 141 | if !String("2017-01-01T00:00:00Z").ConvertToType(TimestampType). |
| 142 | Equal(Timestamp{Time: time.Unix(1483228800, 0).UTC()}).(Bool) { |
| 143 | t.Error("String could not be converted to timestamp") |
| 144 | } |
| 145 | if !String("1h5s").ConvertToType(DurationType). |
| 146 | Equal(Duration{Duration: time.Duration(3605) * time.Second}).(Bool) { |
| 147 | t.Error("String could not be converted to duration") |
| 148 | } |
| 149 | if !String("2.5").ConvertToType(DoubleType).Equal(Double(2.5)).(Bool) { |
| 150 | t.Error("String could not be converted to double") |
| 151 | } |
| 152 | if !String("hello").ConvertToType(BytesType).Equal(Bytes("hello")).(Bool) { |
| 153 | t.Error("String could not be converted to bytes") |
| 154 | } |
| 155 | if !String("goodbye").ConvertToType(TypeType).Equal(StringType).(Bool) { |
| 156 | t.Error("String could not be converted to type") |
| 157 | } |
| 158 | if !String("goodbye").ConvertToType(StringType).Equal(String("goodbye")).(Bool) { |
| 159 | t.Error("String could not be converted to itself") |
| 160 | } |
| 161 | if !IsError(String("map{}").ConvertToType(MapType)) { |
| 162 | t.Error("Unsupported string conversion resulted in value.") |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | func TestStringEqual(t *testing.T) { |
| 167 | if !String("hello").Equal(String("hello")).(Bool) { |
| 168 | t.Error("Two equivalent strings were not equal") |
| 169 | } |
| 170 | if String("hello").Equal(String("hell")).(Bool) { |
| 171 | t.Error("Two inqueal strings were found equal") |
| 172 | } |
| 173 | if IsError(String("c").Equal(Int(99))) { |
| 174 | t.Error("String 'c' equal to int 99 resulted in error, wanted 'false'") |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | func TestStringIsZeroValue(t *testing.T) { |
| 179 | if String("non-zero").IsZeroValue() { |
| 180 | t.Error("String('non-zero').IsZeroValue() returned true, wanted false.") |
| 181 | } |
| 182 | if !String("").IsZeroValue() { |
| 183 | t.Error("String('').IsZeroValue() returned false, wanted true") |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | func TestStringMatch(t *testing.T) { |
| 188 | str := String("hello 1 world") |
| 189 | sw := String("^hello") |
| 190 | ew := String("\\d world$") |
| 191 | if !str.Match(sw).(Bool) { |
| 192 | t.Error("Did not match starts with pattern.") |
| 193 | } |
| 194 | if !str.Match(ew).(Bool) { |
| 195 | t.Error("Did not match ends with pattern.") |
| 196 | } |
| 197 | if str.Match(String("ello 1 worlds")).(Bool) { |
| 198 | t.Error("Matched an incorrect pattern.") |
| 199 | } |
| 200 | if !IsError(str.Match(Int(1))) { |
| 201 | t.Error("Matched a non-string pattern.") |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | func TestStringContains(t *testing.T) { |
| 206 | y := String("goodbye").Receive( |
| 207 | overloads.Contains, |
| 208 | overloads.ContainsString, |
| 209 | []ref.Val{String("db")}) |
| 210 | if y != True { |
| 211 | t.Errorf("Got '%v', expected 'true'", y) |
| 212 | } |
| 213 | n := String("goodbye").Receive( |
| 214 | overloads.Contains, |
| 215 | overloads.ContainsString, |
| 216 | []ref.Val{String("ggood")}) |
| 217 | if n == True { |
| 218 | t.Errorf("Got '%v', expected 'true'", n) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | func TestStringEndsWith(t *testing.T) { |
| 223 | y := String("goodbye").Receive( |
| 224 | overloads.EndsWith, |
| 225 | overloads.EndsWithString, |
| 226 | []ref.Val{String("bye")}) |
| 227 | if y == False { |
| 228 | t.Errorf("Got '%v', expected 'true'", y) |
| 229 | } |
| 230 | n := String("goodbye").Receive( |
| 231 | overloads.EndsWith, |
| 232 | overloads.EndsWithString, |
| 233 | []ref.Val{String("good")}) |
| 234 | if n == True { |
| 235 | t.Errorf("Got '%v', expected 'true'", n) |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | func TestStringStartsWith(t *testing.T) { |
| 240 | y := String("goodbye").Receive( |
| 241 | overloads.StartsWith, |
| 242 | overloads.StartsWithString, |
| 243 | []ref.Val{String("good")}) |
| 244 | if y != True { |
| 245 | t.Errorf("Got '%v', expected 'true'", y) |
| 246 | } |
| 247 | n := String("goodbye").Receive( |
| 248 | overloads.StartsWith, |
| 249 | overloads.StartsWithString, |
| 250 | []ref.Val{String("db")}) |
| 251 | if n == True { |
| 252 | t.Errorf("Got '%v', expected 'true'", n) |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | func TestStringSize(t *testing.T) { |
| 257 | if String("").Size().(Int) != 0 { |
| 258 | t.Error("Empty string had a non-zero size") |
| 259 | } |
| 260 | if String("hello world").Size().(Int) != 11 { |
| 261 | t.Error("String with eleven characters had incorrect size") |
| 262 | } |
| 263 | if String("\u65e5\u672c\u8a9e").Size().(Int) != 3 { |
| 264 | t.Error("String size must be code points, not UTF8 bytes") |
| 265 | } |
| 266 | } |