123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- /*
- * Public Domain Software
- *
- * I (Matthias Ladkau) am the author of the source code in this file.
- * I have placed the source code in this file in the public domain.
- *
- * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
- */
- package parser
- import (
- "fmt"
- "testing"
- )
- func TestStatementParsing(t *testing.T) {
- // Comment parsing without statements
- input := `a := 1
- b := 2; c:= 3`
- expectedOutput := `
- statements
- :=
- identifier: a
- number: 1
- :=
- identifier: b
- number: 2
- :=
- identifier: c
- number: 3
- `[1:]
- if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
- t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
- return
- }
- }
- func TestIdentifierParsing(t *testing.T) {
- input := `a := 1
- a.foo := 2
- a.b.c.foo := a.b
- `
- expectedOutput := `
- statements
- :=
- identifier: a
- number: 1
- :=
- identifier: a
- identifier: foo
- number: 2
- :=
- identifier: a
- identifier: b
- identifier: c
- identifier: foo
- identifier: a
- identifier: b
- `[1:]
- if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
- t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
- return
- }
- input = `a := b[1 + 1]
- a[4].foo["aaa"] := c[i]
- `
- expectedOutput = `
- statements
- :=
- identifier: a
- identifier: b
- compaccess
- plus
- number: 1
- number: 1
- :=
- identifier: a
- compaccess
- number: 4
- identifier: foo
- compaccess
- string: 'aaa'
- identifier: c
- compaccess
- identifier: i
- `[1:]
- if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
- t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
- return
- }
- }
- func TestCommentParsing(t *testing.T) {
- // Comment parsing without statements
- input := `/* This is a comment*/ a := 1 + 1 # foo bar`
- expectedOutput := `
- :=
- identifier: a # This is a comment
- plus
- number: 1
- number: 1 # foo bar
- `[1:]
- if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
- t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
- return
- }
- input = `/* foo */ 1 # foo bar`
- expectedOutput = `
- number: 1 # foo foo bar
- `[1:]
- if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
- t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
- return
- }
- }
|