|
|
@ -16,7 +16,10 @@ |
|
|
|
|
|
|
|
|
|
|
|
package asm |
|
|
|
package asm |
|
|
|
|
|
|
|
|
|
|
|
import "testing" |
|
|
|
import ( |
|
|
|
|
|
|
|
"reflect" |
|
|
|
|
|
|
|
"testing" |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func lexAll(src string) []token { |
|
|
|
func lexAll(src string) []token { |
|
|
|
ch := Lex("test.asm", []byte(src), false) |
|
|
|
ch := Lex("test.asm", []byte(src), false) |
|
|
@ -28,9 +31,41 @@ func lexAll(src string) []token { |
|
|
|
return tokens |
|
|
|
return tokens |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestComment(t *testing.T) { |
|
|
|
func TestLexer(t *testing.T) { |
|
|
|
tokens := lexAll(";; this is a comment") |
|
|
|
tests := []struct { |
|
|
|
if len(tokens) != 2 { // {new line, EOF}
|
|
|
|
input string |
|
|
|
t.Error("expected no tokens") |
|
|
|
tokens []token |
|
|
|
|
|
|
|
}{ |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
input: ";; this is a comment", |
|
|
|
|
|
|
|
tokens: []token{{typ: lineStart}, {typ: eof}}, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
input: "0x12345678", |
|
|
|
|
|
|
|
tokens: []token{{typ: lineStart}, {typ: number, text: "0x12345678"}, {typ: eof}}, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
input: "0x123ggg", |
|
|
|
|
|
|
|
tokens: []token{{typ: lineStart}, {typ: number, text: "0x123"}, {typ: element, text: "ggg"}, {typ: eof}}, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
input: "12345678", |
|
|
|
|
|
|
|
tokens: []token{{typ: lineStart}, {typ: number, text: "12345678"}, {typ: eof}}, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
input: "123abc", |
|
|
|
|
|
|
|
tokens: []token{{typ: lineStart}, {typ: number, text: "123"}, {typ: element, text: "abc"}, {typ: eof}}, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
input: "0123abc", |
|
|
|
|
|
|
|
tokens: []token{{typ: lineStart}, {typ: number, text: "0123"}, {typ: element, text: "abc"}, {typ: eof}}, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, test := range tests { |
|
|
|
|
|
|
|
tokens := lexAll(test.input) |
|
|
|
|
|
|
|
if !reflect.DeepEqual(tokens, test.tokens) { |
|
|
|
|
|
|
|
t.Errorf("input %q\ngot: %+v\nwant: %+v", test.input, tokens, test.tokens) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|