File: helper_test.go

package info (click to toggle)
golang-github-viant-assertly 0.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 240 kB
  • sloc: makefile: 2
file content (195 lines) | stat: -rw-r--r-- 4,173 bytes parent folder | download | duplicates (2)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package assertly

import (
	"github.com/stretchr/testify/assert"
	"github.com/viant/toolbox"
	"reflect"
	"testing"
)

func TestAsDataStructure(t *testing.T) {

	var useCases = []struct {
		Description string
		Input       string
		Kind        reflect.Kind
		Length      int
	}{
		{
			Description: "multiline textual value",
			Input:       "abc\nxyz",
			Kind:        reflect.String,
			Length:      -1,
		},
		{
			Description: "multiline textual with incomplete JSON like struct at the end",
			Input:       "abc\nxyz\n{123}",
			Kind:        reflect.String,
			Length:      -1,
		},
		{
			Description: "multiline textual with incomplete JSON like struct at the begining and at the end",
			Input:       "{123}\nabc\nxyz\n{123}",
			Kind:        reflect.String,
			Length:      -1,
		},
		{
			Description: "multiline textual with incomplete JSON like struct at the begining",
			Input:       "{123}\nabc\nxyz\n{123}",
			Kind:        reflect.String,
			Length:      -1,
		},
		{
			Description: "valida JSON only at the first and last line",
			Input:       "[1,2,3]\nabc\nxyz\n[1,2,3]",
			Kind:        reflect.String,
			Length:      -1,
		},
		{
			Description: "valida JSON only at the first and last line",
			Input:       "[1,2,3]\n[2,3,4]\n[1,2,3]",
			Kind:        reflect.Slice,
			Length:      3,
		},
		{
			Description: "multiline valid JSON object",
			Input: `{
		"k1":"v1",
		"k2": "v2"
}`,
			Kind:   reflect.Map,
			Length: 2,
		},
		{
			Description: "multiline valid JSON array",
			Input: `[1,
2,
3
]`,
			Kind:   reflect.Slice,
			Length: 3,
		},
		{
			Description: "new line delimited valid JSON array with empty line",
			Input: `[1,2,3]

[2,3,4]`,
			Kind:   reflect.Slice,
			Length: 2,
		},

		{
			Description: "multiline invalid JSON object",
			Input: `{
		"k1":"v1", z
		"k2", "v2"
}`,
			Kind:   reflect.String,
			Length: -1,
		},
		{
			Description: "empty string",
			Input:       ` `,
			Kind:        reflect.String,
			Length:      -1,
		},
		{
			Description: "single line valid JSON object",
			Input:       `{"a":1}`,
			Kind:        reflect.Map,
			Length:      1,
		},
	}

	for _, useCase := range useCases {
		output := asDataStructure(useCase.Input)
		actualKind := reflect.TypeOf(output).Kind()

		if assert.EqualValues(t, useCase.Kind, actualKind, useCase.Description) {
			if actualKind == reflect.String { //check for no string modification
				assert.EqualValues(t, useCase.Input, output, useCase.Description)
			}
			if useCase.Length >= 0 {
				var actualLength = 0
				if actualKind == reflect.Map {
					actualLength = len(toolbox.AsMap(output))
				} else {
					actualLength = len(toolbox.AsSlice(output))
				}
				assert.EqualValues(t, useCase.Length, actualLength, useCase.Description)
			}
		}
	}

}

func TestReverseSlice(t *testing.T) {
	var aSlice = []string{"1", "10", "3"}
	reverseSlice(aSlice)
	assert.EqualValues(t, []string{"3", "10", "1"}, aSlice)
}

func TestMergeTextMap(t *testing.T) {

	{
		var source = map[string]string{}
		var target map[string]string
		mergeTextMap(source, &target)
		assert.EqualValues(t, 0, len(target))
	}
	{
		var source = map[string]string{
			"k1": "v1",
		}
		var target map[string]string
		mergeTextMap(source, &target)
		assert.EqualValues(t, 1, len(target))
	}
	{
		var source = map[string]string{
			"k1": "v1",
		}
		var target = make(map[string]string)
		mergeTextMap(source, &target)
		assert.EqualValues(t, 1, len(target))
	}

}

func TestMergeBoolMap(t *testing.T) {

	{
		var source = map[string]bool{}
		var target map[string]bool
		mergeBoolMap(source, &target)
		assert.EqualValues(t, 0, len(target))
	}
	{
		var source = map[string]bool{
			"k1": true,
		}
		var target map[string]bool
		mergeBoolMap(source, &target)
		assert.EqualValues(t, 1, len(target))
	}
	{
		var source = map[string]bool{
			"k1": true,
		}
		var target map[string]bool
		mergeBoolMap(source, &target)
		assert.EqualValues(t, 1, len(target))
	}

}

func Test_ToStringSlice(t *testing.T) {
	{
		aSlice := toStringSlice([]interface{}{"1", 2})
		assert.EqualValues(t, []string{"1", "2"}, aSlice)
	}
	{
		aSlice := toStringSlice(1)
		assert.EqualValues(t, []string{"1"}, aSlice)
	}
}