2424import static org .junit .Assert .assertThrows ;
2525
2626import com .google .common .collect .ImmutableList ;
27+ import com .google .common .collect .ImmutableMap ;
2728import com .google .javascript .jscomp .SyntacticScopeCreator .RedeclarationHandler ;
2829import com .google .javascript .jscomp .colors .Color ;
2930import com .google .javascript .jscomp .colors .ColorId ;
3031import com .google .javascript .jscomp .colors .ColorRegistry ;
3132import com .google .javascript .jscomp .colors .StandardColors ;
33+ import com .google .javascript .jscomp .js .RuntimeJsLibManager ;
3234import com .google .javascript .jscomp .serialization .ConvertTypesToColors ;
3335import com .google .javascript .jscomp .serialization .SerializationOptions ;
3436import com .google .javascript .jscomp .testing .TestExternsBuilder ;
4143import com .google .javascript .rhino .jstype .JSTypeNative ;
4244import com .google .javascript .rhino .jstype .JSTypeRegistry ;
4345import com .google .javascript .rhino .jstype .ObjectType ;
46+ import com .google .javascript .rhino .testing .MapBasedScope ;
4447import org .junit .Before ;
4548import org .junit .Test ;
4649import org .junit .runner .RunWith ;
5154public class AstFactoryTest {
5255
5356 private Compiler compiler ;
57+ private RuntimeJsLibManager runtimeJsLibManager ;
5458
5559 @ Before
5660 public void setUp () throws Exception {
5761 compiler = new Compiler ();
62+ runtimeJsLibManager =
63+ RuntimeJsLibManager .create (
64+ RuntimeJsLibManager .RuntimeLibraryMode .RECORD_AND_VALIDATE_FIELDS ,
65+ new TestResourceProvider (),
66+ compiler .getChangeTracker (),
67+ AstFactoryTest ::alwaysThrowNodeSupplier );
68+ }
69+
70+ private static Node alwaysThrowNodeSupplier () {
71+ throw new UnsupportedOperationException ();
72+ }
73+
74+ private static final class TestResourceProvider implements RuntimeJsLibManager .ResourceProvider {
75+
76+ @ Override
77+ public Node parse (String unused1 , String unused2 ) {
78+ throw new UnsupportedOperationException ();
79+ }
5880 }
5981
6082 private JSTypeRegistry getRegistry () {
@@ -113,7 +135,7 @@ private Node parseAndAddColors(String externs, String source) {
113135
114136 private AstFactory createTestAstFactory () {
115137 return AstFactory .createFactoryWithTypes (
116- compiler .getLifeCycleStage (), getRegistry (), compiler . getRuntimeJsLibManager () );
138+ compiler .getLifeCycleStage (), getRegistry (), runtimeJsLibManager );
117139 }
118140
119141 private AstFactory createTestAstFactoryWithColors () {
@@ -123,12 +145,11 @@ private AstFactory createTestAstFactoryWithColors() {
123145 compiler .hasOptimizationColors ()
124146 ? compiler .getColorRegistry ()
125147 : ColorRegistry .builder ().setDefaultNativeColorsForTesting ().build (),
126- compiler . getRuntimeJsLibManager () );
148+ runtimeJsLibManager );
127149 }
128150
129151 private AstFactory createTestAstFactoryWithoutTypes () {
130- return AstFactory .createFactoryWithoutTypes (
131- compiler .getLifeCycleStage (), compiler .getRuntimeJsLibManager ());
152+ return AstFactory .createFactoryWithoutTypes (compiler .getLifeCycleStage (), runtimeJsLibManager );
132153 }
133154
134155 private Scope getScope (Node root ) {
@@ -2029,6 +2050,61 @@ class Example { constructor(arg0, arg1) {} }
20292050 assertNode (newExpr ).hasColorThat ().isEqualTo (expected .getColor ());
20302051 }
20312052
2053+ @ Test
2054+ public void testCreateRuntimeField_throwsIfFieldNotInjected () {
2055+ // Given
2056+ AstFactory astFactory = createTestAstFactoryWithoutTypes ();
2057+ StaticScope scope = new MapBasedScope (ImmutableMap .of ());
2058+ RuntimeJsLibManager .JsLibField field = runtimeJsLibManager .getJsLibField ("$jscomp.global" );
2059+
2060+ // When
2061+ assertThrows (IllegalStateException .class , () -> astFactory .createQName (scope , field ));
2062+ }
2063+
2064+ @ Test
2065+ public void testCreateRuntimeField_succeeds () {
2066+ // Given
2067+ AstFactory astFactory = createTestAstFactoryWithoutTypes ();
2068+ StaticScope scope = new MapBasedScope (ImmutableMap .of ());
2069+ RuntimeJsLibManager .JsLibField field = runtimeJsLibManager .getJsLibField ("$jscomp.global" );
2070+ runtimeJsLibManager .injectLibForField ("$jscomp.global" );
2071+
2072+ // When
2073+ var result = astFactory .createQName (scope , field );
2074+
2075+ // Then
2076+ assertNode (result ).matchesQualifiedName ("$jscomp.global" );
2077+ }
2078+
2079+ @ Test
2080+ public void testCreateJscompMakeIteratorCall_throwsIfJscompMakeIteratorNotInjected () {
2081+ // Given
2082+ AstFactory astFactory = createTestAstFactoryWithoutTypes ();
2083+ Node iterable = IR .name ("arr" );
2084+ StaticScope scope = new MapBasedScope (ImmutableMap .of ());
2085+
2086+ // When
2087+ assertThrows (
2088+ IllegalStateException .class ,
2089+ () -> astFactory .createJSCompMakeIteratorCall (iterable , scope ));
2090+ }
2091+
2092+ @ Test
2093+ public void testCreateJscompMakeIteratorCall_succeeds () {
2094+ // Given
2095+ AstFactory astFactory = createTestAstFactoryWithoutTypes ();
2096+ Node iterable = IR .name ("arr" );
2097+ runtimeJsLibManager .injectLibForField ("$jscomp.makeIterator" );
2098+
2099+ // When
2100+ Node result = astFactory .createJSCompMakeIteratorCall (iterable , MapBasedScope .emptyScope ());
2101+
2102+ // Then
2103+ assertNode (result ).isCall ();
2104+ assertNode (result ).hasFirstChildThat ().matchesQualifiedName ("$jscomp.makeIterator" );
2105+ assertNode (result ).hasLastChildThat ().isEqualTo (iterable );
2106+ }
2107+
20322108 private static ImmutableList <Node > childList (Node parent ) {
20332109 ImmutableList .Builder <Node > list = ImmutableList .builder ();
20342110 for (Node child = parent .getFirstChild (); child != null ; child = child .getNext ()) {
0 commit comments