blob: f1576ddfb2fd40770e12844361f5f50495e32cff [file] [log] [blame]
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +00001// Copyright 2006-2015 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
15package com.google.devtools.build.lib.syntax;
16
17import static com.google.common.truth.Truth.assertThat;
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.fail;
20
21import com.google.common.collect.ImmutableMap;
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000022
23import org.junit.Test;
24import org.junit.runner.RunWith;
25import org.junit.runners.JUnit4;
26
27import java.util.Arrays;
Florian Weikertf31b9472015-08-04 16:36:58 +000028import java.util.HashMap;
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000029import java.util.IllegalFormatException;
30import java.util.List;
31import java.util.Map;
32
33/**
34 * Test properties of the evaluator's datatypes and utility functions
35 * without actually creating any parse trees.
36 */
37@RunWith(JUnit4.class)
38public class PrinterTest {
39
40 private static List<?> makeList(Object... args) {
41 return EvalUtils.makeSequence(Arrays.<Object>asList(args), false);
42 }
43
44 private static List<?> makeTuple(Object... args) {
45 return EvalUtils.makeSequence(Arrays.<Object>asList(args), true);
46 }
47
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000048 @Test
49 public void testPrinter() throws Exception {
50 // Note that prettyPrintValue and printValue only differ on behaviour of
51 // labels and strings at toplevel.
52 assertEquals("foo\nbar", Printer.str("foo\nbar"));
53 assertEquals("\"foo\\nbar\"", Printer.repr("foo\nbar"));
54 assertEquals("'", Printer.str("'"));
55 assertEquals("\"'\"", Printer.repr("'"));
56 assertEquals("\"", Printer.str("\""));
57 assertEquals("\"\\\"\"", Printer.repr("\""));
58 assertEquals("3", Printer.str(3));
59 assertEquals("3", Printer.repr(3));
Francois-Rene Rideau0f7ba342015-08-31 16:16:21 +000060 assertEquals("None", Printer.repr(Runtime.NONE));
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000061
62 assertEquals("//x:x", Printer.str(Label.parseAbsolute("//x")));
Laurent Le Brun357015e2015-06-29 19:56:32 +000063 assertEquals("\"//x:x\"", Printer.repr(Label.parseAbsolute("//x")));
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000064
65 List<?> list = makeList("foo", "bar");
66 List<?> tuple = makeTuple("foo", "bar");
67
68 assertEquals("(1, [\"foo\", \"bar\"], 3)",
69 Printer.str(makeTuple(1, list, 3)));
70 assertEquals("(1, [\"foo\", \"bar\"], 3)",
71 Printer.repr(makeTuple(1, list, 3)));
72 assertEquals("[1, (\"foo\", \"bar\"), 3]",
73 Printer.str(makeList(1, tuple, 3)));
74 assertEquals("[1, (\"foo\", \"bar\"), 3]",
75 Printer.repr(makeList(1, tuple, 3)));
76
Lukacs Berkicc9ac3f2015-06-16 06:42:41 +000077 Map<Object, Object> dict = ImmutableMap.<Object, Object>of(
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000078 1, tuple,
79 2, list,
80 "foo", makeList());
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 Rideaud61f5312015-06-13 03:34:47 +000085 }
86
Francois-Rene Rideau8d5cce32015-06-16 23:12:04 +000087 private void checkFormatPositionalFails(String errorMessage, String format, Object... arguments) {
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000088 try {
Francois-Rene Rideau8d5cce32015-06-16 23:12:04 +000089 Printer.format(format, arguments);
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +000090 fail();
91 } catch (IllegalFormatException e) {
92 assertThat(e).hasMessage(errorMessage);
93 }
94 }
95
96 @Test
Florian Weikertf31b9472015-08-04 16:36:58 +000097 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 Rideaud61f5312015-06-13 03:34:47 +0000108 public void testFormatPositional() throws Exception {
Francois-Rene Rideau304e1952015-08-25 13:30:10 +0000109 assertEquals("foo 3", Printer.formatToString("%s %d", makeTuple("foo", 3)));
Francois-Rene Rideau8d5cce32015-06-16 23:12:04 +0000110 assertEquals("foo 3", Printer.format("%s %d", "foo", 3));
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +0000111
Francois-Rene Rideau304e1952015-08-25 13:30:10 +0000112 // Note: formatToString doesn't perform scalar x -> (x) conversion;
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +0000113 // The %-operator is responsible for that.
Francois-Rene Rideau304e1952015-08-25 13:30:10 +0000114 assertThat(Printer.formatToString("", makeTuple())).isEmpty();
Francois-Rene Rideau8d5cce32015-06-16 23:12:04 +0000115 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");
124 assertEquals("[1, 2, 3]", Printer.format("%s", makeList(1, 2, 3)));
125 assertEquals("(1, 2, 3)", Printer.format("%s", makeTuple(1, 2, 3)));
126 assertEquals("[]", Printer.format("%s", makeList()));
127 assertEquals("()", Printer.format("%s", makeTuple()));
128 assertEquals("% 1 \"2\" 3", Printer.format("%% %d %r %s", 1, "2", "3"));
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +0000129
Francois-Rene Rideau8d5cce32015-06-16 23:12:04 +0000130 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 Rideaud61f5312015-06-13 03:34:47 +0000139 }
140
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +0000141 @Test
Florian Weikertf07e5442015-07-01 13:08:43 +0000142 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
150 List<?> list = makeList("foo", "bar");
151 List<?> tuple = makeTuple("foo", "bar");
152
153 assertThat(Printer.str(makeTuple(1, list, 3), '\'')).isEqualTo("(1, ['foo', 'bar'], 3)");
154 assertThat(Printer.repr(makeTuple(1, list, 3), '\'')).isEqualTo("(1, ['foo', 'bar'], 3)");
155 assertThat(Printer.str(makeList(1, tuple, 3), '\'')).isEqualTo("[1, ('foo', 'bar'), 3]");
156 assertThat(Printer.repr(makeList(1, tuple, 3), '\'')).isEqualTo("[1, ('foo', 'bar'), 3]");
157
158 Map<Object, Object> dict =
159 ImmutableMap.<Object, Object>of(1, tuple, 2, list, "foo", makeList());
160
161 assertThat(Printer.str(dict, '\''))
162 .isEqualTo("{1: ('foo', 'bar'), 2: ['foo', 'bar'], 'foo': []}");
163 assertThat(Printer.repr(dict, '\''))
164 .isEqualTo("{1: ('foo', 'bar'), 2: ['foo', 'bar'], 'foo': []}");
Francois-Rene Rideaud61f5312015-06-13 03:34:47 +0000165 }
166}