123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- /*
- * ECAL
- *
- * Copyright 2020 Matthias Ladkau. All rights reserved.
- *
- * This Source Code Form is subject to the terms of the MIT
- * License, If a copy of the MIT License was not distributed with this
- * file, You can obtain one at https://opensource.org/licenses/MIT.
- */
- package parser
- import (
- "fmt"
- "testing"
- )
- func TestAssignmentParsing(t *testing.T) {
- input := `
- z := a.b[1].c["3"]["test"]
- [x, y] := a.b
- `
- expectedOutput := `
- statements
- :=
- identifier: z
- identifier: a
- identifier: b
- compaccess
- number: 1
- identifier: c
- compaccess
- string: '3'
- compaccess
- string: 'test'
- :=
- list
- identifier: x
- identifier: y
- 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
- }
- }
- func TestTryContext(t *testing.T) {
- input := `
- try {
- raise("test", [1,2,3])
- } except "test", "bla" as e {
- print(1)
- } except e {
- print(1)
- } except {
- print(1)
- } finally {
- print(2)
- }
- `
- expectedOutput := `
- try
- statements
- identifier: raise
- funccall
- string: 'test'
- list
- number: 1
- number: 2
- number: 3
- except
- string: 'test'
- string: 'bla'
- as
- identifier: e
- statements
- identifier: print
- funccall
- number: 1
- except
- identifier: e
- statements
- identifier: print
- funccall
- number: 1
- except
- statements
- identifier: print
- funccall
- number: 1
- finally
- statements
- identifier: print
- funccall
- number: 2
- `[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 = `
- try {
- raise("test", [1,2,3])
- }
- `
- expectedOutput = `
- try
- statements
- identifier: raise
- funccall
- string: 'test'
- list
- number: 1
- number: 2
- 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
- }
- input = `
- try {
- raise("test", [1,2,3])
- } finally {
- }
- `
- expectedOutput = `
- try
- statements
- identifier: raise
- funccall
- string: 'test'
- list
- number: 1
- number: 2
- number: 3
- finally
- statements
- `[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 TestMutexBlock(t *testing.T) {
- input := `
- mutex foo {
- print(1)
- print(2)
- }
- `
- expectedOutput := `
- mutex
- identifier: foo
- statements
- identifier: print
- funccall
- number: 1
- identifier: print
- funccall
- number: 2
- `[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 TestLoopParsing(t *testing.T) {
- input := `
- for a != null {
- print(1);
- print(2);
- break
- continue
- }
- `
- expectedOutput := `
- loop
- guard
- !=
- identifier: a
- null
- statements
- identifier: print
- funccall
- number: 1
- identifier: print
- funccall
- number: 2
- break
- continue
- `[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 = `
- for a in range(1,2) {
- print(1);
- print(2)
- }
- `
- expectedOutput = `
- loop
- in
- identifier: a
- identifier: range
- funccall
- number: 1
- number: 2
- statements
- identifier: print
- funccall
- number: 1
- identifier: print
- funccall
- number: 2
- `[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 = `
- for a < 1 and b > 2 {
- print(1)
- print(2)
- }
- `
- expectedOutput = `
- loop
- guard
- and
- <
- identifier: a
- number: 1
- >
- identifier: b
- number: 2
- statements
- identifier: print
- funccall
- number: 1
- identifier: print
- funccall
- number: 2
- `[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 = `
- for a in range(1,2,3) {
- ==
- }
- `
- if _, err := UnitTestParse("mytest", input); err.Error() !=
- "Parse error in mytest: Term cannot start an expression (==) (Line:3 Pos:2)" {
- t.Error(err)
- return
- }
- input = `
- for a in == {
- @print(1)
- }
- `
- if _, err := UnitTestParse("mytest", input); err.Error() !=
- "Parse error in mytest: Term cannot start an expression (==) (Line:2 Pos:10)" {
- t.Error(err)
- return
- }
- }
- func TestConditionalParsing(t *testing.T) {
- input := `
- if a == b or c < d {
- print(1);
- foo := 1
- } elif x or y {
- x := 1; y := 2; p := {
- 1:2
- }
- } elif true {
- x := 1; y := 2
- } else {
- x := 1
- }
- `
- expectedOutput := `
- if
- guard
- or
- ==
- identifier: a
- identifier: b
- <
- identifier: c
- identifier: d
- statements
- identifier: print
- funccall
- number: 1
- :=
- identifier: foo
- number: 1
- guard
- or
- identifier: x
- identifier: y
- statements
- :=
- identifier: x
- number: 1
- :=
- identifier: y
- number: 2
- :=
- identifier: p
- map
- kvp
- number: 1
- number: 2
- guard
- true
- statements
- :=
- identifier: x
- number: 1
- :=
- identifier: y
- number: 2
- guard
- true
- statements
- :=
- identifier: x
- number: 1
- `[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 = `
- if a {
- print(1)
- } elif b {
- print(2)
- }
- `
- expectedOutput = `
- if
- guard
- identifier: a
- statements
- identifier: print
- funccall
- number: 1
- guard
- identifier: b
- statements
- identifier: print
- funccall
- number: 2
- `[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 = `
- if a {
- print(1)
- } else {
- print(2)
- }
- `
- expectedOutput = `
- if
- guard
- identifier: a
- statements
- identifier: print
- funccall
- number: 1
- guard
- true
- statements
- identifier: print
- funccall
- number: 2
- `[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
- }
- // Test error output
- input = `else { b }`
- if _, err := UnitTestParse("mytest", input); err.Error() !=
- "Parse error in mytest: Term cannot start an expression (<ELSE>) (Line:1 Pos:1)" {
- t.Error(err)
- return
- }
- input = `elif { b }`
- if _, err := UnitTestParse("mytest", input); err.Error() !=
- "Parse error in mytest: Term cannot start an expression (<ELIF>) (Line:1 Pos:1)" {
- t.Error(err)
- return
- }
- input = `if { b }`
- if _, err := UnitTestParse("mytest", input); err.Error() !=
- "Parse error in mytest: Unexpected end (Line:1 Pos:8)" {
- t.Error(err)
- return
- }
- input = `if == { b }`
- if _, err := UnitTestParse("mytest", input); err.Error() !=
- "Parse error in mytest: Term cannot start an expression (==) (Line:1 Pos:4)" {
- t.Error(err)
- return
- }
- input = `if x { b } elif == { c }`
- if _, err := UnitTestParse("mytest", input); err.Error() !=
- "Parse error in mytest: Term cannot start an expression (==) (Line:1 Pos:17)" {
- t.Error(err)
- return
- }
- }
|