package uk.co.flamingpenguin.jewel.cli;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import uk.co.flamingpenguin.jewel.cli.ArgumentValidationException.ValidationError;
import uk.co.flamingpenguin.jewel.cli.ArgumentValidationException.ValidationError.ErrorType;

/*
 * Copyright 2011 Tim Wood
 *
 * 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. 
 */

public class TestUnparsedArguments {
    public interface UnparsedOption {
        @Unparsed String getName();
    }

    public interface UnparsedListOption {
        @Unparsed List<String> getNames();
    }

    public interface OptionalUnparsedOption {
        @Unparsed String getName();

        boolean isName();
    }

    public interface OptionalUnparsedListOption {
        @Unparsed List<String> getNames();

        boolean isNames();
    }

    @Test public void testUnparsedOption() throws ArgumentValidationException {
        final UnparsedOption result =
                new CliInterfaceImpl<UnparsedOption>(UnparsedOption.class).parseArguments(new String[] { "value" });
        assertEquals("value", result.getName());
    }

    @Test public void testUnparsedOptionMissingValue() {
        try {
            new CliInterfaceImpl<UnparsedOption>(UnparsedOption.class).parseArguments(new String[] {});
            fail();
        } catch (final ArgumentValidationException e) {
            final ArrayList<ValidationError> validationErrors = e.getValidationErrors();
            assertEquals(1, validationErrors.size());
            assertEquals(ErrorType.MissingValue, validationErrors.get(0).getErrorType());
        }
    }

    @Test public void testUnparsedListOption() throws ArgumentValidationException {
        final UnparsedListOption result =
                new CliInterfaceImpl<UnparsedListOption>(UnparsedListOption.class).parseArguments(new String[] {
                        "value0",
                        "value1" });
        assertEquals(2, result.getNames().size());
        assertEquals("value0", result.getNames().get(0));
        assertEquals("value1", result.getNames().get(1));
    }

    @Test public void testUnparsedListOptionMissingValue() throws ArgumentValidationException {
        new CliInterfaceImpl<UnparsedListOption>(UnparsedListOption.class).parseArguments(new String[] {});
    }

    @Test public void testOptionalUnparsedOption() throws ArgumentValidationException {
        final UnparsedOption result =
                new CliInterfaceImpl<UnparsedOption>(UnparsedOption.class).parseArguments(new String[] { "value" });
        assertEquals(result.getName(), "value");
    }

    @Test public void testOptionalUnparsedOptionMissingValue() throws ArgumentValidationException {
        final OptionalUnparsedOption result =
                new CliInterfaceImpl<OptionalUnparsedOption>(OptionalUnparsedOption.class)
                        .parseArguments(new String[] {});
        assertFalse(result.isName());
    }

    @Test public void testOptionalUnparsedListOption() throws ArgumentValidationException {
        final OptionalUnparsedListOption result =
                new CliInterfaceImpl<OptionalUnparsedListOption>(OptionalUnparsedListOption.class)
                        .parseArguments(new String[] {
                                "value0",
                                "value1" });
        assertEquals(2, result.getNames().size());
        assertEquals("value0", result.getNames().get(0));
        assertEquals("value1", result.getNames().get(1));
        assertTrue(result.isNames());
    }

    @Test public void testOptionalUnparsedListOptionMissingValue() throws ArgumentValidationException {
        final OptionalUnparsedListOption result =
                new CliInterfaceImpl<OptionalUnparsedListOption>(OptionalUnparsedListOption.class)
                        .parseArguments(new String[] {});
        assertFalse(result.isNames());
    }
}

@@ -67,16 +67,88 @@
 ssert.*;
+%0Aimport static uk.co.flamingpenguin.jewel.cli.CliFactory.parseArguments;
 %0A%0Aimport
@@ -1530,191 +1530,75 @@
     
-final UnparsedOption result =%0A                new CliInterfaceImpl%3CUnparsedOption%3E(UnparsedOption.class).parseArguments(new String%5B%5D %7B %22value%22 %7D);%0A        assertEquals(%22value%22, result
+assertEquals(%22value%22, parseArguments(UnparsedOption.class, %22value%22)
 .get
@@ -1700,97 +1700,43 @@
     
-new CliInterfaceImpl%3CUnparsedOption%3E(UnparsedOption.class).parseArguments(new String%5B%5D %7B%7D
+parseArguments(UnparsedOption.class
 );%0A 
@@ -2189,179 +2189,59 @@
 lt =
-%0A                new CliInterfaceImpl%3CUnparsedListOption%3E(UnparsedListOption.class).parseArguments(new String%5B%5D %7B%0A                        %22value0%22,%0A                       
+ parseArguments(UnparsedListOption.class, %22value0%22,
  %22va
@@ -2237,34 +2237,32 @@
 alue0%22, %22value1%22
- %7D
 );%0A        asser
@@ -2530,105 +2530,47 @@
     
-new CliInterfaceImpl%3CUnparsedListOption%3E(UnparsedListOption.class).parseArguments(new String%5B%5D %7B%7D
+parseArguments(UnparsedListOption.class
 );%0A 
@@ -2675,532 +2675,259 @@
     
-final UnparsedOption result =%0A                new CliInterfaceImpl%3CUnparsedOption%3E(UnparsedOption.class).parseArguments(new String%5B%5D %7B %22value%22 %7D);%0A        assertEquals(result.getName(), %22value%22);%0A    %7D%0A%0A    @Test public void testOptionalUnparsedOptionMissingValue() throws ArgumentValidationException %7B%0A        final OptionalUnparsedOption result =%0A                new CliInterfaceImpl%3COptionalUnparsedOption%3E(OptionalUnparsedOption.class)%0A                        .parseArguments(new String%5B%5D %7B%7D);%0A        assertFalse(result
+assertEquals(parseArguments(UnparsedOption.class, %22value%22).getName(), %22value%22);%0A    %7D%0A%0A    @Test public void testOptionalUnparsedOptionMissingValue() throws ArgumentValidationException %7B%0A        assertFalse(parseArguments(OptionalUnparsedOption.class)
 .isN
@@ -3074,32 +3074,48 @@
 tOption result =
+ parseArguments(
 %0A               
@@ -3119,162 +3119,42 @@
     
-new CliInterfaceImpl%3COptionalUnparsedListOption%3E(OptionalUnparsedListOption.class)%0A                        .parseArguments(new String%5B%5D %7B%0A                
+OptionalUnparsedListOption.class,%0A
     
@@ -3195,32 +3195,16 @@
     
-                
 %22value1%22
  %7D);
@@ -3199,18 +3199,16 @@
 %22value1%22
- %7D
 );%0A     
@@ -3530,233 +3530,68 @@
     
-final OptionalUnparsedListOption result =%0A                new CliInterfaceImpl%3COptionalUnparsedListOption%3E(OptionalUnparsedListOption.class)%0A                        .parseArguments(new String%5B%5D %7B%7D);%0A        assertFalse(result
+assertFalse(parseArguments(OptionalUnparsedListOption.class)
 .isN

clean up test case