/*
 * Copyright 2013-2016 Netherlands Forensic Institute
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.parsingdata.metal;

import io.parsingdata.metal.data.ParseResult;
import io.parsingdata.metal.token.Token;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.IOException;

import static io.parsingdata.metal.Shorthand.*;
import static io.parsingdata.metal.util.EncodingFactory.enc;
import static io.parsingdata.metal.util.EnvironmentFactory.stream;

@RunWith(JUnit4.class)
public class ShorthandsTest {

    private final Token multiSequence = seq(def("a", con(1), eq(con(1))),
                                            def("b", con(1), eq(con(2))),
                                            def("c", con(1), eq(con(3))));

    private final Token multiChoice = cho(def("a", con(1), gtNum(con(2))),
                                          def("b", con(1), gtNum(con(1))),
                                          def("c", con(1), gtNum(con(0))));

    @Test
    public void sequenceMultiMatch() throws IOException {
        Assert.assertTrue(multiSequence.parse(stream(1, 2, 3), enc()).succeeded);
    }

    @Test
    public void sequenceMultiNoMatch() throws IOException {
        Assert.assertFalse(multiSequence.parse(stream(1, 2, 2), enc()).succeeded);
    }

    @Test
    public void choiceMultiMatchA() throws IOException {
        runChoice(3, "a");
    }

    @Test
    public void choiceMultiMatchB() throws IOException {
        runChoice(2, "b");
    }

    @Test
    public void choiceMultiMatchC() throws IOException {
        runChoice(1, "c");
    }

    private void runChoice(final int data, final String matched) throws IOException {
        final ParseResult res = multiChoice.parse(stream(data), enc());
        Assert.assertTrue(res.succeeded);
        Assert.assertTrue(res.environment.order.current().matches(matched));
    }

    @Test
    public void choiceMultiNoMatch() throws IOException {
        Assert.assertFalse(multiChoice.parse(stream(0), enc()).succeeded);
    }

}

@@ -735,33 +735,8 @@
 en;%0A
-import org.junit.Assert;%0A
 impo
@@ -1025,16 +1025,173 @@
 .stream;
+%0Aimport static io.parsingdata.metal.util.TokenDefinitions.any;%0Aimport static junit.framework.TestCase.assertFalse;%0Aimport static org.junit.Assert.assertTrue;
 %0A%0A@RunWi
@@ -1242,32 +1242,39 @@
  %7B%0A%0A    private 
+static 
 final Token mult
@@ -1473,235 +1473,8 @@
 );%0A%0A
-    private final Token multiChoice = cho(def(%22a%22, con(1), gtNum(con(2))),%0A                                          def(%22b%22, con(1), gtNum(con(1))),%0A                                          def(%22c%22, con(1), gtNum(con(0))));%0A%0A
     
@@ -1537,39 +1537,32 @@
 ption %7B%0A        
-Assert.
 assertTrue(multi
@@ -1689,39 +1689,32 @@
 ption %7B%0A        
-Assert.
 assertFalse(mult
@@ -1764,32 +1764,198 @@
 ceeded);%0A    %7D%0A%0A
+    private static final Token multiChoice = cho(def(%22a%22, con(1), gtNum(con(2))),%0A        def(%22b%22, con(1), gtNum(con(1))),%0A        def(%22c%22, con(1), gtNum(con(0))));%0A%0A
     @Test%0A    pu
@@ -2399,39 +2399,32 @@
 enc());%0A        
-Assert.
 assertTrue(res.s
@@ -2438,31 +2438,24 @@
 d);%0A        
-Assert.
 assertTrue(r
@@ -2587,29 +2587,22 @@
         
-A
+a
 ssert
-.assert
 False(mu
@@ -2624,16 +2624,418 @@
 (stream(
+0), enc()).succeeded);%0A    %7D%0A%0A    private static final Token nonLocalCompare =%0A        seq(any(%22a%22),%0A            def(%22b%22, con(3)),%0A            def(%22c%22, con(1), gtNum(last(ref(%22a%22)), con(0))),%0A            def(%22d%22, con(1), eqStr(last(ref(%22b%22)), con(%22abc%22)))%0A        );%0A%0A    @Test%0A    public void nonLocalCompare() throws IOException %7B%0A        assertTrue(nonLocalCompare.parse(stream(1, 'a', 'b', 'c', 0, 
 0), enc(

Improve coverage.