Debian Codegraph @

test/com/google/javascript/jscomp/VariableMapTest.java

raw symbols SCIP
1 /*
2 * Copyright 2008 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.common.base.Charsets;
20 import com.google.common.collect.ImmutableMap;
21
22 import junit.framework.TestCase;
23
24 import java.text.ParseException;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Map;
28
29 /**
30 * Tests for {@link VariableMap}.
31 *
32 */
33 public class VariableMapTest extends TestCase {
34
35 public void testCycle1() throws ParseException {
36 cycleTest(ImmutableMap.of("AAA", "a", "BBB", "b"));
37 cycleTest(ImmutableMap.of("AA:AA", "a", "BB:BB", "b"));
38 cycleTest(ImmutableMap.of("AAA", "a:a", "BBB", "b:b"));
39 }
40
41 public void cycleTest(ImmutableMap<String, String> map)
42 throws ParseException {
43 VariableMap in = new VariableMap(map);
44 String serialized = new String(in.toBytes(), Charsets.UTF_8);
45 VariableMap out = VariableMap.fromBytes(serialized.getBytes());
46 assertMapsEquals(in.toMap(), out.toMap());
47 }
48
49 public void assertMapsEquals(
50 Map<String, String> expected, Map<String, String> result) {
51 assertEquals(expected.size(), result.size());
52 for (String key : expected.keySet()) {
53 assertEquals(expected.get(key), result.get(key));
54 }
55 }
56
57 public void testToBytes() {
58 VariableMap vm = new VariableMap(ImmutableMap.of("AAA", "a", "BBB", "b"));
59 String serialized = new String(vm.toBytes(), Charsets.UTF_8);
60 assertTrue(serialized.endsWith("\n"));
61
62 List<String> lines = Arrays.asList(serialized.split("\n"));
63 assertEquals(2, lines.size());
64 assertTrue(lines.contains("AAA:a"));
65 assertTrue(lines.contains("BBB:b"));
66 }
67
68 public void testFromBytes() throws ParseException {
69 VariableMap vm = VariableMap.fromBytes("AAA:a\nBBB:b\n".getBytes());
70 assertEquals(2, vm.getOriginalNameToNewNameMap().size());
71 assertEquals("a", vm.lookupNewName("AAA"));
72 assertEquals("b", vm.lookupNewName("BBB"));
73 assertEquals("AAA", vm.lookupSourceName("a"));
74 assertEquals("BBB", vm.lookupSourceName("b"));
75 }
76
77 public void testFileFormat1() {
78 assertEqual(
79 new VariableMap(ImmutableMap.of("x\ny", "a")).toBytes(),
80 "x\\ny:a\n".getBytes());
81
82 assertEqual(
83 new VariableMap(ImmutableMap.of("x:y", "a")).toBytes(),
84 "x\\:y:a\n".getBytes());
85
86 assertEqual(
87 new VariableMap(ImmutableMap.of("x\ny", "a")).toBytes(),
88 "x\\ny:a\n".getBytes());
89
90 assertEqual(
91 new VariableMap(ImmutableMap.of("x\\y", "a")).toBytes(),
92 "x\\\\y:a\n".getBytes());
93
94 assertEqual(
95 new VariableMap(ImmutableMap.of("\n", "a")).toBytes(),
96 "\\n:a\n".getBytes());
97
98 assertEqual(
99 new VariableMap(ImmutableMap.of(":", "a")).toBytes(),
100 "\\::a\n".getBytes());
101
102 assertEqual(
103 new VariableMap(ImmutableMap.of("\n", "a")).toBytes(),
104 "\\n:a\n".getBytes());
105
106 assertEqual(
107 new VariableMap(ImmutableMap.of("\\", "a")).toBytes(),
108 "\\\\:a\n".getBytes());
109 }
110
111 public void testFromBytesComplex1() throws ParseException {
112 // Verify we get out what we put in.
113 cycleTest(ImmutableMap.of("AAA[':f']", "a"));
114
115 // Verify the file format is as expected.
116 VariableMap in = new VariableMap(ImmutableMap.of("AAA[':f']", "a"));
117 assertEqual(in.toBytes(), "AAA['\\:f']:a\n".getBytes());
118 }
119
120 public void testFromBytesComplex2() throws ParseException {
121 VariableMap vm = VariableMap.fromBytes("AAA['\\:f']:a\n".getBytes());
122
123 assertEquals(1, vm.getOriginalNameToNewNameMap().size());
124 assertEquals("a", vm.lookupNewName("AAA[':f']"));
125
126 assertEquals(1, vm.getNewNameToOriginalNameMap().size());
127 assertEquals("AAA[':f']", vm.lookupSourceName("a"));
128
129 }
130
131 private void assertEqual(byte[] bytes1, byte[] bytes2) {
132 if (bytes1 != bytes2) {
133 assertEquals("length differs.", bytes1.length, bytes2.length);
134 for (int i = 0; i < bytes1.length; i++) {
135 assertEquals("byte " + i + "differs.", bytes1[i], bytes2[i]);
136 }
137 }
138 }
139
140 public void testReverseThrowsErrorOnDuplicate() throws ParseException {
141 VariableMap vm = new VariableMap(ImmutableMap.of("AA", "b", "BB", "b"));
142 try {
143 vm.getNewNameToOriginalNameMap();
144 fail();
145 } catch (java.lang.IllegalArgumentException expected) {
146 }
147 }
148
149 public void testReverseLookupOfNullFindsNoName() throws ParseException {
150 VariableMap vm = new VariableMap(ImmutableMap.of("AA", "a", "BB", "b"));
151 assertNull(vm.lookupSourceName(null));
152 }
153 }