Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 1 | // 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 | |
| 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 | |
| 21 | import com.google.common.collect.ImmutableMap; |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 22 | |
| 23 | import org.junit.Test; |
| 24 | import org.junit.runner.RunWith; |
| 25 | import org.junit.runners.JUnit4; |
| 26 | |
| 27 | import java.util.Arrays; |
Florian Weikert | f31b947 | 2015-08-04 16:36:58 +0000 | [diff] [blame] | 28 | import java.util.HashMap; |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 29 | import java.util.IllegalFormatException; |
| 30 | import java.util.List; |
| 31 | import 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) |
| 38 | public 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 Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 48 | @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 Rideau | 0f7ba34 | 2015-08-31 16:16:21 +0000 | [diff] [blame] | 60 | assertEquals("None", Printer.repr(Runtime.NONE)); |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 61 | |
| 62 | assertEquals("//x:x", Printer.str(Label.parseAbsolute("//x"))); |
Laurent Le Brun | 357015e | 2015-06-29 19:56:32 +0000 | [diff] [blame] | 63 | assertEquals("\"//x:x\"", Printer.repr(Label.parseAbsolute("//x"))); |
Francois-Rene Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 64 | |
| 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 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, |
| 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 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 | 304e195 | 2015-08-25 13:30:10 +0000 | [diff] [blame] | 109 | assertEquals("foo 3", Printer.formatToString("%s %d", makeTuple("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 | 304e195 | 2015-08-25 13:30:10 +0000 | [diff] [blame] | 114 | assertThat(Printer.formatToString("", makeTuple())).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"); |
| 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 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 | |
| 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 Rideau | d61f531 | 2015-06-13 03:34:47 +0000 | [diff] [blame] | 165 | } |
| 166 | } |