reflect_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package sprig
  2. import (
  3. "testing"
  4. )
  5. type fixtureTO struct {
  6. Name, Value string
  7. }
  8. func TestTypeOf(t *testing.T) {
  9. f := &fixtureTO{"hello", "world"}
  10. tpl := `{{typeOf .}}`
  11. if err := runtv(tpl, "*sprig.fixtureTO", f); err != nil {
  12. t.Error(err)
  13. }
  14. }
  15. func TestKindOf(t *testing.T) {
  16. tpl := `{{kindOf .}}`
  17. f := fixtureTO{"hello", "world"}
  18. if err := runtv(tpl, "struct", f); err != nil {
  19. t.Error(err)
  20. }
  21. f2 := []string{"hello"}
  22. if err := runtv(tpl, "slice", f2); err != nil {
  23. t.Error(err)
  24. }
  25. var f3 *fixtureTO
  26. if err := runtv(tpl, "ptr", f3); err != nil {
  27. t.Error(err)
  28. }
  29. }
  30. func TestTypeIs(t *testing.T) {
  31. f := &fixtureTO{"hello", "world"}
  32. tpl := `{{if typeIs "*sprig.fixtureTO" .}}t{{else}}f{{end}}`
  33. if err := runtv(tpl, "t", f); err != nil {
  34. t.Error(err)
  35. }
  36. f2 := "hello"
  37. if err := runtv(tpl, "f", f2); err != nil {
  38. t.Error(err)
  39. }
  40. }
  41. func TestTypeIsLike(t *testing.T) {
  42. f := "foo"
  43. tpl := `{{if typeIsLike "string" .}}t{{else}}f{{end}}`
  44. if err := runtv(tpl, "t", f); err != nil {
  45. t.Error(err)
  46. }
  47. // Now make a pointer. Should still match.
  48. f2 := &f
  49. if err := runtv(tpl, "t", f2); err != nil {
  50. t.Error(err)
  51. }
  52. }
  53. func TestKindIs(t *testing.T) {
  54. f := &fixtureTO{"hello", "world"}
  55. tpl := `{{if kindIs "ptr" .}}t{{else}}f{{end}}`
  56. if err := runtv(tpl, "t", f); err != nil {
  57. t.Error(err)
  58. }
  59. f2 := "hello"
  60. if err := runtv(tpl, "f", f2); err != nil {
  61. t.Error(err)
  62. }
  63. }