This repository has been archived on 2026-05-13. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
sfdl/scanner/scanner_test.go
2025-12-04 18:20:07 +08:00

89 lines
1.7 KiB
Go

package scanner
import (
"testing"
)
func TestSimpleLine(t *testing.T) {
src := "origin is (1, 2);"
sc := NewScannerFromString(src)
expect := []TokenType{
ORIGIN, IS, L_BRACKET, CONST_ID, COMMA,
CONST_ID, R_BRACKET, SEMICO, NONTOKEN,
}
for i, et := range expect {
tok := sc.GetToken()
t.Logf("%s: type %d, value: %f, func: %p", tok.Lexeme, tok.Type, tok.Value, tok.Func)
if tok.Type != et {
t.Fatalf("token %d: expect %v, got %v (lexeme=%q)",
i, et, tok.Type, tok.Lexeme)
}
}
}
func TestMultiLine(t *testing.T) {
src := `
-- Arrow
color is (255,255,0);
scale is (1, 1);
size is 10;
origin is (450, 450);
rot is pi;
for t from 0 to 400 step 1 draw( t, t );
-- 心形曲线
origin is (200, 200);
rot is pi/2;
size is 5;
-- 圆润型
color is blue;
scale is (50, 50);
for t from -pi to pi step pi/200 draw((2*cos(t) - cos(2*t)), (2*sin(t)-sin(2*t)) );
-- 尖锐型
origin is (200+80, 200+80);
color is red;
rot is pi;
scale is (8, 8);
for t from 0 to 2*pi step pi/200 draw(
16*(sin(t)**3),
13*cos(t) - 5*cos(2*t) - 2*cos(3*t)-cos(4*t)
);
`
sc := NewScannerFromString(src)
expect := []TokenType{
COLOR, IS, L_BRACKET, CONST_ID, COMMA,
CONST_ID, COMMA, CONST_ID, R_BRACKET, SEMICO,
SCALE,
}
for i, et := range expect {
tok := sc.GetToken()
t.Logf("%s: type %d, value: %f, func: %p", tok.Lexeme, tok.Type, tok.Value, tok.Func)
if tok.Type != et {
t.Fatalf("token %d: expect %v, got %v (lexeme=%q)",
i, et, tok.Type, tok.Lexeme)
}
}
}
func TestWrongNumber(t *testing.T) {
data := "123.456abc"
s := NewScannerFromString(data)
for {
token := s.GetToken()
t.Logf("%s: type %d, value: %f, func: %p", token.Lexeme, token.Type, token.Value, token.Func)
if token.Type == NONTOKEN {
break
}
}
}