31 */ |
31 */ |
32 |
32 |
33 import com.sun.tools.javac.util.*; |
33 import com.sun.tools.javac.util.*; |
34 import com.sun.tools.javac.code.*; |
34 import com.sun.tools.javac.code.*; |
35 import com.sun.tools.javac.code.Scope.*; |
35 import com.sun.tools.javac.code.Scope.*; |
|
36 import com.sun.tools.javac.code.Symbol; |
36 import com.sun.tools.javac.code.Symbol.*; |
37 import com.sun.tools.javac.code.Symbol.*; |
37 import com.sun.tools.javac.file.JavacFileManager; |
38 import com.sun.tools.javac.file.JavacFileManager; |
|
39 import java.util.ArrayList; |
|
40 import java.util.Arrays; |
|
41 import java.util.Collections; |
|
42 import java.util.List; |
38 |
43 |
39 public class RemoveSymbolUnitTest { |
44 public class RemoveSymbolUnitTest { |
40 |
45 |
41 Context context; |
46 Context context; |
42 Names names; |
47 Names names; |
61 VarSymbol v = new VarSymbol(0, hasNext, Type.noType, clazz); |
66 VarSymbol v = new VarSymbol(0, hasNext, Type.noType, clazz); |
62 MethodSymbol m = new MethodSymbol(0, hasNext, Type.noType, clazz); |
67 MethodSymbol m = new MethodSymbol(0, hasNext, Type.noType, clazz); |
63 |
68 |
64 // Try enter and remove in different shuffled combinations. |
69 // Try enter and remove in different shuffled combinations. |
65 // working with fresh scope each time. |
70 // working with fresh scope each time. |
66 WriteableScope cs = WriteableScope.create(clazz); |
71 WriteableScope cs = writeableScope(clazz, v, m); |
67 cs.enter(v); |
|
68 cs.enter(m); |
|
69 cs.remove(v); |
72 cs.remove(v); |
70 Symbol s = cs.findFirst(hasNext); |
73 Symbol s = cs.findFirst(hasNext); |
71 if (s != m) |
74 if (s != m) |
72 throw new AssertionError("Wrong symbol"); |
75 throw new AssertionError("Wrong symbol"); |
73 |
76 |
74 cs = WriteableScope.create(clazz); |
77 cs = writeableScope(clazz, m, v); |
75 cs.enter(m); |
|
76 cs.enter(v); |
|
77 cs.remove(v); |
78 cs.remove(v); |
78 s = cs.findFirst(hasNext); |
79 s = cs.findFirst(hasNext); |
79 if (s != m) |
80 if (s != m) |
80 throw new AssertionError("Wrong symbol"); |
81 throw new AssertionError("Wrong symbol"); |
81 |
82 |
82 cs = WriteableScope.create(clazz); |
83 cs = writeableScope(clazz, v, m); |
83 cs.enter(v); |
|
84 cs.enter(m); |
|
85 cs.remove(m); |
84 cs.remove(m); |
86 s = cs.findFirst(hasNext); |
85 s = cs.findFirst(hasNext); |
87 if (s != v) |
86 if (s != v) |
88 throw new AssertionError("Wrong symbol"); |
87 throw new AssertionError("Wrong symbol"); |
89 |
88 |
90 cs = WriteableScope.create(clazz); |
89 cs = writeableScope(clazz); |
91 cs.enter(m); |
90 cs.enter(m); |
92 cs.enter(v); |
91 cs.enter(v); |
93 cs.remove(m); |
92 cs.remove(m); |
94 s = cs.findFirst(hasNext); |
93 s = cs.findFirst(hasNext); |
95 if (s != v) |
94 if (s != v) |
96 throw new AssertionError("Wrong symbol"); |
95 throw new AssertionError("Wrong symbol"); |
|
96 |
|
97 // Test multiple removals in the same scope. |
|
98 VarSymbol v1 = new VarSymbol(0, names.fromString("name1"), Type.noType, clazz); |
|
99 VarSymbol v2 = new VarSymbol(0, names.fromString("name2"), Type.noType, clazz); |
|
100 VarSymbol v3 = new VarSymbol(0, names.fromString("name3"), Type.noType, clazz); |
|
101 VarSymbol v4 = new VarSymbol(0, names.fromString("name4"), Type.noType, clazz); |
|
102 |
|
103 cs = writeableScope(clazz, v1, v2, v3, v4); |
|
104 cs.remove(v2); |
|
105 assertRemainingSymbols(cs, v1, v3, v4); |
|
106 cs.remove(v3); |
|
107 assertRemainingSymbols(cs, v1, v4); |
|
108 cs.remove(v1); |
|
109 assertRemainingSymbols(cs, v4); |
|
110 cs.remove(v4); |
|
111 assertRemainingSymbols(cs); |
|
112 } |
|
113 |
|
114 private WriteableScope writeableScope(ClassSymbol classSymbol, Symbol... symbols) { |
|
115 WriteableScope cs = WriteableScope.create(classSymbol); |
|
116 for (Symbol symbol : symbols) { |
|
117 cs.enter(symbol); |
|
118 } |
|
119 return cs; |
|
120 } |
|
121 |
|
122 private void assertRemainingSymbols(WriteableScope cs, Symbol... symbols) { |
|
123 List<Symbol> expectedSymbols = Arrays.asList(symbols); |
|
124 List<Symbol> actualSymbols = new ArrayList<>(); |
|
125 cs.getSymbols().forEach(symbol -> actualSymbols.add(symbol)); |
|
126 // The symbols are stored in reverse order |
|
127 Collections.reverse(actualSymbols); |
|
128 if (!actualSymbols.equals(expectedSymbols)) { |
|
129 throw new AssertionError( |
|
130 String.format("Wrong symbols: %s. Expected %s", actualSymbols, expectedSymbols)); |
|
131 } |
97 } |
132 } |
98 } |
133 } |