package org.yggard.brokkgui.style.parser;

import org.junit.Before;
import org.junit.Test;
import org.yggard.brokkgui.style.parser.StyleSelectorParser;
import org.yggard.brokkgui.style.tree.IStyleSelector;
import org.yggard.brokkgui.style.tree.StyleSelector;
import org.yggard.brokkgui.style.tree.StyleSelectorHierarchic;
import org.yggard.brokkgui.style.tree.StyleSelectorType;

import static org.assertj.core.api.Assertions.assertThat;

public class StyleSelectorParserTest
{
    private StyleSelectorParser parser;

    @Before
    public void setup()
    {
        this.parser = new StyleSelectorParser();
    }

    @Test
    public void testSelectorClean()
    {
        String simpleTest = "button .class";

        assertThat(parser.cleanSelector(simpleTest)).isEqualTo("button>>.class");

        String trimTest = "button > .class";

        assertThat(parser.cleanSelector(trimTest)).isEqualTo("button>.class");

        String replaceAndTrim = "button >>  .class text";

        assertThat(parser.cleanSelector(replaceAndTrim)).isEqualTo("button>>.class>>text");
    }

    @Test
    public void testSelectorOr()
    {
        String simpleOr = "button , text";

        IStyleSelector[] selectors = parser.readSelectors(simpleOr);

        assertThat(selectors.length).isEqualTo(2);

        assertThat(selectors[0].match(new StyleSelector().add(StyleSelectorType.TYPE, "button"))).isTrue();
        assertThat(selectors[1].match(new StyleSelector().add(StyleSelectorType.TYPE, "text"))).isTrue();
    }

    @Test
    public void testSimpleHierarchic()
    {
        String simpleHierarchic = "button > text";
        StyleSelector parent = new StyleSelector().add(StyleSelectorType.TYPE, "button");
        StyleSelector child = new StyleSelector().add(StyleSelectorType.TYPE, "text");
        IStyleSelector[] selectors = parser.readSelectors(simpleHierarchic);

        assertThat(selectors.length).isEqualTo(1);

        assertThat(selectors[0]).isInstanceOf(StyleSelectorHierarchic.class);

        assertThat(((StyleSelectorHierarchic) selectors[0]).isDirectChild()).isTrue();
        assertThat(((StyleSelectorHierarchic) selectors[0]).getParentSelector().match(parent)).isTrue();
        assertThat(((StyleSelectorHierarchic) selectors[0]).getChildSelector().match(child)).isTrue();
    }

    @Test
    public void testTwoGenerationHierarchic()
    {
        String twoGenerationHierarchic = "pane button > text";
        StyleSelector grandparent = new StyleSelector().add(StyleSelectorType.TYPE, "pane");
        StyleSelector parent = new StyleSelector().add(StyleSelectorType.TYPE, "button");
        StyleSelector child = new StyleSelector().add(StyleSelectorType.TYPE, "text");

        IStyleSelector[] twoGenerationSelectors = parser.readSelectors(twoGenerationHierarchic);

        assertThat(twoGenerationSelectors[0]).isInstanceOf(StyleSelectorHierarchic.class);

        StyleSelectorHierarchic selector = (StyleSelectorHierarchic) twoGenerationSelectors[0];

        assertThat(selector.getParentSelector().match(grandparent)).isTrue();
        assertThat(selector.isDirectChild()).isFalse();

        assertThat(selector.getChildSelector()).isInstanceOf(StyleSelectorHierarchic.class);
        assertThat(((StyleSelectorHierarchic) selector.getChildSelector()).isDirectChild()).isTrue();

        assertThat(((StyleSelectorHierarchic) selector.getChildSelector()).getParentSelector().match(parent)).isTrue();
        assertThat(((StyleSelectorHierarchic) selector.getChildSelector()).getChildSelector().match(child)).isTrue();
    }
}

@@ -88,69 +88,8 @@
 st;%0A
-import org.yggard.brokkgui.style.parser.StyleSelectorParser;%0A
 impo
@@ -2981,27 +2981,71 @@
 ).match(
-grandparent
+new StyleSelectorHierarchic(grandparent, parent, false)
 )).isTru
@@ -3100,12 +3100,11 @@
 ).is
-Fals
+Tru
 e();
@@ -3175,34 +3175,24 @@
 tyleSelector
-Hierarchic
 .class);%0A   
@@ -3211,35 +3211,8 @@
 hat(
-((StyleSelectorHierarchic) 
 sele
@@ -3211,37 +3211,38 @@
 hat(selector.get
-Child
+Parent
 Selector()).isDi
@@ -3243,154 +3243,52 @@
 ).is
-DirectChild()).isTrue();%0A%0A        assertThat(((StyleSelectorHierarchic) selector.getChildSelector()).getParentSelector().match(parent)).isTrue(
+InstanceOf(StyleSelectorHierarchic.class
 );%0A
+%0A
     
@@ -3333,37 +3333,38 @@
 ic) selector.get
-Child
+Parent
 Selector()).getC
@@ -3358,29 +3358,30 @@
 ector()).get
-Child
+Parent
 Selector().m
@@ -3377,37 +3377,43 @@
 elector().match(
-child
+grandparent
 )).isTrue();%0A   

Fix hierarchic selector test