Esempio n. 1
0
class TestCase(unittest.TestCase):
    def setUp(self):
        super(TestCase, self).setUp()
        self.forge = Forge()
    def tearDown(self):
        self.forge.verify()
        self.forge.restore_all_replacements()
        super(TestCase, self).tearDown()
Esempio n. 2
0
    def __init__(self, game_map, task=TASKS.MINE):
        self.user_interface = UserInterface()
        self.backpack = Backpack()
        self.mine = Mine(self, game_map)
        self.smelt = Smelt(self, game_map)
        self.forge = Forge(self, game_map)

        # Set the default initial task
        self.task = task
        self.action_count = 0
Esempio n. 3
0
 def test__debug_states(self):
     f = Forge()
     self.assertDebugOff(f)
     f.debug.enable()
     self.assertDebugOn(f)
     f.debug.disable()
     self.assertDebugOff(f)
Esempio n. 4
0
def forge(request):

    returned = Forge()

    @request.addfinalizer
    def cleanup():
        returned.verify()
        returned.restore_all_replacements()

    return returned
Esempio n. 5
0
def forge(request):

    returned = Forge()

    @request.addfinalizer
    def cleanup():  # pylint: disable=unused-variable
        returned.verify()
        returned.restore_all_replacements()

    return returned
Esempio n. 6
0
 def test__debug_disable_enable(self):
     prev_value = os.environ.get('FORGE_DEBUG', None)
     os.environ['FORGE_DEBUG'] = "1"
     try:
         f = Forge()
         self.assertDebugOn(f)
     finally:
         if prev_value is None:
             os.environ.pop('FORGE_DEBUG')
         else:
             os.environ['FORGE_DEBUG'] = prev_value
Esempio n. 7
0
 def test__command_propagate_non_enoent(self):
     self.forge = Forge()
     self.forge.replace(command.subprocess, "Popen")
     self.addCleanup(self.forge.restore_all_replacements)
     expected_code = 23232
     command.subprocess.Popen(
         Anything(), cwd=Anything(), shell=True, close_fds=True,
         stdout=Anything(), stderr=command.subprocess.STDOUT, stdin=Anything()
         ).and_raise(OSError(expected_code, "message"))
     with self.forge.verified_replay_context():
         with self.assertRaises(OSError) as caught:
             command.execute_assert_success("bla", shell=True)
     self.assertIsInstance(caught.exception, OSError)
     self.assertEquals(caught.exception.errno, expected_code)
Esempio n. 8
0
class ForgeTest(TestCase):
    def setUp(self):
        super(ForgeTest, self).setUp()
        self.forge = Forge()
    def tearDown(self):
        self.forge.verify()
        self.forge.restore_all_replacements()
        self.forge.reset()
        super(ForgeTest, self).tearDown()
Esempio n. 9
0
class CommandTest(unittest.TestCase):
    def test__command_success(self):
        result, output = command.execute_assert_success("echo hello", shell=True)
        self.assertEquals(result, 0)
        self.assertEquals(output.rstrip().decode('utf-8'), "hello")
    def test__command_no_shell(self):
        with self.assertRaises(command.CommandFailed):
            command.execute_assert_success("echo hello", shell=False)
    def test__command_propagate_non_enoent(self):
        self.forge = Forge()
        self.forge.replace(command.subprocess, "Popen")
        self.addCleanup(self.forge.restore_all_replacements)
        expected_code = 23232
        command.subprocess.Popen(
            Anything(), cwd=Anything(), shell=True, close_fds=True,
            stdout=Anything(), stderr=command.subprocess.STDOUT, stdin=Anything()
            ).and_raise(OSError(expected_code, "message"))
        with self.forge.verified_replay_context():
            with self.assertRaises(OSError) as caught:
                command.execute_assert_success("bla", shell=True)
        self.assertIsInstance(caught.exception, OSError)
        self.assertEquals(caught.exception.errno, expected_code)
    def test__command_cwd(self):
        result, output = command.execute_assert_success("pwd", shell=True, cwd="/")
        self.assertEquals(output.rstrip(), "/".encode('utf-8'))
        self.assertEquals(result, 0)
    def test__command_failure(self):
        cmd = "echo bla > /dev/stderr; false"
        with self.assertRaises(command.CommandFailed) as caught:
            command.execute_assert_success(cmd, shell=True)
        self.assertEquals(caught.exception.output.rstrip(), "bla".encode('utf-8'))
        self.assertEquals(caught.exception.returncode, 1)
        exception_repr = repr(caught.exception)
        self.assertIn("returncode: 1", exception_repr)
        self.assertIn(repr(cmd), exception_repr)
        self.assertIn("output: 'bla", exception_repr)
Esempio n. 10
0
class ForgeTestCase(TestCase):
    def setUp(self):
        super(ForgeTestCase, self).setUp()
        self.forge = Forge()
    def tearDown(self):
        self.assertNoMoreCalls()
        self.forge.verify()
        self.forge.restore_all_replacements()
        self.forge.reset()
        super(ForgeTestCase, self).tearDown()
    def assertNoMoreCalls(self):
        expected = self.forge.queue.get_expected()
        self.assertEquals(len(expected), 0, "len=%d != 0, expected_events=%s, queue=%s" % (len(expected),
            repr(expected), repr(self.forge.queue)))
Esempio n. 11
0
class ForgeTestCase(TestCase):
    def setUp(self):
        super(ForgeTestCase, self).setUp()
        self.forge = Forge()

    def tearDown(self):
        self.assertNoMoreCalls()
        self.forge.verify()
        self.forge.restore_all_replacements()
        self.forge.reset()
        super(ForgeTestCase, self).tearDown()

    def assertNoMoreCalls(self):
        expected = self.forge.queue.get_expected()
        self.assertEquals(
            len(expected), 0, "len=%d != 0, expected_events=%s, queue=%s" %
            (len(expected), repr(expected), repr(self.forge.queue)))
Esempio n. 12
0
class ForgeTest(TestCase):
    def setUp(self):
        super(ForgeTest, self).setUp()
        self.forge = Forge()
    def assertRecording(self):
        self.assertFalse(self.forge.is_replaying())
        self.assertTrue(self.forge.is_recording())
    def assertReplaying(self):
        self.assertTrue(self.forge.is_replaying())
        self.assertFalse(self.forge.is_recording())
    def test__replaying(self):
        self.assertRecording()
        self.forge.replay()
        self.assertReplaying()
        self.forge.verify()
        self.assertReplaying()
    def test__reset(self):
        self.test__replaying()
        self.forge.queue._queue = [1, 2, 3, 4]
        self.forge.reset()
        self.assertRecording()
        self.assertEquals(len(self.forge.queue), 0)
    def test__verified_replay_context(self):
        self.assertRecording()
        check_verify = Checkpoint()
        check_reset = Checkpoint()
        self.forge.verify = check_verify.trigger
        def _fake_reset():
            self.assertTrue(check_verify.called)
            check_reset.trigger()
        self.forge.reset = _fake_reset
        with self.forge.verified_replay_context():
            self.assertReplaying()
            self.assertFalse(check_verify.called)
            self.assertFalse(check_reset.called)
        self.assertTrue(check_reset.called)
        self.assertTrue(check_verify.called)
Esempio n. 13
0
 def should_not_allow_duplicate_factory_names(self):
     with self.assertRaises(DuplicateFactoryError):
         Forge.define('user', color='red')
Esempio n. 14
0
 def should_build_plain_factories(self):
     user = Forge.build('user')
     assert user.name == 'Matte'
Esempio n. 15
0
 def should_allow_custom_attributes_when_building(self):
     user = Forge.build('user', name='Fred')
     assert user.name == 'Fred'
Esempio n. 16
0
class Player():
    """
        This class handles everything relating to the player
        """
    LOW_HEALTH = 150
    TASKS = Enum('Task', 'MINE, SMELT, FORGE')

    def __init__(self, game_map, task=TASKS.MINE):
        self.user_interface = UserInterface()
        self.backpack = Backpack()
        self.mine = Mine(self, game_map)
        self.smelt = Smelt(self, game_map)
        self.forge = Forge(self, game_map)

        # Set the default initial task
        self.task = task
        self.action_count = 0

    def perform_task(self):
        """
            Checks health, organizes backpack and then performs
            one of the following tasks: mine, smelt, or forge
            """
        # Do pre-task checks
        self.action_count += 1

        # Only check health every 25 turns (it is a slow process)
        if self.action_count % 25 == 0:
            self.action_count = 0
            self.check_health()

        # Perform task
        if self.task == self.TASKS.MINE:
            self.task = self.mine.mine()
            delay = 0
        elif self.task == self.TASKS.SMELT:
            self.task = self.smelt.smelt()
            delay = 1.4
        elif self.task == self.TASKS.FORGE:
            self.task = self.forge.forge()
            delay = 2.1

        # Give the task time to complete
        sleep(delay)

        # Organize backpack now that items have been potentially added
        self.organize_backpack()

    def check_health(self):
        """
            Checks player's HP and uses a potion if it is low.
            If no potions are found, game will quit
            """
        # Check if HP is low
        if self.user_interface.get_health() < self.LOW_HEALTH:
            # Attempt to use a potion
            utils.log("INFO", F"Health dropped below {self.LOW_HEALTH}")
            used_potion = self.backpack.use_item('potion', offset=(4, 9))

            # No potions were found
            if not used_potion:
                # Potions may be obscurred by items, try selling
                self.forge.sell_items()
                # Try using a potion again
                used_potion = self.backpack.use_item('potion', offset=(4, 9))

                # Still can't use potions, likely do not have any
                if not used_potion:
                    utils.log("SEVERE", "No potions found")
                    utils.quit_game()

            # Sleep so that there is no issue using the next item
            utils.log("INFO", F"Used a potion")
            sleep(6)

    def is_weight_below_threshold(self, threshold):
        # Calculate how much more weight the player can carry
        current_weight, max_weight = self.user_interface.get_weight()
        difference = max_weight - current_weight

        # Check if the weight the player can carry is below the threshold
        if difference < threshold:
            utils.log("INFO", F"Weight is {difference} from max, threshold was set to {threshold}")
            return True

        return False

    def organize_backpack(self):
        """
            Move all items to the correct areas of the backpack
            """
        self.backpack.move_item('ore', self.backpack.ORE_LOC, (8, 6))
        self.backpack.move_item('gem', self.backpack.GEM_LOC, (4, 2))
        self.backpack.move_item('jewel', self.backpack.GEM_LOC, (4, 2))
        self.backpack.move_item('galantine', self.backpack.GEM_LOC, (5, 4))
        self.backpack.move_item('pickaxe', self.backpack.PICKAXE_LOC, (9, 4))
        self.backpack.move_item('dagger', self.backpack.DAGGER_LOC, (4, 6))
        self.backpack.move_item('hammer', self.backpack.HAMMER_LOC, (7, 3))
        self.backpack.move_item('gold', self.backpack.GEM_LOC, (6, 5))
        self.backpack.move_item('ingot', self.backpack.INGOT_LOC)
Esempio n. 17
0
 def should_support_with_context_format(self):
     with Forge.define('pet') as p:
         p.breed = 'Husky'
     assert Forge._registry['pet'] == {'breed': 'Husky'}
Esempio n. 18
0
 def should_raise_error_without_models_configured(self):
     Forge.configure(models=None)
     with self.assertRaises(ForgeModelError):
         Forge.build('user')
Esempio n. 19
0
 def should_support_shorthand_building(self):
     user = Forge('user')
     assert user.name == 'Matte'
Esempio n. 20
0
 def setUp(self):
     super(ForgeTestCase, self).setUp()
     self.forge = Forge()
Esempio n. 21
0
class ForgeTest(TestCase):
    def setUp(self):
        super(ForgeTest, self).setUp()
        self.forge = Forge()

    def assertRecording(self):
        self.assertFalse(self.forge.is_replaying())
        self.assertTrue(self.forge.is_recording())

    def assertReplaying(self):
        self.assertTrue(self.forge.is_replaying())
        self.assertFalse(self.forge.is_recording())

    def test__replaying(self):
        self.assertRecording()
        self.forge.replay()
        self.assertReplaying()
        self.forge.verify()
        self.assertReplaying()

    def test__reset(self):
        self.test__replaying()
        self.forge.queue._queue = [1, 2, 3, 4]
        self.forge.reset()
        self.assertRecording()
        self.assertTrue(self.forge.queue.is_empty())

    def test__verified_replay_context(self):
        self.assertRecording()
        check_verify = Checkpoint()
        check_reset = Checkpoint()
        self.forge.verify = check_verify.trigger

        def _fake_reset():
            self.assertTrue(check_verify.called)
            check_reset.trigger()

        self.forge.reset = _fake_reset
        with self.forge.verified_replay_context():
            self.assertReplaying()
            self.assertFalse(check_verify.called)
            self.assertFalse(check_reset.called)
        self.assertTrue(check_reset.called)
        self.assertTrue(check_verify.called)
        # we don't assertRecording, since we stubbed out reset
    def test__verified_replay_context_checks_no_more_calls(self):
        expected_call = self.forge.create_wildcard_function_stub()(1, 2, 3)
        with self.assertRaises(ExpectedEventsNotFound):
            with self.forge.verified_replay_context():
                pass
Esempio n. 22
0
 def should_support_shorhand_building_with_attributes(self):
     user = Forge('user', name='Wayne')
     assert user.name == 'Wayne'
Esempio n. 23
0
 def setUp(self):
     super(TestCase, self).setUp()
     self.forge = Forge()
Esempio n. 24
0
 def should_create_associations(self):
     Forge.define('car', owner=Forge.build('user'))
     assert Forge.build('car').owner.name == 'Matte'
Esempio n. 25
0
 def setUp(self):
     Forge.configure(models='forge.test.support.models')
     
     if 'user' not in Forge._registry:
         Forge.define('user', name='Matte', age=25)
Esempio n. 26
0
 def setUp(self):
     super(ForgeTest, self).setUp()
     self.forge = Forge()
Esempio n. 27
0
class ForgeTest(TestCase):
    def setUp(self):
        super(ForgeTest, self).setUp()
        self.forge = Forge()
    def assertRecording(self):
        self.assertFalse(self.forge.is_replaying())
        self.assertTrue(self.forge.is_recording())
    def assertReplaying(self):
        self.assertTrue(self.forge.is_replaying())
        self.assertFalse(self.forge.is_recording())
    def test__replaying(self):
        self.assertRecording()
        self.forge.replay()
        self.assertReplaying()
        self.forge.verify()
        self.assertReplaying()
    def test__reset(self):
        self.test__replaying()
        self.forge.queue._queue = [1, 2, 3, 4]
        self.forge.reset()
        self.assertRecording()
        self.assertTrue(self.forge.queue.is_empty())
    def test__verified_replay_context(self):
        self.assertRecording()
        check_verify = Checkpoint()
        check_reset = Checkpoint()
        self.forge.verify = check_verify.trigger
        def _fake_reset():
            self.assertTrue(check_verify.called)
            check_reset.trigger()
        self.forge.reset = _fake_reset
        with self.forge.verified_replay_context():
            self.assertReplaying()
            self.assertFalse(check_verify.called)
            self.assertFalse(check_reset.called)
        self.assertTrue(check_reset.called)
        self.assertTrue(check_verify.called)
        # we don't assertRecording, since we stubbed out reset
    def test__verified_replay_context_checks_no_more_calls(self):
        expected_call = self.forge.create_wildcard_function_stub()(1, 2, 3)
        with self.assertRaises(ExpectedEventsNotFound):
            with self.forge.verified_replay_context():
                pass
Esempio n. 28
0
 def should_give_a_dict_of_attributes_for_a_factory(self):
     user = Forge.attributes_for('user')
     assert user == dict(name="Matte", age=25)
Esempio n. 29
0
from forge import Forge

with Forge.define('color') as f:
    f.name = 'Red'
Esempio n. 30
0
 def should_return_new_objects_via_build(self):
     admin = Forge.build('user')
     user  = Forge.build('user')
     admin.name = 'Pete'
     assert user.name != 'Pete'