Пример #1
0
def test_add_component(monkeypatch):
    """
    .
    """
    # pylint: disable=unused-argument
    @counter_wrapper
    def make_choice(prompt: str, options: list, default):
        """
        .
        """
        assert None in options, "None added to options"
        return "Power Plant" if make_choice.counter == 1 else None

    @counter_wrapper
    def add_component(self, component):
        """
        .
        """

    monkeypatch.setattr(inputs, "make_choice", make_choice)
    monkeypatch.setattr(ship.Ship, "add_component", add_component)
    monkeypatch.setattr(BaseComponent, "configure", lambda self: None)

    build_shopping_list.add_component(ship.Ship())
    assert make_choice.counter == 1, "Component selected"
    assert add_component.counter == 1, "Component added"

    build_shopping_list.add_component(ship.Ship())
    assert make_choice.counter == 2, "Component selected again"
    assert add_component.counter == 1, "Component NOT added if None selected"
Пример #2
0
def test_update_component(monkeypatch):
    """
    .
    """
    # pylint: disable=unused-argument
    @counter_wrapper
    def configure(self):
        """
        .
        """

    monkeypatch.setattr(BaseComponent, "configure", configure)

    @counter_wrapper
    def make_choice(prompt: str, options: list, default):
        """
        .
        """
        assert None in options, "None added to options"
        return "Power Plant" if make_choice.counter == 1 else None

    monkeypatch.setattr(inputs, "make_choice", make_choice)
    configured_ship = ship.Ship()
    configured_ship.add_component(PowerPlant())

    build_shopping_list.update_component(configured_ship)
    assert make_choice.counter == 1, "Component selected"
    assert configure.counter == 2, "Component configured and re-configured"

    build_shopping_list.update_component(configured_ship)
    assert make_choice.counter == 2, "Component selected again"
    assert configure.counter == 2, "Component NOT re-configured if None selected"
Пример #3
0
def test_remove_component(monkeypatch):
    """
    .
    """

    # pylint: disable=unused-argument
    @counter_wrapper
    def make_choice(prompt: str, options: list, default):
        """
        .
        """
        assert None in options, "None added to options"
        return None if make_choice.counter == 1 else "Power Plant"

    monkeypatch.setattr(BaseComponent, "configure", lambda self: None)
    monkeypatch.setattr(inputs, "make_choice", make_choice)
    configured_ship = ship.Ship()
    configured_ship.add_component(PowerPlant())

    assert len(configured_ship.component_list) == 1, "One component on ship"

    build_shopping_list.remove_component(configured_ship)
    assert make_choice.counter == 1, "Component selected"
    assert len(configured_ship.component_list) == 1, "Component not removed"

    build_shopping_list.remove_component(configured_ship)
    assert make_choice.counter == 2, "Component selected again"
    assert len(configured_ship.component_list) == 0, "Component removed"
Пример #4
0
def test_summarize(monkeypatch, capsys):
    """
    .
    """
    # pylint: disable=unused-argument
    monkeypatch.setattr(ship.Ship, "summarize", lambda self: "Ship")
    build_shopping_list.summarize_ship(ship.Ship())
    printed = capsys.readouterr()
    assert printed.out == "Ship\n", "Summary prints"
Пример #5
0
def test_save_shopping_list(monkeypatch):
    """
    .
    """
    # pylint: disable=unused-argument
    monkeypatch.setattr(inputs, "single_prompt", lambda *args, **kwargs: "")

    assert not build_shopping_list.save_shopping_list(
        ship.Ship(), "cmdr", {}), "Shopping list doesn't save if no file given"

    monkeypatch.setattr(inputs, "single_prompt",
                        lambda *args, **kwargs: "file")
    monkeypatch.setattr(path, "isfile", lambda file_path: True)
    monkeypatch.setattr(inputs, "check_confirmation",
                        lambda *args, **kwargs: False)
    assert not build_shopping_list.save_shopping_list(
        ship.Ship(), "cmdr", {}
    ), "Shopping list doesn't save if user doesn't overwrite existing file"

    @counter_wrapper
    def output_file(*args, **kwargs):
        """
        .
        """

    monkeypatch.setattr(build_shopping_list, "save_data_to_file", output_file)
    monkeypatch.setattr(inputs, "check_confirmation",
                        lambda *args, **kwargs: True)

    assert build_shopping_list.save_shopping_list(
        ship.Ship(), "cmdr",
        {}), "Shopping list written to file if overwritten"
    assert output_file.counter == 1, "File saved"

    monkeypatch.setattr(inputs, "check_confirmation",
                        lambda *args, **kwargs: True)
    monkeypatch.setattr(path, "isfile", lambda file_path: False)

    assert build_shopping_list.save_shopping_list(
        ship.Ship(), "cmdr", {}), "Shopping list written to file if new file"
    assert output_file.counter == 2, "File saved"