| 1 |
"""We test the print function logic in flowspace/specialcase.py""" |
| 2 |
import os |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from rpython.flowspace.specialcase import ( |
| 7 |
stdoutbuffer, |
| 8 |
rpython_print_end, |
| 9 |
rpython_print_item, |
| 10 |
rpython_print_newline, |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
class TestPrintFunctionLogic(object): |
| 15 |
|
| 16 |
def _write_spy(self, monkeypatch): |
| 17 |
spied = [] |
| 18 |
monkeypatch.setattr(stdoutbuffer, "linebuf", []) |
| 19 |
monkeypatch.setattr(os, "write", lambda *args: spied.append(args)) |
| 20 |
return spied |
| 21 |
|
| 22 |
def test_rpython_print_end(self, monkeypatch): |
| 23 |
spied = self._write_spy(monkeypatch) |
| 24 |
|
| 25 |
rpython_print_item("spam") |
| 26 |
rpython_print_item("eggs\n") |
| 27 |
rpython_print_item("spam") |
| 28 |
rpython_print_end("@") |
| 29 |
|
| 30 |
assert stdoutbuffer.linebuf == list("spam eggs\n spam@") |
| 31 |
assert len(spied) == 0 |
| 32 |
|
| 33 |
def test_rpython_print_newline(self, monkeypatch): |
| 34 |
spied = self._write_spy(monkeypatch) |
| 35 |
|
| 36 |
|
| 37 |
rpython_print_newline() |
| 38 |
assert len(spied) == 1 |
| 39 |
assert spied[-1] == (1, "\n") |
| 40 |
assert stdoutbuffer.linebuf == [] |
| 41 |
|
| 42 |
|
| 43 |
rpython_print_item("spam") |
| 44 |
rpython_print_item("eggs\n") |
| 45 |
rpython_print_item("spam") |
| 46 |
rpython_print_newline() |
| 47 |
|
| 48 |
assert len(spied) == 2 |
| 49 |
assert spied[-1] == (1, "spam eggs\n spam\n") |
| 50 |
assert stdoutbuffer.linebuf == [] |
| 51 |
|
| 52 |
@pytest.mark.parametrize( |
| 53 |
("first", "end", "second", "expected"), |
| 54 |
( |
| 55 |
(("one", "two"), "", ("three", "four"), "one twothree four"), |
| 56 |
(("one", "two"), " ", ("three", "four"), "one two three four"), |
| 57 |
(("one", "two"), "", (), "one two"), |
| 58 |
(("one", "two"), " ", (), "one two "), |
| 59 |
((), "", ("three", "four"), "three four"), |
| 60 |
((), " ", ("three", "four"), " three four"), |
| 61 |
), |
| 62 |
) |
| 63 |
def test_rpython_print_newline_end_mixed(self, monkeypatch, first, end, second, expected): |
| 64 |
spied = self._write_spy(monkeypatch) |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
for item in first: |
| 71 |
rpython_print_item(item) |
| 72 |
rpython_print_end(end) |
| 73 |
for item in second: |
| 74 |
rpython_print_item(item) |
| 75 |
rpython_print_newline() |
| 76 |
assert len(spied) == 1 |
| 77 |
expected = expected + "\n" |
| 78 |
assert spied[-1] == (1, expected) |
| 79 |
|
| 80 |
def test_rpython_print_newline_end_complex(self, monkeypatch): |
| 81 |
spied = self._write_spy(monkeypatch) |
| 82 |
|
| 83 |
rpython_print_item("one") |
| 84 |
rpython_print_item("two") |
| 85 |
rpython_print_end(" point\nfive") |
| 86 |
rpython_print_item("three") |
| 87 |
rpython_print_item("four") |
| 88 |
rpython_print_newline() |
| 89 |
|
| 90 |
assert len(spied) == 2 |
| 91 |
assert spied[0] == (1, "one two point\n") |
| 92 |
assert spied[1] == (1, "fivethree four\n") |