from cronus.core import (Cronus, CronusActionsHandler, )
from cronus import parsers
from cronus import actions
from cronus.exceptions import CronusInvalidActionException
import unittest


class TestCronus(unittest.TestCase):
    """
    Testing Cronus class
    """

    def test_cronus_instance_bad_argument(self):
        cmd_line = "rememberme 'Buy milk' +30m"
        self.assertRaises(
            ValueError,
            Cronus,
            cmd_line
        )

    def test_cronus_invalid_action(self):
        cmd_line = ['foobar', 'other messages here']
        self.assertRaises(
            CronusInvalidActionException,
            Cronus,
            cmd_line
        )

    def test_cronus_valid_action(self):
        cmd_line = ['tellme', 'Buy python eggs', '+30m']
        cronus = Cronus(cmd_line)
        action = cronus.action
        self.assertEqual(action.name, 'tellme')
        self.assertEqual(action.parser, parsers.TellmeParser)


class TestCronusActionsHandler(unittest.TestCase):
    """
    Testing Cronus action helper assistant
    """
    def setUp(self):
        self.actions_handler = CronusActionsHandler()

    def test_none_actions(self):
        actions_handler = self.actions_handler
        self.assertEqual(actions_handler.actions_name_list, [])

    def test_register_action(self):
        actions_handler = self.actions_handler
        actions_handler.register_action(name='tellme',
                                        parser=parsers.TellmeParser,
                                        action=actions.NotifyInDesktop, )
        self.assertTrue(len(actions_handler.actions_name_list), 1)

    def test_get_action(self):
        actions_handler = self.actions_handler
        actions_handler.register_action(name='tellme',
                                        parser=parsers.TellmeParser,
                                        action=actions.NotifyInDesktop, )
        action = actions_handler.get_action('tellme')
        self.assertEqual(action.name, 'tellme')
        self.assertEqual(action.parser, parsers.TellmeParser)
        self.assertEqual(action.action, actions.NotifyInDesktop)

    def test_remove_action(self):
        actions_handler = self.actions_handler
        actions_handler.register_action(name='tellme',
                                        parser=parsers.TellmeParser,
                                        action=actions.NotifyInDesktop, )
        actions_handler.remove_action('tellme')
        self.assertEqual(actions_handler.actions_name_list, [])


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