-
Notifications
You must be signed in to change notification settings - Fork 3
/
compose_test.go
68 lines (58 loc) · 1.97 KB
/
compose_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
package examples
import (
"testing"
"github.com/a-h/lexical/input"
"github.com/a-h/lexical/parse"
)
func TestCompose(t *testing.T) {
parser := parse.Or(parse.Rune('A'), parse.Rune('B'))
matchesA := parser(input.NewFromString("A")).Success // true
matchesB := parser(input.NewFromString("B")).Success // true
matchesC := parser(input.NewFromString("C")).Success // false
if !matchesA {
t.Errorf("for 'A', expected true, got false")
}
if !matchesB {
t.Errorf("for 'B', expected true, got false")
}
if matchesC {
t.Errorf("for 'C', expected false, got true")
}
}
func TestMany(t *testing.T) {
// parse.WithIntegerCombiner concatentates the captured runes into a string,
// and parses the result to an integer.
oneToThreeNumbers := parse.Many(parse.WithIntegerCombiner,
1, // minimum match count
3, // maximum match count
parse.ZeroToNine)
resultA := oneToThreeNumbers(input.NewFromString("123"))
if !resultA.Success {
t.Error("for '123' expected success to be true, got false")
}
if resultA.Item != 123 {
t.Errorf("for '123' expected value of 123, but got '%v'", resultA.Item)
}
resultB := oneToThreeNumbers(input.NewFromString("1234"))
if !resultB.Success {
t.Errorf("for '1234', expected success to be true, got false")
}
if resultB.Item != 123 {
t.Errorf("for '1234' expected value of 123, but got '%v'", resultA.Item)
}
// This Many function will stop reading at the 'a'.
resultC := oneToThreeNumbers(input.NewFromString("1a234"))
if !resultC.Success {
t.Errorf("for '1a234', expected success to be true, got false")
}
if resultC.Item != 1 {
t.Errorf("for '1a234' expected value of 1, but got '%v'", resultA.Item)
}
// Parse letters into a string
upToThreeLetters := parse.AtMost(parse.WithStringConcatCombiner, 3, parse.Letter)
letters := upToThreeLetters(input.NewFromString("ABC1"))
resultItem, ok := letters.Item.(string)
if !ok || resultItem != "ABC" {
t.Errorf("for 'ABC1', expected to extract 'ABC', but extracted '%v'", letters.Item)
}
}