forked from iand/gedcom
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscanner_test.go
More file actions
95 lines (77 loc) · 2.48 KB
/
scanner_test.go
File metadata and controls
95 lines (77 loc) · 2.48 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
/*
This is free and unencumbered software released into the public domain. For more
information, see <http://unlicense.org/> or the accompanying UNLICENSE file.
*/
package gedcom
import (
"io"
"testing"
)
type example struct {
input []byte
level int
tag string
value string
xref string
}
var examples = []example{
{[]byte("1 SEX F\n"), 1, `SEX`, `F`, ""},
{[]byte(" 1 SEX F\n"), 1, `SEX`, `F`, ""},
{[]byte(" \r\n\t 1 SEX F\n"), 1, `SEX`, `F`, ""},
{[]byte(" \r\n\t 1 SEX F\n"), 1, `SEX`, `F`, ""},
{[]byte("1 SEX F\r"), 1, `SEX`, `F`, ""},
{[]byte("1 SEX F \r"), 1, `SEX`, `F `, ""},
{[]byte("0 HEAD\r"), 0, `HEAD`, ``, ""},
{[]byte("0 @OTHER@ SUBM\n"), 0, `SUBM`, ``, "OTHER"},
// leading BOM
//{[]byte("\xef\xbb\xbf0 HEAD\r"), 0, `HEAD`, ``, ""},
// email address with escaped @
//{[]byte("1 EMAIL toaddress@@example.com"), 1, `EMAIL`, `toaddress@@example.com`, ""},
// email address with naked @
//{[]byte("1 EMAIL toaddress@example.com"), 1, `EMAIL`, `toaddress@example.com`, ""},
// value includes text and xref
//{[]byte(" 2 SOUR Book, Simple @S10@"), 2, `SOUR`, `Book, Simple`, "S10"},
// value includes text and xref
//{[]byte(" 1 ROLE CHIL @I1@"), 1, `ROLE`, `CHIL`, "I1"},
// value includes non-ASCII character
//{[]byte("1 NAME Æthelreda //"), 1, `NAME`, `Æthelreda`, ""},
}
func TestNextTagFound(t *testing.T) {
s := &scanner{}
for _, ex := range examples {
s.reset()
offset, err := s.nextTag(ex.input)
if err != nil {
t.Fatalf(`nextTag for "%s" returned error "%v", expected no error`, ex.input, err)
}
if offset == 0 {
t.Fatalf(`nextTag for "%s" did not find tag, expected it to find`, ex.input)
}
if s.level != ex.level {
t.Errorf(`nextTag for "%s" returned level %d, expected %d`, ex.input, s.level, ex.level)
}
if string(s.tag) != ex.tag {
t.Errorf(`nextTag for "%s" returned tag "%s", expected "%s"`, ex.input, s.tag, ex.tag)
}
if string(s.value) != ex.value {
t.Errorf(`nextTag for "%s" returned value "%s", expected "%s"`, ex.input, s.value, ex.value)
}
if string(s.xref) != ex.xref {
t.Errorf(`nextTag for "%s" returned xref "%s", expected "%s"`, ex.input, s.xref, ex.xref)
}
}
}
var examplesNot = [][]byte{
[]byte("1 SEX F"),
[]byte(" 1 SEX F "),
}
func TestNextTagNotFound(t *testing.T) {
s := &scanner{}
for _, ex := range examplesNot {
s.reset()
_, err := s.nextTag(ex)
if err != io.EOF {
t.Fatalf(`nextTag for "%s" returned unexpected error "%v", expected io.EOF`, ex, err)
}
}
}