import sys
import os
import cStringIO

from nose.tools import with_setup
from nose.plugins.skip import SkipTest

sys.path.append('../lib/dodo')

from todo import Todo
from task_parser_generator import TaskParserGenerator

from helpers import mktmpdir, want_exception

todo_content_simple = ( "$version 1",
                        "A - - test-task-1",
                        "B - - test-task-2" )

todo_content_complex = ( "$version 1",
                         "A - - test-task-1",
                         "B - - test-task-2",
                         "@test-project-1",
                         "C - - test-task-3",
                         "A - - test-task-4",
                         "@test-project-2",
                         "B - - test-task-5" )

def setup():
    pass

def teardown():
    pass

@with_setup(setup, teardown)
def test_todo_ls():
    tmp = mktmpdir()

    todo_file = os.path.join(tmp, 'todo')
    with file(todo_file, 'w') as f:
        f.write('\n'.join(todo_content_complex))

    tasks, last_index = TaskParserGenerator(todo_file).parse()
    todo = Todo.TodoCommands(tasks, last_index)

    output = cStringIO.StringIO()
    saveout = sys.stdout
    sys.stdout = output
    todo.ls('')

    result = output.getvalue()
    sys.stdout = saveout
    
    want = """  1: test-task-1
  2: test-task-2
@test-project-1
  3: test-task-3
  4: test-task-4
@test-project-2
  5: test-task-5
"""

    assert want == result
    output.close()

def test_todo_ls_project():
    tmp = mktmpdir()

    todo_file = os.path.join(tmp, 'todo')
    with file(todo_file, 'w') as f:
        f.write('\n'.join(todo_content_complex))

    tasks, last_index = TaskParserGenerator(todo_file).parse()
    todo = Todo.TodoCommands(tasks, last_index)

    output = cStringIO.StringIO()
    saveout = sys.stdout
    sys.stdout = output
    todo.ls(['@test-project-1', ])

    result = output.getvalue()
    sys.stdout = saveout
    
    want = """@test-project-1
  3: test-task-3
  4: test-task-4
"""

    assert want == result
    output.close()

@with_setup(setup, teardown)
def test_todo_pri():
    raise SkipTest()

@with_setup(setup, teardown)
def test_todo_rm():
    raise SkipTest()

@with_setup(setup, teardown)
def test_todo_archive():
    raise SkipTest()

@with_setup(setup, teardown)
def test_todo_help():
    raise SkipTest()

@with_setup(setup, teardown)
def test_todo_add():
    raise SkipTest()

@with_setup(setup, teardown)
def test_todo_dl():
    raise SkipTest()

@with_setup(setup, teardown)
def test_todo_do():
    raise SkipTest()

@@ -630,33 +630,33 @@
                %22
-A
+B
  - - test-task-4
@@ -2092,32 +2092,791 @@
 ():%0A    
-raise SkipTest()
+tmp = mktmpdir()%0A%0A    todofile = os.path.join(tmp, 'todo')%0A    with file(todofile, 'w') as fobj:%0A        fobj.write('%5Cn'.join(todo_content_complex))%0A%0A    parsergenerator = TaskParserGenerator(todofile)%0A    tasks, lastindex = parsergenerator.parse()%0A    todo = Todo.TodoCommands(tasks, lastindex)%0A%0A    todo.pri(%5B'@test-project-1', %22A%22%5D)%0A    parsergenerator.generate(tasks)%0A%0A    with file(todofile) as fobj:%0A        want = ( %22$version 1%22,%0A                 %22A - - test-task-1%22,%0A                 %22B - - test-task-2%22,%0A                 %22@test-project-1%22,%0A                 %22A - - test-task-3%22,%0A                 %22A - - test-task-4%22,%0A                 %22@test-project-2%22,%0A                 %22B - - test-task-5%22)%0A        result = fobj.read()%0A        assert result == '%5Cn'.join(want) + '%5Cn'
 %0A%0A@with_

Update test