markdown_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package static
  2. import (
  3. "html/template"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. )
  10. var parseTemplate *template.Template
  11. var readfileError, templateError, createError, mkdirallError, parseError, walkError error
  12. var mockOperation = func(b []byte) []byte { return b }
  13. func init() {
  14. readfile = func(_ string) ([]byte, error) { return nil, readfileError }
  15. create = func(_ string) (*os.File, error) { return nil, createError }
  16. mkdirall = func(_ string, _ os.FileMode) error { return mkdirallError }
  17. parseFiles = func(...string) (*template.Template, error) { return parseTemplate, parseError }
  18. walk = func(_ string, _ filepath.WalkFunc) error { return walkError }
  19. }
  20. type mockLogger struct{}
  21. func (self *mockLogger) Error(_ string, _ ...interface{}) {}
  22. func (self *mockLogger) Debug(_ string, _ ...interface{}) {}
  23. func (self *mockLogger) Info(_ string, _ ...interface{}) {}
  24. type mockTemplate struct{}
  25. func (self *mockTemplate) Execute(_ io.Writer, _ interface{}) error { return templateError }
  26. type mockFileInfo struct {
  27. N string
  28. S int64
  29. Fm uint32
  30. T time.Time
  31. D bool
  32. So interface{}
  33. }
  34. func (self *mockFileInfo) Name() string { return self.N }
  35. func (self *mockFileInfo) Size() int64 { return self.S }
  36. func (self *mockFileInfo) Mode() os.FileMode { return os.FileMode(self.Fm) }
  37. func (self *mockFileInfo) ModTime() time.Time { return self.T }
  38. func (self *mockFileInfo) IsDir() bool { return self.D }
  39. func (self *mockFileInfo) Sys() interface{} { return self.So }
  40. func TestIor(t *testing.T) {
  41. t.Parallel()
  42. g := Markdown{}
  43. if s := g.ior("some.md"); len(s) == 0 {
  44. t.FailNow()
  45. }
  46. }
  47. func TestDepth(t *testing.T) {
  48. t.Parallel()
  49. absp := "/abs/path/"
  50. g := Markdown{Output: absp}
  51. // test abs depth
  52. if d := g.depth("somefile"); len(d) > 0 {
  53. t.FailNow()
  54. }
  55. // test relative depth
  56. g.Relative = true
  57. if d := g.depth(absp + "somefile"); len(d) == 0 {
  58. t.Logf("Path: %s\n", d)
  59. t.FailNow()
  60. }
  61. }
  62. func TestWalk(t *testing.T) {
  63. t.Parallel()
  64. g := Markdown{}
  65. p := "valid.md"
  66. var f os.FileInfo = &mockFileInfo{S: 1}
  67. var e error
  68. // test with valid file
  69. if err := g.walk(p, f, e); err != nil {
  70. t.FailNow()
  71. }
  72. }
  73. func TestMulti(t *testing.T) {
  74. g := Markdown{L: &mockLogger{}, template: &mockTemplate{}, pages: []string{"fuck.md", "deeper/than/index.md", "deeper/than/data.md"}}
  75. // set expected defaults
  76. notExist = false
  77. statError = nil
  78. // no pages
  79. if e := g.multi(mockOperation); e != nil {
  80. t.FailNow()
  81. }
  82. // test full pass
  83. if e := g.multi(mockOperation); e != nil {
  84. t.FailNow()
  85. }
  86. // test full pass relative
  87. g.Relative = true
  88. if e := g.multi(mockOperation); e != nil {
  89. t.FailNow()
  90. }
  91. // test failing execute
  92. templateError = mockError
  93. if e := g.multi(mockOperation); e == nil {
  94. t.FailNow()
  95. }
  96. // test failing file creation
  97. createError = mockError
  98. if e := g.multi(mockOperation); e == nil {
  99. t.FailNow()
  100. }
  101. // test failing to read the file
  102. readfileError = mockError
  103. if e := g.multi(mockOperation); e == nil {
  104. t.FailNow()
  105. }
  106. // test dir creation failure
  107. mkdirallError = mockError
  108. statError = mockError
  109. if e := g.multi(mockOperation); e == nil {
  110. t.FailNow()
  111. }
  112. }
  113. func TestSingle(t *testing.T) {
  114. g := Markdown{L: &mockLogger{}, template: &mockTemplate{}, pages: []string{"fuck.md", "deeper/than/index.md", "deeper/than/data.md"}}
  115. // reset expected defaults
  116. statError = nil
  117. readfileError = nil
  118. createError = nil
  119. templateError = nil
  120. // test full pass
  121. if e := g.single(mockOperation); e != nil {
  122. t.FailNow()
  123. }
  124. // test failing execute
  125. templateError = mockError
  126. if e := g.single(mockOperation); e == nil {
  127. t.FailNow()
  128. }
  129. // test create error
  130. createError = mockError
  131. if e := g.single(mockOperation); e == nil {
  132. t.FailNow()
  133. }
  134. // test fail mkdirall
  135. mkdirallError = mockError
  136. statError = mockError
  137. if e := g.single(mockOperation); e == nil {
  138. t.FailNow()
  139. }
  140. // test fail readfile
  141. readfileError = mockError
  142. if e := g.single(mockOperation); e == nil {
  143. t.FailNow()
  144. }
  145. }
  146. func TestGenerate(t *testing.T) {
  147. g := Markdown{L: &mockLogger{}}
  148. // set template for stand-alone execution
  149. parseTemplate = template.New("test")
  150. // test full pass
  151. if e := g.Generate(mockOperation); e != nil {
  152. t.FailNow()
  153. }
  154. // test book mode full pass
  155. g.Book = true
  156. if e := g.Generate(mockOperation); e == nil {
  157. t.FailNow()
  158. }
  159. // test walk error
  160. walkError = mockError
  161. if e := g.Generate(mockOperation); e == nil {
  162. t.FailNow()
  163. }
  164. // test template error
  165. parseError = mockError
  166. if e := g.Generate(mockOperation); e == nil {
  167. t.FailNow()
  168. }
  169. }