-
Notifications
You must be signed in to change notification settings - Fork 1
/
simplepeg_test.go
52 lines (40 loc) · 1020 Bytes
/
simplepeg_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
package simplepeg
import (
"github.com/SimplePEG/Go/speg"
"testing"
)
func TestParse(t *testing.T) {
var grammarText = `GRAMMAR url
url -> scheme "://" host pathname search hash?;
scheme -> "http" "s"?;
host -> hostname port?;
hostname -> segment ("." segment)*;
segment -> [a-z0-9-]+;
port -> ":" [0-9]+;
pathname -> "/" [^ ?]*;
search -> ("?" [^ #]*)?;
hash -> "#" [^ ]*;
`
var spegParser = speg.NewSPEGParser()
var ast, err = spegParser.ParseGrammar(grammarText)
if ast.Match != grammarText {
t.Error("Grammar not matched to original")
}
if !err {
textAst, textErr := speg.ParseText(ast, "https://simplepeg.github.io/")
if textErr {
t.Error("Text not parsed")
}
if textAst.Match != "https://simplepeg.github.io/" {
t.Error("Text not matched")
}
if textAst.StartPosition != 0 {
t.Error("Text in not position")
}
if textAst.EndPosition != 28 {
t.Error("Text in not position")
}
} else {
t.Error("Grammar not parsed")
}
}