def test_errors_without_character_opposite(capsys): s = Shakespeare("Foo. Juliet, a test. Romeo, a test. Macbeth, a test.") s.run_event("[Enter Juliet]") assert s.state.character_by_name("Romeo").value == 0 assert s.state.character_by_name("Macbeth").value == 0 with pytest.raises(ShakespeareRuntimeError) as exc: s.run_sentence("You are as good as a furry animal!", "Juliet") assert "talking to nobody" in str(exc.value).lower() assert ">>You are as good as a furry animal!<<" in str(exc.value) assert exc.value.interpreter == s assert s.state.character_by_name("Romeo").value == 0 assert s.state.character_by_name("Macbeth").value == 0 s.run_event("[Enter Macbeth and Romeo]") assert s.state.character_by_name("Romeo").value == 0 assert s.state.character_by_name("Macbeth").value == 0 with pytest.raises(ShakespeareRuntimeError) as exc: s.run_sentence("You are as good as a furry animal!", "Juliet") assert "ambiguous" in str(exc.value).lower() assert ">>You are as good as a furry animal!<<" in str(exc.value) assert exc.value.interpreter == s assert s.state.character_by_name("Romeo").value == 0 assert s.state.character_by_name("Macbeth").value == 0 captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def test_correctly_parses_number(monkeypatch, capsys): monkeypatch.setattr("sys.stdin", StringIO("4257")) s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") s.run_sentence("Listen to your heart!", "Juliet") assert s.state.character_by_name("Romeo").value == 4257 captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def test_interactive_style(monkeypatch, capsys): monkeypatch.setattr("sys.stdin", StringIO("4257\n3211")) s = Shakespeare("Foo. Juliet, a test. Romeo, a test.", input_style="interactive") s.run_event("[Enter Romeo and Juliet]") s.run_sentence("Listen to your heart!", "Juliet") assert s.state.character_by_name("Romeo").value == 4257 captured = capsys.readouterr() assert captured.out == "Taking input number: " assert captured.err == ""
def test_goto_next(capsys): s = Shakespeare(SAMPLE_PLAY) assert s.current_position == 0 s.step_forward() assert s.current_position == 1 s.run_sentence("Let us proceed to scene II.", "Juliet") assert s.current_position == 2 captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def test_errors_on_eof(monkeypatch, capsys): monkeypatch.setattr("sys.stdin", StringIO("")) s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") with pytest.raises(ShakespeareRuntimeError) as exc: s.run_sentence("Listen to your heart!", "Juliet") assert "end of file" in str(exc.value).lower() assert ">>Listen to your heart!<<" in str(exc.value) assert exc.value.interpreter == s assert s.state.character_by_name("Romeo").value == 0 captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def test_conditional(capsys): s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") s.state.character_by_name("Romeo").value = 4100 s.state.global_boolean = False s.run_sentence("If so, open your heart!", "Juliet") captured = capsys.readouterr() assert captured.out == "" assert captured.err == "" s.state.global_boolean = True s.run_sentence("If not, open your heart!", "Juliet") captured = capsys.readouterr() assert captured.out == "" assert captured.err == "" s.state.global_boolean = True s.run_sentence("If so, open your heart!", "Juliet") captured = capsys.readouterr() assert captured.out == "4100" assert captured.err == "" s.state.character_by_name("Romeo").value = -5 s.state.global_boolean = False s.run_sentence("If not, open your heart!", "Juliet") captured = capsys.readouterr() assert captured.out == "-5" assert captured.err == ""
def test_assign_character(capsys): s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") assert s.state.character_by_name("Romeo").value == 0 s.run_sentence("You are as good as a furry animal!", "Juliet") assert s.state.character_by_name("Romeo").value == 2 s.state.character_by_name("Romeo").value = 0 s.run_sentence("You are a pig!", "Juliet") assert s.state.character_by_name("Romeo").value == -1 captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def test_conditional_pop(): s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") c = s.state.character_by_name("Juliet") assert c.stack == [] assert c.value == 0 c.stack = [234, 123, 678] s.state.global_boolean = False s.run_sentence("If so, recall thy terrible memory of thy imminent death.", "Romeo") assert c.stack == [234, 123, 678] assert c.value == 0 s.state.global_boolean = True s.run_sentence("If not, recall thy terrible memory of thy imminent death.", "Romeo") assert c.stack == [234, 123, 678] assert c.value == 0 s.state.global_boolean = True s.run_sentence("If so, recall thy terrible memory of thy imminent death.", "Romeo") assert c.stack == [234, 123] assert c.value == 678 s.state.global_boolean = False s.run_sentence("If not, recall thy terrible memory of thy imminent death.", "Romeo") assert c.stack == [234] assert c.value == 123
def test_eof_character_code(monkeypatch, capsys): monkeypatch.setattr("sys.stdin", StringIO("&")) s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 38 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == -1 captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def test_conditional_push(): s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") c = s.state.character_by_name("Juliet") assert c.stack == [] assert c.value == 0 s.state.global_boolean = False s.run_sentence("If so, remember a furry animal.", "Romeo") assert c.stack == [] assert c.value == 0 s.state.global_boolean = True s.run_sentence("If not, remember a furry animal.", "Romeo") assert c.stack == [] assert c.value == 0 s.state.global_boolean = True s.run_sentence("If so, remember a furry animal.", "Romeo") assert c.stack == [2] assert c.value == 0 s.state.global_boolean = False s.run_sentence("If not, remember a furry furry animal.", "Romeo") assert c.stack == [2, 4] assert c.value == 0
def test_conditional(capsys): s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") assert s.state.character_by_name("Romeo").value == 0 s.state.global_boolean = False s.run_sentence("If so, you are as good as a furry animal!", "Juliet") assert s.state.character_by_name("Romeo").value == 0 assert s.state.character_by_name("Romeo").value == 0 s.state.global_boolean = True s.run_sentence("If not, you are as good as a furry animal!", "Juliet") assert s.state.character_by_name("Romeo").value == 0 assert s.state.character_by_name("Romeo").value == 0 s.state.global_boolean = True s.run_sentence("If so, you are as good as a furry animal!", "Juliet") assert s.state.character_by_name("Romeo").value == 2 assert s.state.character_by_name("Romeo").value == 2 s.state.global_boolean = False s.run_sentence("If not, you are as good as a furry furry animal!", "Juliet") assert s.state.character_by_name("Romeo").value == 4 captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def test_conditional(capsys): s = Shakespeare("Foo. Juliet, a test. Romeo, a test. Act I: One. Scene I: One.") s.run_event("[Enter Romeo and Juliet]") s.state.character_by_name("Romeo").value = 97 s.state.global_boolean = False s.run_sentence("If so, speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "" assert captured.err == "" s.state.global_boolean = True s.run_sentence("If not, speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "" assert captured.err == "" s.state.global_boolean = False s.run_sentence("If not, speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "a" assert captured.err == "" s.state.character_by_name("Romeo").value = 98 s.state.global_boolean = True s.run_sentence("If so, speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "b" assert captured.err == ""
def test_consumes_trailing_newline(monkeypatch, capsys): monkeypatch.setattr("sys.stdin", StringIO("4257\na")) s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") s.run_sentence("Listen to your heart!", "Juliet") assert s.state.character_by_name("Romeo").value == 4257 assert input() == "a" captured = capsys.readouterr() assert captured.out == "" assert captured.err == "" # Make sure there isn't a '\n' still living in the buffer s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == -1
def test_errors_on_goto_nonexistent(capsys): s = Shakespeare(SAMPLE_PLAY) assert s.current_position == 0 s.step_forward() assert s.current_position == 1 with pytest.raises(ShakespeareRuntimeError) as exc: s.run_sentence("Let us proceed to scene IV.", "Juliet") assert "does not exist" in str(exc.value).lower() assert ">>Let us proceed to scene IV.<<" in str(exc.value) assert exc.value.interpreter == s assert s.current_position == 1 captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def test_goto_without_opposite_character(capsys): s = Shakespeare(SAMPLE_PLAY) assert s.current_position == 0 s.step_forward() assert s.current_position == 1 s.run_event("[Exit Romeo]") s.run_sentence("Let us proceed to scene II.", "Juliet") assert s.current_position == 2 s.run_event("[Enter Romeo and Macbeth]") s.run_sentence("Let us proceed to scene I.", "Juliet") assert s.current_position == 0 captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def test_errors_on_pop_from_empty(): s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") c = s.state.character_by_name("Juliet") assert c.stack == [] assert c.value == 0 with pytest.raises(ShakespeareRuntimeError) as exc: s.run_sentence("Recall thy terrible memory of thy imminent death.", "Romeo") assert "empty stack" in str(exc.value).lower() assert ">>Recall thy terrible memory of thy imminent death.<<" in str( exc.value) assert exc.value.interpreter == s assert c.stack == [] assert c.value == 0
def test_errors_on_invalid_code(capsys): s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") s.state.character_by_name("Romeo").value = 100000000 with pytest.raises(ShakespeareRuntimeError) as exc: s.run_sentence("Speak your mind!", "Juliet") assert "invalid character code" in str(exc.value).lower() assert ">>Speak your mind!<<" in str(exc.value) assert exc.value.interpreter == s s.state.character_by_name("Romeo").value = -1 with pytest.raises(ShakespeareRuntimeError) as exc: s.run_sentence("Speak your mind!", "Juliet") assert "invalid character code" in str(exc.value).lower() assert ">>Speak your mind!<<" in str(exc.value) assert exc.value.interpreter == s captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def test_goto_conditionals(capsys): s = Shakespeare(SAMPLE_PLAY) assert s.current_position == 0 s.step_forward() assert s.current_position == 1 s.state.global_boolean = True s.run_sentence("If so, let us proceed to scene II.", "Juliet") assert s.current_position == 2 s.state.global_boolean = True s.run_sentence("If not, let us proceed to scene I.", "Juliet") assert s.current_position == 2 s.state.global_boolean = False s.run_sentence("If so, let us proceed to scene I.", "Juliet") assert s.current_position == 2 s.state.global_boolean = False s.run_sentence("If not, let us proceed to scene I.", "Juliet") assert s.current_position == 0 captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def test_conditional(monkeypatch, capsys): monkeypatch.setattr("sys.stdin", StringIO("ab")) s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") s.state.global_boolean = False s.run_sentence("If so, open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 0 s.state.global_boolean = True s.run_sentence("If not, open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 0 s.state.global_boolean = True s.run_sentence("If so, open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 97 s.state.global_boolean = False s.run_sentence("If not, open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 98 captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def test_push(): s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") c = s.state.character_by_name("Juliet") assert c.stack == [] assert c.value == 0 s.run_sentence("Remember a furry animal.", "Romeo") assert c.stack == [2] assert c.value == 0 s.run_sentence("Remember a furry furry animal.", "Romeo") assert c.stack == [2, 4] assert c.value == 0 s.run_sentence("Remember a furry furry furry animal.", "Romeo") assert c.stack == [2, 4, 8] assert c.value == 0
def test_outputs_numbers(capsys): s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") s.state.character_by_name("Romeo").value = 4100 s.run_sentence("Open your heart!", "Juliet") captured = capsys.readouterr() assert captured.out == "4100" assert captured.err == "" s.state.character_by_name("Romeo").value = -5 s.run_sentence("Open your heart!", "Juliet") captured = capsys.readouterr() assert captured.out == "-5" assert captured.err == "" s.state.character_by_name("Romeo").value = 9 s.run_sentence("Open your heart!", "Juliet") captured = capsys.readouterr() assert captured.out == "9" assert captured.err == ""
def test_pop(): s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") c = s.state.character_by_name("Juliet") assert c.stack == [] assert c.value == 0 c.stack = [234, 123, 678] s.run_sentence("Recall thy terrible memory of thy imminent death.", "Romeo") assert c.stack == [234, 123] assert c.value == 678 s.run_sentence("Recall thy terrible memory of thy imminent death.", "Romeo") assert c.stack == [234] assert c.value == 123 s.run_sentence("Recall thy terrible memory of thy imminent death.", "Romeo") assert c.stack == [] assert c.value == 234
def test_goto_based_on_numeral_not_order(capsys): s = Shakespeare(""" Test. Romeo, a test. Juliet, a test. Act I: Nothing to see here. Scene III: These are not the actors you're looking for. [Enter Romeo and Juliet] Juliet: Are you as good as nothing? Scene I: Still nothing. [A pause] Scene II: Nothing strikes back. [A pause] """) assert s.current_position == 0 s.step_forward() assert s.current_position == 1 s.run_sentence("Let us return to scene I.", "Juliet") assert s.current_position == 2 s.run_sentence("Let us return to scene III.", "Juliet") assert s.current_position == 0 s.run_sentence("Let us return to scene II.", "Juliet") assert s.current_position == 3 captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def test_unicode(capsys): s = Shakespeare("Foo. Juliet, a test. Romeo, a test. Act I: One. Scene I: One.") s.run_event("[Enter Romeo and Juliet]") s.state.character_by_name("Romeo").value = 664 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "ʘ" assert captured.err == "" s.state.character_by_name("Romeo").value = 613 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "ɥ" assert captured.err == "" s.state.character_by_name("Romeo").value = 1244 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "Ӝ" assert captured.err == "" s.state.character_by_name("Romeo").value = 2310 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "आ" assert captured.err == "" s.state.character_by_name("Romeo").value = 2708 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "ઔ" assert captured.err == "" s.state.character_by_name("Romeo").value = 3494 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "ඦ" assert captured.err == "" s.state.character_by_name("Romeo").value = 6326 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "ᢶ" assert captured.err == "" s.state.character_by_name("Romeo").value = 6662 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "ᨆ" assert captured.err == "" s.state.character_by_name("Romeo").value = 7495 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "ᵇ" assert captured.err == "" s.state.character_by_name("Romeo").value = 7716 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "Ḥ" assert captured.err == ""
def test_reads_characters_accurately(monkeypatch, capsys): monkeypatch.setattr("sys.stdin", StringIO("ab\nAB\t&@ ")) s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 97 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 98 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 10 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 65 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 66 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 9 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 38 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 64 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 32 captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""
def test_outputs_correct_character(capsys): s = Shakespeare("Foo. Juliet, a test. Romeo, a test. Act I: One. Scene I: One.") s.run_event("[Enter Romeo and Juliet]") s.state.character_by_name("Romeo").value = 97 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "a" assert captured.err == "" s.state.character_by_name("Romeo").value = 98 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "b" assert captured.err == "" s.state.character_by_name("Romeo").value = 10 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "\n" assert captured.err == "" s.state.character_by_name("Romeo").value = 65 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "A" assert captured.err == "" s.state.character_by_name("Romeo").value = 66 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "B" assert captured.err == "" s.state.character_by_name("Romeo").value = 9 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "\t" assert captured.err == "" s.state.character_by_name("Romeo").value = 38 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "&" assert captured.err == "" s.state.character_by_name("Romeo").value = 64 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == "@" assert captured.err == "" s.state.character_by_name("Romeo").value = 32 s.run_sentence("Speak your mind!", "Juliet") captured = capsys.readouterr() assert captured.out == " " assert captured.err == ""
def test_unicode(monkeypatch, capsys): monkeypatch.setattr("sys.stdin", StringIO("ʘɥӜआઔඦᢶᨆᵇḤ")) s = Shakespeare("Foo. Juliet, a test. Romeo, a test.") s.run_event("[Enter Romeo and Juliet]") s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 664 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 613 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 1244 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 2310 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 2708 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 3494 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 6326 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 6662 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 7495 s.run_sentence("Open your mind!", "Juliet") assert s.state.character_by_name("Romeo").value == 7716 captured = capsys.readouterr() assert captured.out == "" assert captured.err == ""