package ru.matevosyan;

import org.junit.Test;

import java.util.Iterator;
import java.util.NoSuchElementException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertTrue;

/**
 * DynamicSetByLinkedListTest class for testing DynamicLinkedList class.
 * Created on 29.05.2017.
 * @author Matevosyan Vardan
 * @version 1.0
 */

public class DynamicSetByLinkedListTest {

    /**
     * Create whenAddTwoElementsThanCheckOneOfThenGettingByIndex() method to check added element to the list.
     */

    @Test
    public void whenAddTwoElementsThanCheckOneOfThenGettingByIndex() {

        DynamicLinkedList<Integer> integerDynamicLinkedList = new DynamicLinkedList<>();

        integerDynamicLinkedList.add(12);
        integerDynamicLinkedList.add(42);

        assertThat(integerDynamicLinkedList.get(1), is(42));

    }

    /**
     * Create whenGetValueFromListThanCheckTheGetResults() to invoke {@link DynamicLinkedList#get(int)} to check.
     * the result.
     */

    @Test
    public void whenGetValueFromListThanCheckTheGetResults() {
        DynamicLinkedList<Integer> integerDynamicLinkedList = new DynamicLinkedList<>();

        integerDynamicLinkedList.add(12);
        integerDynamicLinkedList.add(2);
        integerDynamicLinkedList.add(2);

        int value = integerDynamicLinkedList.get(2);
        assertThat(value, is(2));
    }

    /**
     * Create whenGetValueFromLinkedListWithForLoopThanCheckTheGetResults() to check value of elements.
     * When added elements to the LinkedList using for loop, that check the result.
     */

    @Test
    public void whenGetValueFromLinkedListWithForLoopThanCheckTheGetResults() {
        DynamicLinkedList<Integer> integerDynamicLinkedList = new DynamicLinkedList<>();

        for (int i = 0; i < 20; i++) {
            integerDynamicLinkedList.add(i);
        }

        int value = integerDynamicLinkedList.get(2);
        assertThat(value, is(2));
    }

    /**
     * Create whenInvokeNextMethodWithoutAddingTheValuesToLinkedListThanCheckWaitingForException to check an exception.
     */

    @Test
    public void whenInvokeNextMethodWithoutAddingTheValuesToLinkedListThanCheckWaitingForException() {

        Throwable e = null;

        try {
            DynamicLinkedList<Integer> integerDynamicLinkedList = new DynamicLinkedList<>();
            Iterator<Integer> itr = integerDynamicLinkedList.iterator();
            itr.next();
        } catch (Throwable ex) {
            e = ex;
        }

        assertTrue(e instanceof NoSuchElementException);
    }


    /**
     * Create whenGetIteratorFromLinkedListAndUseHasNextAndNextThanCheckTheGetResults() to check next method.
     * In {@link DynamicLinkedList#iterator()}.
     */

    @Test
    public void whenGetIteratorFromLinkedListAndUseHasNextAndNextThanCheckTheGetResults() {
        DynamicLinkedList<Integer> integerDynamicLinkedList = new DynamicLinkedList<>();

        Iterator<Integer> itr = integerDynamicLinkedList.iterator();

        integerDynamicLinkedList.add(12);
        integerDynamicLinkedList.add(2);
        integerDynamicLinkedList.add(3);

        int value = 0;
        if (itr.hasNext()) {
            value = itr.next();
        }


        assertThat(value, is(12));
    }

    /**
     * Create whenInvokeHasNextThanCheckTheGetResultsIsGoingToBeTrue().
     * To check hasNext() in {@link DynamicLinkedList#iterator()}.
     */

    @Test
    public void whenInvokeHasNextThanCheckTheGetResultsIsGoingToBeTrue() {
        DynamicLinkedList<Integer> integerDynamicLinkedList = new DynamicLinkedList<>();

        Iterator<Integer> itr = integerDynamicLinkedList.iterator();

        integerDynamicLinkedList.add(12);
        integerDynamicLinkedList.add(2);

        boolean has = itr.hasNext();

        assertThat(has, is(true));
    }

    /**
     * Create whenInvokeHasNextAndNextThanCheckTheGetResultsIsGoingToBeFalse() to check hasNext().
     * when in the LinkedList does not have any element and expected return false.
     */

    @Test
    public void whenInvokeHasNextAndNextThanCheckTheGetResultsIsGoingToBeFalse() {
        DynamicLinkedList<Integer> integerDynamicLinkedList = new DynamicLinkedList<>();

        Iterator<Integer> itr = integerDynamicLinkedList.iterator();

        integerDynamicLinkedList.add(12);
        integerDynamicLinkedList.add(2);

        itr.hasNext();
        int a = itr.next();
        int c = itr.next();
        boolean has = itr.hasNext();

        assertThat(has, is(false));
        assertThat(a, is(12));
        assertThat(c, is(2));

    }



}
@@ -658,32 +658,37 @@
 %0A        Dynamic
+SetBy
 LinkedList%3CInteg
@@ -721,32 +721,37 @@
 st = new Dynamic
+SetBy
 LinkedList%3C%3E();%0A
@@ -848,1175 +848,194 @@
     
-assertThat(integerDynamicLinkedList.get(1), is(42));%0A%0A    %7D%0A%0A    /**%0A     * Create whenGetValueFromListThanCheckTheGetResults() to invoke %7B@link DynamicLinkedList#get(int)%7D to check.%0A     * the result.%0A     */%0A%0A    @Test%0A    public void whenGetValueFromListThanCheckTheGetResults() %7B%0A        DynamicLinkedList%3CInteger%3E integerDynamicLinkedList = new DynamicLinkedList%3C%3E();%0A%0A        integerDynamicLinkedList.add(12);%0A        integerDynamicLinkedList.add(2);%0A        integerDynamicLinkedList.add(2);%0A%0A        int value = integerDynamicLinkedList.get(2);%0A        assertThat(value, is(2));%0A    %7D%0A%0A    /**%0A     * Create whenGetValueFromLinkedListWithForLoopThanCheckTheGetResults() to check value of elements.%0A     * When added elements to the LinkedList using for loop, that check the result.%0A     */%0A%0A    @Test%0A    public void whenGetValueFromLinkedListWithForLoopThanCheckTheGetResults() %7B%0A        DynamicLinkedList%3CInteger%3E integerDynamicLinkedList = new DynamicLinkedList%3C%3E();%0A%0A        for (int i = 0; i %3C 20; i++) %7B%0A            integerDynamicLinkedList.add(i);%0A        %7D%0A%0A        int value = integerDynamicLinkedList.get(2);%0A        assertThat(value
+Iterator%3CInteger%3E integerIterator = integerDynamicLinkedList.iterator();%0A        assertThat(integerIterator.next(), is(12));%0A        assertThat(integerIterator.next()
 , is(
+4
 2));%0A
+%0A
     %7D%0A%0A
+%0A
     
@@ -1335,32 +1335,37 @@
          Dynamic
+SetBy
 LinkedList%3CInteg
@@ -1398,32 +1398,37 @@
 st = new Dynamic
+SetBy
 LinkedList%3C%3E();%0A
@@ -1787,32 +1787,37 @@
 n %7B@link Dynamic
+SetBy
 LinkedList#itera
@@ -1942,32 +1942,37 @@
 %0A        Dynamic
+SetBy
 LinkedList%3CInteg
@@ -2005,32 +2005,37 @@
 st = new Dynamic
+SetBy
 LinkedList%3C%3E();%0A
@@ -2483,32 +2483,37 @@
 n %7B@link Dynamic
+SetBy
 LinkedList#itera
@@ -2621,32 +2621,37 @@
 %0A        Dynamic
+SetBy
 LinkedList%3CInteg
@@ -2684,32 +2684,37 @@
 st = new Dynamic
+SetBy
 LinkedList%3C%3E();%0A
@@ -3247,32 +3247,37 @@
 %0A        Dynamic
+SetBy
 LinkedList%3CInteg
@@ -3310,32 +3310,37 @@
 st = new Dynamic
+SetBy
 LinkedList%3C%3E();%0A

delete a few tests