package function.builtin;

import static org.junit.Assert.*;
import static testutil.TestUtilities.evaluateString;

import java.util.*;

import org.junit.*;

import environment.RuntimeEnvironment;
import function.ArgumentValidator.TooManyArgumentsException;

public class EXITTest {

    private static final String TERMINATED = "terminated";
    private RuntimeEnvironment environment;
    private Set<String> indicatorSet;

    public EXITTest() {
        this.environment = RuntimeEnvironment.getInstance();
    }

    private void assertTerminated() {
        assertTrue(indicatorSet.contains(TERMINATED));
    }

    private void assertNotTerminated() {
        assertFalse(indicatorSet.contains(TERMINATED));
    }

    @Before
    public void setUp() {
        indicatorSet = new HashSet<>();
        environment.reset();
        environment.setTerminationFunction(() -> indicatorSet.add(TERMINATED));
    }

    @After
    public void tearDown() {
        environment.reset();
    }

    @Test
    public void exitWorks() {
        evaluateString("(exit)");
        assertTerminated();
    }

    @Test
    public void exitNotCalled_IndicatorSetIsClean() {
        assertNotTerminated();
    }

    @Test(expected = TooManyArgumentsException.class)
    public void exitWithTooManyArguments() {
        evaluateString("(exit 1)");
    }

}
