Debian Codegraph @

internal/test/t1/t1_test.go

raw symbols SCIP
1 package t1
2
3 import (
4 "testing"
5
6 "github.com/goccmack/gocc/internal/test/t1/errors"
7 "github.com/goccmack/gocc/internal/test/t1/lexer"
8 "github.com/goccmack/gocc/internal/test/t1/parser"
9 "github.com/goccmack/gocc/internal/test/t1/token"
10 )
11
12 type Equals interface {
13 Equals(interface{}) bool
14 }
15
16 func Test1(t *testing.T) {
17 ast, err := parser.NewParser().Parse(lexer.NewLexer([]byte(`c`)))
18 if err != nil {
19 t.Fail()
20 }
21 if slice, ok := ast.([]interface{}); !ok {
22 t.Fail()
23 } else {
24 if len(slice) != 2 {
25 t.Fatalf("len(slice)==%d", len(slice))
26 }
27 if slice[0] != nil {
28 t.Fatal("slice[0] != nil")
29 }
30 if str, ok := slice[1].(string); !ok {
31 t.Fatalf("%T", slice[1])
32 } else {
33 if str != "c" {
34 t.Fatal(`str != "c"`)
35 }
36 }
37 }
38 }
39
40 func Test2(t *testing.T) {
41 ast, err := parser.NewParser().Parse(lexer.NewLexer([]byte(`b c`)))
42 if err != nil {
43 t.Fatal(err)
44 }
45 if slice, ok := ast.([]interface{}); !ok {
46 t.Fail()
47 } else {
48 if len(slice) != 2 {
49 t.Fatalf("len(slice)==%d", len(slice))
50 }
51 if str, ok := slice[0].(string); !ok {
52 t.Fatal(`str, ok := slice[0].(string); !ok`)
53 } else {
54 if str != "b" {
55 t.Fatal(`str != "b"`)
56 }
57 }
58 if str, ok := slice[1].(string); !ok {
59 t.Fatalf("%T", slice[1])
60 } else {
61 if str != "c" {
62 t.Fatal(`str != "c"`)
63 }
64 }
65 }
66 }
67
68 func Test3(t *testing.T) {
69 toks := lexer.NewLexer([]byte(`c b`))
70 _, err := parser.NewParser().Parse(toks)
71 if err == nil {
72 t.Fatal("No error for erronous input.")
73 }
74 if errs, ok := err.(*errors.Error); !ok {
75 t.Fatal("Incompatible error type for erronous input.")
76 } else {
77 if errs.ErrorToken.Column != 3 {
78 t.Fatal("errs.ErrorToken.Column = ", errs.ErrorToken.Column, " != 3")
79 }
80 }
81 }
82
83 // Tests that require access to the generated go, but interact directly
84 // with components.
85
86 func mockToken(name string, tokenType token.Type, line, column int) *token.Token {
87 return &token.Token{
88 Type: tokenType,
89 Lit: []byte(name),
90 Pos: token.Pos{Offset: (line-1)*80 + column, Line: line, Column: column},
91 }
92 }
93
94 // assertEqualityIs will verify that lhs.Equals(rhs) and rhs.Equals(lhs) is true/false
95 // depending on 'equality'.
96 func assertEqualityIs(t *testing.T, lhs, rhs Equals, equality bool) {
97 t.Helper()
98 if lhs.Equals(rhs) != equality {
99 t.Fatalf("expected lhs.Equals(rhs) to be %v: %v vs %v", equality, lhs, rhs)
100 }
101 if rhs.Equals(lhs) != equality {
102 t.Fatalf("expected rhs.Equals(lhs) to be %v: %v vs %v", equality, rhs, lhs)
103 }
104 }
105
106 func assertEqual(t *testing.T, lhs, rhs Equals) {
107 t.Helper()
108 assertEqualityIs(t, lhs, rhs, true)
109 }
110
111 func assertNotEqual(t *testing.T, lhs, rhs Equals) {
112 t.Helper()
113 assertEqualityIs(t, lhs, rhs, false)
114 }
115
116 func Test_Token(t *testing.T) {
117 var (
118 t1 = mockToken("1st", 101, 7, 11)
119 t2 = mockToken("2nd", 9010, 15, 22)
120 t3 = mockToken("3rd", 101, 7, 15)
121 t4 = mockToken("bad", token.INVALID, 1, 1)
122 t5 = mockToken("bad", token.EOF, 1, 1)
123 )
124 // none should be equal: assertNotEqual tests symterrically, so we only need
125 // to compare one way.
126 assertNotEqual(t, t1, t2)
127 assertNotEqual(t, t1, t3)
128 assertNotEqual(t, t1, t4)
129 assertNotEqual(t, t2, t5)
130 assertNotEqual(t, t2, t3)
131 assertNotEqual(t, t2, t4)
132 assertNotEqual(t, t2, t5)
133 assertNotEqual(t, t3, t4)
134 assertNotEqual(t, t3, t5)
135 assertNotEqual(t, t4, t5)
136
137 // They should self-equal, because of the short-circuit
138 assertEqual(t, t1, t1)
139 assertEqual(t, t2, t2)
140 assertEqual(t, t3, t3)
141 assertEqual(t, t4, t4)
142 assertEqual(t, t5, t5)
143
144 // Test an explicit value match by creating a new token that
145 // does not match t1 and incrementally bringing it closer.
146 altT1 := mockToken("x", 1000, 100, 10)
147 assertNotEqual(t, t1, altT1)
148
149 altT1.Type = t1.Type
150 assertNotEqual(t, t1, altT1)
151
152 altT1.Lit = []byte("1st")
153 altT1.Type = 999
154 assertNotEqual(t, t1, altT1)
155
156 // meet the equality criteria.
157 altT1.Type = t1.Type
158 assertEqual(t, t1, altT1)
159 }