From 9574a26da7fca6f50a6e4ae867ce1bcf04b4c2f6 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 16 Feb 2025 23:42:05 +0100 Subject: [PATCH] Added more tests --- src/expression/Expression_test.go | 10 +++++--- src/expression/Parse.go | 1 - src/token/List_test.go | 42 ++++++++++++++++++++----------- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/expression/Expression_test.go b/src/expression/Expression_test.go index 64a09ba..143b2d4 100644 --- a/src/expression/Expression_test.go +++ b/src/expression/Expression_test.go @@ -98,15 +98,19 @@ func TestParse(t *testing.T) { {"Function calls 24", "a((b(1,2,bb())+c(3,4,cc(0)))*d(5,6,dd(0)),e(7+8,9-10*11,12,ee(0)))", "(λ a (* (+ (λ b 1 2 (λ bb)) (λ c 3 4 (λ cc 0))) (λ d 5 6 (λ dd 0))) (λ e (+ 7 8) (- 9 (* 10 11)) 12 (λ ee 0)))"}, {"Function calls 25", "a(1-2*3)", "(λ a (- 1 (* 2 3)))"}, {"Function calls 26", "1+2*a()+4", "(+ (+ 1 (* 2 (λ a))) 4)"}, - {"Function calls 27", "sum(a,b)*2+15*4", "(+ (* (λ sum a b) 2) (* 15 4))"}, + {"Function calls 27", "a(b,c)*2+15*4", "(+ (* (λ a b c) 2) (* 15 4))"}, - {"Package function calls", "math.sum(a,b)", "(λ (. math sum) a b)"}, - {"Package function calls 2", "generic.math.sum(a,b)", "(λ (. (. generic math) sum) a b)"}, + {"Package function calls", "a.b(c)", "(λ (. a b) c)"}, + {"Package function calls 2", "a.b(c,d)", "(λ (. a b) c d)"}, + {"Package function calls 3", "a.b.c(d,e)", "(λ (. (. a b) c) d e)"}, {"Array access", "a[0]", "(@ a 0)"}, {"Array access 2", "a[b+c]", "(@ a (+ b c))"}, {"Array access 3", "a.b[c]", "(@ (. a b) c)"}, {"Array access 4", "a.b[c+d]", "(@ (. a b) (+ c d))"}, + {"Array access 5", "a()[b]", "(@ (λ a) b)"}, + {"Array access 6", "a.b()[c]", "(@ (λ (. a b)) c)"}, + // {"Array access 7", "a.b(c)[d]", "(@ (λ (. a b) c) d)"}, } for _, test := range tests { diff --git a/src/expression/Parse.go b/src/expression/Parse.go index 3ed513e..f839ac8 100644 --- a/src/expression/Parse.go +++ b/src/expression/Parse.go @@ -36,7 +36,6 @@ func Parse(tokens []token.Token) *Expression { // Function call or array access if isComplete(cursor) { parameters := NewList(tokens[groupPosition:i]) - node := New() node.Token.Position = tokens[groupPosition].Position diff --git a/src/token/List_test.go b/src/token/List_test.go index 9daf6a9..9b3638f 100644 --- a/src/token/List_test.go +++ b/src/token/List_test.go @@ -32,20 +32,6 @@ func TestSplit(t *testing.T) { assert.DeepEqual(t, parameters, []string{"1+2", "3*4", "5*6", "7+8"}) } -func TestSplitGroups(t *testing.T) { - src := []byte("f(1,2),g(3,4)") - tokens := token.Tokenize(src) - parameters := []string{} - - err := tokens.Split(func(parameter token.List) error { - parameters = append(parameters, parameter.Text(src)) - return nil - }) - - assert.Nil(t, err) - assert.DeepEqual(t, parameters, []string{"f(1,2)", "g(3,4)"}) -} - func TestSplitEmpty(t *testing.T) { tokens := token.List{} @@ -67,3 +53,31 @@ func TestSplitError(t *testing.T) { assert.NotNil(t, err) assert.Equal(t, err.Error(), "error") } + +func TestSplitGroups(t *testing.T) { + src := []byte("f(1,2),g(3,4)") + tokens := token.Tokenize(src) + parameters := []string{} + + err := tokens.Split(func(parameter token.List) error { + parameters = append(parameters, parameter.Text(src)) + return nil + }) + + assert.Nil(t, err) + assert.DeepEqual(t, parameters, []string{"f(1,2)", "g(3,4)"}) +} + +func TestSplitSingle(t *testing.T) { + src := []byte("123") + tokens := token.Tokenize(src) + parameters := []string{} + + err := tokens.Split(func(parameter token.List) error { + parameters = append(parameters, parameter.Text(src)) + return nil + }) + + assert.Nil(t, err) + assert.DeepEqual(t, parameters, []string{"123"}) +}