markdown_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // Blackfriday Markdown Processor
  3. // Available at http://github.com/russross/blackfriday
  4. //
  5. // Copyright © 2011 Russ Ross <[email protected]>.
  6. // Distributed under the Simplified BSD License.
  7. // See README.md for details.
  8. //
  9. //
  10. // Unit tests for full document parsing and rendering
  11. //
  12. package blackfriday
  13. import (
  14. "testing"
  15. )
  16. func runMarkdown(input string) string {
  17. return string(MarkdownCommon([]byte(input)))
  18. }
  19. func doTests(t *testing.T, tests []string) {
  20. // catch and report panics
  21. var candidate string
  22. defer func() {
  23. if err := recover(); err != nil {
  24. t.Errorf("\npanic while processing [%#v]: %s\n", candidate, err)
  25. }
  26. }()
  27. for i := 0; i+1 < len(tests); i += 2 {
  28. input := tests[i]
  29. candidate = input
  30. expected := tests[i+1]
  31. actual := runMarkdown(candidate)
  32. if actual != expected {
  33. t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]",
  34. candidate, expected, actual)
  35. }
  36. // now test every substring to stress test bounds checking
  37. if !testing.Short() {
  38. for start := 0; start < len(input); start++ {
  39. for end := start + 1; end <= len(input); end++ {
  40. candidate = input[start:end]
  41. _ = runMarkdown(candidate)
  42. }
  43. }
  44. }
  45. }
  46. }
  47. func TestDocument(t *testing.T) {
  48. var tests = []string{
  49. // Empty document.
  50. "",
  51. "",
  52. " ",
  53. "",
  54. // This shouldn't panic.
  55. // https://github.com/russross/blackfriday/issues/172
  56. "[]:<",
  57. "<p>[]:&lt;</p>\n",
  58. // This shouldn't panic.
  59. // https://github.com/russross/blackfriday/issues/173
  60. " [",
  61. "<p>[</p>\n",
  62. }
  63. doTests(t, tests)
  64. }