variable_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package tengo_test
  2. import (
  3. "testing"
  4. "github.com/d5/tengo/v2"
  5. "github.com/d5/tengo/v2/require"
  6. )
  7. type VariableTest struct {
  8. Name string
  9. Value interface{}
  10. ValueType string
  11. IntValue int
  12. Int64Value int64
  13. FloatValue float64
  14. CharValue rune
  15. BoolValue bool
  16. StringValue string
  17. Object tengo.Object
  18. IsUndefined bool
  19. }
  20. func TestVariable(t *testing.T) {
  21. vars := []VariableTest{
  22. {
  23. Name: "a",
  24. Value: int64(1),
  25. ValueType: "int",
  26. IntValue: 1,
  27. Int64Value: 1,
  28. FloatValue: 1.0,
  29. CharValue: rune(1),
  30. BoolValue: true,
  31. StringValue: "1",
  32. Object: tengo.Int{Value: 1},
  33. },
  34. {
  35. Name: "b",
  36. Value: "52.11",
  37. ValueType: "string",
  38. FloatValue: 52.11,
  39. StringValue: "52.11",
  40. BoolValue: true,
  41. Object: &tengo.String{Value: "52.11"},
  42. },
  43. {
  44. Name: "c",
  45. Value: true,
  46. ValueType: "bool",
  47. IntValue: 1,
  48. Int64Value: 1,
  49. FloatValue: 0,
  50. BoolValue: true,
  51. StringValue: "true",
  52. Object: tengo.TrueValue,
  53. },
  54. {
  55. Name: "d",
  56. Value: nil,
  57. ValueType: "undefined",
  58. Object: tengo.UndefinedValue,
  59. IsUndefined: true,
  60. },
  61. }
  62. for _, tc := range vars {
  63. v, err := tengo.NewVariable(tc.Name, tc.Value)
  64. require.NoError(t, err)
  65. require.Equal(t, tc.Value, v.Value(), "Name: %s", tc.Name)
  66. require.Equal(t, tc.ValueType, v.ValueType(), "Name: %s", tc.Name)
  67. require.Equal(t, tc.IntValue, v.Int(), "Name: %s", tc.Name)
  68. require.Equal(t, tc.Int64Value, v.Int64(), "Name: %s", tc.Name)
  69. require.Equal(t, tc.FloatValue, v.Float(), "Name: %s", tc.Name)
  70. require.Equal(t, tc.CharValue, v.Char(), "Name: %s", tc.Name)
  71. require.Equal(t, tc.BoolValue, v.Bool(), "Name: %s", tc.Name)
  72. require.Equal(t, tc.StringValue, v.String(), "Name: %s", tc.Name)
  73. require.Equal(t, tc.Object, v.Object(), "Name: %s", tc.Name)
  74. require.Equal(t, tc.IsUndefined, v.IsUndefined(), "Name: %s", tc.Name)
  75. }
  76. }