-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathconversion_test.go
145 lines (127 loc) · 2.77 KB
/
conversion_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package udf
import (
"github.com/stretchr/testify/assert"
"github.com/viant/toolbox/data"
"log"
"reflect"
"testing"
)
func Test_AsBool(t *testing.T) {
ok, err := AsBool("true", nil)
assert.Nil(t, err)
assert.Equal(t, true, ok)
}
func Test_AsFloat(t *testing.T) {
value, err := AsFloat(0.3, nil)
assert.Nil(t, err)
assert.Equal(t, 0.3, value)
}
func Test_AsInt(t *testing.T) {
value, err := AsInt(4.3, nil)
assert.Nil(t, err)
assert.Equal(t, 4, value)
}
func Test_AsMap(t *testing.T) {
{
var aMap, err = AsMap(map[string]interface{}{}, nil)
assert.Nil(t, err)
assert.NotNil(t, aMap)
}
{
var aMap, err = AsMap("{\"abc\":1}", nil)
assert.Nil(t, err)
assert.NotNil(t, aMap)
}
{
var aMap, err = AsMap(`abc: 1`, nil)
assert.Nil(t, err)
assert.NotNil(t, aMap)
}
{
_, err := AsMap("{\"abc\":1, \"a}", nil)
assert.NotNil(t, err)
}
}
func Test_AsCollection(t *testing.T) {
{
var aSlice, err = AsCollection([]interface{}{1}, nil)
assert.Nil(t, err)
assert.NotNil(t, aSlice)
}
{
var aSlice, err = AsCollection("[1,2]", nil)
assert.Nil(t, err)
assert.NotNil(t, aSlice)
}
{
var aSlice, err = AsCollection(`
- 1
- 2`, nil)
assert.Nil(t, err)
assert.NotNil(t, aSlice)
}
{
_, err := AsCollection("[\"a,2]", nil)
assert.NotNil(t, err)
}
{
var aMap, err = AsData(`abc: 1`, nil)
assert.Nil(t, err)
assert.NotNil(t, aMap)
}
}
func Test_AsData(t *testing.T) {
{
var aSlice, err = AsData("[1,2]", nil)
assert.Nil(t, err)
assert.NotNil(t, aSlice)
}
{
var aMap, err = AsData("{\"abc\":1}", nil)
assert.Nil(t, err)
assert.NotNil(t, aMap)
}
}
func Test_YamlAsCollection(t *testing.T) {
var YAML = `- Requests:
- URL: http://localhost:5000
Method: GET
Header:
aHeader:
- "myField=a-value; path=/; domain=localhost; Expires=Tue, 19 Jan 2038 03:14:07 GMT;"
someOtherHeader:
- "CP=RTO"
Body: "hey there"
Cookies:
- Name: aHeader
Value: a-value
DYAMLomain: "localhost"
Expires: "2023-12-16T20:17:38Z"
RawExpires: Sat, 16 Dec 2023 20:17:38 GMT`
expanded, err := AsCollection(YAML, nil)
if !assert.Nil(t, err) {
log.Fatal(err)
}
assert.Equal(t, reflect.Slice, reflect.TypeOf(expanded).Kind())
}
func Test_YamlAsMap(t *testing.T) {
YAML := `default: &default
Name: Jack
person:
<<: *default
Name: Bob`
expanded, err := AsCollection(YAML, nil)
assert.Nil(t, err)
assert.NotNil(t, expanded)
}
func Test_AsString(t *testing.T) {
aMap := data.NewMap()
Register(aMap)
aMap.Put("k0", true)
expanded := aMap.ExpandAsText(" $AsString(${k0})")
assert.EqualValues(t, "true", expanded)
//64 bit int
aMap.Put("k1", 2323232323223)
expanded = aMap.ExpandAsText(" $AsString(${k1})")
assert.EqualValues(t, "2323232323223", expanded)
}