make_strings_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2017 Google Inc. All rights reserved.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package parser
  15. import (
  16. "strings"
  17. "testing"
  18. )
  19. var splitNTestCases = []struct {
  20. in *MakeString
  21. expected []*MakeString
  22. sep string
  23. n int
  24. }{
  25. {
  26. in: &MakeString{
  27. Strings: []string{
  28. "a b c",
  29. "d e f",
  30. " h i j",
  31. },
  32. Variables: []Variable{
  33. Variable{Name: SimpleMakeString("var1", NoPos)},
  34. Variable{Name: SimpleMakeString("var2", NoPos)},
  35. },
  36. },
  37. sep: " ",
  38. n: -1,
  39. expected: []*MakeString{
  40. SimpleMakeString("a", NoPos),
  41. SimpleMakeString("b", NoPos),
  42. &MakeString{
  43. Strings: []string{"c", "d"},
  44. Variables: []Variable{
  45. Variable{Name: SimpleMakeString("var1", NoPos)},
  46. },
  47. },
  48. SimpleMakeString("e", NoPos),
  49. &MakeString{
  50. Strings: []string{"f", ""},
  51. Variables: []Variable{
  52. Variable{Name: SimpleMakeString("var2", NoPos)},
  53. },
  54. },
  55. SimpleMakeString("h", NoPos),
  56. SimpleMakeString("i", NoPos),
  57. SimpleMakeString("j", NoPos),
  58. },
  59. },
  60. {
  61. in: &MakeString{
  62. Strings: []string{
  63. "a b c",
  64. "d e f",
  65. " h i j",
  66. },
  67. Variables: []Variable{
  68. Variable{Name: SimpleMakeString("var1", NoPos)},
  69. Variable{Name: SimpleMakeString("var2", NoPos)},
  70. },
  71. },
  72. sep: " ",
  73. n: 3,
  74. expected: []*MakeString{
  75. SimpleMakeString("a", NoPos),
  76. SimpleMakeString("b", NoPos),
  77. &MakeString{
  78. Strings: []string{"c", "d e f", " h i j"},
  79. Variables: []Variable{
  80. Variable{Name: SimpleMakeString("var1", NoPos)},
  81. Variable{Name: SimpleMakeString("var2", NoPos)},
  82. },
  83. },
  84. },
  85. },
  86. }
  87. func TestMakeStringSplitN(t *testing.T) {
  88. for _, test := range splitNTestCases {
  89. got := test.in.SplitN(test.sep, test.n)
  90. gotString := dumpArray(got)
  91. expectedString := dumpArray(test.expected)
  92. if gotString != expectedString {
  93. t.Errorf("expected:\n%s\ngot:\n%s", expectedString, gotString)
  94. }
  95. }
  96. }
  97. var valueTestCases = []struct {
  98. in *MakeString
  99. expected string
  100. }{
  101. {
  102. in: SimpleMakeString("a b", NoPos),
  103. expected: "a b",
  104. },
  105. {
  106. in: SimpleMakeString("a\\ \\\tb\\\\", NoPos),
  107. expected: "a \tb\\",
  108. },
  109. {
  110. in: SimpleMakeString("a\\b\\", NoPos),
  111. expected: "a\\b\\",
  112. },
  113. }
  114. func TestMakeStringValue(t *testing.T) {
  115. for _, test := range valueTestCases {
  116. got := test.in.Value(nil)
  117. if got != test.expected {
  118. t.Errorf("\nwith: %q\nwant: %q\n got: %q", test.in.Dump(), test.expected, got)
  119. }
  120. }
  121. }
  122. var splitWordsTestCases = []struct {
  123. in *MakeString
  124. expected []*MakeString
  125. }{
  126. {
  127. in: SimpleMakeString("", NoPos),
  128. expected: []*MakeString{},
  129. },
  130. {
  131. in: SimpleMakeString(" a b\\ c d", NoPos),
  132. expected: []*MakeString{
  133. SimpleMakeString("a", NoPos),
  134. SimpleMakeString("b\\ c", NoPos),
  135. SimpleMakeString("d", NoPos),
  136. },
  137. },
  138. {
  139. in: SimpleMakeString(" a\tb\\\t\\ c d ", NoPos),
  140. expected: []*MakeString{
  141. SimpleMakeString("a", NoPos),
  142. SimpleMakeString("b\\\t\\ c", NoPos),
  143. SimpleMakeString("d", NoPos),
  144. },
  145. },
  146. {
  147. in: SimpleMakeString(`a\\ b\\\ c d`, NoPos),
  148. expected: []*MakeString{
  149. SimpleMakeString(`a\\`, NoPos),
  150. SimpleMakeString(`b\\\ c`, NoPos),
  151. SimpleMakeString("d", NoPos),
  152. },
  153. },
  154. }
  155. func TestMakeStringWords(t *testing.T) {
  156. for _, test := range splitWordsTestCases {
  157. got := test.in.Words()
  158. gotString := dumpArray(got)
  159. expectedString := dumpArray(test.expected)
  160. if gotString != expectedString {
  161. t.Errorf("with:\n%q\nexpected:\n%s\ngot:\n%s", test.in.Dump(), expectedString, gotString)
  162. }
  163. }
  164. }
  165. func dumpArray(a []*MakeString) string {
  166. ret := make([]string, len(a))
  167. for i, s := range a {
  168. ret[i] = s.Dump()
  169. }
  170. return strings.Join(ret, "|||")
  171. }