Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2007 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.javascript.rhino.Node;
20 import com.google.javascript.rhino.Token;
21
22 import junit.framework.TestCase;
23
24 public class DotFormatterTest extends TestCase {
25 /**
26 * Tests that keys are assigned sequentially.
27 */
28 public void testKeyAssignementSequential() throws Exception {
29 DotFormatter dot = DotFormatter.newInstanceForTesting();
30 assertEquals(0, dot.key(new Node(Token.BLOCK)));
31 assertEquals(1, dot.key(new Node(Token.BLOCK)));
32 assertEquals(2, dot.key(new Node(Token.BLOCK)));
33 assertEquals(3, dot.key(new Node(Token.BLOCK)));
34 assertEquals(4, dot.key(new Node(Token.BLOCK)));
35 }
36
37 /**
38 * Tests that keys are assigned once per node.
39 */
40 public void testKeyAssignementOncePerNode() throws Exception {
41 DotFormatter dot = DotFormatter.newInstanceForTesting();
42 Node node0 = new Node(Token.BLOCK);
43 Node node1 = new Node(Token.BLOCK);
44 Node node2 = new Node(Token.BLOCK);
45
46 assertEquals(0, dot.key(node0));
47 assertEquals(1, dot.key(node1));
48 assertEquals(2, dot.key(node2));
49 assertEquals(0, dot.key(node0));
50 assertEquals(1, dot.key(node1));
51 assertEquals(2, dot.key(node2));
52 }
53
54 /**
55 * Tests the formatting (simple tree).
56 */
57 public void testToDotSimple() throws Exception {
58 Node ast = new Node(Token.BITOR);
59
60 String expected = "digraph AST {\n" +
61 " node [color=lightblue2, style=filled];\n" +
62 " node0 [label=\"BITOR\"];\n" +
63 "}\n";
64 test(expected, ast);
65 }
66
67 /**
68 * Tests the formatting (3 element tree).
69 */
70 public void testToDot3Elements() throws Exception {
71 Node ast = new Node(Token.BLOCK);
72 ast.addChildToBack(new Node(Token.NAME));
73 ast.addChildToBack(new Node(Token.STRING));
74
75 String expected = "digraph AST {\n" +
76 " node [color=lightblue2, style=filled];\n" +
77 " node0 [label=\"BLOCK\"];\n" +
78 " node1 [label=\"NAME\"];\n" +
79 " node0 -> node1 [weight=1];\n" +
80 " node2 [label=\"STRING\"];\n" +
81 " node0 -> node2 [weight=1];\n" +
82 "}\n";
83 test(expected, ast);
84 }
85
86 private void test(String expected, Node ast) {
87 try {
88 assertEquals(expected, DotFormatter.toDot(ast));
89 } catch (java.io.IOException e) {
90 fail("Tests failed with IOExceptions");
91 }
92 }
93 }