template_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2019 Yunion
  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 conditionparser
  15. import (
  16. "testing"
  17. "yunion.io/x/jsonutils"
  18. )
  19. func TestEvalTemplate(t *testing.T) {
  20. input := jsonutils.NewDict()
  21. input.Add(jsonutils.NewString("myhost"), "host")
  22. input.Add(jsonutils.NewString("jxq"), "zone")
  23. input.Add(jsonutils.NewString("myprojId"), "project", "id")
  24. input.Add(jsonutils.NewString("my"), "project", "name")
  25. input.Add(jsonutils.NewString("my"), "proj_name")
  26. cases := []struct {
  27. input string
  28. want string
  29. }{
  30. {
  31. input: "${host}-1",
  32. want: "myhost-1",
  33. },
  34. {
  35. input: "${zone}-${host}-1",
  36. want: "jxq-myhost-1",
  37. },
  38. {
  39. input: "${zone}-${host}-${project.name}-1",
  40. want: "jxq-myhost-my-1",
  41. },
  42. {
  43. input: "${zone}-${host}-${proj_name}-1",
  44. want: "jxq-myhost-my-1",
  45. },
  46. }
  47. for _, c := range cases {
  48. got, err := EvalTemplate(c.input, input)
  49. if err != nil {
  50. t.Errorf("EvalTemplate %s error %s", c.input, err)
  51. } else if got != c.want {
  52. t.Errorf("EvalTemplate %s got %s want %s", c.input, got, c.want)
  53. }
  54. }
  55. }