eval_test.go 908 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package tengo_test
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/d5/tengo/v2"
  6. "github.com/d5/tengo/v2/require"
  7. )
  8. func TestEval(t *testing.T) {
  9. eval := func(
  10. expr string,
  11. params map[string]interface{},
  12. expected interface{},
  13. ) {
  14. ctx := context.Background()
  15. actual, err := tengo.Eval(ctx, expr, params)
  16. require.NoError(t, err)
  17. require.Equal(t, expected, actual)
  18. }
  19. eval(`undefined`, nil, nil)
  20. eval(`1`, nil, int64(1))
  21. eval(`19 + 23`, nil, int64(42))
  22. eval(`"foo bar"`, nil, "foo bar")
  23. eval(`[1, 2, 3][1]`, nil, int64(2))
  24. eval(
  25. `5 + p`,
  26. map[string]interface{}{
  27. "p": 7,
  28. },
  29. int64(12),
  30. )
  31. eval(
  32. `"seven is " + p`,
  33. map[string]interface{}{
  34. "p": 7,
  35. },
  36. "seven is 7",
  37. )
  38. eval(
  39. `"" + a + b`,
  40. map[string]interface{}{
  41. "a": 7,
  42. "b": " is seven",
  43. },
  44. "7 is seven",
  45. )
  46. eval(
  47. `a ? "success" : "fail"`,
  48. map[string]interface{}{
  49. "a": 1,
  50. },
  51. "success",
  52. )
  53. }