Debian Codegraph @

common/types/optional_test.go

raw symbols SCIP
1 // Copyright 2022 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 "fmt"
19 "reflect"
20 "testing"
21
22 "github.com/google/cel-go/common/types/ref"
23 )
24
25 func TestOptionalOptionalOf(t *testing.T) {
26 opt := OptionalOf(IntOne)
27 if !opt.HasValue() {
28 t.Error("OptionalOf(1) returned result with a value")
29 }
30 }
31
32 func TestOptionalGetValue(t *testing.T) {
33 opt := OptionalOf(IntOne)
34 if opt.GetValue() != IntOne {
35 t.Errorf("opt.GetValue() got %v, wanted 1", opt.GetValue())
36 }
37 if !IsError(OptionalNone.GetValue()) {
38 t.Errorf("OptionalNone.GetValue() got %v, wanted error", OptionalNone.GetValue())
39 }
40 }
41
42 func TestOptionalConvertToNative(t *testing.T) {
43 out, err := OptionalNone.ConvertToNative(reflect.TypeOf(1))
44 if err == nil {
45 t.Errorf("OptionalNone.GetValue() got %v, wanted error", out)
46 }
47 out, err = OptionalOf(String("hello")).ConvertToNative(reflect.TypeOf(""))
48 if err != nil {
49 t.Fatalf("OptionalOf('hello').ConvertToNative(string) failed: %v", err)
50 }
51 if out != "hello" {
52 t.Errorf("OptionalOf('hello').ConvertToNative(string) got %v, wanted 'hello'", out)
53 }
54 }
55
56 func TestOptionalConvertToType(t *testing.T) {
57 if OptionalNone.ConvertToType(OptionalType) != OptionalNone {
58 t.Errorf("ConvertToType(OptionalType) got %v, wanted optional", OptionalNone.ConvertToType(OptionalType))
59 }
60 if OptionalNone.ConvertToType(TypeType) != OptionalType {
61 t.Errorf("ConvertToType(TypeType) got %v, wanted a type", OptionalNone.ConvertToType(TypeType))
62 }
63 if !IsError(OptionalNone.ConvertToType(ErrType)) {
64 t.Errorf("ConvertToType(ErrType) got %v, wanted error", OptionalNone.ConvertToType(ErrType))
65 }
66 }
67
68 func TestOptionalEqual(t *testing.T) {
69 tests := []struct {
70 a ref.Val
71 b ref.Val
72 out Bool
73 }{
74 {
75 a: OptionalNone,
76 b: OptionalNone,
77 out: True,
78 },
79 {
80 a: IntOne,
81 b: OptionalNone,
82 out: False,
83 },
84 {
85 a: IntOne,
86 b: OptionalNone,
87 out: False,
88 },
89 {
90 a: OptionalOf(IntOne),
91 b: OptionalNone,
92 out: False,
93 },
94 {
95 a: OptionalNone,
96 b: OptionalOf(IntOne),
97 out: False,
98 },
99 {
100 a: OptionalOf(IntOne),
101 b: OptionalOf(Double(0.0)),
102 out: False,
103 },
104 {
105 a: OptionalOf(IntOne),
106 b: OptionalOf(Double(1.0)),
107 out: True,
108 },
109 }
110
111 for i, tst := range tests {
112 tc := tst
113 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
114 if tc.a.Equal(tc.b) != tc.out {
115 t.Errorf("%v.Equal(%v) got %v, wanted %v", tc.a, tc.b, !tc.out, tc.out)
116 }
117 })
118 }
119 }
120
121 func TestOptionalType(t *testing.T) {
122 if OptionalOf(False).Type() != OptionalType {
123 t.Errorf("OptionalOf(false).Type() got %v, wanted optional", OptionalOf(False).Type())
124 }
125 }
126
127 func TestOptionalValue(t *testing.T) {
128 if OptionalOf(False).Value() != false {
129 t.Errorf("OptionalOf(false).Value() got %v, wanted false", OptionalOf(False).Value())
130 }
131 if OptionalNone.Value() != nil {
132 t.Errorf("OptionalNone.Value() got %v, wanted nil", OptionalNone.Value())
133 }
134 }