Skip to content
Snippets Groups Projects
expr_test.go 1.69 KiB
package expr

import (
	"fmt"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestIsExpression(t *testing.T) {
	now := time.Now()

	tests := []struct {
		name string
		eval string
		want bool
	}{
		{"equal", "i == 3", true},
		{"in array", "i in [1,2,3]", true},
		{"contains", "value contains 'some'", true},
		{"contains with . + () $ {} ^", "value contains 'something with . + () $ {} ^'", true},
		{"startsWith", "value startsWith 'some'", true},
		{"startsWith . + () $ {} ^", "value startsWith '. + () $ {} ^'", true},
		{"endsWith", "value endsWith 'some'", true},
		{"endsWith . + () $ {} ^", "value endsWith '. + () $ {} ^'", true},
		{"icontains", "icontains(value, 'some')", true},
		{"icontains with . + () $ {} ^", "icontains (value, 'something with . + () $ {} ^')", true},
		{"istartsWith", "istartsWith(value, 'Some')", true},
		{"istartsWith . + () $ {} ^ . + () $ {} ^", "istartsWith(value, '. + () $ {} ^')", true},
		{"iendsWith", "iendsWith(value, 'some')", true},
		{"iendsWith . + () $ {} ^", "iendsWith(value,'. + () $ {} ^')", true},
		{"or", "i == 2 || i > 10", true},
		{"search", "search('some') || i > 10", true},
		{"vars:or", "i == a + 2 || i > a + 10", true},
		{"near", "near(a, [55.5, 37.5], 1000)", true},
		{"within", "within(a, 'box', [[54.54, 36.36], [55.55, 37.37]])", true},
		{"time", "d > Time.Date('2021-08-31')", true},
		{"time", fmt.Sprintf("d > Time.Time('%s')", now.Format(time.RFC3339)), true},
		{"in", "In(s, [1,2,3])", true},
		{"in", "In(s, 1)", true},
		{"text search or id", "id", false},
		{"numbers", "3", false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := IsExpression(tt.eval)
			assert.Equal(t, tt.want, got)
		})
	}
}