package calculator;

/*
 * The MIT License
 *
 * Copyright 2013 Marcin Zarebski <zarebski.m[AT]gmail.com>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import calculator.evaluator.Evaluator;
import calculator.exception.ExpressionExecuteException;
import calculator.function.FunctionRepository;
import calculator.parser.FunctionParser;
import org.easymock.EasyMockSupport;
import org.junit.Before;
import org.junit.Test;

public class CalculatorTest {
    private static double EPSILON = 1e-10;

    private EasyMockSupport support;

    private Evaluator evaluatorMock;

    private FunctionRepository functionRepositoryMock;

    private FunctionParser functionParserMock;

    private Calculator testedObject;

    @Before
    public void setUp() {
        support = new EasyMockSupport();
        evaluatorMock = support.createMock(Evaluator.class);
        functionRepositoryMock = support.createMock(FunctionRepository.class);
        functionParserMock = support.createMock(FunctionParser.class);

        testedObject = new Calculator();
        testedObject.setEvaluator(evaluatorMock);
        testedObject.setFunctionRepository(functionRepositoryMock);
        testedObject.setFunctionParser(functionParserMock);
    }

    @Test
    public void testExecute_noExecution() throws Exception {
        assertEquals(0.0, testedObject.getResult(), EPSILON);
    }

    @Test
    public void testExecute_singleExecution() throws Exception {
        final String expr = "expression";
        final double expected = 12.3;

        expect(evaluatorMock.execute(expr)).andReturn(expected);

        support.replayAll();
        testedObject.execute(expr);
        support.verifyAll();

        assertEquals(expected, testedObject.getResult(), EPSILON);
    }

    @Test
    public void testExecute_executionError() throws Exception {
        final String expr = "bad expression";

        expect(evaluatorMock.execute(expr)).andThrow(new ExpressionExecuteException(expr, null));

        try {
            support.replayAll();
            testedObject.execute(expr);
            support.verifyAll();
        } catch (ExpressionExecuteException ex) {
            return;
        } catch (Exception ex) {
            fail(ex.getMessage());
        } finally {
            assertTrue(Double.isNaN(testedObject.getResult()));
        }

        fail();
    }
}

@@ -182,19 +182,16 @@
 g a copy
-%0A *
  of this
@@ -214,16 +214,19 @@
 sociated
+%0A *
  documen
@@ -263,19 +263,16 @@
  to deal
-%0A *
  in the 
@@ -329,16 +329,19 @@
 mitation
+%0A *
  the rig
@@ -343,19 +343,16 @@
 e rights
-%0A *
  to use,
@@ -417,19 +417,16 @@
 /or sell
-%0A *
  copies 
@@ -445,16 +445,19 @@
 are, and
+%0A *
  to perm
@@ -490,19 +490,16 @@
 tware is
-%0A *
  furnish
@@ -628,19 +628,16 @@
 luded in
-%0A *
  all cop
@@ -662,24 +662,27 @@
  portions of
+%0A *
  the Softwar
@@ -764,19 +764,16 @@
 PRESS OR
-%0A *
  IMPLIED
@@ -802,16 +802,19 @@
 MITED TO
+%0A *
  THE WAR
@@ -840,19 +840,16 @@
 ABILITY,
-%0A *
  FITNESS
@@ -990,19 +990,16 @@
 OR OTHER
-%0A *
  LIABILI
@@ -1025,16 +1025,19 @@
 CTION OF
+%0A *
  CONTRAC
@@ -1071,19 +1071,16 @@
 NG FROM,
-%0A *
  OUT OF 
@@ -1133,16 +1133,19 @@
 OR OTHER
+%0A *
  DEALING
@@ -1148,19 +1148,16 @@
 LINGS IN
-%0A *
  THE SOF
@@ -1167,17 +1167,16 @@
 RE.%0A */%0A
-%0A
 import s

Add unit tests for calculator #2