util_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. "strings"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func Test_updateMacroBlock(t *testing.T) {
  20. token := "TOKEN"
  21. BEGIN := beginMungeTag(token)
  22. END := endMungeTag(token)
  23. var cases = []struct {
  24. in string
  25. out string
  26. }{
  27. {"", ""},
  28. {"Lorem ipsum\ndolor sit amet\n",
  29. "Lorem ipsum\ndolor sit amet\n"},
  30. {"Lorem ipsum \n" + BEGIN + "\ndolor\n" + END + "\nsit amet\n",
  31. "Lorem ipsum \n" + BEGIN + "\nfoo\n" + END + "\nsit amet\n"},
  32. }
  33. for _, c := range cases {
  34. in := getMungeLines(c.in)
  35. expected := getMungeLines(c.out)
  36. actual, err := updateMacroBlock(in, token, getMungeLines("foo"))
  37. assert.NoError(t, err)
  38. if !expected.Equal(actual) {
  39. t.Errorf("Expected '%v' but got '%v'", expected.String(), expected.String())
  40. }
  41. }
  42. }
  43. func Test_updateMacroBlock_errors(t *testing.T) {
  44. token := "TOKEN"
  45. b := beginMungeTag(token)
  46. e := endMungeTag(token)
  47. var cases = []struct {
  48. in string
  49. }{
  50. {b + "\n"},
  51. {"blah\n" + b + "\nblah"},
  52. {e + "\n"},
  53. {"blah\n" + e + "\nblah\n"},
  54. {e + "\n" + b},
  55. {b + "\n" + e + "\n" + e},
  56. {b + "\n" + b + "\n" + e},
  57. {b + "\n" + b + "\n" + e + "\n" + e},
  58. }
  59. for _, c := range cases {
  60. in := getMungeLines(c.in)
  61. _, err := updateMacroBlock(in, token, getMungeLines("foo"))
  62. assert.Error(t, err)
  63. }
  64. }
  65. func TestHasLine(t *testing.T) {
  66. cases := []struct {
  67. haystack string
  68. needle string
  69. expected bool
  70. }{
  71. {"abc\ndef\nghi", "abc", true},
  72. {" abc\ndef\nghi", "abc", true},
  73. {"abc \ndef\nghi", "abc", true},
  74. {"\n abc\ndef\nghi", "abc", true},
  75. {"abc \n\ndef\nghi", "abc", true},
  76. {"abc\ndef\nghi", "def", true},
  77. {"abc\ndef\nghi", "ghi", true},
  78. {"abc\ndef\nghi", "xyz", false},
  79. }
  80. for i, c := range cases {
  81. in := getMungeLines(c.haystack)
  82. if hasLine(in, c.needle) != c.expected {
  83. t.Errorf("case[%d]: %q, expected %t, got %t", i, c.needle, c.expected, !c.expected)
  84. }
  85. }
  86. }
  87. func TestHasMacroBlock(t *testing.T) {
  88. token := "<<<"
  89. b := beginMungeTag(token)
  90. e := endMungeTag(token)
  91. cases := []struct {
  92. lines []string
  93. expected bool
  94. }{
  95. {[]string{b, e}, true},
  96. {[]string{b, "abc", e}, true},
  97. {[]string{b, b, "abc", e}, true},
  98. {[]string{b, "abc", e, e}, true},
  99. {[]string{b, e, b, e}, true},
  100. {[]string{b}, false},
  101. {[]string{e}, false},
  102. {[]string{b, "abc"}, false},
  103. {[]string{"abc", e}, false},
  104. }
  105. for i, c := range cases {
  106. in := getMungeLines(strings.Join(c.lines, "\n"))
  107. if hasMacroBlock(in, token) != c.expected {
  108. t.Errorf("case[%d]: expected %t, got %t", i, c.expected, !c.expected)
  109. }
  110. }
  111. }
  112. func TestAppendMacroBlock(t *testing.T) {
  113. token := "<<<"
  114. b := beginMungeTag(token)
  115. e := endMungeTag(token)
  116. cases := []struct {
  117. in []string
  118. expected []string
  119. }{
  120. {[]string{}, []string{b, e}},
  121. {[]string{"bob"}, []string{"bob", "", b, e}},
  122. {[]string{b, e}, []string{b, e, "", b, e}},
  123. }
  124. for i, c := range cases {
  125. in := getMungeLines(strings.Join(c.in, "\n"))
  126. expected := getMungeLines(strings.Join(c.expected, "\n"))
  127. out := appendMacroBlock(in, token)
  128. if !out.Equal(expected) {
  129. t.Errorf("Case[%d]: expected '%q' but got '%q'", i, expected.String(), out.String())
  130. }
  131. }
  132. }
  133. func TestPrependMacroBlock(t *testing.T) {
  134. token := "<<<"
  135. b := beginMungeTag(token)
  136. e := endMungeTag(token)
  137. cases := []struct {
  138. in []string
  139. expected []string
  140. }{
  141. {[]string{}, []string{b, e}},
  142. {[]string{"bob"}, []string{b, e, "", "bob"}},
  143. {[]string{b, e}, []string{b, e, "", b, e}},
  144. }
  145. for i, c := range cases {
  146. in := getMungeLines(strings.Join(c.in, "\n"))
  147. expected := getMungeLines(strings.Join(c.expected, "\n"))
  148. out := prependMacroBlock(token, in)
  149. if !out.Equal(expected) {
  150. t.Errorf("Case[%d]: expected '%q' but got '%q'", i, expected.String(), out.String())
  151. }
  152. }
  153. }