-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser_test.go
More file actions
140 lines (133 loc) · 3.85 KB
/
parser_test.go
File metadata and controls
140 lines (133 loc) · 3.85 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
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
package lawparser
import (
"testing"
)
var input = "hola como estás"
var testHeader = "Este es un preambulo chafa\ncapítulo 10\n"
var testContainers = "\nCapítulo 10\nDe los perritos\nArtículo 10.-Los perritos no comerán solitos en la calle bajo ninguna circunstancia\nI.-No sé que estoy haciendo\na)pero lo estoy haciendo\nb)y tú no\nArtículo 20.-Los perritos no comerán solitos en la calle bajo ninguna circunstancia\nI.-No sé que estoy haciendo\n2.-No , yo tampoco\nArtículo 30.-Los perritos no comerán solitos en la calle bajo ninguna circunstancia\nI.-No sé que estoy haciendo\nII.-No,yo tampoco\nIII.-Yo menos"
func TestPeekItem(t *testing.T) {
inputB := []byte(input)
p := parser{
lexer: lex(inputB),
pos: Pos(-1),
}
p.peekItem()
if p.item.typ != itemWord || p.item.val != "hola" {
t.Error("expected hola got", p.item)
}
p.peekItem()
if p.item.typ != itemWord || p.item.val != "como" {
t.Error("expected como got", p.item)
}
p.peekItem()
if p.item.typ != itemWord || p.item.val != "estás" {
}
p.peekItem()
if p.item.typ != itemEOF {
t.Error("expected itemEOF", p.item)
}
}
func TestResetItem(t *testing.T) {
inputB := []byte(input)
p := parser{
lexer: lex(inputB),
pos: Pos(-1),
}
p.peekItem()
backup := p.pos
p.peekItem()
p.resetItem(backup)
if p.item.typ != itemWord || p.item.val != "hola" {
t.Error("expected hola got", p.item)
}
p.peekItem()
if p.item.typ != itemWord || p.item.val != "como" {
t.Error("expected como got", p.item)
}
p.peekItem()
p.peekItem()
p.resetItem(backup)
if p.item.typ != itemWord || p.item.val != "hola" {
t.Error("expected hola got", p.item)
}
}
func TestConfirmItem(t *testing.T) {
inputB := []byte(input)
p := parser{
lexer: lex(inputB),
pos: Pos(-1),
}
p.peekItem()
p.peekItem()
p.peekItem()
str := p.confirmItem()
if str != "hola como" || p.pos != Pos(0) ||
p.item.typ != itemWord || p.item.val != "estás" {
t.Error(str, p.pos, p.item)
}
}
func TestRender(t *testing.T) {
inputB := []byte(input)
p := parser{
lexer: lex(inputB),
pos: Pos(-1),
}
for p.peekItem(); !isItemEOF(p.item); p.peekItem() {
}
str := p.renderItem()
if str != input {
t.Error("expected '", input, "' got '", str, "'")
}
}
func TestParseHeader(t *testing.T) {
testHeaderB := []byte(testHeader)
p := &parser{
lexer: lex(testHeaderB),
pos: Pos(-1),
}
parseHeader(p)
if p.item.typ != itemChapter || p.item.val != "10" {
t.Error("expected itemChapter with val 10, got ", p.item)
}
}
func TestParents(t *testing.T) {
testContainersB := []byte(testContainers)
p, _ := Parse("", "", "", "", "", "", "", "", testContainersB)
if len(p.Articles) != 3 {
t.Error("It was expected 3 articles, found ", len(p.Articles))
}
for _, a := range p.Articles {
for _, p := range a.Parents {
if len(p.Order) == 0 {
t.Error("Parent must have a valid value")
}
}
}
}
func TestParseArticles(t *testing.T) {
testContainersB := []byte(testContainers)
p, _ := Parse("", "", "", "", "", "", "", "", testContainersB)
if len(p.Articles) != 3 {
t.Error("It was expected 3 articles, found ", len(p.Articles))
}
}
func TestParseFractions(t *testing.T) {
testContainersB := []byte(testContainers)
p, _ := Parse("", "", "", "", "", "", "", "", testContainersB)
if len(p.Articles[0].Fractions) != 1 {
t.Error("It was expected 1 fraction not ", len(p.Articles[0].Fractions))
}
if len(p.Articles[1].Fractions) != 2 {
t.Error("It was expected 2 fractions not ", len(p.Articles[1].Fractions))
}
if len(p.Articles[2].Fractions) != 3 {
t.Error("It was expected 3 fractions not ", len(p.Articles[2].Fractions))
}
}
func TestArtSubFraction(t *testing.T) {
testContainersB := []byte(testContainers)
p, _ := Parse("", "", "", "", "", "", "", "", testContainersB)
if len(p.Articles[0].Fractions[0].Sub) != 2 {
t.Error("It was expected 2 subfractions not ", len(p.Articles[0].Fractions[0].Sub))
}
}