documents_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package documents_test
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "testing"
  7. "testing/iotest"
  8. "cdop.pt/go/free/platepipe/documents"
  9. "cdop.pt/go/free/platepipe/documents/markdown"
  10. . "cdop.pt/go/open/assertive"
  11. )
  12. func TestMarkdown(t *testing.T) {
  13. t.Run("no file", func(t *testing.T) {
  14. _, _, err := documents.FromFile("non-existent-file.md")
  15. Need(t, err != nil)
  16. Want(t, err.Error() ==
  17. "open non-existent-file.md: no such file or directory")
  18. })
  19. t.Run("markdown to html error", func(t *testing.T) {
  20. f := mkTestFile(t, "md-processing-error-*.md", "[link](broken")
  21. defer os.Remove(f)
  22. oldConv := markdown.ToHTML
  23. markdown.ToHTML = func([]byte, io.Writer) error {
  24. return fmt.Errorf("markdown: error processing buffer")
  25. }
  26. text, data, err := documents.FromFile(f)
  27. markdown.ToHTML = oldConv
  28. Need(t, err != nil)
  29. Want(t, err.Error() == "markdown: error processing buffer")
  30. Want(t, len(data) == 0)
  31. Want(t, len(text) == 0)
  32. })
  33. t.Run("success", func(t *testing.T) {
  34. f := mkTestFile(t, "success-*.md",
  35. "key = 'value'\n\n# header\n\nparagraph")
  36. defer os.Remove(f)
  37. text, data, err := documents.FromFile(f)
  38. Need(t, err == nil)
  39. Want(t, fmt.Sprint(data) == fmt.Sprint(map[string]any{"key": "value"}))
  40. Want(t, string(text) == "<h1>header</h1>\n<p>paragraph</p>\n")
  41. })
  42. }
  43. func TestText(t *testing.T) {
  44. t.Run("no file", func(t *testing.T) {
  45. _, _, err := documents.FromFile("non-existent-file.txt")
  46. Need(t, err != nil)
  47. Want(t, err.Error() ==
  48. "open non-existent-file.txt: no such file or directory")
  49. })
  50. t.Run("no metadata", func(t *testing.T) {
  51. f := mkTestFile(t, "no-metadata-*.txt", "no metadata in this file")
  52. defer os.Remove(f)
  53. text, data, err := documents.FromFile(f)
  54. Need(t, err == nil)
  55. Want(t, len(data) == 0)
  56. Want(t, string(text) == "no metadata in this file")
  57. })
  58. t.Run("metadata error", func(t *testing.T) {
  59. f := mkTestFile(t, "metadata-error-*.txt", "key: value\n\ntext")
  60. defer os.Remove(f)
  61. text, data, err := documents.FromFile(f)
  62. Need(t, err == nil)
  63. Want(t, len(data) == 0)
  64. Want(t, string(text) == "key: value\n\ntext")
  65. })
  66. t.Run("success", func(t *testing.T) {
  67. f := mkTestFile(t, "success-*.txt", "key = 'value'\n\ntext")
  68. defer os.Remove(f)
  69. text, data, err := documents.FromFile(f)
  70. Need(t, err == nil)
  71. Want(t, fmt.Sprint(data) == fmt.Sprint(map[string]any{"key": "value"}))
  72. Want(t, string(text) == "text")
  73. })
  74. }
  75. func TestReadErrors(t *testing.T) {
  76. msg := "error reading document"
  77. r := iotest.ErrReader(fmt.Errorf(msg))
  78. _, _, err := documents.FromMarkdownStream(r)
  79. Need(t, err != nil)
  80. Want(t, err.Error() == msg)
  81. }
  82. func mkTestFile(t *testing.T, pattern, content string) string {
  83. f, err := os.CreateTemp("", pattern)
  84. Need(t, err == nil)
  85. name := f.Name()
  86. _, err = f.WriteString(content)
  87. Need(t, err == nil)
  88. err = f.Sync()
  89. Need(t, err == nil)
  90. err = f.Close()
  91. Need(t, err == nil)
  92. return name
  93. }