-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifier_test.go
98 lines (75 loc) · 2.03 KB
/
modifier_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
package chomp_test
import (
"strconv"
"testing"
"github.com/purpleclay/chomp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMap(t *testing.T) {
t.Parallel()
type Coord struct {
X int
Y int
}
_, out, err := chomp.Map(
chomp.SepPair(chomp.While(chomp.IsDigit), chomp.Tag(","), chomp.While(chomp.IsDigit)),
func(in []string) Coord {
x, _ := strconv.Atoi(in[0])
y, _ := strconv.Atoi(in[1])
return Coord{X: x, Y: y}
},
)("1,2")
require.NoError(t, err)
assert.Equal(t, 1, out.X)
assert.Equal(t, 2, out.Y)
}
func TestOpt(t *testing.T) {
t.Parallel()
rem, ext, err := chomp.Opt(chomp.Tag("the"))("dark knight")
require.NoError(t, err)
assert.Equal(t, "dark knight", rem)
assert.Equal(t, "", ext)
}
func TestS(t *testing.T) {
t.Parallel()
rem, ext, err := chomp.S(chomp.Tag("hello"))("hello and good morning")
require.NoError(t, err)
assert.Equal(t, " and good morning", rem)
require.Len(t, ext, 1)
assert.Equal(t, "hello", ext[0])
}
func TestI(t *testing.T) {
t.Parallel()
rem, ext, err := chomp.I(
chomp.Repeat(chomp.All(chomp.Until(" "), chomp.Tag(" ")), 3),
2)("hello and good morning")
require.NoError(t, err)
assert.Equal(t, "morning", rem)
assert.Equal(t, "and", ext)
}
func TestPeek(t *testing.T) {
t.Parallel()
rem, ext, err := chomp.Peek(chomp.Tag("Hello"))("Hello and Good Morning!")
require.NoError(t, err)
assert.Equal(t, "Hello and Good Morning!", rem)
assert.Equal(t, "Hello", ext)
}
func TestPeekUsingSequence(t *testing.T) {
t.Parallel()
rem, ext, err := chomp.Peek(
chomp.Many(chomp.Suffixed(chomp.Until(" "), chomp.Tag(" "))),
)("Hello and Good Morning!")
require.NoError(t, err)
assert.Equal(t, "Hello and Good Morning!", rem)
assert.Equal(t, []string{"Hello", "and", "Good"}, ext)
}
func TestFlatten(t *testing.T) {
t.Parallel()
rem, ext, err := chomp.Flatten(
chomp.Many(chomp.Parentheses()),
)("(H)(el)(lo) and Good Morning!")
require.NoError(t, err)
assert.Equal(t, " and Good Morning!", rem)
assert.Equal(t, "Hello", ext)
}