parser_func_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package parser
  10. import (
  11. "fmt"
  12. "testing"
  13. )
  14. func TestImportParsing(t *testing.T) {
  15. input := `import "foo/bar.ecal" as foobar
  16. i := foobar`
  17. expectedOutput := `
  18. statements
  19. import
  20. string: 'foo/bar.ecal'
  21. identifier: foobar
  22. :=
  23. identifier: i
  24. identifier: foobar
  25. `[1:]
  26. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  27. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  28. return
  29. }
  30. }
  31. func TestFunctionCalling(t *testing.T) {
  32. input := `import "foo/bar.ecal" as foobar
  33. foobar.test()`
  34. expectedOutput := `
  35. statements
  36. import
  37. string: 'foo/bar.ecal'
  38. identifier: foobar
  39. identifier: foobar
  40. identifier: test
  41. funccall
  42. `[1:]
  43. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  44. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  45. return
  46. }
  47. input = `a := 1
  48. a().foo := x2.foo()
  49. a.b.c().foo := a()
  50. `
  51. expectedOutput = `
  52. statements
  53. :=
  54. identifier: a
  55. number: 1
  56. :=
  57. identifier: a
  58. funccall
  59. identifier: foo
  60. identifier: x2
  61. identifier: foo
  62. funccall
  63. :=
  64. identifier: a
  65. identifier: b
  66. identifier: c
  67. funccall
  68. identifier: foo
  69. identifier: a
  70. funccall
  71. `[1:]
  72. if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
  73. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  74. return
  75. }
  76. input = `a(1+2).foo := x2.foo(foo)
  77. a.b.c(x()).foo := a(1,a(),3, x, y) + 1
  78. `
  79. expectedOutput = `
  80. statements
  81. :=
  82. identifier: a
  83. funccall
  84. plus
  85. number: 1
  86. number: 2
  87. identifier: foo
  88. identifier: x2
  89. identifier: foo
  90. funccall
  91. identifier: foo
  92. :=
  93. identifier: a
  94. identifier: b
  95. identifier: c
  96. funccall
  97. identifier: x
  98. funccall
  99. identifier: foo
  100. plus
  101. identifier: a
  102. funccall
  103. number: 1
  104. identifier: a
  105. funccall
  106. number: 3
  107. identifier: x
  108. identifier: y
  109. number: 1
  110. `[1:]
  111. if res, err := UnitTestParseWithPPResult("mytest", input, ""); err != nil || fmt.Sprint(res) != expectedOutput {
  112. t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
  113. return
  114. }
  115. }