import os

from spec import eq_, skip, Spec

from invoke import run
from invoke.parser import Parser, Context
from invoke.collection import Collection
from invoke.task import task

from _utils import support


class CLI(Spec):
    "Command-line interface"

    def setup(self):
        @task
        def mytask(mystring, s, boolean=False, b=False):
            pass
        @task
        def mytask2():
            pass
        @task
        def mytask3(mystring):
            pass
        c = Collection()
        c.add_task('mytask', mytask)
        c.add_task('mytask2', mytask2)
        c.add_task('mytask3', mytask3)
        self.c = c

    def _parser(self):
        return Parser(self.c.to_contexts())

    def _parse(self, argstr):
        return self._parser().parse_argv(argstr.split())

    def _compare(self, invoke, flagname, value):
        invoke = "mytask " + invoke
        result = self._parse(invoke)
        eq_(result.to_dict()['mytask'][flagname], value)

    # Yo dogfood, I heard you like invoking
    def basic_invocation(self):
        os.chdir(support)
        result = run("invoke -c integration print_foo")
        eq_(result.stdout, "foo\n")

    def implicit_task_module(self):
        # Contains tasks.py
        os.chdir(support + '/implicit/')
        # Doesn't specify --collection
        result = run("invoke foo")
        eq_(result.stdout, "Hm\n")

    def boolean_args(self):
        self._compare("--boolean", 'boolean', True)

    def flag_then_space_then_value(self):
        self._compare("--mystring foo", 'mystring', 'foo')

    def flag_then_equals_sign_then_value(self):
        self._compare("--mystring=foo", 'mystring', 'foo')

    def short_boolean_flag(self):
        self._compare("-b", 'b', True)

    def short_flag_then_space_then_value(self):
        self._compare("-s value", 's', 'value')

    def short_flag_then_equals_sign_then_value(self):
        self._compare("-s=value", 's', 'value')

    def short_flag_with_adjacent_value(self):
        "taskname -fvalue"
        skip()

    def _flag_value_task(self, value):
        r = self._parse("mytask -s %s mytask2" % value)
        eq_(len(r), 2)
        eq_(r[0].name, 'mytask')
        eq_(r[0].args.s.value, value)
        eq_(r[1].name, 'mytask2')

    def flag_value_then_task(self):
        self._flag_value_task('value')

    def flag_value_same_as_task_name(self):
        self._flag_value_task('mytask2')

    def three_tasks_with_args(self):
        r = self._parse("mytask --boolean mytask3 --mystring foo mytask2")
        eq_(len(r), 3)
        eq_([x.name for x in r], ['mytask', 'mytask3', 'mytask2'])
        eq_(r[0].args.boolean.value, True)
        eq_(r[1].args.mystring.value, 'foo')

    def tasks_with_duplicately_named_kwargs(self):
        r = self._parse("mytask --mystring foo mytask3 --mystring bar")
        eq_(r[0].name, 'mytask')
        eq_(r[0].args.mystring.value, 'foo')
        eq_(r[1].name, 'mytask3')
        eq_(r[1].args.mystring.value, 'bar')

    def complex_multitask_invocation(self):
        "-c integration task1 --bool_arg --val_arg=value task2 --val_arg othervalue -b"
        skip()

    def multiple_short_flags_adjacent(self):
        "task1 -abc => equiv to task1 -a -b -c"
        skip()

@@ -2997,156 +2997,8 @@
 ')%0A%0A
-    def complex_multitask_invocation(self):%0A        %22-c integration task1 --bool_arg --val_arg=value task2 --val_arg othervalue -b%22%0A        skip()%0A%0A
     

Remove extraneous test (others are just as complex now)