package ru.dyatel.tsuschedule.parsing;

import org.junit.BeforeClass;
import org.junit.Test;
import ru.dyatel.tsuschedule.parsing.util.Filter;
import ru.dyatel.tsuschedule.parsing.util.IterableFilter;

import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class ParserTest {

    private Filter<Lesson> evenParity = new Filter<Lesson>() {
        @Override
        public boolean accept(Lesson obj) {
            return obj.getParity().equals(Parity.EVEN) || obj.getParity().equals(Parity.BOTH);
        }
    };
    private Filter<Lesson> oddParity = new Filter<Lesson>() {
        @Override
        public boolean accept(Lesson obj) {
            return obj.getParity().equals(Parity.ODD) || obj.getParity().equals(Parity.BOTH);
        }
    };
    private Filter<Lesson> firstSubgroup = new Filter<Lesson>() {
        @Override
        public boolean accept(Lesson obj) {
            return obj.getSubgroup() == 0 || obj.getSubgroup() == 1;
        }
    };

    private static Set<Lesson> lessons;

    @BeforeClass
    public static void setup() throws Exception {
        lessons = Parser.getLessons("221251");
    }

    @Test
    public void testGetLessonsMonday() throws Exception {
        IterableFilter<Lesson> filter = new IterableFilter<Lesson>();
        filter.apply(oddParity);
        filter.apply(new Filter<Lesson>() {
            @Override
            public boolean accept(Lesson obj) {
                return obj.getWeekday().equals("");
            }
        });
        filter.apply(firstSubgroup);

        Set<Lesson> result = filter.filter(lessons);
        assertEquals(3, result.size());
        assertTrue(containsDiscipline(result, ":"));
        assertTrue(containsDiscipline(result, ":"));
        assertTrue(containsDiscipline(result, ": "));
    }

    @Test
    public void testGetLessonsTuesday() throws Exception {
        IterableFilter<Lesson> filter = new IterableFilter<Lesson>();
        filter.apply(oddParity);
        filter.apply(new Filter<Lesson>() {
            @Override
            public boolean accept(Lesson obj) {
                return obj.getWeekday().equals("");
            }
        });
        filter.apply(firstSubgroup);

        Set<Lesson> result = filter.filter(lessons);
        assertEquals(3, result.size());
        assertTrue(containsDiscipline(result, " "));
        assertTrue(containsDiscipline(result, ": "));
        assertTrue(containsDiscipline(result, ":   "));
    }

    @Test
    public void testGetLessonsFridayEven() throws Exception {
        IterableFilter<Lesson> filter = new IterableFilter<Lesson>();
        filter.apply(evenParity);
        filter.apply(new Filter<Lesson>() {
            @Override
            public boolean accept(Lesson obj) {
                return obj.getWeekday().equals("");
            }
        });
        filter.apply(firstSubgroup);

        Set<Lesson> result = filter.filter(lessons);
        assertEquals(4, result.size());
        removeDiscipline(result, ":   ");
        assertEquals(2, result.size());
        assertTrue(containsDiscipline(result, ": "));
        assertTrue(containsDiscipline(result, ":   "));
    }

    @Test
    public void testGetLessonsFridayOdd() throws Exception {
        IterableFilter<Lesson> filter = new IterableFilter<Lesson>();
        filter.apply(oddParity);
        filter.apply(new Filter<Lesson>() {
            @Override
            public boolean accept(Lesson obj) {
                return obj.getWeekday().equals("");
            }
        });
        filter.apply(firstSubgroup);

        Set<Lesson> result = filter.filter(lessons);
        assertEquals(4, result.size());
        removeDiscipline(result, ":   ");
        assertEquals(2, result.size());
        assertTrue(containsDiscipline(result, ": "));
        assertTrue(containsDiscipline(result, ": "));
    }

    private boolean containsDiscipline(Set<Lesson> lessons, String discipline) {
        for (Lesson l : lessons) {
            if (l.getDiscipline().equals(discipline)) return true;
        }
        return false;
    }

    private void removeDiscipline(Set<Lesson> lessons, String discipline) {
        while (true) {
            Lesson lesson = null;
            for (Lesson l : lessons) {
                if (l.getDiscipline().equals(discipline)) {
                    lesson = l;
                    break;
                }
            }
            if (lesson == null) break;
            else lessons.remove(lesson);
        }
    }

}
@@ -1306,35 +1306,29 @@
 rableFilter%3C
-Lesson
 %3E();%0A
+
         filt
@@ -1958,38 +1958,32 @@
  IterableFilter%3C
-Lesson
 %3E();%0A        fil
@@ -2620,38 +2620,32 @@
  IterableFilter%3C
-Lesson
 %3E();%0A        fil
@@ -2963,33 +2963,33 @@
    assertEquals(
-4
+5
 , result.size())
@@ -3045,33 +3045,33 @@
    assertEquals(
-2
+3
 , result.size())
@@ -3118,32 +3118,86 @@
 result, %22: %22));%0A
+        assertTrue(containsDiscipline(result, %22: %22));%0A
         assertTr
@@ -3373,22 +3373,16 @@
 eFilter%3C
-Lesson
 %3E();%0A   
@@ -3719,9 +3719,9 @@
 als(
-4
+5
 , re
@@ -3797,17 +3797,17 @@
 tEquals(
-2
+3
 , result
@@ -3916,32 +3916,86 @@
 result, %22: %22));%0A
+        assertTrue(containsDiscipline(result, %22: %22));%0A
     %7D%0A%0A    priva

Update tests