Improved test coverage

This commit is contained in:
Eduard Urbach 2024-06-26 15:24:10 +02:00
parent 68ec1467ef
commit 49b75dbda4
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
3 changed files with 60 additions and 15 deletions

View File

@ -26,15 +26,6 @@ func NewLeaf(t token.Token) *Expression {
return expr return expr
} }
// NewBinary creates a new binary operator expression.
func NewBinary(left *Expression, operator token.Token, right *Expression) *Expression {
expr := New()
expr.Token = operator
expr.AddChild(left)
expr.AddChild(right)
return expr
}
// AddChild adds a child to the expression. // AddChild adds a child to the expression.
func (expr *Expression) AddChild(child *Expression) { func (expr *Expression) AddChild(child *Expression) {
expr.Children = append(expr.Children, child) expr.Children = append(expr.Children, child)

View File

@ -1,6 +1,7 @@
package expression_test package expression_test
import ( import (
"fmt"
"testing" "testing"
"git.akyoto.dev/cli/q/src/build/expression" "git.akyoto.dev/cli/q/src/build/expression"
@ -95,12 +96,47 @@ func TestExpressionParse(t *testing.T) {
} }
} }
func BenchmarkExpression(b *testing.B) { func TestEachLeaf(t *testing.T) {
src := []byte("(1+2-3*4)+(5*6-7+8)\n") src := []byte("(1+2-3*4)+(5*6-7+8)")
tokens := token.Tokenize(src) tokens := token.Tokenize(src)
for i := 0; i < b.N; i++ {
expr := expression.Parse(tokens) expr := expression.Parse(tokens)
expr.Close() leaves := []string{}
}
expr.EachLeaf(func(leaf *expression.Expression) error {
leaves = append(leaves, leaf.Token.Text())
return nil
})
assert.DeepEqual(t, leaves, []string{"1", "2", "3", "4", "5", "6", "7", "8"})
err := expr.EachLeaf(func(leaf *expression.Expression) error {
return fmt.Errorf("error")
})
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "error")
}
func TestRemoveChild(t *testing.T) {
src := []byte("(1+2-3*4)+(5*6-7+8)")
tokens := token.Tokenize(src)
expr := expression.Parse(tokens)
left := expr.Children[0]
right := expr.Children[1]
expr.RemoveChild(left)
assert.Equal(t, expr.Children[0], right)
}
func TestNilExpression(t *testing.T) {
src := []byte("")
tokens := token.Tokenize(src)
expr := expression.Parse(tokens)
assert.Nil(t, expr)
}
func TestNilGroup(t *testing.T) {
src := []byte("()")
tokens := token.Tokenize(src)
expr := expression.Parse(tokens)
assert.Nil(t, expr)
} }

View File

@ -0,0 +1,18 @@
package expression_test
import (
"testing"
"git.akyoto.dev/cli/q/src/build/expression"
"git.akyoto.dev/cli/q/src/build/token"
)
func BenchmarkExpression(b *testing.B) {
src := []byte("(1+2-3*4)+(5*6-7+8)")
tokens := token.Tokenize(src)
for i := 0; i < b.N; i++ {
expr := expression.Parse(tokens)
expr.Close()
}
}