1 | // Copyright (C) 2008 Google Inc. |
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.caja.ancillary.linter; |
16 | |
17 | import com.google.caja.lexer.FilePosition; |
18 | import com.google.caja.parser.js.BreakStmt; |
19 | import com.google.caja.parser.js.Declaration; |
20 | import com.google.caja.parser.js.Identifier; |
21 | |
22 | import junit.framework.TestCase; |
23 | |
24 | public class ExitModesTest extends TestCase { |
25 | public final void testUnion() { |
26 | assertEquals( |
27 | ExitModes.COMPLETES, ExitModes.COMPLETES.union(ExitModes.COMPLETES)); |
28 | |
29 | LiveSet x = LiveSet.EMPTY.with(decl("x")); |
30 | LiveSet y = LiveSet.EMPTY.with(decl("y")); |
31 | |
32 | ExitModes foo = ExitModes.COMPLETES.withBreak(b("foo"), x); |
33 | ExitModes bar = ExitModes.COMPLETES.withBreak(b("bar"), y); |
34 | ExitModes foobar = foo.withBreak(b("bar"), y); |
35 | |
36 | assertEquals("{bfoo=((x@-1) always)}", foo.toString()); |
37 | assertEquals("{bbar=((y@-1) always)}", bar.toString()); |
38 | assertEquals("{bfoo=((x@-1) always), bbar=((y@-1) always)}", |
39 | foobar.toString()); |
40 | |
41 | assertEquals(foo, ExitModes.COMPLETES.union(foo)); |
42 | assertEquals(foo, foo.union(ExitModes.COMPLETES)); |
43 | assertEquals(bar, ExitModes.COMPLETES.union(bar)); |
44 | assertEquals(bar, bar.union(ExitModes.COMPLETES)); |
45 | assertEquals(foobar, bar.union(foo)); |
46 | assertEquals(foobar, foo.union(bar)); |
47 | } |
48 | |
49 | public final void testIntersection() { |
50 | assertEquals( |
51 | ExitModes.COMPLETES, |
52 | ExitModes.COMPLETES.intersection(ExitModes.COMPLETES)); |
53 | |
54 | LiveSet x = LiveSet.EMPTY.with(decl("x")); |
55 | LiveSet y = LiveSet.EMPTY.with(decl("y")); |
56 | LiveSet xy = x.with(decl("y")); |
57 | |
58 | ExitModes foo = ExitModes.COMPLETES.withBreak(b("foo"), x); |
59 | ExitModes bar = ExitModes.COMPLETES.withBreak(b("bar"), y); |
60 | ExitModes baz = ExitModes.COMPLETES.withBreak(b("baz"), xy); |
61 | ExitModes foobar = foo.withBreak(b("bar"), y); |
62 | ExitModes foobaz = foo.withBreak(b("baz"), xy); |
63 | ExitModes barbaz = bar.withBreak(b("baz"), xy); |
64 | ExitModes foobarbaz = foobar.withBreak(b("baz"), xy); |
65 | |
66 | ExitModes fooS = ExitModes.COMPLETES.intersection(foo); |
67 | ExitModes barS = ExitModes.COMPLETES.intersection(bar); |
68 | ExitModes bazS = ExitModes.COMPLETES.intersection(baz); |
69 | ExitModes fooSbarS = ExitModes.COMPLETES.intersection(foobar); |
70 | ExitModes fooSbarSbaz = fooSbarS.union(baz); |
71 | ExitModes fooSbarSbazS = fooSbarS.intersection(bazS); |
72 | |
73 | assertEquals("{bfoo=((x@-1) always)}", foo.toString()); |
74 | assertEquals("{bbar=((y@-1) always)}", bar.toString()); |
75 | assertEquals("{bbaz=((x@-1 y@-1) always)}", baz.toString()); |
76 | assertEquals("{bfoo=((x@-1))}", fooS.toString()); |
77 | assertEquals("{bbar=((y@-1))}", barS.toString()); |
78 | assertEquals("{bbaz=((x@-1 y@-1))}", bazS.toString()); |
79 | assertEquals("{bfoo=((x@-1)), bbar=((y@-1))}", |
80 | fooSbarS.toString()); |
81 | assertEquals("{bbaz=((x@-1 y@-1) always), bfoo=((x@-1)), bbar=((y@-1))}", |
82 | fooSbarSbaz.toString()); |
83 | |
84 | assertFalse(foo.equals(fooS)); |
85 | assertFalse(bar.equals(barS)); |
86 | assertFalse(foobar.equals(fooSbarS)); |
87 | |
88 | assertEquals(foo, foo.intersection(foo)); |
89 | assertEquals(fooS, ExitModes.COMPLETES.intersection(foo)); |
90 | assertEquals(fooS, foo.intersection(ExitModes.COMPLETES)); |
91 | assertEquals(bar, bar.intersection(bar)); |
92 | assertEquals(barS, ExitModes.COMPLETES.intersection(bar)); |
93 | assertEquals(barS, bar.intersection(ExitModes.COMPLETES)); |
94 | // Since neither foo nor bar complete, the always bits don't get stripped. |
95 | assertEquals(foobar, bar.intersection(foo)); |
96 | assertEquals(foobar, foo.intersection(bar)); |
97 | assertEquals(foobarbaz, foobaz.intersection(barbaz)); |
98 | assertEquals(foobarbaz, foobaz.intersection(barbaz)); |
99 | assertEquals(foobarbaz, barbaz.intersection(foobaz)); |
100 | // But since fooSBarS does complete, the always bits get stripped from baz |
101 | assertEquals(fooSbarSbazS, fooSbarS.intersection(barbaz)); |
102 | assertEquals(fooSbarSbazS, barbaz.intersection(fooSbarS)); |
103 | } |
104 | |
105 | private static BreakStmt b(String label) { |
106 | return new BreakStmt(FilePosition.UNKNOWN, label); |
107 | } |
108 | private static Declaration decl(String name) { |
109 | return new Declaration( |
110 | FilePosition.UNKNOWN, new Identifier(FilePosition.UNKNOWN, name), null); |
111 | } |
112 | } |