예제 #1
0
class TestPennyCanInspectAStory:
    @pytest.fixture()
    def load_library(self):
        self.library_path = join(getcwd(), "tests", "fixtures", "library")
        self.pennys_story = "penny-pinnacle-pennyslvania"
        self.library = Knot(self.library_path)
        self.stories = self.library.stories()

    def library_path(self):
        return join(getcwd(), "tests", "fixtures", "library")

    def test_penny_can_load_story_info(self):
        # Penny loads up the library
        # Penny lists the stories available(self):
        assert self.pennys_story in self.stories

        # She loads the story
        story = self.library.get_story(self.pennys_story)

        # She checks the author
        assert "Penny Pennington" == story.author

        # She checks the title
        assert "Pen's Pennyslvanian Pinnacle" == story.title

        # She checks the filename for the first scene
        assert "Runaway Train Showdown" == story.scene

        # She checks the story synopsis
        assert "Lorem ipsum" in story.synopsis

        # Satisfied, she goes on her way

    def check_location(self, x, y):
        assert x == self.player_state["location"]["x"]
        assert y == self.player_state["location"]["y"]

    def test_penny_can_view_enter_and_move_around_a_map_stored_in_the_scene(
            self):
        # Penny returns within minutes, she has not been able to stop thinking about the
        # possibilities presented by the first scene the Runaway Train Showdown.
        # Starts the story
        self.player_state = self.library.init_story(self.pennys_story)
        self.player_state = self.library.play(self.player_state)

        # Penny receives a description of the scene.  She is on a railway platform in
        # Pennyslvania awaiting the arrival of her friend who will be on a train
        setting = self.library.narrate(self.player_state)
        assert "awaiting the arrival of your friend" in setting
        assert "train" in setting

        # Penny asks for and recieves a map from the story.
        scene_map = self.library.scene_map(self.player_state)
        rows = ["###############", "###############", "      #@       "]

        expected = "\n".join(rows)

        self.check_location(7, 0)
        assert expected == scene_map

        # Penny moves north once and asks for another map
        self.player_state = self.library.move(self.player_state, 'n')

        scene_map = self.library.scene_map(self.player_state)
        rows = ["###############", "#######@#######", "      ##       "]

        expected = "\n".join(rows)

        self.check_location(7, 1)
        assert expected == scene_map

        # Penny moves east seven times and asks for another map
        self.player_state = self.library.move(self.player_state, 'e', 7)

        scene_map = self.library.scene_map(self.player_state)
        rows = ["###############", "##############@", "      ##       "]

        expected = "\n".join(rows)

        self.check_location(14, 1)
        assert expected == scene_map

        # Penny tries to move east again but cannot and asks for another map
        self.player_state = self.library.move(self.player_state, 'e')

        scene_map = self.library.scene_map(self.player_state)
        rows = ["###############", "##############@", "      ##       "]

        expected = "\n".join(rows)

        self.check_location(14, 1)
        assert expected == scene_map

        # Penny moves north once and asks for another description of the scene
        self.player_state = self.library.move(self.player_state, 'n')

        scene_map = self.library.scene_map(self.player_state)
        rows = ["##############@", "###############", "      ##       "]

        expected = "\n".join(rows)

        self.check_location(14, 2)
        assert expected == scene_map
        setting = self.library.narrate(self.player_state)

        # Penny reads the scene setting and sees that there is a train approaching from western horizon
        assert "western horizon" in setting

        # Penny runs to the western end of the platform and asks for another description
        self.player_state = self.library.move(self.player_state, 'w', 14)

        scene_map = self.library.scene_map(self.player_state)
        rows = ["@##############", "###############", "      ##       "]

        expected = "\n".join(rows)

        self.check_location(0, 2)
        assert expected == scene_map
        setting = self.library.narrate(self.player_state)

        # Penny reads in the description that the train with a full head of steam wizzes past her
        # going much faster than it should be
        assert "full head of steam" in setting
        assert "much faster than it should be" in setting

    def test_penny_finds_items_in_the_scene(self):
        # Penny again returns within minutes, she has not been able to stop
        # thinking about the interactive experience that she has had with the
        # story.  This time she has decided to stop and look around and examine
        # the platform more closely.

        # Starts the story
        self.player_state = self.library.init_story(self.pennys_story)
        self.player_state = self.library.play(self.player_state)

        # Penny starts the story and uses the 'look' action to view what is
        # around her.
        self.player_state, seen = self.library.look(self.player_state)

        # She sees a newspaper on the ground.  It is dated today and it
        # announces that the Governor will be stopping in town today to
        # announce her new controversial policy
        assert "newspaper" in seen

        self.player_state, description = self.library.describe(
            self.player_state, "n")
        assert "Governor" in description
        assert "policy" in description

        # She now sees the newspaper on the map
        # Looking at the map she also sees something else
        scene_map = self.library.scene_map(self.player_state)
        rows = ["###############", "##########b####", "      n@       "]

        expected = "\n".join(rows)
        assert expected == scene_map

        # She asks for more detail about the item and it informs her that
        # the location contains a small box, labeled "penny"
        self.player_state, description = self.library.describe(
            self.player_state, "b")

        assert "small box" in description
        assert "penny" in description
예제 #2
0
class TestKnot:
    @pytest.fixture()
    def prepare_knot(self):
        self.path = join(getcwd(), "tests", "fixtures", "library")
        self.library = Knot(self.path)

    def set_location(self, player_state, x, y):
        player_state["location"]["x"] = x
        player_state["location"]["y"] = y
        return player_state

    def test_knot_will_accept_story_path(self):
        assert self.library.path == self.path

    def test_knot_will_list_available_stories(self):
        stories = self.library.stories()
        assert "basic" in stories
        assert "test" in stories

    def test_knot_get_story_returns_story(self):
        story = self.library.get_story("basic")
        assert isinstance(story, Story)

    def test_build_story_path_includes_story_name(self):
        path = self.library.build_story_path("wonky")
        assert "wonky" in path

    def test_init_story_returns_a_dict(self):
        player_state = self.library.init_story("basic")
        assert type(player_state) is dict

    def test_play_accepts_and_returns_a_dict(self):
        player_state = self.library.init_story("basic")
        player_state = self.library.play(player_state)
        assert type(player_state) is dict

    def test_narrate_returns_a_description_of_the_scene(self):
        player_state = self.library.init_story("basic")
        description = self.library.narrate(player_state)
        assert "This is a basic description of a basic scene" == description

    def test_narrate_returns_a_positional_description_of_the_scene(self):
        player_state = self.library.init_story("basic")
        player_state = self.set_location(player_state, 3, 2)
        description = self.library.narrate(player_state)
        assert "You are at the upper right." == description

    def test_player_state_contains_the_current_scene(self):
        player_state = self.library.init_story("basic")
        assert "flourescent" == player_state["current_scene"]

    def test_player_state_contains_location(self):
        player_state = self.library.init_story("basic")
        assert "location" in player_state.keys()
        assert "x" in player_state["location"].keys()
        assert "y" in player_state["location"].keys()

    def test_player_state_contains_the_current_story(self):
        player_state = self.library.init_story("basic")
        assert "basic" == player_state["story"]

    def test_player_state_contains_the_number_of_turns_played(self):
        player_state = self.library.init_story("basic")
        assert 0 == player_state["turn"]
        player_state = self.library.move(player_state, "e")
        assert 1 == player_state["turn"]
        player_state = self.library.move(player_state, "e")
        assert 2 == player_state["turn"]

    def test_load_scene_returns_scene(self):
        player_state = self.library.init_story("basic")
        assert isinstance(self.library.load_scene(player_state), Scene)

    def test_can_return_scene_map(self):
        player_state = self.library.init_story("basic")
        player_state = self.library.play(player_state)
        scene_map = self.library.scene_map(player_state)
        assert "####" in scene_map

    def test_play_will_update_the_starting_location(self):
        player_state = self.library.init_story("basic")
        old_x = player_state["location"]["x"]
        old_y = player_state["location"]["y"]

        player_state = self.library.play(player_state)
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x != old_x
        assert new_y != old_y

    def test_move_will_move_a_player_north(self):
        player_state = self.library.init_story("basic")
        old_x = player_state["location"]["x"]
        old_y = player_state["location"]["y"]

        player_state = self.library.move(player_state, "n")
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == old_x
        assert new_y == old_y + 1

    def test_move_will_move_a_player_south(self):
        player_state = self.library.init_story("basic")
        player_state["location"]["x"] = 3
        player_state["location"]["y"] = 2

        player_state = self.library.move(player_state, "s")
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == 3
        assert new_y == 1

    def test_move_will_move_a_player_east(self):
        player_state = self.library.init_story("basic")
        old_x = player_state["location"]["x"]
        old_y = player_state["location"]["y"]

        player_state = self.library.move(player_state, "e")
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == old_x + 1
        assert new_y == old_y

    def test_move_will_move_a_player_west(self):
        player_state = self.library.init_story("basic")
        player_state["location"]["x"] = 3
        player_state["location"]["y"] = 0

        player_state = self.library.move(player_state, "w")
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == 2
        assert new_y == 0

    def test_move_accepts_an_optional_repetiton(self):
        player_state = self.library.init_story("basic")
        old_x = player_state["location"]["x"]
        old_y = player_state["location"]["y"]

        player_state = self.library.move(player_state, "e", 2)
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == old_x + 2
        assert new_y == old_y

    def test_player_can_look_to_see_items_on_map(self):
        state = self.library.init_story("basic")
        state = self.library.play(state)
        scene_map = self.library.scene_map(state)
        assert "#@d#" not in scene_map
        assert 1 not in state["seen"]

        state, seen = self.library.look(state)
        assert "dog" in seen
        assert 1 in state["seen"]

        scene_map = self.library.scene_map(state)
        assert "#@d#" in scene_map
예제 #3
0
class TestKnot:
    @pytest.fixture()
    def prepare_knot(self):
        self.path = join(getcwd(), "tests", "fixtures", "library")
        self.library = Knot(self.path)

    def set_location(self, player_state, x, y):
        player_state["location"]["x"] = x
        player_state["location"]["y"] = y
        return player_state

    def test_knot_will_accept_story_path(self):
        assert self.library.path == self.path

    def test_knot_will_list_available_stories(self):
        stories = self.library.stories()
        assert "basic" in stories
        assert "test" in stories

    def test_knot_get_story_returns_story(self):
        story = self.library.get_story("basic")
        assert isinstance(story, Story)

    def test_build_story_path_includes_story_name(self):
        path = self.library.build_story_path("wonky")
        assert "wonky" in path

    def test_init_story_returns_a_dict(self):
        player_state = self.library.init_story("basic")
        assert type(player_state) is dict

    def test_play_accepts_and_returns_a_dict(self):
        player_state = self.library.init_story("basic")
        player_state = self.library.play(player_state)
        assert type(player_state) is dict

    def test_narrate_returns_a_description_of_the_scene(self):
        player_state = self.library.init_story("basic")
        description = self.library.narrate(player_state)
        assert "This is a basic description of a basic scene" == description

    def test_narrate_returns_a_positional_description_of_the_scene(self):
        player_state = self.library.init_story("basic")
        player_state = self.set_location(player_state, 3, 2)
        description = self.library.narrate(player_state)
        assert "You are at the upper right." == description

    def test_player_state_contains_the_current_scene(self):
        player_state = self.library.init_story("basic")
        assert "flourescent" == player_state["current_scene"]

    def test_player_state_contains_location(self):
        player_state = self.library.init_story("basic")
        assert "location" in player_state.keys()
        assert "x" in player_state["location"].keys()
        assert "y" in player_state["location"].keys()

    def test_player_state_contains_the_current_story(self):
        player_state = self.library.init_story("basic")
        assert "basic" == player_state["story"]

    def test_player_state_contains_the_number_of_turns_played(self):
        player_state = self.library.init_story("basic")
        assert 0 == player_state["turn"]
        player_state = self.library.move(player_state, "e")
        assert 1 == player_state["turn"]
        player_state = self.library.move(player_state, "e")
        assert 2 == player_state["turn"]

    def test_load_scene_returns_scene(self):
        player_state = self.library.init_story("basic")
        assert isinstance(self.library.load_scene(player_state), Scene)

    def test_can_return_scene_map(self):
        player_state = self.library.init_story("basic")
        player_state = self.library.play(player_state)
        scene_map = self.library.scene_map(player_state)
        assert "####" in scene_map

    def test_play_will_update_the_starting_location(self):
        player_state = self.library.init_story("basic")
        old_x = player_state["location"]["x"]
        old_y = player_state["location"]["y"]

        player_state = self.library.play(player_state)
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x != old_x
        assert new_y != old_y

    def test_move_will_move_a_player_north(self):
        player_state = self.library.init_story("basic")
        old_x = player_state["location"]["x"]
        old_y = player_state["location"]["y"]

        player_state = self.library.move(player_state, "n")
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == old_x
        assert new_y == old_y + 1

    def test_move_will_move_a_player_south(self):
        player_state = self.library.init_story("basic")
        player_state["location"]["x"] = 3
        player_state["location"]["y"] = 2

        player_state = self.library.move(player_state, "s")
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == 3
        assert new_y == 1

    def test_move_will_move_a_player_east(self):
        player_state = self.library.init_story("basic")
        old_x = player_state["location"]["x"]
        old_y = player_state["location"]["y"]

        player_state = self.library.move(player_state, "e")
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == old_x + 1
        assert new_y == old_y

    def test_move_will_move_a_player_west(self):
        player_state = self.library.init_story("basic")
        player_state["location"]["x"] = 3
        player_state["location"]["y"] = 0

        player_state = self.library.move(player_state, "w")
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == 2
        assert new_y == 0

    def test_move_accepts_an_optional_repetiton(self):
        player_state = self.library.init_story("basic")
        old_x = player_state["location"]["x"]
        old_y = player_state["location"]["y"]

        player_state = self.library.move(player_state, "e", 2)
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == old_x + 2
        assert new_y == old_y

    def test_player_can_look_to_see_items_on_map(self):
        state = self.library.init_story("basic")
        state = self.library.play(state)
        scene_map = self.library.scene_map(state)
        assert "#@d#" not in scene_map
        assert 1 not in state["seen"]

        state, seen = self.library.look(state)
        assert "dog" in seen
        assert 1 in state["seen"]

        scene_map = self.library.scene_map(state)
        assert "#@d#" in scene_map
class TestPennyCanInspectAStory:
    @pytest.fixture()
    def load_library(self):
        self.library_path = join(getcwd(), "tests", "fixtures", "library")
        self.pennys_story = "penny-pinnacle-pennyslvania"
        self.library = Knot(self.library_path)
        self.stories = self.library.stories()

    def library_path(self):
        return join(getcwd(), "tests", "fixtures", "library")

    def test_penny_can_load_story_info(self):
        # Penny loads up the library
        # Penny lists the stories available(self):
        assert self.pennys_story in self.stories

        # She loads the story
        story = self.library.get_story(self.pennys_story)

        # She checks the author
        assert "Penny Pennington" == story.author

        # She checks the title
        assert "Pen's Pennyslvanian Pinnacle" == story.title

        # She checks the filename for the first scene
        assert "Runaway Train Showdown" == story.scene

        # She checks the story synopsis
        assert "Lorem ipsum" in story.synopsis

        # Satisfied, she goes on her way

    def check_location(self, x, y):
        assert x == self.player_state["location"]["x"]
        assert y == self.player_state["location"]["y"]

    def test_penny_can_view_enter_and_move_around_a_map_stored_in_the_scene(self):
        # Penny returns within minutes, she has not been able to stop thinking about the
        # possibilities presented by the first scene the Runaway Train Showdown.
        # Starts the story
        self.player_state = self.library.init_story(self.pennys_story)
        self.player_state = self.library.play(self.player_state)

        # Penny receives a description of the scene.  She is on a railway platform in
        # Pennyslvania awaiting the arrival of her friend who will be on a train
        setting = self.library.narrate(self.player_state)
        assert "awaiting the arrival of your friend" in setting
        assert "train" in setting

        # Penny asks for and recieves a map from the story.
        scene_map = self.library.scene_map(self.player_state)
        rows = ["###############", "###############", "      #@       "]

        expected = "\n".join(rows)

        self.check_location(7, 0)
        assert expected == scene_map

        # Penny moves north once and asks for another map
        self.player_state = self.library.move(self.player_state, "n")

        scene_map = self.library.scene_map(self.player_state)
        rows = ["###############", "#######@#######", "      ##       "]

        expected = "\n".join(rows)

        self.check_location(7, 1)
        assert expected == scene_map

        # Penny moves east seven times and asks for another map
        self.player_state = self.library.move(self.player_state, "e", 7)

        scene_map = self.library.scene_map(self.player_state)
        rows = ["###############", "##############@", "      ##       "]

        expected = "\n".join(rows)

        self.check_location(14, 1)
        assert expected == scene_map

        # Penny tries to move east again but cannot and asks for another map
        self.player_state = self.library.move(self.player_state, "e")

        scene_map = self.library.scene_map(self.player_state)
        rows = ["###############", "##############@", "      ##       "]

        expected = "\n".join(rows)

        self.check_location(14, 1)
        assert expected == scene_map

        # Penny moves north once and asks for another description of the scene
        self.player_state = self.library.move(self.player_state, "n")

        scene_map = self.library.scene_map(self.player_state)
        rows = ["##############@", "###############", "      ##       "]

        expected = "\n".join(rows)

        self.check_location(14, 2)
        assert expected == scene_map
        setting = self.library.narrate(self.player_state)

        # Penny reads the scene setting and sees that there is a train approaching from western horizon
        assert "western horizon" in setting

        # Penny runs to the western end of the platform and asks for another description
        self.player_state = self.library.move(self.player_state, "w", 14)

        scene_map = self.library.scene_map(self.player_state)
        rows = ["@##############", "###############", "      ##       "]

        expected = "\n".join(rows)

        self.check_location(0, 2)
        assert expected == scene_map
        setting = self.library.narrate(self.player_state)

        # Penny reads in the description that the train with a full head of steam wizzes past her
        # going much faster than it should be
        assert "full head of steam" in setting
        assert "much faster than it should be" in setting

    def test_penny_finds_items_in_the_scene(self):
        # Penny again returns within minutes, she has not been able to stop
        # thinking about the interactive experience that she has had with the
        # story.  This time she has decided to stop and look around and examine
        # the platform more closely.

        # Starts the story
        self.player_state = self.library.init_story(self.pennys_story)
        self.player_state = self.library.play(self.player_state)

        # Penny starts the story and uses the 'look' action to view what is
        # around her.
        self.player_state, seen = self.library.look(self.player_state)

        # She sees a newspaper on the ground.  It is dated today and it
        # announces that the Governor will be stopping in town today to
        # announce her new controversial policy
        assert "newspaper" in seen

        self.player_state, description = self.library.describe(self.player_state, "n")
        assert "Governor" in description
        assert "policy" in description

        # She now sees the newspaper on the map
        # Looking at the map she also sees something else
        scene_map = self.library.scene_map(self.player_state)
        rows = ["###############", "##########b####", "      n@       "]

        expected = "\n".join(rows)
        assert expected == scene_map

        # She asks for more detail about the item and it informs her that
        # the location contains a small box, labeled "penny"
        self.player_state, description = self.library.describe(self.player_state, "b")

        assert "small box" in description
        assert "penny" in description