package io.github.hjuergens.time;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.testng.Assert;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

public class PeriodTermTest {
    @Test
    public void plus3M1W() throws Exception {
        ANTLRInputStream inputStream = new ANTLRInputStream("+3M1W");
        PeriodTermLexer lexer = new PeriodTermLexer(inputStream);
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        PeriodTermParser parser = new PeriodTermParser(tokenStream);

        final DateTime result
                = parser.shift().adjusterOut.adjustInto(DateTime.parse("2016-11-13"));
        assertEquals(result, DateTime.parse("2017-02-20"));
    }

    @Test
    public void monday() throws Exception {
        ANTLRInputStream inputStream = new ANTLRInputStream(">>=monday");
        PeriodTermLexer lexer = new PeriodTermLexer(inputStream);
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        PeriodTermParser parser = new PeriodTermParser(tokenStream);

        final DateTime result
                = parser.selector().adjusterOut.adjustInto(DateTime.parse("2016-11-13"));
        assertEquals(result, DateTime.parse("2016-11-21"));
    }

    @Test
    public void sameMonday() throws Exception {
        {
            ANTLRInputStream inputStream = new ANTLRInputStream("<<=monday");
            PeriodTermLexer lexer = new PeriodTermLexer(inputStream);
            CommonTokenStream tokenStream = new CommonTokenStream(lexer);
            PeriodTermParser parser = new PeriodTermParser(tokenStream);

            final DateTime result
                    = parser.selector().adjusterOut.adjustInto(DateTime.parse("2016-11-14"));

            Assert.assertTrue(result.withTimeAtStartOfDay().isEqual(
                    DateTime.parse("2016-11-07T23:59:59.999+01:00").withTimeAtStartOfDay()));
        }

        {
            ANTLRInputStream inputStream = new ANTLRInputStream("<<monday");
            PeriodTermLexer lexer = new PeriodTermLexer(inputStream);
            CommonTokenStream tokenStream = new CommonTokenStream(lexer);
            PeriodTermParser parser = new PeriodTermParser(tokenStream);

            final DateTime result
                    = parser.selector().adjusterOut.adjustInto(DateTime.parse("2016-11-14"));

            Assert.assertTrue(result.withTimeAtStartOfDay().isEqual(
                    DateTime.parse("2016-10-31T23:59:59.999+01:00").withTimeAtStartOfDay()));
        }


    }


}
@@ -1918,36 +1918,38 @@
    Assert.assert
-True
+Equals
 (result.withTime
@@ -1959,32 +1959,35 @@
 tartOfDay().
-isEqual(
+toString(),
 %0A           
@@ -2057,32 +2057,42 @@
 meAtStartOfDay()
+.toString(
 ));%0A        %7D%0A%0A 
@@ -2553,12 +2553,14 @@
 sert
-True
+Equals
 (res
@@ -2590,16 +2590,19 @@
 y().
-isEqual(
+toString(),
 %0A   
@@ -2688,16 +2688,26 @@
 tOfDay()
+.toString(
 ));%0A    

equal fails