Colin Cross | d00350c | 2017-11-17 10:55:38 -0800 | [diff] [blame] | 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 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 15 | package parser |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | var splitNTestCases = []struct { |
| 23 | in *MakeString |
| 24 | expected []*MakeString |
| 25 | sep string |
| 26 | n int |
| 27 | }{ |
| 28 | { |
| 29 | in: &MakeString{ |
Dan Willemsen | 88b4c29 | 2015-06-23 19:45:56 -0700 | [diff] [blame] | 30 | Strings: []string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 31 | "a b c", |
| 32 | "d e f", |
| 33 | " h i j", |
| 34 | }, |
Dan Willemsen | 88b4c29 | 2015-06-23 19:45:56 -0700 | [diff] [blame] | 35 | Variables: []Variable{ |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 36 | Variable{Name: SimpleMakeString("var1", NoPos)}, |
| 37 | Variable{Name: SimpleMakeString("var2", NoPos)}, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 38 | }, |
| 39 | }, |
| 40 | sep: " ", |
| 41 | n: -1, |
| 42 | expected: []*MakeString{ |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 43 | SimpleMakeString("a", NoPos), |
| 44 | SimpleMakeString("b", NoPos), |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 45 | &MakeString{ |
Dan Willemsen | 88b4c29 | 2015-06-23 19:45:56 -0700 | [diff] [blame] | 46 | Strings: []string{"c", "d"}, |
| 47 | Variables: []Variable{ |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 48 | Variable{Name: SimpleMakeString("var1", NoPos)}, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 49 | }, |
| 50 | }, |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 51 | SimpleMakeString("e", NoPos), |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 52 | &MakeString{ |
Dan Willemsen | 88b4c29 | 2015-06-23 19:45:56 -0700 | [diff] [blame] | 53 | Strings: []string{"f", ""}, |
| 54 | Variables: []Variable{ |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 55 | Variable{Name: SimpleMakeString("var2", NoPos)}, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 56 | }, |
| 57 | }, |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 58 | SimpleMakeString("h", NoPos), |
| 59 | SimpleMakeString("i", NoPos), |
| 60 | SimpleMakeString("j", NoPos), |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 61 | }, |
| 62 | }, |
| 63 | { |
| 64 | in: &MakeString{ |
Dan Willemsen | 88b4c29 | 2015-06-23 19:45:56 -0700 | [diff] [blame] | 65 | Strings: []string{ |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 66 | "a b c", |
| 67 | "d e f", |
| 68 | " h i j", |
| 69 | }, |
Dan Willemsen | 88b4c29 | 2015-06-23 19:45:56 -0700 | [diff] [blame] | 70 | Variables: []Variable{ |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 71 | Variable{Name: SimpleMakeString("var1", NoPos)}, |
| 72 | Variable{Name: SimpleMakeString("var2", NoPos)}, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 73 | }, |
| 74 | }, |
| 75 | sep: " ", |
| 76 | n: 3, |
| 77 | expected: []*MakeString{ |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 78 | SimpleMakeString("a", NoPos), |
| 79 | SimpleMakeString("b", NoPos), |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 80 | &MakeString{ |
Dan Willemsen | 88b4c29 | 2015-06-23 19:45:56 -0700 | [diff] [blame] | 81 | Strings: []string{"c", "d e f", " h i j"}, |
| 82 | Variables: []Variable{ |
Colin Cross | 08693d2 | 2016-05-25 17:25:40 -0700 | [diff] [blame] | 83 | Variable{Name: SimpleMakeString("var1", NoPos)}, |
| 84 | Variable{Name: SimpleMakeString("var2", NoPos)}, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 85 | }, |
| 86 | }, |
| 87 | }, |
| 88 | }, |
| 89 | } |
| 90 | |
| 91 | func TestMakeStringSplitN(t *testing.T) { |
| 92 | for _, test := range splitNTestCases { |
| 93 | got := test.in.SplitN(test.sep, test.n) |
| 94 | gotString := dumpArray(got) |
| 95 | expectedString := dumpArray(test.expected) |
| 96 | if gotString != expectedString { |
| 97 | t.Errorf("expected:\n%s\ngot:\n%s", expectedString, gotString) |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
Dan Willemsen | 4339853 | 2018-02-21 02:10:29 -0800 | [diff] [blame^] | 102 | var valueTestCases = []struct { |
| 103 | in *MakeString |
| 104 | expected string |
| 105 | }{ |
| 106 | { |
| 107 | in: SimpleMakeString("a b", NoPos), |
| 108 | expected: "a b", |
| 109 | }, |
| 110 | { |
| 111 | in: SimpleMakeString("a\\ \\\tb\\\\", NoPos), |
| 112 | expected: "a \tb\\", |
| 113 | }, |
| 114 | { |
| 115 | in: SimpleMakeString("a\\b\\", NoPos), |
| 116 | expected: "a\\b\\", |
| 117 | }, |
| 118 | } |
| 119 | |
| 120 | func TestMakeStringValue(t *testing.T) { |
| 121 | for _, test := range valueTestCases { |
| 122 | got := test.in.Value(nil) |
| 123 | if got != test.expected { |
| 124 | t.Errorf("\nwith: %q\nwant: %q\n got: %q", test.in.Dump(), test.expected, got) |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | var splitWordsTestCases = []struct { |
| 130 | in *MakeString |
| 131 | expected []*MakeString |
| 132 | }{ |
| 133 | { |
| 134 | in: SimpleMakeString("", NoPos), |
| 135 | expected: []*MakeString{}, |
| 136 | }, |
| 137 | { |
| 138 | in: SimpleMakeString(" a b\\ c d", NoPos), |
| 139 | expected: []*MakeString{ |
| 140 | SimpleMakeString("a", NoPos), |
| 141 | SimpleMakeString("b\\ c", NoPos), |
| 142 | SimpleMakeString("d", NoPos), |
| 143 | }, |
| 144 | }, |
| 145 | { |
| 146 | in: SimpleMakeString(" a\tb\\\t\\ c d ", NoPos), |
| 147 | expected: []*MakeString{ |
| 148 | SimpleMakeString("a", NoPos), |
| 149 | SimpleMakeString("b\\\t\\ c", NoPos), |
| 150 | SimpleMakeString("d", NoPos), |
| 151 | }, |
| 152 | }, |
| 153 | { |
| 154 | in: SimpleMakeString(`a\\ b\\\ c d`, NoPos), |
| 155 | expected: []*MakeString{ |
| 156 | SimpleMakeString(`a\\`, NoPos), |
| 157 | SimpleMakeString(`b\\\ c`, NoPos), |
| 158 | SimpleMakeString("d", NoPos), |
| 159 | }, |
| 160 | }, |
| 161 | } |
| 162 | |
| 163 | func TestMakeStringWords(t *testing.T) { |
| 164 | for _, test := range splitWordsTestCases { |
| 165 | got := test.in.Words() |
| 166 | gotString := dumpArray(got) |
| 167 | expectedString := dumpArray(test.expected) |
| 168 | if gotString != expectedString { |
| 169 | t.Errorf("with:\n%q\nexpected:\n%s\ngot:\n%s", test.in.Dump(), expectedString, gotString) |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 174 | func dumpArray(a []*MakeString) string { |
| 175 | ret := make([]string, len(a)) |
| 176 | |
| 177 | for i, s := range a { |
| 178 | ret[i] = s.Dump() |
| 179 | } |
| 180 | |
| 181 | return strings.Join(ret, "|||") |
| 182 | } |