コード例 #1
0
def test_print_chains_backwards():
    from textworld.generator import print_chains

    allowed_rules = data.get_rules().get_matching("take/.*")
    allowed_rules += data.get_rules().get_matching("go.*")
    allowed_rules += data.get_rules().get_matching("insert.*", "put.*")
    allowed_rules += data.get_rules().get_matching("open.*", "close.*")
    allowed_rules += data.get_rules().get_matching("lock.*", "unlock.*")
    allowed_rules += data.get_rules().get_matching("eat.*")

    # No possible action since the wooden door is locked and
    # the player doesn't have the key.
    state = build_state(locked_door=True)
    tree = chaining.get_chains(
        state,
        max_depth=5,
        rules_per_depth={i: allowed_rules
                         for i in range(5)})
    chains = list(tree.traverse_preorder())
    assert len(chains) == 0

    # The door is now closed instead of locked.
    state = build_state(locked_door=False)
    tree = chaining.get_chains(
        state,
        max_depth=5,
        rules_per_depth={i: allowed_rules
                         for i in range(5)})
    chains = list(tree.traverse_preorder())

    # Only validates that printing chains does not raise exception.
    with testing.capture_stdout() as stdout:
        print_chains(chains, backward=True)
        stdout.seek(0)
        assert len(stdout.read()) > 0
コード例 #2
0
    def test_render(self):
        # Only validates that render does not raise exception.
        with testing.capture_stdout() as stdout:
            self.env.render()
            stdout.seek(0)
            assert len(stdout.read()) > 0

        assert self.env.render(mode="text").strip() == self.game_state.feedback.strip()

        # Try rendering to a file.
        f = self.env.render(mode="ansi")
        f.seek(0)
        assert f.read().strip() == self.game_state.feedback.strip()

        # Display command that was just entered.
        self.env.step("look")
        text1 = self.env.render(mode="text")
        self.env.display_command_during_render = True
        text2 = self.env.render(mode="text")
        assert "> look" not in text1
        assert "> look" in text2