from contextshell.ActionFinder import ActionFinder
from contextshell.NodePath import NodePath

import unittest
from unittest.mock import MagicMock, call


class FindActionTests(unittest.TestCase):
    def test_action_lookup_order(self):
        tree = MagicMock()
        tree.exists.return_value = False
        finder = ActionFinder(tree)
        target = NodePath('.foo.bar.spam')
        action = NodePath('nonexistent')

        finder.find_action(target, action)

        expected_exists_calls = [
            call(finder.make_action_path(NodePath('.foo.bar.spam'), action)),
            call(finder.make_action_path(NodePath('.foo.bar'), action)),
            call(finder.make_action_path(NodePath('.foo'), action)),
            call(finder.make_action_path(NodePath('.'), action))
        ]
        self.assertEqual(tree.exists.mock_calls, expected_exists_calls)

    def test_action_doesnt_exists(self):
        tree = MagicMock()
        tree.exists.return_value = False
        finder = ActionFinder(tree)

        found_action = finder.find_action(NodePath('.'), NodePath('nonexistent'))

        self.assertIsNone(found_action)

    def test_action_exists(self):
        tree = MagicMock()
        tree.exists.return_value = True
        tree.get.return_value = 'action_implementation'
        finder = ActionFinder(tree)

        found_action = finder.find_action(NodePath('.'), NodePath('action'))

        self.assertIs(found_action, tree.get.return_value)

@@ -148,16 +148,347 @@
  call%0A%0A%0A
+class MakeActionPathTests(unittest.TestCase):%0A    def test_action_path_include_attribute_directory(self):%0A        finder = ActionFinder(MagicMock())%0A        action = NodePath('spam')%0A%0A        action_path = finder.make_action_path(NodePath('foo.bar'), action)%0A%0A        self.assertIn(ActionFinder.actions_branch_name, action_path)%0A%0A%0A
 class Fi
@@ -553,32 +553,91 @@
 up_order(self):%0A
+        # FIXME: this bottom-top lookup doesn't make sense%0A
         tree = M
@@ -728,51 +728,8 @@
 ee)%0A
-        target = NodePath('.foo.bar.spam')%0A
     
@@ -797,14 +797,28 @@
 ion(
-target
+NodePath('.foo.bar')
 , ac
@@ -862,86 +862,8 @@
 = %5B%0A
-            call(finder.make_action_path(NodePath('.foo.bar.spam'), action)),%0A
     

Refactor ActionFinderTests