123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- // raku_test.go
- package raku
- import (
- "strings"
- "testing"
- _ "gitlab.com/beoran/woe/monolog"
- // "gitlab.com/beoran/woe/tree"
- )
- func HelperTryLexing(me *Lexer, test *testing.T) {
- go me.Start()
- me.Advance()
- test.Logf("Lexing started:")
- test.Logf("Lexer buffer: %v", me.buffer)
- for token := range me.Output {
- test.Logf("Token %s", token)
- _ = token
- }
- }
- func LexAll(me *Lexer) []*Token {
- res := make([]*Token, 0)
- go me.Start()
- for token := range me.Output {
- res = append(res, token)
- }
- return res
- }
- func LexText(input string) []*Token {
- lexer := OpenLexer(strings.NewReader(input))
- tokens := LexAll(lexer)
- return tokens
- }
- func Assert(test *testing.T, ok bool, text string) bool {
- if !ok {
- test.Error(text)
- }
- return ok
- }
- func TestLexing(test *testing.T) {
- const input = `
- say "hello \"world\\"
- define open a door do
- set (door's open) true
- let door 's open be true
- end
- def increment variable by value do
- ( variable = ( variable + value ) )
- end
- `
- lexer := OpenLexer(strings.NewReader(input))
- HelperTryLexing(lexer, test)
- test.Log("Hi test!")
- }
- func TestLexing2(test *testing.T) {
- const input = `say`
- lexer := OpenLexer(strings.NewReader(input))
- HelperTryLexing(lexer, test)
- test.Log("Hi test!")
- }
- func TestLexing3(test *testing.T) {
- const input = `$sym`
- lexer := OpenLexer(strings.NewReader(input))
- HelperTryLexing(lexer, test)
- test.Log("Hi test!")
- }
- func TestParseValue(test *testing.T) {
- const input = `"hello \"world\\"`
- parser := NewParserForText(input)
- Assert(test, parser.ParseValue(), "Could not parse value")
- // tree.Display(parser.Ast)
- }
- func TestParseValue2(test *testing.T) {
- const input = `2.1`
- parser := NewParserForText(input)
- Assert(test, parser.ParseValue(), "Could not parse value")
- // tree.Display(parser.Ast)
- }
- func TestParseValue3(test *testing.T) {
- const input = `$sym`
- parser := NewParserForText(input)
- Assert(test, parser.ParseValue(), "Could not parse value")
- // tree.Display(parser.Ast)
- }
- func TestParseEox(test *testing.T) {
- const input = `
- `
- parser := NewParserForText(input)
- Assert(test, parser.ParseEOX(), "Could not parse EOX")
- // tree.Display(parser.Ast)
- }
- func TestParseEox2(test *testing.T) {
- const input = `.
- `
- parser := NewParserForText(input)
- Assert(test, parser.ParseEOX(), "Could not parse EOX")
- // tree.Display(parser.Ast)
- }
- func TestParseWord(test *testing.T) {
- const input = `say`
- parser := NewParserForText(input)
- Assert(test, parser.ParseWord(), "Could not parse word")
- // tree.Display(parser.Ast)
- }
- func TestParseWordExpression(test *testing.T) {
- const input = `say "hello world" three times
- `
- parser := NewParserForText(input)
- Assert(test, parser.ParseExpression(), "Could not parse word expression")
- // tree.Display(parser.Ast)
- }
- func TestParseWordExpression2(test *testing.T) {
- const input = `val + 10 * z
- `
- parser := NewParserForText(input)
- Assert(test, parser.ParseExpression(), "Could not parse word expression with operators")
- // tree.Display(parser.Ast)
- }
- func TestParseStatements(test *testing.T) {
- const input = `val + 10 * z. open door.
- `
- parser := NewParserForText(input)
- Assert(test, parser.ParseStatements(), "Could not parse statements with only a parse word expression with operators")
- // tree.Display(parser.Ast)
- }
- func TestParseProgram(test *testing.T) {
- const input = `val + 10 * z. open door.
- `
- parser := NewParserForText(input)
- Assert(test, parser.ParseProgram(), "Could not parse program.")
- // tree.Display(parser.Ast)
- }
- func TestParseProgram2(test *testing.T) {
- const input = `define greet person do
- say "hello" someone
- end
- greet bob
- greet sally
- if 0 do
- foo
- end else {
- bar
- }
- if mp < cost do
- say "Not enough mana!"
- end else do
- say "Zap!"
- end
- `
- parser := NewParserForText(input)
- Assert(test, parser.ParseProgram(), "Could not parse program.")
- // tree.Display(parser.Ast)
- // parser.Ast.ToAscii()
- }
- func TestParseblock(test *testing.T) {
- // monolog.Setup("raku_test.log", true, false)
- const input = `{
- say "hello"
- say "world"
- let i be 3 + 4
- let j be 7 + 5
- let ij be i * j
- return ij
- }
- `
- parser := NewParserForText(input)
- Assert(test, parser.ParseBlock(), "Could not parse block.")
- // tree.Display(parser.Ast)
- parser.Ast.ToAscii()
- }
- func TestParseProgram3(test *testing.T) {
- // monolog.Setup("raku_test.log", true, false)
- const input = `set foo to (3 + 4)
- `
- parser := NewParserForText(input)
- Assert(test, parser.ParseProgram(), "Could not parse program.")
- // tree.Display(parser.Ast)
- // parser.Ast.Dotty()
- }
- func TestParseParenthesis(test *testing.T) {
- // monolog.Setup("raku_test.log", true, false)
- const input = `(3 + 4 * 5)`
- parser := NewParserForText(input)
- Assert(test, parser.ParseParenthesis(), "Could not parse parenthesis.")
- // tree.Display(parser.Ast)
- parser.Ast.ToAscii()
- }
- func TestParseBlock2(test *testing.T) {
- // monolog.Setup("raku_test.log", true, false)
- const input = `{ . }`
- parser := NewParserForText(input)
- Assert(test, parser.ParseBlock(), "Could not parse block.")
- // tree.Display(parser.Ast)
- // parser.Ast.ToAscii()
- }
- func LexingTest(test *testing.T, input string, expected ...TokenType) {
- tokens := LexText(input)
- if len(tokens) != len(expected) {
- test.Errorf("Amount of tokens does not match expected amount: %d, should be %d", len(tokens), len(expected))
- }
- for index := 0; index < len(expected); index++ {
- want := expected[index]
- tok := tokens[index]
- if tok.TokenType != want {
- test.Errorf("Wrong token type recognized: %v, should be %s", tok, want)
- }
- }
- }
- func TestLexingParen(test *testing.T) {
- LexingTest(test, "(", TokenOpenParen, TokenEOF)
- LexingTest(test, "((", TokenOpenParen, TokenOpenParen, TokenEOF)
- }
- func TestLexingDoEnd(test *testing.T) {
- LexingTest(test, "do", TokenDo, TokenEOF)
- LexingTest(test, "end", TokenEnd, TokenEOF)
- LexingTest(test, "do\nend", TokenDo, TokenEOL, TokenEnd, TokenEOF)
- LexingTest(test, ".}", TokenPeriod, TokenCloseBrace, TokenEOF)
- LexingTest(test, "{.}", TokenOpenBrace, TokenPeriod, TokenCloseBrace, TokenEOF)
- }
|