package function.builtin;

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

import org.junit.*;

import function.ArgumentValidator.*;
import function.builtin.SYMBOL_FUNCTION.UndefinedSymbolFunctionException;
import sexpression.Nil;
import table.FunctionTable;

public class SYMBOL_FUNCTIONTester {

    @Before
    public void setUp() {
        FunctionTable.reset();
    }

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

    @Test
    public void testSymbolFunction_BuiltinFunction() {
        String input = "(symbol-function '+)";

        assertEquals("#<SYSTEM-FUNCTION +>", evaluateString(input).toString());
    }

    @Test
    public void testSymbolFunction_UserDefinedFunction() {
        String defineUserFunction = "(defun y (n m) (+ n m))";
        String input = "(symbol-function 'y)";

        evaluateString(defineUserFunction);
        assertEquals("(Y (N M) (+ N M))", evaluateString(input).toString());
    }

    @Test(expected = UndefinedSymbolFunctionException.class)
    public void testSymbolFunction_NonFunction() {
        String input = "(symbol-function 'a)";

        evaluateString(input);
    }

    @Test(expected = BadArgumentTypeException.class)
    public void testSymbolFunctionWithBadArgumentType() {
        evaluateString("(symbol-function 2)");
    }

    @Test(expected = TooManyArgumentsException.class)
    public void testSymbolFunctionWithTooManyArguments() {
        evaluateString("(symbol-function 'a 'b)");
    }

    @Test(expected = TooFewArgumentsException.class)
    public void testSymbolFunctionWithTooFewArguments() {
        evaluateString("(symbol-function)");
    }

    @Test
    public void undefinedSymbolFunctionException_HasMessageText() {
        UndefinedSymbolFunctionException e = new UndefinedSymbolFunctionException(Nil.getInstance());

        assertNotNull(e.getMessage());
        assertTrue(e.getMessage().length() > 0);
    }

}
