Added more tests

This commit is contained in:
Eduard Urbach 2025-02-16 23:42:05 +01:00
parent 216e66473e
commit 9574a26da7
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
3 changed files with 35 additions and 18 deletions

View File

@ -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 {

View File

@ -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

View File

@ -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"})
}