File: test_print_function.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (92 lines) | stat: -rw-r--r-- 2,887 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""We test the print function logic in flowspace/specialcase.py"""
import os

import pytest

from rpython.flowspace.specialcase import (
    stdoutbuffer,
    rpython_print_end,
    rpython_print_item,
    rpython_print_newline,
)


class TestPrintFunctionLogic(object):

    def _write_spy(self, monkeypatch):
        spied = []
        monkeypatch.setattr(stdoutbuffer, "linebuf", [])
        monkeypatch.setattr(os, "write", lambda *args: spied.append(args))
        return spied

    def test_rpython_print_end(self, monkeypatch):
        spied = self._write_spy(monkeypatch)

        rpython_print_item("spam")
        rpython_print_item("eggs\n")
        rpython_print_item("spam")
        rpython_print_end("@")

        assert stdoutbuffer.linebuf == list("spam eggs\n spam@")
        assert len(spied) == 0  # writes are buffered

    def test_rpython_print_newline(self, monkeypatch):
        spied = self._write_spy(monkeypatch)

        # empty print() call
        rpython_print_newline()
        assert len(spied) == 1
        assert spied[-1] == (1, "\n")
        assert stdoutbuffer.linebuf == []

        # print call with a few items in it
        rpython_print_item("spam")
        rpython_print_item("eggs\n")
        rpython_print_item("spam")
        rpython_print_newline()

        assert len(spied) == 2
        assert spied[-1] == (1, "spam eggs\n spam\n")
        assert stdoutbuffer.linebuf == []

    @pytest.mark.parametrize(
        ("first", "end", "second", "expected"),
        (
            (("one", "two"), "", ("three", "four"), "one twothree four"),
            (("one", "two"), " ", ("three", "four"), "one two three four"),
            (("one", "two"), "", (), "one two"),
            (("one", "two"), " ", (), "one two "),
            ((), "", ("three", "four"), "three four"),
            ((), " ", ("three", "four"), " three four"),
        ),
    )
    def test_rpython_print_newline_end_mixed(self, monkeypatch, first, end, second, expected):
        spied = self._write_spy(monkeypatch)

        # Tests the equivalent of
        # print(*first, end=end)
        # print(*second)

        for item in first:
            rpython_print_item(item)
        rpython_print_end(end)
        for item in second:
            rpython_print_item(item)
        rpython_print_newline()
        assert len(spied) == 1
        expected = expected + "\n"
        assert spied[-1] == (1, expected)

    def test_rpython_print_newline_end_complex(self, monkeypatch):
        spied = self._write_spy(monkeypatch)

        rpython_print_item("one")
        rpython_print_item("two")
        rpython_print_end(" point\nfive")
        rpython_print_item("three")
        rpython_print_item("four")
        rpython_print_newline()

        assert len(spied) == 2
        assert spied[0] == (1, "one two point\n")
        assert spied[1] == (1, "fivethree four\n")