123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- /*
- * 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 (
- "testing"
- )
- func TestErrorHandling(t *testing.T) {
- input := "c:= a + b"
- astres, err := ParseWithRuntime("mytest", input, &DummyRuntimeProvider{})
- if err != nil {
- t.Errorf("Unexpected parser output:\n%vError: %v", astres, err)
- return
- }
- // Make ast invalid
- astres.Children[1].Children[1] = nil
- ppres, err := PrettyPrint(astres)
- if err == nil || err.Error() != "Nil pointer in AST" {
- t.Errorf("Unexpected result: %v error: %v", ppres, err)
- return
- }
- }
- func TestArithmeticExpressionPrinting(t *testing.T) {
- input := "a + b * 5 /2-1"
- expectedOutput := `
- minus
- plus
- identifier: a
- div
- times
- identifier: b
- number: 5
- number: 2
- number: 1
- `[1:]
- if err := UnitTestPrettyPrinting(input, expectedOutput,
- "a + b * 5 / 2 - 1"); err != nil {
- t.Error(err)
- return
- }
- input = `-a + "\"'b"`
- expectedOutput = `
- plus
- minus
- identifier: a
- string: '"'b'
- `[1:]
- if err := UnitTestPrettyPrinting(input, expectedOutput,
- `-a + "\"'b"`); err != nil {
- t.Error(err)
- return
- }
- input = `a // 5 % (50 + 1)`
- expectedOutput = `
- modint
- divint
- identifier: a
- number: 5
- plus
- number: 50
- number: 1
- `[1:]
- if err := UnitTestPrettyPrinting(input, expectedOutput,
- `a // 5 % (50 + 1)`); err != nil {
- t.Error(err)
- return
- }
- input = "(a + 1) * 5 / (6 - 2)"
- expectedOutput = `
- div
- times
- plus
- identifier: a
- number: 1
- number: 5
- minus
- number: 6
- number: 2
- `[1:]
- if err := UnitTestPrettyPrinting(input, expectedOutput,
- "(a + 1) * 5 / (6 - 2)"); err != nil {
- t.Error(err)
- return
- }
- input = "a + (1 * 5) / 6 - 2"
- expectedOutput = `
- minus
- plus
- identifier: a
- div
- times
- number: 1
- number: 5
- number: 6
- number: 2
- `[1:]
- if err := UnitTestPrettyPrinting(input, expectedOutput,
- "a + 1 * 5 / 6 - 2"); err != nil {
- t.Error(err)
- return
- }
- }
- func TestLogicalExpressionPrinting(t *testing.T) {
- input := "not (a + 1) * 5 and tRue or not 1 - 5 != '!test'"
- expectedOutput := `
- or
- and
- not
- times
- plus
- identifier: a
- number: 1
- number: 5
- true
- not
- !=
- minus
- number: 1
- number: 5
- string: '!test'
- `[1:]
- if err := UnitTestPrettyPrinting(input, expectedOutput,
- "not (a + 1) * 5 and true or not 1 - 5 != \"!test\""); err != nil {
- t.Error(err)
- return
- }
- input = "not x < null and a > b or 1 <= c and 2 >= false or c == true"
- expectedOutput = `
- or
- or
- and
- not
- <
- identifier: x
- null
- >
- identifier: a
- identifier: b
- and
- <=
- number: 1
- identifier: c
- >=
- number: 2
- false
- ==
- identifier: c
- true
- `[1:]
- if err := UnitTestPrettyPrinting(input, expectedOutput,
- "not x < null and a > b or 1 <= c and 2 >= false or c == true"); err != nil {
- t.Error(err)
- return
- }
- input = "a hasPrefix 'a' and b hassuffix 'c' or d like '^.*' and 3 notin x"
- expectedOutput = `
- or
- and
- hasprefix
- identifier: a
- string: 'a'
- hassuffix
- identifier: b
- string: 'c'
- and
- like
- identifier: d
- string: '^.*'
- notin
- number: 3
- identifier: x
- `[1:]
- if err := UnitTestPrettyPrinting(input, expectedOutput,
- `a hasprefix "a" and b hassuffix "c" or d like "^.*" and 3 notin x`); err != nil {
- t.Error(err)
- return
- }
- }
- func TestSpecialCasePrinting1(t *testing.T) {
- input := `a := {"a":1,"b":1,"c":1,"d" : 1, "e":1,"f":1,"g":1,"h":1,}`
- if err := UnitTestPrettyPrinting(input, "",
- `a := {
- "a" : 1,
- "b" : 1,
- "c" : 1,
- "d" : 1,
- "e" : 1,
- "f" : 1,
- "g" : 1,
- "h" : 1
- }`); err != nil {
- t.Error(err)
- return
- }
- input = `a := {"a":1,"b":1,"c":1,"d" : {"a":1,"b":{"a":1,"b":1,"c":1,"d":1},"c":1,"d" : 1, "e":1,"f":{"a":1,"b":1},"g":1,"h":1,}, "e":1,"f":1,"g":1,"h":1,}`
- if err := UnitTestPrettyPrinting(input, "",
- `a := {
- "a" : 1,
- "b" : 1,
- "c" : 1,
- "d" : {
- "a" : 1,
- "b" : {
- "a" : 1,
- "b" : 1,
- "c" : 1,
- "d" : 1
- },
- "c" : 1,
- "d" : 1,
- "e" : 1,
- "f" : {"a" : 1, "b" : 1},
- "g" : 1,
- "h" : 1
- },
- "e" : 1,
- "f" : 1,
- "g" : 1,
- "h" : 1
- }`); err != nil {
- t.Error(err)
- return
- }
- input = `a := [1,2,3,[1,2,[1,2],3,[1,2,3,4],[1,2,3,4,5],4,5],4,5]`
- if err := UnitTestPrettyPrinting(input, "",
- `a := [
- 1,
- 2,
- 3,
- [
- 1,
- 2,
- [1, 2],
- 3,
- [1, 2, 3, 4],
- [
- 1,
- 2,
- 3,
- 4,
- 5
- ],
- 4,
- 5
- ],
- 4,
- 5
- ]`); err != nil {
- t.Error(err)
- return
- }
- input = `a := [1,2,3,[1,2,{"a":1,"b":1,"c":1,"d":1},3,[1,2,3,4],4,5],4,5]`
- if err := UnitTestPrettyPrinting(input, "",
- `a := [
- 1,
- 2,
- 3,
- [
- 1,
- 2,
- {
- "a" : 1,
- "b" : 1,
- "c" : 1,
- "d" : 1
- },
- 3,
- [1, 2, 3, 4],
- 4,
- 5
- ],
- 4,
- 5
- ]`); err != nil {
- t.Error(err)
- return
- }
- }
- func TestSpecialCasePrinting2(t *testing.T) {
- input := `
- a := 1
- a := 2
- sink RegisterNewPlayer
- kindmatch ["foo",2]
- statematch {"a":1,"b":1,"c":1,"d":1}
- scopematch []
- suppresses ["abs"]
- priority 0
- {
- log("1223")
- log("1223")
- func foo (z=[1,2,3,4,5]) {
- a := 1
- b := 2
- }
- log("1223")
- try {
- x := [1,2,3,4]
- raise("test 12", null, [1,2,3])
- } except e {
- p := 1
- }
- }
- `
- if err := UnitTestPrettyPrinting(input, "",
- `a := 1
- a := 2
- sink RegisterNewPlayer
- kindmatch ["foo", 2]
- statematch {
- "a" : 1,
- "b" : 1,
- "c" : 1,
- "d" : 1
- }
- scopematch []
- suppresses ["abs"]
- priority 0
- {
- log("1223")
- log("1223")
- func foo(z=[
- 1,
- 2,
- 3,
- 4,
- 5
- ]) {
- a := 1
- b := 2
- }
- log("1223")
- try {
- x := [1, 2, 3, 4]
- raise("test 12", null, [1, 2, 3])
- } except e {
- p := 1
- }
- }`); err != nil {
- t.Error(err)
- }
- input = `
- /*
- Some initial comment
-
- bla
- */
- a := 1
- func aaa() {
- mutex myresource {
- globalResource := "new value"
- }
- func myfunc(a, b, c=1) {
- a := 1 + 1 # Test
- }
- x := [ 1,2,3,4,5]
- a:=1;b:=1
- /*Foo*/
- Foo := {
- "super" : [ Bar ]
-
- /*
- * Object IDs
- */
- "id" : 0 # aaaa
- "idx" : 0
- /*Constructor*/
- "init" : func(id)
- {
- super[0]()
- this.id := id
- }
- /*
- Return the object ID
- */
- "getId" : func() {
- return this.idx
- }
- /*
- Set the object ID
- */
- "setId" : func(id) {
- this.idx := id
- }
- }
- for a in range(2, 10, 2) {
- a := 1
- }
- for a > 0 {
- a := 1
- }
- if a == 1 {
- a := a + 1
- } elif a == 2 {
- a := a + 2
- } else {
- a := 99
- }
- try {
- raise("MyError", "My error message", [1,2,3])
- } except "MyError" as e {
- log(e)
- }
- }
- b:=1
- `
- if err := UnitTestPrettyPrinting(input, "",
- `/*
-
- Some initial comment
-
- bla
- */
- a := 1
- func aaa() {
- mutex myresource {
- globalResource := "new value"
- }
-
- func myfunc(a, b, c=1) {
- a := 1 + 1 # Test
- }
- x := [
- 1,
- 2,
- 3,
- 4,
- 5
- ]
- a := 1
- b := 1
- /* Foo */
- Foo := {
- "super" : [Bar],
- /*
- * Object IDs
- */
- "id" : 0 # aaaa,
- "idx" : 0,
- /* Constructor */
- "init" : func (id) {
- super[0]()
- this.id := id
- },
- /*
- Return the object ID
- */
- "getId" : func () {
- return this.idx
- },
- /*
- Set the object ID
- */
- "setId" : func (id) {
- this.idx := id
- }
- }
- for a in range(2, 10, 2) {
- a := 1
- }
- for a > 0 {
- a := 1
- }
- if a == 1 {
- a := a + 1
- } elif a == 2 {
- a := a + 2
- } else {
- a := 99
- }
- try {
- raise("MyError", "My error message", [1, 2, 3])
- } except "MyError" as e {
- log(e)
- }
- }
- b := 1`); err != nil {
- t.Error(err)
- return
- }
- }
|