import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

from dynmen.common import TraitMenu, Option, Flag
from dynmen.menu import MenuError

class Grep(TraitMenu):
    _base_command = ['grep']
    ignore_case = Flag('-i')
    pattern = Option('-e')

import pytest
MAX_WAIT = 3.0
@pytest.fixture(scope='function')
def grep():
    return Grep()

def assert_record(rec, name, value, transformed):
    assert rec == (name, value, transformed)

def assert_future(future, selected, value):
    out = future.result(MAX_WAIT)
    assert out.selected == selected
    assert out.value == value

def test_flag(grep):
    assert_record(grep.ignore_case, 'ignore_case', False, [])
    grep.ignore_case = True
    assert_record(grep.ignore_case, 'ignore_case', True, ['-i'])
    grep.ignore_case = False
    assert_record(grep.ignore_case, 'ignore_case', False, [])

def test_option(grep):
    assert_record(grep.pattern, 'pattern', None, [])
    txt = 'some pattern'
    grep.pattern = txt
    assert_record(grep.pattern, 'pattern', txt, ['-e', txt])
    grep.pattern = None
    assert_record(grep.pattern, 'pattern', None, [])

def test_usage(grep):
    grep.menu.process_mode = 'futures'
    inp = list('abcdefghijklmnopqrstuv')
    grep.pattern = 'm'
    future = grep(inp)
    assert_future(future, 'm', None)

def test_usage_fail(grep):
    grep.menu.process_mode = 'futures'
    inp = list('abcdefghijklmnopqrstuv')
    grep.pattern = 'M'
    future = grep(inp)
    with pytest.raises(MenuError):
        future.result(MAX_WAIT)

def test_usage_update(grep):
    grep.menu.process_mode = 'futures'
    inp = list('abcdefghijklmnopqrstuv')
    grep.pattern = 'o'
    assert_future(grep(inp), 'o', None)
    grep.pattern = 'C'
    with pytest.raises(MenuError):
        grep(inp).result(MAX_WAIT)
    grep.ignore_case = True
    assert_future(grep(inp), 'c', None)

@@ -866,32 +866,255 @@
 e', False, %5B%5D)%0A%0A
+def test_flag2(grep):%0A    assert_record(grep.ignore_case, 'ignore_case', False, %5B%5D)%0A    from copy import copy%0A    grep.ignore_case = copy(grep.ignore_case)%0A    assert_record(grep.ignore_case, 'ignore_case', False, %5B%5D)%0A    %0A
 def test_option(
@@ -1112,32 +1112,32 @@
 t_option(grep):%0A
-
     assert_recor
@@ -1352,32 +1352,198 @@
 rn', None, %5B%5D)%0A%0A
+def test_option2(grep):%0A    txt = 'some pattern'%0A    grep.pattern = txt%0A    grep.pattern = grep.pattern%0A    assert_record(grep.pattern, 'pattern', txt, %5B'-e', txt%5D)%0A%0A
 def test_usage(g
@@ -2218,32 +2218,32 @@
 ore_case = True%0A
-
     assert_futur
@@ -2258,16 +2258,308 @@
 np), 'c', None)%0A
+%0Adef test_trait_menu_kwargs():%0A    grep = Grep(ignore_case=True)%0A    assert_record(grep.ignore_case, 'ignore_case', True, %5B'-i'%5D)%0A%0Adef test_update_base():%0A    grep = Grep(ignore_case=True)%0A    grep._needs_update = False%0A    grep.base_command = %5B'cat'%5D%0A    assert grep._needs_update == True%0A%0A%0A

Add more tests for dynmen.common