import unittest
import os

from robot.running import userkeyword
from robot.errors import DataError
from robot.parsing.model import UserKeyword
from robot.utils.asserts import (assert_equals, assert_none,
                                 assert_raises_with_msg, assert_true)


class UserHandlerStub:

    def __init__(self, kwdata, library):
        self.name = kwdata.name
        if kwdata.name == 'FAIL':
            raise Exception('Expected failure')


class EmbeddedArgsTemplateStub:

    def __init__(self, kwdata, library, embedded):
        self.name = kwdata.name
        if kwdata.name != 'Embedded ${arg}':
            raise TypeError

    def matches(self, name):
        return True


class TestUserLibrary(unittest.TestCase):

    def setUp(self):
        self._orig_userhandler = userkeyword.UserKeywordHandler
        self._orig_embeddedargstemplate = userkeyword.EmbeddedArgsTemplate
        userkeyword.UserKeywordHandler = UserHandlerStub
        userkeyword.EmbeddedArgsTemplate = EmbeddedArgsTemplateStub

    def tearDown(self):
        userkeyword.UserKeywordHandler = self._orig_userhandler
        userkeyword.EmbeddedArgsTemplate = self._orig_embeddedargstemplate

    def test_name_from_resource(self):
        for source, exp in [('resources.html', 'resources'),
                            (os.path.join('..','res','My Res.HTM'), 'My Res'),
                            (os.path.abspath('my_res.xhtml'), 'my_res')]:
            lib = userkeyword.UserLibrary([], source)
            assert_equals(lib.name, exp)

    def test_name_from_test_case_file(self):
        assert_none(self._get_userlibrary('NOT_RESOURCE').name)

    def test_creating_keyword(self):
        lib = self._get_userlibrary('source', 'kw 1', 'kw 2')
        assert_equals(len(lib.handlers), 2)
        assert_true('kw 1' in lib.handlers)
        assert_true('kw 2' in lib.handlers)

    def test_creating_keyword_when_kw_name_has_embedded_arg(self):
        lib = self._get_userlibrary('source', 'Embedded ${arg}')
        self._lib_has_embedded_arg_keyword(lib)

    def test_creating_keywords_when_normal_and_embedded_arg_kws(self):
        lib = self._get_userlibrary('source', 'kw1', 'Embedded ${arg}', 'kw2')
        assert_equals(len(lib.handlers), 3)
        assert_true('kw1' in lib.handlers)
        assert_true('kw 2' in lib.handlers)
        self._lib_has_embedded_arg_keyword(lib)

    def test_creating_duplicate_embedded_arg_keyword_in_resource_file(self):
        lib = self._get_userlibrary('source', 'Embedded ${arg}',
                                    'kw', 'Embedded ${arg}')
        assert_equals(len(lib.handlers), 2)
        assert_true('kw' in lib.handlers)

    def test_creating_duplicate_keyword_in_resource_file(self):
        lib = self._get_userlibrary('source', 'kw', 'kw', 'kw 2')
        assert_equals(len(lib.handlers), 2)
        assert_true('kw' in lib.handlers)
        assert_true('kw 2' in lib.handlers)
        assert_equals(lib.handlers['kw'].error,
                      "Keyword 'kw' defined multiple times.")

    def test_creating_duplicate_keyword_in_test_case_file(self):
        lib = self._get_userlibrary('NOT_RESOURCE', 'MYKW', 'my kw')
        assert_equals(len(lib.handlers), 1)
        assert_true('mykw' in lib.handlers)
        assert_equals(lib.handlers['mykw'].error,
                      "Keyword 'my kw' defined multiple times.")

    def test_handlers_contains(self):
        lib = self._get_userlibrary('source', 'kw')
        assert_true('kw' in lib.handlers)
        assert_true('nonex' not in lib.handlers)

    def test_handlers_getitem_with_non_existing_keyword(self):
        lib = self._get_userlibrary('source', 'kw')
        assert_raises_with_msg(
            DataError,
            "Test case file contains no keywords matching name 'non existing'.",
            lib.handlers.__getitem__, 'non existing')

    def test_handlers_getitem_with_existing_keyword(self):
        lib = self._get_userlibrary('source', 'kw')
        handler = lib.handlers['kw']
        assert_true(isinstance(handler, UserHandlerStub))

    def _get_userlibrary(self, source, *keyword_names):
        return userkeyword.UserLibrary([UserKeyword(None, name) for name in keyword_names])

    def _lib_has_embedded_arg_keyword(self, lib):
        assert_true('Embedded ${arg}' in lib.handlers)
        embedded = lib.handlers._embedded
        assert_equals(len(embedded), 1)
        assert_equals(embedded[0].name, 'Embedded ${arg}')


if __name__ == '__main__':
    unittest.main()

@@ -1625,30 +1625,16 @@
 ary(
-'NOT_RESOURCE'
 ).name)%0A
 %0A   
@@ -1629,16 +1629,16 @@
 ).name)%0A
+
 %0A    def
@@ -1703,26 +1703,16 @@
 library(
-'source', 
 'kw 1', 
@@ -1951,34 +1951,24 @@
 userlibrary(
-'source', 
 'Embedded $%7B
@@ -2130,26 +2130,16 @@
 library(
-'source', 
 'kw1', '
@@ -2456,26 +2456,16 @@
 library(
-'source', 
 'Embedde
@@ -2719,34 +2719,24 @@
 userlibrary(
-'source', 
 'kw', 'kw', 
@@ -3089,24 +3089,8 @@
 ary(
-'NOT_RESOURCE', 
 'MYK
@@ -3376,34 +3376,24 @@
 userlibrary(
-'source', 
 'kw')%0A      
@@ -3573,34 +3573,24 @@
 userlibrary(
-'source', 
 'kw')%0A      
@@ -3873,18 +3873,8 @@
 ary(
-'source', 
 'kw'
@@ -4001,24 +4001,16 @@
 ry(self,
- source,
  *keywor
@@ -4082,16 +4082,56 @@
 e, name)
+%0A                                       
  for nam

remove unused argument from utests