1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package dcl_test
16
17 import (
18 "testing"
19
20 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl"
21 )
22
23 func TestCanonicalizeIntegerValue(t *testing.T) {
24 tests := []struct {
25 name string
26 value interface{}
27 expected int64
28 hasError bool
29 }{
30 {
31 name: "int64 to int64",
32 value: int64(10),
33 expected: int64(10),
34 },
35 {
36 name: "int to int64",
37 value: int(20),
38 expected: int64(20),
39 },
40 {
41 name: "float64 to int64",
42 value: float64(30.5),
43 expected: int64(30),
44 },
45 {
46 name: "non integer value",
47 value: "wrong value",
48 hasError: true,
49 },
50 }
51
52 for _, tc := range tests {
53 t.Run(tc.name, func(t *testing.T) {
54 canonicalizedVal, err := dcl.CanonicalizeIntegerValue(tc.value)
55 if err != nil {
56 if !tc.hasError {
57 t.Fatalf("error canonicalizing integer value: got an error, but want no error: %v", err)
58 }
59 return
60 }
61 if got, want := canonicalizedVal, tc.expected; got != want {
62 t.Fatalf("error canonicalizing integer value: got %v, want %v", got, want)
63 }
64 })
65 }
66 }
67
68 func TestCanonicalizeNumberValue(t *testing.T) {
69 tests := []struct {
70 name string
71 value interface{}
72 expected float64
73 hasError bool
74 }{
75 {
76 name: "int64 to float64",
77 value: int64(10),
78 expected: float64(10),
79 },
80 {
81 name: "int to float64",
82 value: int(20),
83 expected: float64(20),
84 },
85 {
86 name: "float64 to float64",
87 value: float64(30.5),
88 expected: float64(30.5),
89 },
90 {
91 name: "float32 to float64",
92 value: float32(40.8),
93 expected: float64(float32(40.8)),
94 },
95 {
96 name: "non number value",
97 value: "wrong value",
98 hasError: true,
99 },
100 }
101
102 for _, tc := range tests {
103 t.Run(tc.name, func(t *testing.T) {
104 canonicalizedVal, err := dcl.CanonicalizeNumberValue(tc.value)
105 if err != nil {
106 if !tc.hasError {
107 t.Fatalf("error canonicalizing integer value: got an error, but want no error: %v", err)
108 }
109 return
110 }
111 if got, want := canonicalizedVal, tc.expected; got != want {
112 t.Fatalf("error canonicalizing integer value: got %v, want %v", got, want)
113 }
114 })
115 }
116 }
117
View as plain text