forked from stephens2424/php
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer_test.go
145 lines (119 loc) · 3.85 KB
/
lexer_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
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
141
142
143
144
145
package php
import (
"testing"
"github.com/stephens2424/php/token"
)
var testFile = `<?php
$str = "I'm a string!";
$num = -230.098;
session_start();
function foo(barType $bar, $foobar) {
if (buzz()) {
fizzbuzz();
}
}
class MyClass {
public myMethod() {
++self::$num;
}
}
$myvar = $this->myMethod();
?>
<html>
<? echo something(); ?>
</html>`
func assertNext(t *testing.T, l *lexer, typ token.Token) Item {
i := l.nextItem()
if i.typ != typ {
t.Fatal("Incorrect lexing. Expected:", typ, "Found:", i)
}
return i
}
func assertItem(t *testing.T, i Item, expected string) {
if i.val != expected {
t.Fatal("Did not correctly parse token.", i)
}
}
func TestPHPLexer(t *testing.T) {
l := newLexer(testFile)
var i Item
i = assertNext(t, l, token.PHPBegin)
i = assertNext(t, l, token.VariableOperator)
i = assertNext(t, l, token.Identifier)
i = assertNext(t, l, token.AssignmentOperator)
i = assertNext(t, l, token.StringLiteral)
i = assertNext(t, l, token.StatementEnd)
i = assertNext(t, l, token.VariableOperator)
i = assertNext(t, l, token.Identifier)
i = assertNext(t, l, token.AssignmentOperator)
i = assertNext(t, l, token.SubtractionOperator)
i = assertNext(t, l, token.NumberLiteral)
i = assertNext(t, l, token.StatementEnd)
i = assertNext(t, l, token.Identifier)
i = assertNext(t, l, token.OpenParen)
i = assertNext(t, l, token.CloseParen)
i = assertNext(t, l, token.StatementEnd)
i = assertNext(t, l, token.Function)
i = assertNext(t, l, token.Identifier)
i = assertNext(t, l, token.OpenParen)
i = assertNext(t, l, token.Identifier)
i = assertNext(t, l, token.VariableOperator)
i = assertNext(t, l, token.Identifier)
i = assertNext(t, l, token.Comma)
i = assertNext(t, l, token.VariableOperator)
i = assertNext(t, l, token.Identifier)
i = assertNext(t, l, token.CloseParen)
i = assertNext(t, l, token.BlockBegin)
i = assertNext(t, l, token.If)
i = assertNext(t, l, token.OpenParen)
i = assertNext(t, l, token.Identifier)
i = assertNext(t, l, token.OpenParen)
i = assertNext(t, l, token.CloseParen)
i = assertNext(t, l, token.CloseParen)
i = assertNext(t, l, token.BlockBegin)
i = assertNext(t, l, token.Identifier)
i = assertNext(t, l, token.OpenParen)
i = assertNext(t, l, token.CloseParen)
i = assertNext(t, l, token.StatementEnd)
i = assertNext(t, l, token.BlockEnd)
i = assertNext(t, l, token.BlockEnd)
i = assertNext(t, l, token.Class)
i = assertNext(t, l, token.Identifier)
i = assertNext(t, l, token.BlockBegin)
i = assertNext(t, l, token.Public)
i = assertNext(t, l, token.Identifier)
i = assertNext(t, l, token.OpenParen)
i = assertNext(t, l, token.CloseParen)
i = assertNext(t, l, token.BlockBegin)
i = assertNext(t, l, token.UnaryOperator)
i = assertNext(t, l, token.Self)
i = assertNext(t, l, token.ScopeResolutionOperator)
i = assertNext(t, l, token.VariableOperator)
i = assertNext(t, l, token.Identifier)
i = assertNext(t, l, token.StatementEnd)
i = assertNext(t, l, token.BlockEnd)
i = assertNext(t, l, token.BlockEnd)
i = assertNext(t, l, token.VariableOperator)
i = assertNext(t, l, token.Identifier)
i = assertNext(t, l, token.AssignmentOperator)
i = assertNext(t, l, token.VariableOperator)
i = assertNext(t, l, token.Identifier)
i = assertNext(t, l, token.ObjectOperator)
i = assertNext(t, l, token.Identifier)
i = assertNext(t, l, token.OpenParen)
i = assertNext(t, l, token.CloseParen)
i = assertNext(t, l, token.StatementEnd)
i = assertNext(t, l, token.PHPEnd)
i = assertNext(t, l, token.HTML)
assertItem(t, i, "\n<html>\n")
i = assertNext(t, l, token.PHPBegin)
i = assertNext(t, l, token.Echo)
i = assertNext(t, l, token.Identifier)
i = assertNext(t, l, token.OpenParen)
i = assertNext(t, l, token.CloseParen)
i = assertNext(t, l, token.StatementEnd)
i = assertNext(t, l, token.PHPEnd)
i = assertNext(t, l, token.HTML)
assertItem(t, i, "\n</html>")
i = assertNext(t, l, token.EOF)
}