Florian Weikert | d7b64bd | 2015-09-29 14:10:16 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All Rights Reserved. |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 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 | |
| 15 | package com.google.devtools.build.lib.syntax; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.fail; |
| 20 | |
Florian Weikert | 2591e19 | 2015-10-05 14:24:51 +0000 | [diff] [blame] | 21 | import com.google.common.base.Joiner; |
| 22 | import com.google.common.base.Strings; |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableMap; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.cmdline.Label; |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.syntax.SkylarkList.MutableList; |
| 26 | import com.google.devtools.build.lib.syntax.SkylarkList.Tuple; |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 27 | |
| 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
| 30 | import org.junit.runners.JUnit4; |
| 31 | |
| 32 | import java.util.Arrays; |
Florian Weikert | f31b947 | 2015-08-04 16:36:58 +0000 | [diff] [blame] | 33 | import java.util.HashMap; |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 34 | import java.util.IllegalFormatException; |
Florian Weikert | 2591e19 | 2015-10-05 14:24:51 +0000 | [diff] [blame] | 35 | import java.util.LinkedList; |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 36 | import java.util.List; |
| 37 | import java.util.Map; |
| 38 | |
| 39 | /** |
| 40 | * Test properties of the evaluator's datatypes and utility functions |
| 41 | * without actually creating any parse trees. |
| 42 | */ |
| 43 | @RunWith(JUnit4.class) |
| 44 | public class PrinterTest { |
| 45 | |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 46 | @Test |
| 47 | public void testPrinter() throws Exception { |
| 48 | // Note that prettyPrintValue and printValue only differ on behaviour of |
| 49 | // labels and strings at toplevel. |
| 50 | assertEquals("foo\nbar", Printer.str("foo\nbar")); |
| 51 | assertEquals("\"foo\\nbar\"", Printer.repr("foo\nbar")); |
| 52 | assertEquals("'", Printer.str("'")); |
| 53 | assertEquals("\"'\"", Printer.repr("'")); |
| 54 | assertEquals("\"", Printer.str("\"")); |
| 55 | assertEquals("\"\\\"\"", Printer.repr("\"")); |
| 56 | assertEquals("3", Printer.str(3)); |
| 57 | assertEquals("3", Printer.repr(3)); |
Francois-Rene Rideau | 0f7ba34 | 2015-08-31 16:16:21 +0000 | [diff] [blame] | 58 | assertEquals("None", Printer.repr(Runtime.NONE)); |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 59 | |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 60 | assertEquals("//x:x", Printer.str( |
| 61 | Label.parseAbsolute("//x"))); |
| 62 | assertEquals("\"//x:x\"", Printer.repr( |
| 63 | Label.parseAbsolute("//x"))); |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 64 | |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 65 | List<?> list = MutableList.of(null, "foo", "bar"); |
| 66 | List<?> tuple = Tuple.of("foo", "bar"); |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 67 | |
| 68 | assertEquals("(1, [\"foo\", \"bar\"], 3)", |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 69 | Printer.str(Tuple.of(1, list, 3))); |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 70 | assertEquals("(1, [\"foo\", \"bar\"], 3)", |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 71 | Printer.repr(Tuple.of(1, list, 3))); |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 72 | assertEquals("[1, (\"foo\", \"bar\"), 3]", |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 73 | Printer.str(MutableList.of(null, 1, tuple, 3))); |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 74 | assertEquals("[1, (\"foo\", \"bar\"), 3]", |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 75 | Printer.repr(MutableList.of(null, 1, tuple, 3))); |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 76 | |
Lukacs Berki | cc9ac3f | 2015-06-16 06:42:41 +0000 | [diff] [blame] | 77 | Map<Object, Object> dict = ImmutableMap.<Object, Object>of( |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 78 | 1, tuple, |
| 79 | 2, list, |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 80 | "foo", MutableList.of(null)); |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 81 | assertEquals("{1: (\"foo\", \"bar\"), 2: [\"foo\", \"bar\"], \"foo\": []}", |
| 82 | Printer.str(dict)); |
| 83 | assertEquals("{1: (\"foo\", \"bar\"), 2: [\"foo\", \"bar\"], \"foo\": []}", |
| 84 | Printer.repr(dict)); |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Francois-Rene Rideau | 8d5cce3 | 2015-06-16 23:12:04 +0000 | [diff] [blame] | 87 | private void checkFormatPositionalFails(String errorMessage, String format, Object... arguments) { |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 88 | try { |
Francois-Rene Rideau | 8d5cce3 | 2015-06-16 23:12:04 +0000 | [diff] [blame] | 89 | Printer.format(format, arguments); |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 90 | fail(); |
| 91 | } catch (IllegalFormatException e) { |
| 92 | assertThat(e).hasMessage(errorMessage); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | @Test |
Florian Weikert | f31b947 | 2015-08-04 16:36:58 +0000 | [diff] [blame] | 97 | public void testSortedOutputOfUnsortedMap() throws Exception { |
| 98 | Map<Integer, Integer> map = new HashMap<>(); |
| 99 | int[] data = {5, 7, 3}; |
| 100 | |
| 101 | for (int current : data) { |
| 102 | map.put(current, current); |
| 103 | } |
| 104 | assertThat(Printer.str(map)).isEqualTo("{3: 3, 5: 5, 7: 7}"); |
| 105 | } |
| 106 | |
| 107 | @Test |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 108 | public void testFormatPositional() throws Exception { |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 109 | assertEquals("foo 3", Printer.formatToString("%s %d", Tuple.of("foo", 3))); |
Francois-Rene Rideau | 8d5cce3 | 2015-06-16 23:12:04 +0000 | [diff] [blame] | 110 | assertEquals("foo 3", Printer.format("%s %d", "foo", 3)); |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 111 | |
Francois-Rene Rideau | 304e195 | 2015-08-25 13:30:10 +0000 | [diff] [blame] | 112 | // Note: formatToString doesn't perform scalar x -> (x) conversion; |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 113 | // The %-operator is responsible for that. |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 114 | assertThat(Printer.formatToString("", Tuple.of())).isEmpty(); |
Francois-Rene Rideau | 8d5cce3 | 2015-06-16 23:12:04 +0000 | [diff] [blame] | 115 | assertEquals("foo", Printer.format("%s", "foo")); |
| 116 | assertEquals("3.14159", Printer.format("%s", 3.14159)); |
| 117 | checkFormatPositionalFails("not all arguments converted during string formatting", |
| 118 | "%s", 1, 2, 3); |
| 119 | assertEquals("%foo", Printer.format("%%%s", "foo")); |
| 120 | checkFormatPositionalFails("not all arguments converted during string formatting", |
| 121 | "%%s", "foo"); |
| 122 | checkFormatPositionalFails("unsupported format character \" \" at index 1 in \"% %s\"", |
| 123 | "% %s", "foo"); |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 124 | assertEquals("[1, 2, 3]", Printer.format("%s", MutableList.of(null, 1, 2, 3))); |
| 125 | assertEquals("(1, 2, 3)", Printer.format("%s", Tuple.of(1, 2, 3))); |
| 126 | assertEquals("[]", Printer.format("%s", MutableList.of(null))); |
| 127 | assertEquals("()", Printer.format("%s", Tuple.of())); |
Francois-Rene Rideau | 8d5cce3 | 2015-06-16 23:12:04 +0000 | [diff] [blame] | 128 | assertEquals("% 1 \"2\" 3", Printer.format("%% %d %r %s", 1, "2", "3")); |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 129 | |
Francois-Rene Rideau | 8d5cce3 | 2015-06-16 23:12:04 +0000 | [diff] [blame] | 130 | checkFormatPositionalFails( |
| 131 | "invalid argument \"1\" for format pattern %d", |
| 132 | "%d", "1"); |
| 133 | checkFormatPositionalFails("unsupported format character \".\" at index 1 in \"%.3g\"", |
| 134 | "%.3g"); |
| 135 | checkFormatPositionalFails("unsupported format character \".\" at index 1 in \"%.3g\"", |
| 136 | "%.3g", 1, 2); |
| 137 | checkFormatPositionalFails("unsupported format character \".\" at index 1 in \"%.s\"", |
| 138 | "%.s"); |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 141 | @Test |
Florian Weikert | f07e544 | 2015-07-01 13:08:43 +0000 | [diff] [blame] | 142 | public void testSingleQuotes() throws Exception { |
| 143 | assertThat(Printer.str("test", '\'')).isEqualTo("test"); |
| 144 | assertThat(Printer.repr("test", '\'')).isEqualTo("'test'"); |
| 145 | |
| 146 | assertEquals("'\\''", Printer.repr("'", '\'')); |
| 147 | assertEquals("\"", Printer.str("\"", '\'')); |
| 148 | assertEquals("'\"'", Printer.repr("\"", '\'')); |
| 149 | |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 150 | List<?> list = MutableList.of(null, "foo", "bar"); |
| 151 | List<?> tuple = Tuple.of("foo", "bar"); |
Florian Weikert | f07e544 | 2015-07-01 13:08:43 +0000 | [diff] [blame] | 152 | |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 153 | assertThat(Printer.str(Tuple.of(1, list, 3), '\'')).isEqualTo("(1, ['foo', 'bar'], 3)"); |
| 154 | assertThat(Printer.repr(Tuple.of(1, list, 3), '\'')).isEqualTo("(1, ['foo', 'bar'], 3)"); |
| 155 | assertThat(Printer.str(MutableList.of(null, 1, tuple, 3), '\'')) |
| 156 | .isEqualTo("[1, ('foo', 'bar'), 3]"); |
| 157 | assertThat(Printer.repr(MutableList.of(null, 1, tuple, 3), '\'')) |
| 158 | .isEqualTo("[1, ('foo', 'bar'), 3]"); |
Florian Weikert | f07e544 | 2015-07-01 13:08:43 +0000 | [diff] [blame] | 159 | |
| 160 | Map<Object, Object> dict = |
Francois-Rene Rideau | a2c9ac6 | 2016-01-22 10:54:38 +0000 | [diff] [blame] | 161 | ImmutableMap.<Object, Object>of(1, tuple, 2, list, "foo", MutableList.of(null)); |
Florian Weikert | f07e544 | 2015-07-01 13:08:43 +0000 | [diff] [blame] | 162 | |
| 163 | assertThat(Printer.str(dict, '\'')) |
| 164 | .isEqualTo("{1: ('foo', 'bar'), 2: ['foo', 'bar'], 'foo': []}"); |
| 165 | assertThat(Printer.repr(dict, '\'')) |
| 166 | .isEqualTo("{1: ('foo', 'bar'), 2: ['foo', 'bar'], 'foo': []}"); |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 167 | } |
Florian Weikert | 2591e19 | 2015-10-05 14:24:51 +0000 | [diff] [blame] | 168 | |
| 169 | @Test |
| 170 | public void testListLimitStringLength() throws Exception { |
| 171 | int lengthDivisibleByTwo = Printer.SUGGESTED_CRITICAL_LIST_ELEMENTS_STRING_LENGTH; |
| 172 | if (lengthDivisibleByTwo % 2 == 1) { |
| 173 | ++lengthDivisibleByTwo; |
| 174 | } |
| 175 | String limit = Strings.repeat("x", lengthDivisibleByTwo); |
| 176 | String half = Strings.repeat("x", lengthDivisibleByTwo / 2); |
| 177 | |
| 178 | List<String> list = Arrays.asList(limit + limit); |
| 179 | |
| 180 | // String is way too long -> shorten. |
| 181 | assertThat(printListWithLimit(list)).isEqualTo("[\"" + limit + "...\"]"); |
| 182 | |
| 183 | LinkedList<List<String>> nestedList = new LinkedList<>(); |
| 184 | nestedList.add(list); |
| 185 | |
| 186 | // Same as above, but with one additional level of indirection. |
| 187 | assertThat(printListWithLimit(nestedList)).isEqualTo("[[\"" + limit + "...\"]]"); |
| 188 | |
| 189 | // The inner list alone would meet the limit, but because of the first element, it has to be |
| 190 | // shortened. |
| 191 | assertThat(printListWithLimit(Arrays.asList(half, Arrays.asList(limit)))) |
| 192 | .isEqualTo("[\"" + half + "\", [\"" + half + "...\"]]"); |
| 193 | |
| 194 | // String is too long, but the ellipsis make it even longer. |
| 195 | assertThat(printListWithLimit(Arrays.asList(limit + "x"))).isEqualTo("[\"" + limit + "...\"]"); |
| 196 | |
| 197 | // We hit the limit exactly -> everything is printed. |
| 198 | assertThat(printListWithLimit(Arrays.asList(limit))).isEqualTo("[\"" + limit + "\"]"); |
| 199 | |
| 200 | // Exact hit, but with two arguments -> everything is printed. |
| 201 | assertThat(printListWithLimit(Arrays.asList(half, half))) |
| 202 | .isEqualTo("[\"" + half + "\", \"" + half + "\"]"); |
| 203 | |
| 204 | // First argument hits the limit -> remaining argument is shortened. |
| 205 | assertThat(printListWithLimit(Arrays.asList(limit, limit))) |
| 206 | .isEqualTo("[\"" + limit + "\", \"...\"]"); |
| 207 | |
| 208 | String limitMinusOne = limit.substring(0, limit.length() - 1); |
| 209 | |
| 210 | // First arguments is one below the limit -> print first character of remaining argument. |
| 211 | assertThat(printListWithLimit(Arrays.asList(limitMinusOne, limit))) |
| 212 | .isEqualTo("[\"" + limitMinusOne + "\", \"x...\"]"); |
| 213 | |
| 214 | // First argument hits the limit -> we skip the remaining two arguments. |
| 215 | assertThat(printListWithLimit(Arrays.asList(limit, limit, limit))) |
| 216 | .isEqualTo("[\"" + limit + "\", <2 more arguments>]"); |
| 217 | } |
| 218 | |
| 219 | @Test |
| 220 | public void testListLimitTooManyArgs() throws Exception { |
| 221 | StringBuilder builder = new StringBuilder(); |
| 222 | List<Integer> maxLength = new LinkedList<>(); |
| 223 | |
| 224 | int next; |
| 225 | for (next = 0; next < Printer.SUGGESTED_CRITICAL_LIST_ELEMENTS_COUNT; ++next) { |
| 226 | maxLength.add(next); |
| 227 | if (next > 0) { |
| 228 | builder.append(", "); |
| 229 | } |
| 230 | builder.append(next); |
| 231 | } |
| 232 | |
| 233 | // There is one too many, but we print every argument nonetheless. |
| 234 | maxLength.add(next); |
| 235 | assertThat(printListWithLimit(maxLength)).isEqualTo("[" + builder + ", " + next + "]"); |
| 236 | |
| 237 | // There are two too many, hence we don't print them. |
| 238 | ++next; |
| 239 | maxLength.add(next); |
| 240 | assertThat(printListWithLimit(maxLength)).isEqualTo("[" + builder + ", <2 more arguments>]"); |
| 241 | } |
| 242 | |
| 243 | @Test |
| 244 | public void testPrintListDefaultNoLimit() throws Exception { |
| 245 | List<Integer> list = new LinkedList<>(); |
| 246 | // Make sure that the resulting string is longer than the suggestion. This should also lead to |
| 247 | // way more items than suggested. |
| 248 | for (int i = 0; i < Printer.SUGGESTED_CRITICAL_LIST_ELEMENTS_STRING_LENGTH * 2; ++i) { |
| 249 | list.add(i); |
| 250 | } |
| 251 | assertThat(Printer.str(list)).isEqualTo(String.format("[%s]", Joiner.on(", ").join(list))); |
| 252 | } |
| 253 | |
| 254 | private String printListWithLimit(List<?> list) { |
| 255 | return printList(list, Printer.SUGGESTED_CRITICAL_LIST_ELEMENTS_COUNT, |
| 256 | Printer.SUGGESTED_CRITICAL_LIST_ELEMENTS_STRING_LENGTH); |
| 257 | } |
| 258 | |
| 259 | private String printList(List<?> list, int criticalElementsCount, int criticalStringLength) { |
| 260 | StringBuilder builder = new StringBuilder(); |
| 261 | Printer.printList( |
| 262 | builder, list, "[", ", ", "]", "", '"', criticalElementsCount, criticalStringLength); |
| 263 | return builder.toString(); |
| 264 | } |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 265 | } |