forked from svelterust/vom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
combinator_test.v
156 lines (135 loc) · 2.93 KB
/
combinator_test.v
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
module vom
fn test_all_consuming() ! {
mut rest := ''
mut output := ''
mut len := 0
parser := all_consuming(alpha1)
rest, output, len = parser('abcd')!
assert output == 'abcd'
assert rest == ''
assert len == 4
rest, output, len = parser('abcd1') or { 'err', 'err', 99 }
assert output == 'err'
assert rest == 'err'
assert len == 99
}
fn test_cond() ! {
mut rest := ''
mut output := ''
mut len := 0
parser := fn (b bool, i string) !(string, string, int) {
temp := cond(b, alpha1)
return temp(i)
}
rest, output, len = parser(true, 'abcd;')!
assert output == 'abcd'
assert rest == ';'
assert len == 4
rest, output, len = parser(false, 'abcd;')!
assert output == ''
assert rest == 'abcd;'
assert len == 0
}
fn test_recognize() ! {
mut rest := ''
mut output := ''
mut len := 0
parser := recognize(separated_pair(alpha1, tag(','), alpha1))
rest, output, len = parser('abcd,efgh')!
assert output == 'abcd,efgh'
assert rest == ''
assert len == 9
}
fn test_eof() ! {
mut rest := ''
mut output := ''
mut len := 0
rest, output, len = eof('')!
assert output == ''
assert rest == ''
assert len == 0
rest, output, len = eof('abc') or { 'err', 'err', 99 }
assert output == 'err'
assert rest == 'err'
assert len == 99
}
fn test_fail() ! {
mut rest := ''
mut output := ''
mut len := 0
rest, output, len = fail('string') or { 'err', 'err', 99 }
assert rest == 'err'
assert output == 'err'
assert len == 99
}
/*
fn test_flat_map() ! {
parser := flat_map(u8, take)
rest, output := parser('2 78334') !
assert rest == '334'
assert output == '78'
}
*/
// fn test_map() ! {
//}
fn test_not() ! {
mut rest := ''
mut output := ''
mut len := 0
parser := not(alpha1)
rest, output, len = parser('123')!
assert output == ''
assert rest == '123'
assert len == 0
rest, output, len = parser('abc') or { 'err', 'err', 99 }
assert output == 'err'
assert rest == 'err'
assert len == 99
}
fn test_opt() ! {
mut rest := ''
mut output := ''
mut len := 0
parser := opt(alpha1)
rest, output, len = parser('abcd;')!
assert output == 'abcd'
assert rest == ';'
assert len == 4
rest, output, len = parser('123;')!
assert output == ''
assert rest == '123;'
assert len == 0
}
fn test_peek() ! {
mut rest := ''
mut output := ''
mut len := 0
parser := peek(alpha1)
rest, output, len = parser('abcd;')!
assert output == 'abcd'
assert rest == 'abcd;'
assert len == 4
rest, output, len = parser('123;') or { 'err', 'err', 99 }
assert output == 'err'
assert rest == 'err'
assert len == 99
}
fn test_value() ! {
mut output := 0
parser := value(1234, alpha1)
output = parser('abcd')!
assert output == 1234
output = parser('4323abcd') or { 99 }
assert output == 99
}
fn string_len_equ_4(input string) bool {
return input.len == 4
}
fn test_verify() ! {
mut output := ''
parser := verify(alpha1, string_len_equ_4)
output = parser('abcd')!
assert output == 'abcd'
output = parser('abcde') or { 'err' }
assert output == 'err'
}