1 package ucd
2
3 import (
4 "strings"
5 "testing"
6 )
7
8 const file = `
9 # Comments should be skipped
10 # rune; bool; uint; int; float; runes; # Y
11 0..0005; Y; 0; 2; -5.25 ; 0 1 2 3 4 5;
12 6..0007; Yes ; 6; 1; -4.25 ; 0006 0007;
13 8; T ; 8 ; 0 ;-3.25 ;;# T
14 9; True ;9 ; -1;-2.25 ; 0009;
15
16 # more comments to be ignored
17 @Part0
18
19 A; N; 10 ; -2; -1.25; ;# N
20 B; No; 11 ; -3; -0.25;
21 C; False;12; -4; 0.75;
22 D; ;13;-5;1.75;
23
24 @Part1 # Another part.
25 # We test part comments get removed by not commenting the next line.
26 E..10FFFF; F; 14 ; -6; 2.75;
27 `
28
29 var want = []struct {
30 start, end rune
31 }{
32 {0x00, 0x05},
33 {0x06, 0x07},
34 {0x08, 0x08},
35 {0x09, 0x09},
36 {0x0A, 0x0A},
37 {0x0B, 0x0B},
38 {0x0C, 0x0C},
39 {0x0D, 0x0D},
40 {0x0E, 0x10FFFF},
41 }
42
43 func TestGetters(t *testing.T) {
44 parts := [][2]string{
45 {"Part0", ""},
46 {"Part1", "Another part."},
47 }
48 handler := func(p *Parser) {
49 if len(parts) == 0 {
50 t.Error("Part handler invoked too many times.")
51 return
52 }
53 want := parts[0]
54 parts = parts[1:]
55 if got0, got1 := p.String(0), p.Comment(); got0 != want[0] || got1 != want[1] {
56 t.Errorf(`part: got %q, %q; want %q"`, got0, got1, want)
57 }
58 }
59
60 p := New(strings.NewReader(file), KeepRanges, Part(handler))
61 for i := 0; p.Next(); i++ {
62 start, end := p.Range(0)
63 w := want[i]
64 if start != w.start || end != w.end {
65 t.Fatalf("%d:Range(0); got %#x..%#x; want %#x..%#x", i, start, end, w.start, w.end)
66 }
67 if w.start == w.end && p.Rune(0) != w.start {
68 t.Errorf("%d:Range(0).start: got %U; want %U", i, p.Rune(0), w.start)
69 }
70 if got, want := p.Bool(1), w.start <= 9; got != want {
71 t.Errorf("%d:Bool(1): got %v; want %v", i, got, want)
72 }
73 if got := p.Rune(4); got != 0 || p.Err() == nil {
74 t.Errorf("%d:Rune(%q): got no error; want error", i, p.String(1))
75 }
76 p.err = nil
77 if got := p.Uint(2); rune(got) != start {
78 t.Errorf("%d:Uint(2): got %v; want %v", i, got, start)
79 }
80 if got, want := p.Int(3), 2-i; got != want {
81 t.Errorf("%d:Int(3): got %v; want %v", i, got, want)
82 }
83 if got, want := p.Float(4), -5.25+float64(i); got != want {
84 t.Errorf("%d:Int(3): got %v; want %v", i, got, want)
85 }
86 if got := p.Runes(5); got == nil {
87 if p.String(5) != "" {
88 t.Errorf("%d:Runes(5): expected non-empty list", i)
89 }
90 } else {
91 if got[0] != start || got[len(got)-1] != end {
92 t.Errorf("%d:Runes(5): got %#x; want %#x..%#x", i, got, start, end)
93 }
94 }
95 if got := p.Comment(); got != "" && got != p.String(1) {
96 t.Errorf("%d:Comment(): got %v; want %v", i, got, p.String(1))
97 }
98 }
99 if err := p.Err(); err != nil {
100 t.Errorf("Parser error: %v", err)
101 }
102 if len(parts) != 0 {
103 t.Errorf("expected %d more invocations of part handler", len(parts))
104 }
105 }
106
View as plain text