stringFromTemplate_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package stringfromtemplate
  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("%v", result)
  18. } else {
  19. t.Log("String constructed properly.")
  20. }
  21. }
  22. func TestToStringBadTemplate(t *testing.T) {
  23. const templateString = `Something here {{.First()}}
  24. And something else here {{.Second}}`
  25. test := Something{First: 15, Second: "and the string"}
  26. result := ToString(templateString, test)
  27. if expected == result {
  28. t.Error("It should fail on template parsing!")
  29. } else {
  30. t.Log(result)
  31. }
  32. }
  33. func TestToStringBadObject(t *testing.T) {
  34. const templateString = `Something here {{.First}}
  35. And something else here {{.Second}}`
  36. type Something struct {
  37. Third int
  38. Second string
  39. }
  40. test := Something{Third: 15, Second: "and the string"}
  41. result := ToString(templateString, test)
  42. if expected == result {
  43. t.Error("It should fail!")
  44. } else {
  45. t.Log(result)
  46. }
  47. }