stringFromTemplate_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package sft
  2. import (
  3. "testing"
  4. )
  5. type Something struct {
  6. First int
  7. Second string
  8. }
  9. const expected = `Something here 15
  10. And something else here and the string`
  11. func TestToString(t *testing.T) {
  12. const templateString = `Something here {{.First}}
  13. And something else here {{.Second}}`
  14. test := Something{First: 15, Second: "and the string"}
  15. result := ToString(templateString, test)
  16. if expected != result {
  17. t.Errorf("Wrong result: %v", result)
  18. }
  19. }
  20. func TestToStringBadTemplate(t *testing.T) {
  21. const templateString = `Something here {{.First()}}
  22. And something else here {{.Second}}`
  23. test := Something{First: 15, Second: "and the string"}
  24. result := ToString(templateString, test)
  25. if expected == result {
  26. t.Error("It should fail on template parsing!")
  27. }
  28. }
  29. func TestToStringBadObject(t *testing.T) {
  30. const templateString = `Something here {{.First}}
  31. And something else here {{.Second}}`
  32. type Something struct {
  33. Third int
  34. Second string
  35. }
  36. test := Something{Third: 15, Second: "and the string"}
  37. result := ToString(templateString, test)
  38. if expected == result {
  39. t.Error("It should fail!")
  40. }
  41. }