-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil_test.go
More file actions
115 lines (95 loc) · 2.48 KB
/
util_test.go
File metadata and controls
115 lines (95 loc) · 2.48 KB
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
package xparse
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type UtilSuite struct {
suite.Suite
rawJSON string
}
func TestUtil(t *testing.T) {
suite.Run(t, new(UtilSuite))
}
func (s *UtilSuite) SetupSuite() {
s.rawJSON = `
{
"name": {"first": "Tom", "last": "Anderson"},
"age":37,
"children": ["Sara","Alex","Jack"],
"not_existed": null,
"fav.movie": "Deer Hunter",
"friends": [
{"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
{"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
{"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
]
}
`
}
func (s *UtilSuite) TearDownSuite() {
}
func (s *UtilSuite) Test01_GetKeys() {
dat := make(map[string]any)
err := Structify(s.rawJSON, &dat)
s.Nil(err)
// keys := GetKeys(dat["friends"], "friends")
var all []string
GetMapKeys(&all, dat)
fmt.Println(all)
// PrintAllKeys(dat)
}
func (s *UtilSuite) Test02_CutStr() {
raw := "a,b,c,d,e"
v, b := GetStrBySplit(raw, ",", 6)
s.Equal("e", v)
s.True(b)
v, b = GetStrBySplit(raw, ",", -1)
s.Equal("e", v)
s.True(b)
}
func (s *UtilSuite) Test03_load() {
r0 := getBytes("html_yaml/0000.yaml")
r1 := getBytes("html_yaml/0001.yaml")
cf := Yaml2Config(r1, r0)
want := map[string]any{
"__raw": map[string]any{
"country": "CH",
"language": []any{
"en",
},
"site": uint64(895),
"site_url": "https://www.jobisjob.ch/",
"test_keys": []any{
"jobs.*",
},
"verify_keys": []any{
"salary_range",
"listing_date",
"external_id",
},
},
}
s.Equal(want, cf.Data())
}
func TestParseNumberRanges(t *testing.T) {
assert := assert.New(t)
// Empty input
assert.Equal([]int{}, ParseNumberRanges(""))
// Single numbers
assert.Equal([]int{0}, ParseNumberRanges("0"))
assert.Equal([]int{-1}, ParseNumberRanges("-1"))
// Multiple numbers
assert.Equal([]int{0, 1, 2, 3}, ParseNumberRanges("0,1,2,3"))
assert.Equal([]int{-2, -1, 0, 1}, ParseNumberRanges("-2,-1,0,1"))
// Inclusive ranges
assert.Equal([]int{0, 1, 2, 3}, ParseNumberRanges("0-3"))
assert.Equal([]int{-2, -1, 0, 1}, ParseNumberRanges("-2-1"))
// Exclusive ranges
assert.Equal([]int{0, 1, 2}, ParseNumberRanges("0~3"))
assert.Equal([]int{-2, -1, 0}, ParseNumberRanges("-2~1"))
// Mixed formats with spaces
assert.Equal([]int{0, 3, 4, 5, 6, 7, 13, 14}, ParseNumberRanges("0, 3-7, 13~15"))
assert.Equal([]int{-3, -2, -1, 0, 1, 2}, ParseNumberRanges("-3~0, 0-2"))
}