preformatted_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func TestPreformatted(t *testing.T) {
  19. var cases = []struct {
  20. in string
  21. expected string
  22. }{
  23. {"", ""},
  24. {
  25. "```\nbob\n```",
  26. "\n```\nbob\n```\n\n",
  27. },
  28. {
  29. "```\nbob\n```\n```\nnotbob\n```\n",
  30. "\n```\nbob\n```\n\n```\nnotbob\n```\n\n",
  31. },
  32. {
  33. "```bob```\n",
  34. "```bob```\n",
  35. },
  36. {
  37. " ```\n bob\n ```",
  38. "\n ```\n bob\n ```\n\n",
  39. },
  40. }
  41. for i, c := range cases {
  42. in := getMungeLines(c.in)
  43. expected := getMungeLines(c.expected)
  44. actual, err := updatePreformatted("filename.md", in)
  45. assert.NoError(t, err)
  46. if !actual.Equal(expected) {
  47. t.Errorf("case[%d]: expected %q got %q", i, c.expected, actual.String())
  48. }
  49. }
  50. }
  51. func TestPreformattedImbalance(t *testing.T) {
  52. var cases = []struct {
  53. in string
  54. ok bool
  55. }{
  56. {"", true},
  57. {"```\nin\n```", true},
  58. {"```\nin\n```\nout", true},
  59. {"```", false},
  60. {"```\nin\n```\nout\n```", false},
  61. }
  62. for i, c := range cases {
  63. in := getMungeLines(c.in)
  64. out, err := checkPreformatBalance("filename.md", in)
  65. if err != nil && c.ok {
  66. t.Errorf("case[%d]: expected success", i)
  67. }
  68. if err == nil && !c.ok {
  69. t.Errorf("case[%d]: expected failure", i)
  70. }
  71. // Even in case of misformat, return all the text,
  72. // so that the user's work is not lost.
  73. if !equalMungeLines(out, in) {
  74. t.Errorf("case[%d]: expected munged text to be identical to input text", i)
  75. }
  76. }
  77. }
  78. func equalMungeLines(a, b mungeLines) bool {
  79. if len(a) != len(b) {
  80. return false
  81. }
  82. for i := range a {
  83. if a[i] != b[i] {
  84. return false
  85. }
  86. }
  87. return true
  88. }