package org.yi.happy.archive.commandLine;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

/**
 * Tests for {@link EnvHandler}, also tests {@link CommandParseEngine}.
 */
public class EnvHandlerTest {
    /**
     * parse a command line with no arguments or options.
     * 
     * @throws CommandParseException
     */
    @Test
    public void testBareCommand() throws CommandParseException {
        EnvBuilder env = new EnvBuilder();

        new CommandParseEngine(new EnvHandler(env)).parse("show-env");

        Env want = new EnvBuilder().withCommand("show-env").create();
        assertEquals(want, env.create());
    }

    /**
     * parse a command line with no arguments but with options.
     * 
     * @throws CommandParseException
     */
    @Test
    public void testWithOption() throws CommandParseException {
        EnvBuilder env = new EnvBuilder();

        new CommandParseEngine(new EnvHandler(env)).parse("show-env",
                "--store", "somewhere");

        Env want = new EnvBuilder().withCommand("show-env")
                .withStore("somewhere").create();
        assertEquals(want, env.create());
    }

    /**
     * parse a command line with arguments and options.
     * 
     * @throws CommandParseException
     */
    @Test
    public void testWithOptionAndFile() throws CommandParseException {
        EnvBuilder env = new EnvBuilder();

        new CommandParseEngine(new EnvHandler(env)).parse("show-env",
                "--store", "somewhere", "file");

        Env want = new EnvBuilder().withCommand("show-env")
                .withStore("somewhere").addArgument("file").create();
        assertEquals(want, env.create());
    }

    /**
     * I want an empty command line to parse successfully.
     * 
     * @throws CommandParseException
     */
    @Test
    public void testBlank() throws CommandParseException {
        EnvBuilder env = new EnvBuilder();

        new CommandParseEngine(new EnvHandler(env)).parse();

        Env want = new EnvBuilder().create();
        assertEquals(want, env.create());
    }

    /**
     * I want an option with no name to not parse
     * 
     * @throws CommandParseException
     */
    @Test(expected = CommandParseException.class)
    public void testNoName() throws CommandParseException {
        EnvBuilder env = new EnvBuilder();

        new CommandParseEngine(new EnvHandler(env))
                .parse("show-env", "--=true");
    }

    /**
     * I want an option starting with "---" to not parse
     * 
     * @throws CommandParseException
     */
    @Test(expected = CommandParseException.class)
    public void testThreeDashes() throws CommandParseException {
        EnvBuilder env = new EnvBuilder();

        new CommandParseEngine(new EnvHandler(env)).parse("show-env", "---");
    }

}

@@ -82,16 +82,65 @@
 quals;%0A%0A
+import org.junit.After;%0Aimport org.junit.Before;%0A
 import o
@@ -265,16 +265,535 @@
 rTest %7B%0A
+    private CommandParseEngine commandParseEngine;%0A    private EnvBuilder envBuilder;%0A    private EnvHandler envHandler;%0A%0A    /**%0A     * set up.%0A     */%0A    @Before%0A    public void before() %7B%0A        envBuilder = new EnvBuilder();%0A        envHandler = new EnvHandler(envBuilder);%0A        commandParseEngine = new CommandParseEngine(envHandler);%0A    %7D%0A%0A    /**%0A     * tear down.%0A     */%0A    @After%0A    public void after() %7B%0A        envBuilder = null;%0A        envHandler = null;%0A        commandParseEngine = null;%0A    %7D%0A%0A
     /**%0A
@@ -981,51 +981,8 @@
 on %7B
-%0A        EnvBuilder env = new EnvBuilder();
 %0A%0A  
@@ -979,37 +979,33 @@
 tion %7B%0A%0A        
-new C
+c
 ommandParseEngin
@@ -1005,37 +1005,16 @@
 seEngine
-(new EnvHandler(env))
 .parse(%22
@@ -1118,32 +1118,39 @@
 Equals(want, env
+Builder
 .create());%0A    
@@ -1354,51 +1354,8 @@
 on %7B
-%0A        EnvBuilder env = new EnvBuilder();
 %0A%0A  
@@ -1352,37 +1352,33 @@
 tion %7B%0A%0A        
-new C
+c
 ommandParseEngin
@@ -1378,37 +1378,16 @@
 seEngine
-(new EnvHandler(env))
 .parse(%22
@@ -1396,32 +1396,16 @@
 ow-env%22,
-%0A               
  %22--stor
@@ -1555,32 +1555,39 @@
 Equals(want, env
+Builder
 .create());%0A    
@@ -1790,51 +1790,8 @@
 on %7B
-%0A        EnvBuilder env = new EnvBuilder();
 %0A%0A  
@@ -1788,37 +1788,33 @@
 tion %7B%0A%0A        
-new C
+c
 ommandParseEngin
@@ -1814,37 +1814,16 @@
 seEngine
-(new EnvHandler(env))
 .parse(%22
@@ -1832,32 +1832,16 @@
 ow-env%22,
-%0A               
  %22--stor
@@ -2019,32 +2019,39 @@
 Equals(want, env
+Builder
 .create());%0A    
@@ -2245,51 +2245,8 @@
 on %7B
-%0A        EnvBuilder env = new EnvBuilder();
 %0A%0A  
@@ -2243,37 +2243,33 @@
 tion %7B%0A%0A        
-new C
+c
 ommandParseEngin
@@ -2269,37 +2269,16 @@
 seEngine
-(new EnvHandler(env))
 .parse()
@@ -2356,16 +2356,23 @@
 ant, env
+Builder
 .create(
@@ -2615,112 +2615,26 @@
     
-EnvBuilder env = new EnvBuilder();%0A%0A        new CommandParseEngine(new EnvHandler(env))%0A                
+commandParseEngine
 .par
@@ -2912,95 +2912,26 @@
     
-EnvBuilder env = new EnvBuilder();%0A%0A        new CommandParseEngine(new EnvHandler(env))
+commandParseEngine
 .par
@@ -2959,11 +2959,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

simplify EnvHandlerTest