package com.avg.zaap.crashlibrary.core;

import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * A test class for {@link CrashUncaughtExceptionHandlerHelper}.
 *
 * Created by marek.osvald on 30.5.2016.
 */
public class CrashUncaughtExceptionHandlerHelperTests {

    private static final String CLASS_1 = "class1";
    private static final String CLASS_2 = "class2";
    private static final String CLASS_3 = "class3";

    private static final String METHOD_1 = "method1";
    private static final String METHOD_2 = "method2";
    private static final String METHOD_3 = "method3";

    private static final String FILE_1 = "file1";
    private static final String FILE_2 = "file2";
    private static final String FILE_3 = "file3";

    private static final int LINE_1 = 1;
    private static final int LINE_2 = 2;
    private static final int LINE_3 = 3;

    private static final String EXPECTED_RESULT = "class1.method1(file1:1)\nclass2.method2(file2:2)\nclass3.method3(file3:3)\n";
    private static final int STACKTRACE_LENGTH = 3;

    private ICrashUncaughtExceptionHandlerHelper mHelper;

    @Before
    public void setUp() throws Exception {
        mHelper = new CrashUncaughtExceptionHandlerHelper();
    }

    @Test
    public void getErrorDetails() throws Exception {
        // arrange
        StackTraceElement[] stacktrace = new StackTraceElement[STACKTRACE_LENGTH];
        stacktrace[0] = new StackTraceElement(CLASS_1, METHOD_1, FILE_1, LINE_1);
        stacktrace[1] = new StackTraceElement(CLASS_2, METHOD_2, FILE_2, LINE_2);
        stacktrace[2] = new StackTraceElement(CLASS_3, METHOD_3, FILE_3, LINE_3);

        Throwable throwable = mock(Throwable.class);
        when(throwable.getStackTrace()).thenReturn(stacktrace);

        // act
        String result = mHelper.getErrorDetails(throwable);

        // assert
        assertThat(result, is(EXPECTED_RESULT));
    }

}