stringFromTemplate_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package sft
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. type Something struct {
  7. First int
  8. Second string
  9. }
  10. const expected = `Something here 15
  11. And something else here and the string`
  12. func ExampleToString() {
  13. const templateString = `Something here {{.First}}
  14. And something else here {{.Second}}`
  15. test := Something{First: 15, Second: "and the string"}
  16. result := ToString(templateString, test)
  17. fmt.Println(result)
  18. // Output:
  19. // Something here 15
  20. // And something else here and the string
  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. }
  30. }
  31. func TestToStringBadObject(t *testing.T) {
  32. const templateString = `Something here {{.First}}
  33. And something else here {{.Second}}`
  34. type Something struct {
  35. Third int
  36. Second string
  37. }
  38. test := Something{Third: 15, Second: "and the string"}
  39. result := ToString(templateString, test)
  40. if expected == result {
  41. t.Error("It should fail!")
  42. }
  43. }