# Path: nappingcat/patterns.py
# def patterns(path, *args):
#     return CommandPatterns(path, args) 
# 
# Path: nappingcat/exceptions.py
# class NappingCatUnhandled(NappingCatException):
#     pass
# 
# class NappingCatBadPatterns(NappingCatBadConfig):
#     pass


from unittest import TestCase
from nappingcat import patterns
from nappingcat.exceptions import NappingCatUnhandled, NappingCatBadPatterns
import random

class TestOfCommandPatterns(TestCase):
    def test_init_assigns_appropriately(self):
        random_map = random.randint(1,100)
        result = patterns.CommandPatterns('', random_map)
        self.assertEqual(result.path, '')
        self.assertEqual(result.map, random_map)

        results = patterns.CommandPatterns('tests.patterns_test', '')
        self.assertEqual(random.__class__, results.module.__class__)

        self.assertRaises(ImportError, patterns.CommandPatterns, 'dne', '')

    def test_match_raises_unhandled_if_no_match(self):
        test = patterns.CommandPatterns('', [])
        anything = 'random-%d' % random.randint(1,100)
        self.assertRaises(NappingCatUnhandled, test.match, anything)

    def test_delegates_to_included_patterns(self):
        random_result = random.randint(1,100)
        class TriggerCommandPatterns(patterns.CommandPatterns):
            def match(self, command):
                return random_result
        pat = patterns.CommandPatterns('', [('^hey', TriggerCommandPatterns('', []))])
        self.assertEqual(pat.match('hey'), random_result)

    def test_continues_to_next_match_if_delegate_raises_unhandled(self):
        random_result = random.randint(1,100)
        class TriggerCommandPatterns(patterns.CommandPatterns):
            def match(self, command):

# Path: nappingcat/patterns.py
# def patterns(path, *args):
#     return CommandPatterns(path, args) 
# 
# Path: nappingcat/exceptions.py
# class NappingCatUnhandled(NappingCatException):
#     pass
# 
# class NappingCatBadPatterns(NappingCatBadConfig):
#     pass


# Path: tests/patterns.py
from unittest import TestCase
from nappingcat import patterns
from nappingcat.exceptions import NappingCatUnhandled, NappingCatBadPatterns
import random


class TestOfCommandPatterns(TestCase):
    def test_init_assigns_appropriately(self):
        random_map = random.randint(1,100)
        result = patterns.CommandPatterns('', random_map)
        self.assertEqual(result.path, '')
        self.assertEqual(result.map, random_map)

        results = patterns.CommandPatterns('tests.patterns_test', '')
        self.assertEqual(random.__class__, results.module.__class__)

        self.assertRaises(ImportError, patterns.CommandPatterns, 'dne', '')

    def test_match_raises_unhandled_if_no_match(self):
        test = patterns.CommandPatterns('', [])
        anything = 'random-%d' % random.randint(1,100)
        self.assertRaises(NappingCatUnhandled, test.match, anything)

    def test_delegates_to_included_patterns(self):
        random_result = random.randint(1,100)
        class TriggerCommandPatterns(patterns.CommandPatterns):
            def match(self, command):
                return random_result
        pat = patterns.CommandPatterns('', [('^hey', TriggerCommandPatterns('', []))])
        self.assertEqual(pat.match('hey'), random_result)

    def test_continues_to_next_match_if_delegate_raises_unhandled(self):
        random_result = random.randint(1,100)
        class TriggerCommandPatterns(patterns.CommandPatterns):
            def match(self, command):

                raise NappingCatUnhandled("OH no!")