Search in sources :

Example 1 with GlobalStateMemento

use of org.eclipse.xtext.testing.GlobalRegistries.GlobalStateMemento in project n4js by eclipse.

the class N4jscTestFactory method setAfterStoringState.

/**
 * Like {@link #set(boolean, Optional)}, but stores the global state prior to setting the test factory.
 */
public static State setAfterStoringState(boolean isEnabledBackend, Optional<Class<? extends Module>> overridingModule) {
    N4jscFactory oldFactory = N4jscFactory.INSTANCE;
    GlobalStateMemento oldGlobalState = GlobalRegistries.makeCopyOfGlobalState();
    // next line is important: otherwise #resetInjector() in #set() would change the state of 'oldFactory'
    N4jscFactory.INSTANCE = new N4jscFactory();
    set(isEnabledBackend, overridingModule);
    return new State(oldFactory, oldGlobalState);
}
Also used : GlobalStateMemento(org.eclipse.xtext.testing.GlobalRegistries.GlobalStateMemento)

Example 2 with GlobalStateMemento

use of org.eclipse.xtext.testing.GlobalRegistries.GlobalStateMemento in project n4js by eclipse.

the class N4CliHelper method doWithGlobalStateBackup.

private static <T extends Exception> void doWithGlobalStateBackup(RunnableWithException<T> runnable) throws T {
    GlobalStateMemento originalGlobalState = null;
    if (!JSONStandaloneSetup.isSetUp()) {
        originalGlobalState = GlobalRegistries.makeCopyOfGlobalState();
        JSONStandaloneSetup.doSetup();
    }
    try {
        runnable.run();
    } finally {
        if (originalGlobalState != null) {
            // Restore the original global state (if we had to change it)
            // This is important for these cases in particular:
            // 1) tests that invoke N4jscBase#doMain() should run *without* any global setup, because #doMain()
            // should work if invoked from a plain Java main() method; if we provided the JSON setup as a
            // side-effect of this utility method we would "help" the implementation of #doMain() and might overlook
            // problems in #doMain()'s own setup.
            // 2) some tests deliberately run without any or with an incomplete setup in order to test some failure
            // behavior in this broken case.
            originalGlobalState.restoreGlobalState();
        }
    }
}
Also used : GlobalStateMemento(org.eclipse.xtext.testing.GlobalRegistries.GlobalStateMemento)

Aggregations

GlobalStateMemento (org.eclipse.xtext.testing.GlobalRegistries.GlobalStateMemento)2