"""General tests of the ``hooks`` module.

Each specific hook has it's own test(s).
"""

from nose.tools import assert_raises
from feedplatform import hooks


def test_invalid():
    # invalid identifers result in exceptions
    assert_raises(Exception, hooks.add_callback, 'worldpeace', lambda: None)
    assert_raises(Exception, hooks.trigger, 'worldpeace')

    # can't register the same function twice
    def foo(): pass
    hooks.add_callback('alien_invasion', foo)
    assert_raises(Exception, hooks.add_callback, 'worldpeace', foo)

def test_valid():
    # valid identifers work
    hooks.reset()
    hooks.add_callback('alien_invasion', lambda x: x)
    assert hooks.trigger('alien_invasion', [5]) == 5

def test_multiple():
    # test handling of multiple callbacks for a hook

    hooks.reset()
    # making this an attribute of a global avoids all kinds of scoping issues
    test_multiple.counter = 0
    def mkinc():   # can't use same callback twice
        def inc():
            test_multiple.counter += 1;
            return 42
        return inc

    hooks.add_callback('alien_invasion', mkinc())
    hooks.add_callback('alien_invasion', mkinc())
    hooks.add_callback('alien_invasion', mkinc())

    # by default, the first successfull callback returns
    test_multiple.counter = 0
    assert hooks.trigger('alien_invasion') == 42
    assert test_multiple.counter == 1

    # we can forcefully go through all callbacks (and get None back)
    test_multiple.counter = 0
    assert hooks.trigger('alien_invasion', all=True) == None
    assert test_multiple.counter == 3

def test_priority():
    # fifo: without a priority, the callback added first is called first
    hooks.reset()
    hooks.add_callback('alien_invasion', lambda: 1)
    hooks.add_callback('alien_invasion', lambda: 2)
    hooks.add_callback('alien_invasion', lambda: 3)
    assert hooks.trigger('alien_invasion') == 1

    # but callback priorization works as well
    hooks.reset()
    hooks.add_callback('alien_invasion', lambda: 'p10', priority=10)
    hooks.add_callback('alien_invasion', lambda: 'p20', priority=20)
    hooks.add_callback('alien_invasion', lambda: 'p5', priority=5)
    assert hooks.trigger('alien_invasion') == 'p20'

def test_reset():
    # reset() was already used throughout previous tests,
    # but for good measure, do it specifically.
    hooks.add_callback('alien_invasion', lambda: 42)
    hooks.reset()
    assert hooks.trigger('alien_invasion') == None
@@ -170,23 +170,24 @@
 ef test_
-in
 valid
+ity
 ():%0D%0A   
@@ -244,33 +244,32 @@
 sert_raises(
-Exception
+KeyError
 , hooks.add_
@@ -321,33 +321,32 @@
 sert_raises(
-Exception
+KeyError
 , hooks.trig
@@ -503,17 +503,18 @@
 ses(
-Exception
+ValueError
 , ho
@@ -536,46 +536,31 @@
 k, '
-worldpeace', foo)%0D%0A%0D%0Adef test_valid():
+alien_invasion', foo)%0D%0A
 %0D%0A  
@@ -712,24 +712,26 @@
 5%5D) == 5%0D%0A%0D%0A
+%0D%0A
 def test_mul
@@ -748,11 +748,12 @@
     
-# t
+%22%22%22T
 est 
@@ -793,16 +793,26 @@
 r a hook
+.%0D%0A    %22%22%22
 %0D%0A%0D%0A    

test of hook framework was using exception tests that were not strict enough, which caused a bug in the test itself to go unnoticed