statement_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ast
  15. import (
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func TestFieldResult(t *testing.T) {
  20. fields := Fields{
  21. {
  22. Name: "Name",
  23. AName: "AName",
  24. Expr: &IndexExpr{},
  25. },
  26. {
  27. Name: "Name",
  28. AName: "AName",
  29. Expr: &FieldRef{},
  30. },
  31. {
  32. Name: "Name",
  33. AName: "",
  34. Expr: nil,
  35. },
  36. {
  37. Name: "",
  38. AName: "AName",
  39. Expr: nil,
  40. },
  41. }
  42. fields.node()
  43. fields.Swap(0, 1)
  44. assert.Equal(t, Field{
  45. Name: "Name",
  46. AName: "AName",
  47. Expr: &FieldRef{},
  48. }, fields[0])
  49. assert.Equal(t, 4, fields.Len())
  50. for _, f := range fields {
  51. if f.AName != "" {
  52. assert.Equal(t, f.AName, f.GetName())
  53. assert.False(t, f.IsColumn())
  54. assert.True(t, f.IsSelectionField())
  55. } else {
  56. if _, ok := f.Expr.(*FieldRef); ok {
  57. assert.True(t, f.IsColumn())
  58. assert.True(t, f.IsSelectionField())
  59. } else {
  60. assert.False(t, f.IsColumn())
  61. assert.False(t, f.IsSelectionField())
  62. }
  63. assert.Equal(t, f.Name, f.GetName())
  64. }
  65. }
  66. }