<commit_before>import unittest
from robot.parsing import TestCase
from robot.parsing.settings import Fixture, Documentation, Timeout, Tags, Return

from robot.utils.asserts import (assert_equals, assert_true, assert_false,
                                 assert_none, assert_raises_with_msg)
from robotide.controller.settingcontroller import *
from robotide.controller.filecontroller import *
from robotide.controller.tablecontrollers import _WithListOperations
from robotide.controller.tablecontrollers import *
from robotide.controller.chiefcontroller import ChiefController
from robotide.controller import NewDatafile
from resources import SUITEPATH


class _FakeParent(object):
    def __init__(self):
        self.dirty = False
        self.datafile = None
    def mark_dirty(self):
        self.dirty = True


class DocumentationControllerTest(unittest.TestCase):

    def setUp(self):
        self.doc = Documentation('Documentation')
        self.doc.value = 'Initial doc'
        self.parent = _FakeParent()
        self.ctrl = DocumentationController(self.parent, self.doc)

    def test_creation(self):
        assert_equals(self.ctrl.display_value, 'Initial doc')
        assert_true(self.ctrl.datafile is None)
        ctrl = DocumentationController(self.parent, Documentation('[Documentation]'))
        assert_equals(ctrl.label, 'Documentation')

    def test_setting_value(self):
        self.ctrl.set_value('Doc changed')
        assert_equals(self.doc.value, 'Doc changed')
        self.ctrl.set_value('Doc changed | again')
        assert_equals(self.doc.value, 'Doc changed | again')

    def test_get_editable_value(self):
        self.doc.value = 'My doc \\n with enters \\\\\\r\\n and \\t tabs and escapes \\\\n \\\\\\\\r'
        assert_equals(self.ctrl.editable_value, 'My doc \n with enters \\\\\n'
                                                ' and \\t tabs and escapes \\\\n \\\\\\\\r')

    def test_set_editable_value(self):
        test_text = '''My doc
 with enters
 and \t tabs'''
        self.ctrl.editable_value = test_text
        assert_equals(self.doc.value, 'My doc\\n with enters\\n and \t tabs')
        assert_equals(self.ctrl.editable_value, test_text)

    def test_set_editable_value_should_escape_leading_hash(self):
        self.ctrl.editable_value = '# Not # Comment'
        assert_equals(self.doc.value, '\\# Not # Comment')
        assert_equals(self.ctrl.editable_value, '\\# Not # Comment')

    def test_get_visible_value(self):
        self.doc.value = 'My doc \\n with enters \\n and \t tabs'
        assert_equals(self.ctrl.visible_value, '''My doc <br />
with enters <br />
and &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tabs''')

    def test_setting_value_informs_parent_controller_about_dirty_model(self):
        self.ctrl.set_value('Blaa')
        assert_true(self.ctrl.dirty)

    def test_set_empty_value(self):
        self.ctrl.set_value('')
        assert_equals(self.doc.value, '')
        assert_true(self.ctrl.dirty)

    def test_same_value(self):
        self.ctrl.set_value('Initial doc')
        assert_false(self.ctrl.dirty)

    def test_clear(self):
        self.ctrl.clear()
        assert_equals(self.doc.value, '')
        assert_true(self.ctrl.dirty)


class FixtureControllerTest(unittest.TestCase):

    def setUp(self):
        self.fix = Fixture('Suite Setup')
        self.fix.name = 'My Setup'
        self.fix.args = ['argh', 'urgh']
        self.parent = _FakeParent()
        self.ctrl = FixtureController(self.parent, self.fix)

    def test_creation(self):
        assert_equals(self.ctrl.display_value, 'My Setup | argh | urgh')
        assert_equals(self.ctrl.label, 'Suite Setup')
        assert_true(self.ctrl.is_set)

    def test_value_with_empty_fixture(self):
        assert_equals(FixtureController(self.parent, Fixture('Teardown')).display_value, '')

    def test_setting_value_changes_fixture_state(self):
        self.ctrl.set_value('Blaa')
        assert_equals(self.fix.name, 'Blaa')
        assert_equals(self.fix.args, [])
        self.ctrl.set_value('Blaa | a')
        assert_equals(self.fix.name, 'Blaa')
        assert_equals(self.fix.args, ['a'])

    def test_whitespace_is_ignored_in_value(self):
        self.ctrl.set_value('Name |   a    |    b      |     c')
        assert_equals(self.fix.name, 'Name')
        assert_equals(self.fix.args, ['a', 'b', 'c'])
        assert_equals(self.ctrl.display_value, 'Name | a | b | c')

    def test_setting_value_informs_parent_controller_about_dirty_model(self):
        self.ctrl.set_value('Blaa')
        assert_true(self.ctrl.dirty)

    def test_set_empty_value(self):
        self.ctrl.set_value('')
        assert_equals(self.fix.name, '')
        assert_equals(self.fix.args, [])
        assert_true(self.ctrl.dirty)

    def test_same_value(self):
        self.ctrl.set_value('My Setup | argh | urgh')
        assert_false(self.ctrl.dirty)

    def test_setting_comment(self):
        self.ctrl.set_comment('My comment')
        assert_equals(self.ctrl.comment, 'My comment')
        assert_true(self.ctrl.dirty)


class TagsControllerTest(unittest.TestCase):

    def setUp(self):
        self.tags = Tags('Force Tags')
        self.tags.value = ['f1', 'f2']
        self.parent = _FakeParent()
        self.ctrl = TagsController(self.parent, self.tags)

    def test_creation(self):
        assert_equals(self.ctrl.display_value, 'f1 | f2')
        assert_true(self.ctrl.is_set)

    def test_value_with_empty_fixture(self):
        assert_equals(TagsController(self.parent, Tags('Tags')).display_value, '')

    def test_setting_value_changes_fixture_state(self):
        self.ctrl.set_value('Blaa')
        assert_equals(self.tags.value, ['Blaa'])
        self.ctrl.set_value('a1 | a2 | a3')
        assert_equals(self.tags.value, ['a1', 'a2', 'a3'])

    def test_setting_value_informs_parent_controller_about_dirty_model(self):
        self.ctrl.set_value('Blaa')
        assert_true(self.ctrl.dirty)

    def test_set_empty_value(self):
        self.ctrl.set_value('')
        assert_equals(self.tags.value, [])
        assert_true(self.ctrl.dirty)

    def test_same_value(self):
        self.ctrl.set_value('f1 | f2')
        assert_false(self.ctrl.dirty)


class TimeoutControllerTest(unittest.TestCase):

    def setUp(self):
        self.to = Timeout('Timeout')
        self.to.value = '1 s'
        self.to.message = 'message'
        self.parent = _FakeParent()
        self.ctrl = TimeoutController(self.parent, self.to)

    def test_creation(self):
        assert_equals(self.ctrl.display_value, '1 s | message')
        assert_true(self.ctrl.is_set)

    def test_value_with_empty_timeout(self):
        assert_equals(TimeoutController(self.parent,
                                        Timeout('Timeout')).display_value, '')

    def test_setting_value_changes_fixture_state(self):
        self.ctrl.set_value('3 s')
        assert_equals(self.to.value, '3 s')
        assert_equals(self.to.message, '')
        self.ctrl.set_value('3 s | new message')
        assert_equals(self.to.value, '3 s')
        assert_equals(self.to.message, 'new message')

    def test_setting_value_informs_parent_controller_about_dirty_model(self):
        self.ctrl.set_value('1 min')
        assert_true(self.ctrl.dirty)

    def test_set_empty_value(self):
        self.ctrl.set_value('')
        assert_equals(self.to.value, '')
        assert_equals(self.to.message, '')
        assert_true(self.ctrl.dirty)

    def test_same_value(self):
        self.ctrl.set_value('1 s | message')
        assert_false(self.ctrl.dirty)


class ReturnValueControllerTest(unittest.TestCase):

    def test_creation(self):
        ctrl = ReturnValueController(_FakeParent(), Return('[Return]'))
        assert_equals(ctrl.label, 'Return Value')


class TestCaseFileControllerTest(unittest.TestCase):

    def test_creation(self):
        ctrl = self._file_ctrl()
        for st in ctrl.settings:
            assert_true(st is not None)
        assert_equals(len(ctrl.settings), 9)
        assert_false(ctrl.dirty)

    def test_has_format(self):
        ctrl = self._file_ctrl()
        assert_true(ctrl.has_format())
        ctrl.data.source = '/tmp/.path.with.dots/test.cases.html'
        assert_true(ctrl.has_format())

    def test_get_format(self):
        ctrl = self._file_ctrl()
        ctrl.data.source = '/tmp/.path.with.dots/test.cases.html'
        assert_equals(ctrl.get_format(), 'html')

    def test_set_format(self):
        ctrl = self._file_ctrl()
        ctrl.data.source = '/tmp/.path.with.dots/test.cases.html'
        assert_equals(ctrl.source, '/tmp/.path.with.dots/test.cases.html')
        ctrl.set_format('txt')
        assert_equals(ctrl.source, '/tmp/.path.with.dots/test.cases.txt')

    def test_add_test_or_kw(self):
        ctrl = self._file_ctrl()
        assert_equals(len(ctrl.tests), 0)
        new_test = TestCaseController(ctrl, TestCase(TestCaseFile(), 'New test'))
        ctrl.add_test_or_keyword(new_test)
        assert_equals(len(ctrl.tests), 1)
        assert_true(ctrl.tests[0]._test.parent is ctrl.datafile)
        assert_true(ctrl.dirty)

    def test_new_test(self):
        test_ctrl = self._file_ctrl().new_test('Foo')
        assert_equals(test_ctrl.name, 'Foo')

    def test_new_keyword(self):
        kw_ctrl = self._file_ctrl().new_keyword('An UK')
        assert_equals(kw_ctrl.name, 'An UK')

    def test_new_keyword_with_args(self):
        kw_ctrl = self._file_ctrl().new_keyword('UK', '${a1} | ${a2}')
        assert_equals(kw_ctrl.name, 'UK')
        assert_equals(kw_ctrl.data.args.value, ['${a1}', '${a2}'])

    def _file_ctrl(self):
        return TestCaseFileController(TestCaseFile())


class TestDataDirectoryControllerTest(unittest.TestCase):

    def test_creation(self):
        ctrl = TestDataDirectoryController(TestDataDirectory())
        for st in ctrl.settings:
            assert_true(st is not None)
        assert_equals(len(ctrl.settings), 6)

    def test_has_format(self):
        ctrl = TestDataDirectoryController(TestDataDirectory())
        assert_false(ctrl.has_format())
        ctrl.mark_dirty()
        assert_false(ctrl.has_format())
        ctrl.data.initfile = '/tmp/__init__.html'
        assert_true(ctrl.has_format())

    def test_set_format(self):
        dir = TestDataDirectory()
        dir.source = '/tmp/'
        ctrl = TestDataDirectoryController(dir)
        assert_false(ctrl.has_format())
        ctrl.set_format('txt')
        assert_true(ctrl.has_format())
        assert_equals(ctrl.source, '/tmp/__init__.txt')

    def test_adding_new_child(self):
        ctrl = TestDataDirectoryController(TestDataDirectory())
        assert_true(ctrl.new_datafile(NewDatafile('path/to/data.txt',
                                                  is_dir_type=False)))


class _BaseWithSteps(unittest.TestCase):

    def _test_copy_empty(self):
        for setting in self.ctrl.settings:
            assert_false(setting.is_set, 'not empty %s' % setting.__class__)
        new = self.ctrl.copy('new name')
        for setting in new.settings:
            assert_false(setting.is_set, 'not empty %s' % setting.__class__)

    def _test_copy_content(self):
        for setting in self.ctrl.settings:
            assert_false(setting.is_set, 'not empty %s' % setting.__class__)
            setting.set_value('boo')
            setting.set_comment('hobo')
        new = self.ctrl.copy('new name')
        for setting in new.settings:
            assert_true(setting.is_set, 'empty %s' % setting.__class__)
            assert_equals(setting.value, 'boo', 'not boo %s' % setting.__class__)
            assert_equals(setting.comment, 'hobo', 'comment not copied %s' % setting.__class__)

    def _test_uk_creation(self):
        observer = self._creation_oberver()
        num_keywords = len(self.tcf.keywords)
        self.ctrl.create_user_keyword('New UK', [], observer.observe)
        assert_true(isinstance(observer.item, UserKeywordController))
        self._check_argument_names(observer.item, 0)
        assert_equals(len(self.tcf.keywords), num_keywords + 1)

    def _test_creation_with_conflicting_name(self):
        self.tcf.keyword_table.add('Duplicate name')
        num_kws = len(self.tcf.keywords)
        assert_raises_with_msg(ValueError, 'User keyword with this name already exists.',
                               self.ctrl.create_user_keyword, 'Duplicate name',
                               [], self._creation_oberver().observe)
        assert_equals(len(self.tcf.keywords), num_kws)
        assert_false(self.ctrl.dirty)

    def _test_creation_with_arguments(self):
        observer = self._creation_oberver()
        self.ctrl.create_user_keyword('Uk w args', ['some', 'value'],
                                      observer.observe)
        kw_ctrl = observer.item
        self._check_argument_names(kw_ctrl, 2)

    def _creation_oberver(self):
        class CreationObserver(object):
            item = None
            def observe(self, controller):
                self.item = controller
        return CreationObserver()

    def _check_argument_names(self, kw_ctrl, num_args):
        for exp, act in zip(('${arg%s}' % i for i in range(1, num_args + 1)),
                            kw_ctrl.data.args.value):
            assert_equals(exp, act)


class TestCaseControllerTest(_BaseWithSteps):

    def setUp(self):
        self.tcf = TestCaseFile()
        self.testcase = self.tcf.testcase_table.add('Test')
        self.testcase.add_step(['Log', 'Hello'])
        self.testcase.add_step(['No Operation'])
        self.testcase.add_step(['Foo'])
        self.tcf.testcase_table.add('Another Test')
        tctablectrl = TestCaseTableController(TestCaseFileController(self.tcf),
                                              self.tcf.testcase_table)
        self.ctrl = TestCaseController(tctablectrl, self.testcase)

    def test_creation(self):
        for st in self.ctrl.settings:
            assert_true(st is not None)
        assert_true(self.ctrl.datafile is self.tcf, self.ctrl.datafile)

    def test_rename_validation(self):
        assert_false(self.ctrl.validate_name('This name is valid'))
        assert_none(self.ctrl.validate_name('Another test'))
        assert_equals(self.ctrl.validate_name('Test'),
                      'Test case with this name already exists.')

    def test_rename(self):
        self.ctrl.rename('Foo Barness')
        assert_equals(self.ctrl.name, 'Foo Barness')
        assert_true(self.ctrl.dirty)

    def test_copy_empty(self):
        self._test_copy_empty()

    def test_copy_content(self):
        self._test_copy_content()

    def test_create_user_keyword(self):
        self._test_uk_creation()

    def test_creating_user_keyword_with_conflicting_name_does_nothing(self):
        self._test_creation_with_conflicting_name()

    def test_creation_with_arguments(self):
        self._test_creation_with_arguments()

    def test_extract_kw(self):
        obs = self._creation_oberver()
        self.ctrl.extract_keyword('New KW', '${argh}', (0,1), obs.observe)
        assert_equals(self.testcase.steps[0].keyword, 'New KW')
        assert_equals(len(self.testcase.steps), 2)
        
        assert_equals(obs.item.steps[0].keyword, 'Log')
        assert_equals(obs.item.steps[1].keyword, 'No Operation')
        assert_equals(obs.item.arguments.value, '${argh}')
        assert_true(self.ctrl.dirty)

    def test_extract_kw_from_the_middle(self):<commit_msg>cleanup<commit_after>import unittest
from robot.parsing import TestCase
from robot.parsing.settings import Fixture, Documentation, Timeout, Tags, Return

from robot.utils.asserts import (assert_equals, assert_true, assert_false,
                                 assert_none, assert_raises_with_msg)
from robotide.controller.settingcontroller import *
from robotide.controller.filecontroller import *
from robotide.controller.tablecontrollers import _WithListOperations
from robotide.controller.tablecontrollers import *
from robotide.controller import NewDatafile
from resources import SUITEPATH


class _FakeParent(object):
    def __init__(self):
        self.dirty = False
        self.datafile = None
    def mark_dirty(self):
        self.dirty = True


class DocumentationControllerTest(unittest.TestCase):

    def setUp(self):
        self.doc = Documentation('Documentation')
        self.doc.value = 'Initial doc'
        self.parent = _FakeParent()
        self.ctrl = DocumentationController(self.parent, self.doc)

    def test_creation(self):
        assert_equals(self.ctrl.display_value, 'Initial doc')
        assert_true(self.ctrl.datafile is None)
        ctrl = DocumentationController(self.parent, Documentation('[Documentation]'))
        assert_equals(ctrl.label, 'Documentation')

    def test_setting_value(self):
        self.ctrl.set_value('Doc changed')
        assert_equals(self.doc.value, 'Doc changed')
        self.ctrl.set_value('Doc changed | again')
        assert_equals(self.doc.value, 'Doc changed | again')

    def test_get_editable_value(self):
        self.doc.value = 'My doc \\n with enters \\\\\\r\\n and \\t tabs and escapes \\\\n \\\\\\\\r'
        assert_equals(self.ctrl.editable_value, 'My doc \n with enters \\\\\n'
                                                ' and \\t tabs and escapes \\\\n \\\\\\\\r')

    def test_set_editable_value(self):
        test_text = '''My doc
 with enters
 and \t tabs'''
        self.ctrl.editable_value = test_text
        assert_equals(self.doc.value, 'My doc\\n with enters\\n and \t tabs')
        assert_equals(self.ctrl.editable_value, test_text)

    def test_set_editable_value_should_escape_leading_hash(self):
        self.ctrl.editable_value = '# Not # Comment'
        assert_equals(self.doc.value, '\\# Not # Comment')
        assert_equals(self.ctrl.editable_value, '\\# Not # Comment')

    def test_get_visible_value(self):
        self.doc.value = 'My doc \\n with enters \\n and \t tabs'
        assert_equals(self.ctrl.visible_value, '''My doc <br />
with enters <br />
and &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tabs''')

    def test_setting_value_informs_parent_controller_about_dirty_model(self):
        self.ctrl.set_value('Blaa')
        assert_true(self.ctrl.dirty)

    def test_set_empty_value(self):
        self.ctrl.set_value('')
        assert_equals(self.doc.value, '')
        assert_true(self.ctrl.dirty)

    def test_same_value(self):
        self.ctrl.set_value('Initial doc')
        assert_false(self.ctrl.dirty)

    def test_clear(self):
        self.ctrl.clear()
        assert_equals(self.doc.value, '')
        assert_true(self.ctrl.dirty)


class FixtureControllerTest(unittest.TestCase):

    def setUp(self):
        self.fix = Fixture('Suite Setup')
        self.fix.name = 'My Setup'
        self.fix.args = ['argh', 'urgh']
        self.parent = _FakeParent()
        self.ctrl = FixtureController(self.parent, self.fix)

    def test_creation(self):
        assert_equals(self.ctrl.display_value, 'My Setup | argh | urgh')
        assert_equals(self.ctrl.label, 'Suite Setup')
        assert_true(self.ctrl.is_set)

    def test_value_with_empty_fixture(self):
        assert_equals(FixtureController(self.parent, Fixture('Teardown')).display_value, '')

    def test_setting_value_changes_fixture_state(self):
        self.ctrl.set_value('Blaa')
        assert_equals(self.fix.name, 'Blaa')
        assert_equals(self.fix.args, [])
        self.ctrl.set_value('Blaa | a')
        assert_equals(self.fix.name, 'Blaa')
        assert_equals(self.fix.args, ['a'])

    def test_whitespace_is_ignored_in_value(self):
        self.ctrl.set_value('Name |   a    |    b      |     c')
        assert_equals(self.fix.name, 'Name')
        assert_equals(self.fix.args, ['a', 'b', 'c'])
        assert_equals(self.ctrl.display_value, 'Name | a | b | c')

    def test_setting_value_informs_parent_controller_about_dirty_model(self):
        self.ctrl.set_value('Blaa')
        assert_true(self.ctrl.dirty)

    def test_set_empty_value(self):
        self.ctrl.set_value('')
        assert_equals(self.fix.name, '')
        assert_equals(self.fix.args, [])
        assert_true(self.ctrl.dirty)

    def test_same_value(self):
        self.ctrl.set_value('My Setup | argh | urgh')
        assert_false(self.ctrl.dirty)

    def test_setting_comment(self):
        self.ctrl.set_comment('My comment')
        assert_equals(self.ctrl.comment, 'My comment')
        assert_true(self.ctrl.dirty)


class TagsControllerTest(unittest.TestCase):

    def setUp(self):
        self.tags = Tags('Force Tags')
        self.tags.value = ['f1', 'f2']
        self.parent = _FakeParent()
        self.ctrl = TagsController(self.parent, self.tags)

    def test_creation(self):
        assert_equals(self.ctrl.display_value, 'f1 | f2')
        assert_true(self.ctrl.is_set)

    def test_value_with_empty_fixture(self):
        assert_equals(TagsController(self.parent, Tags('Tags')).display_value, '')

    def test_setting_value_changes_fixture_state(self):
        self.ctrl.set_value('Blaa')
        assert_equals(self.tags.value, ['Blaa'])
        self.ctrl.set_value('a1 | a2 | a3')
        assert_equals(self.tags.value, ['a1', 'a2', 'a3'])

    def test_setting_value_informs_parent_controller_about_dirty_model(self):
        self.ctrl.set_value('Blaa')
        assert_true(self.ctrl.dirty)

    def test_set_empty_value(self):
        self.ctrl.set_value('')
        assert_equals(self.tags.value, [])
        assert_true(self.ctrl.dirty)

    def test_same_value(self):
        self.ctrl.set_value('f1 | f2')
        assert_false(self.ctrl.dirty)


class TimeoutControllerTest(unittest.TestCase):

    def setUp(self):
        self.to = Timeout('Timeout')
        self.to.value = '1 s'
        self.to.message = 'message'
        self.parent = _FakeParent()
        self.ctrl = TimeoutController(self.parent, self.to)

    def test_creation(self):
        assert_equals(self.ctrl.display_value, '1 s | message')
        assert_true(self.ctrl.is_set)

    def test_value_with_empty_timeout(self):
        assert_equals(TimeoutController(self.parent,
                                        Timeout('Timeout')).display_value, '')

    def test_setting_value_changes_fixture_state(self):
        self.ctrl.set_value('3 s')
        assert_equals(self.to.value, '3 s')
        assert_equals(self.to.message, '')
        self.ctrl.set_value('3 s | new message')
        assert_equals(self.to.value, '3 s')
        assert_equals(self.to.message, 'new message')

    def test_setting_value_informs_parent_controller_about_dirty_model(self):
        self.ctrl.set_value('1 min')
        assert_true(self.ctrl.dirty)

    def test_set_empty_value(self):
        self.ctrl.set_value('')
        assert_equals(self.to.value, '')
        assert_equals(self.to.message, '')
        assert_true(self.ctrl.dirty)

    def test_same_value(self):
        self.ctrl.set_value('1 s | message')
        assert_false(self.ctrl.dirty)


class ReturnValueControllerTest(unittest.TestCase):

    def test_creation(self):
        ctrl = ReturnValueController(_FakeParent(), Return('[Return]'))
        assert_equals(ctrl.label, 'Return Value')


class TestCaseFileControllerTest(unittest.TestCase):

    def test_creation(self):
        ctrl = self._file_ctrl()
        for st in ctrl.settings:
            assert_true(st is not None)
        assert_equals(len(ctrl.settings), 9)
        assert_false(ctrl.dirty)

    def test_has_format(self):
        ctrl = self._file_ctrl()
        assert_true(ctrl.has_format())
        ctrl.data.source = '/tmp/.path.with.dots/test.cases.html'
        assert_true(ctrl.has_format())

    def test_get_format(self):
        ctrl = self._file_ctrl()
        ctrl.data.source = '/tmp/.path.with.dots/test.cases.html'
        assert_equals(ctrl.get_format(), 'html')

    def test_set_format(self):
        ctrl = self._file_ctrl()
        ctrl.data.source = '/tmp/.path.with.dots/test.cases.html'
        assert_equals(ctrl.source, '/tmp/.path.with.dots/test.cases.html')
        ctrl.set_format('txt')
        assert_equals(ctrl.source, '/tmp/.path.with.dots/test.cases.txt')

    def test_add_test_or_kw(self):
        ctrl = self._file_ctrl()
        assert_equals(len(ctrl.tests), 0)
        new_test = TestCaseController(ctrl, TestCase(TestCaseFile(), 'New test'))
        ctrl.add_test_or_keyword(new_test)
        assert_equals(len(ctrl.tests), 1)
        assert_true(ctrl.tests[0]._test.parent is ctrl.datafile)
        assert_true(ctrl.dirty)

    def test_new_test(self):
        test_ctrl = self._file_ctrl().new_test('Foo')
        assert_equals(test_ctrl.name, 'Foo')

    def test_new_keyword(self):
        kw_ctrl = self._file_ctrl().new_keyword('An UK')
        assert_equals(kw_ctrl.name, 'An UK')

    def test_new_keyword_with_args(self):
        kw_ctrl = self._file_ctrl().new_keyword('UK', '${a1} | ${a2}')
        assert_equals(kw_ctrl.name, 'UK')
        assert_equals(kw_ctrl.data.args.value, ['${a1}', '${a2}'])

    def _file_ctrl(self):
        return TestCaseFileController(TestCaseFile())


class TestDataDirectoryControllerTest(unittest.TestCase):

    def test_creation(self):
        ctrl = TestDataDirectoryController(TestDataDirectory())
        for st in ctrl.settings:
            assert_true(st is not None)
        assert_equals(len(ctrl.settings), 6)

    def test_has_format(self):
        ctrl = TestDataDirectoryController(TestDataDirectory())
        assert_false(ctrl.has_format())
        ctrl.mark_dirty()
        assert_false(ctrl.has_format())
        ctrl.data.initfile = '/tmp/__init__.html'
        assert_true(ctrl.has_format())

    def test_set_format(self):
        dir = TestDataDirectory()
        dir.source = '/tmp/'
        ctrl = TestDataDirectoryController(dir)
        assert_false(ctrl.has_format())
        ctrl.set_format('txt')
        assert_true(ctrl.has_format())
        assert_equals(ctrl.source, '/tmp/__init__.txt')

    def test_adding_new_child(self):
        ctrl = TestDataDirectoryController(TestDataDirectory())
        assert_true(ctrl.new_datafile(NewDatafile('path/to/data.txt',
                                                  is_dir_type=False)))


class _BaseWithSteps(unittest.TestCase):

    def _test_copy_empty(self):
        for setting in self.ctrl.settings:
            assert_false(setting.is_set, 'not empty %s' % setting.__class__)
        new = self.ctrl.copy('new name')
        for setting in new.settings:
            assert_false(setting.is_set, 'not empty %s' % setting.__class__)

    def _test_copy_content(self):
        for setting in self.ctrl.settings:
            assert_false(setting.is_set, 'not empty %s' % setting.__class__)
            setting.set_value('boo')
            setting.set_comment('hobo')
        new = self.ctrl.copy('new name')
        for setting in new.settings:
            assert_true(setting.is_set, 'empty %s' % setting.__class__)
            assert_equals(setting.value, 'boo', 'not boo %s' % setting.__class__)
            assert_equals(setting.comment, 'hobo', 'comment not copied %s' % setting.__class__)

    def _test_uk_creation(self):
        observer = self._creation_oberver()
        num_keywords = len(self.tcf.keywords)
        self.ctrl.create_user_keyword('New UK', [], observer.observe)
        assert_true(isinstance(observer.item, UserKeywordController))
        self._check_argument_names(observer.item, 0)
        assert_equals(len(self.tcf.keywords), num_keywords + 1)

    def _test_creation_with_conflicting_name(self):
        self.tcf.keyword_table.add('Duplicate name')
        num_kws = len(self.tcf.keywords)
        assert_raises_with_msg(ValueError, 'User keyword with this name already exists.',
                               self.ctrl.create_user_keyword, 'Duplicate name',
                               [], self._creation_oberver().observe)
        assert_equals(len(self.tcf.keywords), num_kws)
        assert_false(self.ctrl.dirty)

    def _test_creation_with_arguments(self):
        observer = self._creation_oberver()
        self.ctrl.create_user_keyword('Uk w args', ['some', 'value'],
                                      observer.observe)
        kw_ctrl = observer.item
        self._check_argument_names(kw_ctrl, 2)

    def _creation_oberver(self):
        class CreationObserver(object):
            item = None
            def observe(self, controller):
                self.item = controller
        return CreationObserver()

    def _check_argument_names(self, kw_ctrl, num_args):
        for exp, act in zip(('${arg%s}' % i for i in range(1, num_args + 1)),
                            kw_ctrl.data.args.value):
            assert_equals(exp, act)


class TestCaseControllerTest(_BaseWithSteps):

    def setUp(self):
        self.tcf = TestCaseFile()
        self.testcase = self.tcf.testcase_table.add('Test')
        self.testcase.add_step(['Log', 'Hello'])
        self.testcase.add_step(['No Operation'])
        self.testcase.add_step(['Foo'])
        self.tcf.testcase_table.add('Another Test')
        tctablectrl = TestCaseTableController(TestCaseFileController(self.tcf),
                                              self.tcf.testcase_table)
        self.ctrl = TestCaseController(tctablectrl, self.testcase)

    def test_creation(self):
        for st in self.ctrl.settings:
            assert_true(st is not None)
        assert_true(self.ctrl.datafile is self.tcf, self.ctrl.datafile)

    def test_rename_validation(self):
        assert_false(self.ctrl.validate_name('This name is valid'))
        assert_none(self.ctrl.validate_name('Another test'))
        assert_equals(self.ctrl.validate_name('Test'),
                      'Test case with this name already exists.')

    def test_rename(self):
        self.ctrl.rename('Foo Barness')
        assert_equals(self.ctrl.name, 'Foo Barness')
        assert_true(self.ctrl.dirty)

    def test_copy_empty(self):
        self._test_copy_empty()

    def test_copy_content(self):
        self._test_copy_content()

    def test_create_user_keyword(self):
        self._test_uk_creation()

    def test_creating_user_keyword_with_conflicting_name_does_nothing(self):
        self._test_creation_with_conflicting_name()

    def test_creation_with_arguments(self):
        self._test_creation_with_arguments()

    def test_extract_kw(self):
        obs = self._creation_oberver()
        self.ctrl.extract_keyword('New KW', '${argh}', (0,1), obs.observe)
        assert_equals(self.testcase.steps[0].keyword, 'New KW')
        assert_equals(len(self.testcase.steps), 2)

        assert_equals(obs.item.steps[0].keyword, 'Log')
        assert_equals(obs.item.steps[1].keyword, 'No Operation')
        assert_equals(obs.item.arguments.value, '${argh}')
        assert_true(self.ctrl.dirty)

    def test_extract_kw_from_the_middle(self):
{'fraction_non_alphanumeric': 0.11977517927514697, 'fraction_numerical': 0.002584146262678468, 'mean_word_length': 2.8358319910791723, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3207347', 'n_tokens_mistral': 9763, 'n_tokens_neox': 9488, 'n_words': 1581}