blob: 5adbd10b4e4a09a0f6952b648a9d0ee57ba96547 [file] [log] [blame]
Florian Weikertd7b64bd2015-09-29 14:10:16 +00001// Copyright 2015 The Bazel Authors. All Rights Reserved.
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +00002//
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
15package com.google.devtools.build.lib.syntax;
16
17import static com.google.common.truth.Truth.assertThat;
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000018import static org.junit.Assert.fail;
19
Florian Weikert2591e192015-10-05 14:24:51 +000020import com.google.common.base.Joiner;
21import com.google.common.base.Strings;
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000022import com.google.common.collect.ImmutableMap;
Lukacs Berki6e91eb92015-09-21 09:12:37 +000023import com.google.devtools.build.lib.cmdline.Label;
vladmos6ff634d2017-07-05 10:25:01 -040024import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter;
25import com.google.devtools.build.lib.skylarkinterface.SkylarkValue;
Francois-Rene Rideaua2c9ac62016-01-22 10:54:38 +000026import com.google.devtools.build.lib.syntax.SkylarkList.MutableList;
27import com.google.devtools.build.lib.syntax.SkylarkList.Tuple;
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000028import java.util.Arrays;
29import java.util.IllegalFormatException;
lberkiaea56b32017-05-30 12:35:33 +020030import java.util.LinkedHashMap;
Florian Weikert2591e192015-10-05 14:24:51 +000031import java.util.LinkedList;
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000032import java.util.List;
33import java.util.Map;
lberkiaea56b32017-05-30 12:35:33 +020034import org.junit.Test;
35import org.junit.runner.RunWith;
36import org.junit.runners.JUnit4;
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000037
38/**
39 * Test properties of the evaluator's datatypes and utility functions
40 * without actually creating any parse trees.
41 */
42@RunWith(JUnit4.class)
43public class PrinterTest {
44
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000045 @Test
46 public void testPrinter() throws Exception {
47 // Note that prettyPrintValue and printValue only differ on behaviour of
48 // labels and strings at toplevel.
vladmoscd6d8ae2017-10-12 15:35:17 +020049 assertThat(Printer.str(createObjWithStr())).isEqualTo("<str marker>");
50 assertThat(Printer.repr(createObjWithStr())).isEqualTo("<repr marker>");
51
lberkiaea56b32017-05-30 12:35:33 +020052 assertThat(Printer.str("foo\nbar")).isEqualTo("foo\nbar");
53 assertThat(Printer.repr("foo\nbar")).isEqualTo("\"foo\\nbar\"");
54 assertThat(Printer.str("'")).isEqualTo("'");
55 assertThat(Printer.repr("'")).isEqualTo("\"'\"");
56 assertThat(Printer.str("\"")).isEqualTo("\"");
57 assertThat(Printer.repr("\"")).isEqualTo("\"\\\"\"");
58 assertThat(Printer.str(3)).isEqualTo("3");
59 assertThat(Printer.repr(3)).isEqualTo("3");
60 assertThat(Printer.repr(Runtime.NONE)).isEqualTo("None");
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000061
lberkiaea56b32017-05-30 12:35:33 +020062 assertThat(Printer.str(Label.parseAbsolute("//x"))).isEqualTo("//x:x");
vladmos6ff634d2017-07-05 10:25:01 -040063 assertThat(Printer.repr(Label.parseAbsolute("//x"))).isEqualTo("Label(\"//x:x\")");
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000064
Francois-Rene Rideaua2c9ac62016-01-22 10:54:38 +000065 List<?> list = MutableList.of(null, "foo", "bar");
66 List<?> tuple = Tuple.of("foo", "bar");
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000067
lberkiaea56b32017-05-30 12:35:33 +020068 assertThat(Printer.str(Tuple.of(1, list, 3))).isEqualTo("(1, [\"foo\", \"bar\"], 3)");
69 assertThat(Printer.repr(Tuple.of(1, list, 3))).isEqualTo("(1, [\"foo\", \"bar\"], 3)");
70 assertThat(Printer.str(MutableList.of(null, 1, tuple, 3)))
71 .isEqualTo("[1, (\"foo\", \"bar\"), 3]");
72 assertThat(Printer.repr(MutableList.of(null, 1, tuple, 3)))
73 .isEqualTo("[1, (\"foo\", \"bar\"), 3]");
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000074
Lukacs Berkicc9ac3f2015-06-16 06:42:41 +000075 Map<Object, Object> dict = ImmutableMap.<Object, Object>of(
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000076 1, tuple,
77 2, list,
Francois-Rene Rideaua2c9ac62016-01-22 10:54:38 +000078 "foo", MutableList.of(null));
lberkiaea56b32017-05-30 12:35:33 +020079 assertThat(Printer.str(dict))
80 .isEqualTo("{1: (\"foo\", \"bar\"), 2: [\"foo\", \"bar\"], \"foo\": []}");
81 assertThat(Printer.repr(dict))
82 .isEqualTo("{1: (\"foo\", \"bar\"), 2: [\"foo\", \"bar\"], \"foo\": []}");
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000083 }
84
Francois-Rene Rideau8d5cce32015-06-16 23:12:04 +000085 private void checkFormatPositionalFails(String errorMessage, String format, Object... arguments) {
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000086 try {
Francois-Rene Rideau8d5cce32015-06-16 23:12:04 +000087 Printer.format(format, arguments);
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000088 fail();
89 } catch (IllegalFormatException e) {
90 assertThat(e).hasMessage(errorMessage);
91 }
92 }
93
94 @Test
Vladimir Moskva2b690e32017-02-17 13:01:02 +000095 public void testOutputOrderOfMap() throws Exception {
96 Map<Object, Object> map = new LinkedHashMap<>();
97 map.put(5, 5);
98 map.put(3, 3);
99 map.put("foo", 42);
100 map.put(7, "bar");
101 assertThat(Printer.str(map)).isEqualTo("{5: 5, 3: 3, \"foo\": 42, 7: \"bar\"}");
Florian Weikertf31b9472015-08-04 16:36:58 +0000102 }
103
104 @Test
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +0000105 public void testFormatPositional() throws Exception {
vladmos46907932017-06-30 14:01:45 +0200106 assertThat(Printer.formatWithList("%s %d", Tuple.of("foo", 3))).isEqualTo("foo 3");
lberkiaea56b32017-05-30 12:35:33 +0200107 assertThat(Printer.format("%s %d", "foo", 3)).isEqualTo("foo 3");
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +0000108
brandjonf107a532017-08-16 22:46:02 +0200109 assertThat(Printer.format("%s %s %s", 1, null, 3)).isEqualTo("1 null 3");
110
Francois-Rene Rideau304e1952015-08-25 13:30:10 +0000111 // Note: formatToString doesn't perform scalar x -> (x) conversion;
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +0000112 // The %-operator is responsible for that.
vladmos46907932017-06-30 14:01:45 +0200113 assertThat(Printer.formatWithList("", Tuple.of())).isEmpty();
lberkiaea56b32017-05-30 12:35:33 +0200114 assertThat(Printer.format("%s", "foo")).isEqualTo("foo");
115 assertThat(Printer.format("%s", 3.14159)).isEqualTo("3.14159");
Francois-Rene Rideau8d5cce32015-06-16 23:12:04 +0000116 checkFormatPositionalFails("not all arguments converted during string formatting",
117 "%s", 1, 2, 3);
lberkiaea56b32017-05-30 12:35:33 +0200118 assertThat(Printer.format("%%%s", "foo")).isEqualTo("%foo");
Francois-Rene Rideau8d5cce32015-06-16 23:12:04 +0000119 checkFormatPositionalFails("not all arguments converted during string formatting",
120 "%%s", "foo");
121 checkFormatPositionalFails("unsupported format character \" \" at index 1 in \"% %s\"",
122 "% %s", "foo");
lberkiaea56b32017-05-30 12:35:33 +0200123 assertThat(Printer.format("%s", MutableList.of(null, 1, 2, 3))).isEqualTo("[1, 2, 3]");
124 assertThat(Printer.format("%s", Tuple.of(1, 2, 3))).isEqualTo("(1, 2, 3)");
125 assertThat(Printer.format("%s", MutableList.of(null))).isEqualTo("[]");
126 assertThat(Printer.format("%s", Tuple.of())).isEqualTo("()");
127 assertThat(Printer.format("%% %d %r %s", 1, "2", "3")).isEqualTo("% 1 \"2\" 3");
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +0000128
Francois-Rene Rideau8d5cce32015-06-16 23:12:04 +0000129 checkFormatPositionalFails(
130 "invalid argument \"1\" for format pattern %d",
131 "%d", "1");
132 checkFormatPositionalFails("unsupported format character \".\" at index 1 in \"%.3g\"",
133 "%.3g");
134 checkFormatPositionalFails("unsupported format character \".\" at index 1 in \"%.3g\"",
135 "%.3g", 1, 2);
136 checkFormatPositionalFails("unsupported format character \".\" at index 1 in \"%.s\"",
137 "%.s");
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +0000138 }
139
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +0000140 @Test
Florian Weikert2591e192015-10-05 14:24:51 +0000141 public void testListLimitStringLength() throws Exception {
142 int lengthDivisibleByTwo = Printer.SUGGESTED_CRITICAL_LIST_ELEMENTS_STRING_LENGTH;
143 if (lengthDivisibleByTwo % 2 == 1) {
144 ++lengthDivisibleByTwo;
145 }
146 String limit = Strings.repeat("x", lengthDivisibleByTwo);
147 String half = Strings.repeat("x", lengthDivisibleByTwo / 2);
148
149 List<String> list = Arrays.asList(limit + limit);
150
151 // String is way too long -> shorten.
152 assertThat(printListWithLimit(list)).isEqualTo("[\"" + limit + "...\"]");
153
154 LinkedList<List<String>> nestedList = new LinkedList<>();
155 nestedList.add(list);
156
157 // Same as above, but with one additional level of indirection.
158 assertThat(printListWithLimit(nestedList)).isEqualTo("[[\"" + limit + "...\"]]");
159
160 // The inner list alone would meet the limit, but because of the first element, it has to be
161 // shortened.
162 assertThat(printListWithLimit(Arrays.asList(half, Arrays.asList(limit))))
163 .isEqualTo("[\"" + half + "\", [\"" + half + "...\"]]");
164
165 // String is too long, but the ellipsis make it even longer.
166 assertThat(printListWithLimit(Arrays.asList(limit + "x"))).isEqualTo("[\"" + limit + "...\"]");
167
168 // We hit the limit exactly -> everything is printed.
169 assertThat(printListWithLimit(Arrays.asList(limit))).isEqualTo("[\"" + limit + "\"]");
170
171 // Exact hit, but with two arguments -> everything is printed.
172 assertThat(printListWithLimit(Arrays.asList(half, half)))
173 .isEqualTo("[\"" + half + "\", \"" + half + "\"]");
174
175 // First argument hits the limit -> remaining argument is shortened.
176 assertThat(printListWithLimit(Arrays.asList(limit, limit)))
177 .isEqualTo("[\"" + limit + "\", \"...\"]");
178
179 String limitMinusOne = limit.substring(0, limit.length() - 1);
180
181 // First arguments is one below the limit -> print first character of remaining argument.
182 assertThat(printListWithLimit(Arrays.asList(limitMinusOne, limit)))
183 .isEqualTo("[\"" + limitMinusOne + "\", \"x...\"]");
184
185 // First argument hits the limit -> we skip the remaining two arguments.
186 assertThat(printListWithLimit(Arrays.asList(limit, limit, limit)))
187 .isEqualTo("[\"" + limit + "\", <2 more arguments>]");
188 }
189
190 @Test
191 public void testListLimitTooManyArgs() throws Exception {
192 StringBuilder builder = new StringBuilder();
193 List<Integer> maxLength = new LinkedList<>();
194
195 int next;
196 for (next = 0; next < Printer.SUGGESTED_CRITICAL_LIST_ELEMENTS_COUNT; ++next) {
197 maxLength.add(next);
198 if (next > 0) {
199 builder.append(", ");
200 }
201 builder.append(next);
202 }
203
204 // There is one too many, but we print every argument nonetheless.
205 maxLength.add(next);
206 assertThat(printListWithLimit(maxLength)).isEqualTo("[" + builder + ", " + next + "]");
207
208 // There are two too many, hence we don't print them.
209 ++next;
210 maxLength.add(next);
211 assertThat(printListWithLimit(maxLength)).isEqualTo("[" + builder + ", <2 more arguments>]");
212 }
213
214 @Test
215 public void testPrintListDefaultNoLimit() throws Exception {
216 List<Integer> list = new LinkedList<>();
217 // Make sure that the resulting string is longer than the suggestion. This should also lead to
218 // way more items than suggested.
219 for (int i = 0; i < Printer.SUGGESTED_CRITICAL_LIST_ELEMENTS_STRING_LENGTH * 2; ++i) {
220 list.add(i);
221 }
222 assertThat(Printer.str(list)).isEqualTo(String.format("[%s]", Joiner.on(", ").join(list)));
223 }
224
225 private String printListWithLimit(List<?> list) {
226 return printList(list, Printer.SUGGESTED_CRITICAL_LIST_ELEMENTS_COUNT,
227 Printer.SUGGESTED_CRITICAL_LIST_ELEMENTS_STRING_LENGTH);
228 }
229
230 private String printList(List<?> list, int criticalElementsCount, int criticalStringLength) {
vladmos46907932017-06-30 14:01:45 +0200231 return Printer.printAbbreviatedList(
232 list, "[", ", ", "]", "", criticalElementsCount, criticalStringLength);
Florian Weikert2591e192015-10-05 14:24:51 +0000233 }
vladmos6ff634d2017-07-05 10:25:01 -0400234
235 private SkylarkValue createObjWithStr() {
236 return new SkylarkValue() {
237 @Override
238 public void repr(SkylarkPrinter printer) {
239 printer.append("<repr marker>");
240 }
241
242 @Override
vladmos6ff634d2017-07-05 10:25:01 -0400243 public void str(SkylarkPrinter printer) {
244 printer.append("<str marker>");
245 }
vladmos6ff634d2017-07-05 10:25:01 -0400246 };
247 }
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +0000248}