예제 #1
0
def test_multiline():
    a = 1
    b = 2
    ll = list(range(15))
    # fmt: off
    s = y((a, b), [ll, ll], as_str=True)
    # fmt: on
    assert (s == """\
y|
    (a, b): (1, 2)
    [ll,
    ll]:
        [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
         [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]]
""")

    lines = "\n".join("line{i}".format(i=i) for i in range(4))
    result = y(lines, as_str=True)
    assert (result == """\
y|
    lines:
        'line0
        line1
        line2
        line3'
""")
예제 #2
0
    def test_serialize(capsys):
        def serialize(s):
            return repr(s) + " [len=" + str(len(s)) + "]"

        hello = "world"
        y(hello, serialize=serialize)
        out, err = capsys.readouterr()
        assert err == "y| hello: 'world' [len=5]\n"
예제 #3
0
def test_one_arguments(capsys):
    hello = "world"
    result = y(hello)
    y(hello)
    out, err = capsys.readouterr()
    assert (err == """\
y| hello: 'world'
y| hello: 'world'
""")
    assert result == hello
예제 #4
0
def test_wrap_indent():
    s = 4 * ["*******************"]
    res = y(s, compact=True, as_str=True)
    assert res.splitlines()[1].startswith("    s")
    res = y(s, compact=True, as_str=True, wrap_indent="....")
    assert res.splitlines()[1].startswith("....s")
    res = y(s, compact=True, as_str=True, wrap_indent=2)
    assert res.splitlines()[1].startswith("  s")
    res = y(s, compact=True, as_str=True, wrap_indent=[])
    assert res.splitlines()[1].startswith("[]s")
예제 #5
0
def test_sort_dicts():
    world = {"EN": "world", "NL": "wereld", "FR": "monde", "DE": "Welt"}
    s0 = y(world, as_str=True)
    s1 = y(world, sort_dicts=False, as_str=True)
    s2 = y(world, sort_dicts=True, as_str=True)
    if PY2:
        assert s0 == s1 == s2 == "y| world: {'DE': 'Welt', 'EN': 'world', 'FR': 'monde', 'NL': 'wereld'}\n"
    if PY3:
        assert s0 == s1 == "y| world: {'EN': 'world', 'NL': 'wereld', 'FR': 'monde', 'DE': 'Welt'}\n"
        assert s2 == "y| world: {'DE': 'Welt', 'EN': 'world', 'FR': 'monde', 'NL': 'wereld'}\n"
예제 #6
0
def test_enforce_line_length(capsys):
    s = 80 * "*"
    y(s)
    y(s, enforce_line_length=True)
    out, err = capsys.readouterr()
    assert (err == """\
y|
    s: '********************************************************************************'
y|
    s: '************************************************************************
""")
    with y.preserve():
        y.configure(line_length=20, show_line_number=True)
        y()
        out, err1 = capsys.readouterr()
        y(enforce_line_length=True)
        out, err2 = capsys.readouterr()
        err1 = err1.rstrip("\n")
        err2 = err2.rstrip("\n")
        assert len(err2) == 20
        assert err1[10:20] == err2[10:20]
        assert len(err1) > 20
    res = y("abcdefghijklmnopqrstuvwxyz", p="", ell=1, ll=20,
            as_str=True).rstrip("\n")
    assert res == "'abcdefghijklmnopqrs"
    assert len(res) == 20
예제 #7
0
def test_calls():
    with pytest.raises(TypeError):
        y.new(a=1)
    with pytest.raises(TypeError):
        y.clone(a=1)
    with pytest.raises(TypeError):
        y.configure(a=1)
    with pytest.raises(TypeError):
        y(12, a=1)
    with pytest.raises(TypeError):
        y(a=1)
예제 #8
0
def test_time_delta():
    sdelta0 = y(1, show_delta=True, as_str=True)
    stime0 = y(1, show_time=True, as_str=True)
    time.sleep(0.001)
    sdelta1 = y(1, show_delta=True, as_str=True)
    stime1 = y(1, show_time=True, as_str=True)
    assert sdelta0 != sdelta1
    assert stime0 != stime1
    y.delta = 10
    time.sleep(0.1)
    assert 10.05 < y.delta < 11
예제 #9
0
def test_return_none(capsys):
    a = 2
    result = y(a, a)
    assert result == (a, a)
    result = y(a, a, return_none=True)
    assert result is None
    out, err = capsys.readouterr()
    assert (err == """\
y| a: 2, a: 2
y| a: 2, a: 2
""")
예제 #10
0
def test_context_manager(capsys):
    ycecream.fix_perf_counter(0)
    with y():
        y(3)
    out, err = capsys.readouterr()
    assert (err == """\
y| enter
y| 3
y| exit in 0.000000 seconds
""")
    ycecream.fix_perf_counter(None)
예제 #11
0
def test_dynamic_prefix(capsys):
    g.i = 0

    def prefix():
        g.i += 1
        return str(g.i) + ")"

    hello = "world"
    y(hello, prefix=prefix)
    y(hello, prefix=prefix)
    out, err = capsys.readouterr()
    assert err == "1)hello: 'world'\n2)hello: 'world'\n"
예제 #12
0
    def test_as_str(capsys):
        hello = "world"
        s = y(hello, as_str=True)
        y(hello)
        out, err = capsys.readouterr()
        assert err == s

        with pytest.raises(TypeError):

            @y(as_str=True)
            def add2(x):
                return x + 2

        with pytest.raises(TypeError):
            with y(as_str=True):
                pass
예제 #13
0
def test_two_arguments(capsys):
    hello = "world"
    ll = [1, 2, 3]
    result = y(hello, ll)
    out, err = capsys.readouterr()
    assert err == "y| hello: 'world', ll: [1, 2, 3]\n"
    assert result == (hello, ll)
예제 #14
0
def test_traceback(capsys):
    with y.preserve():
        y.show_traceback = True
        y()
        out, err = capsys.readouterr()
        assert err.count("traceback") == 2

        @y
        def p():
            pass

        p()
        out, err = capsys.readouterr()
        assert err.count("traceback") == 2
        with y():
            pass
        out, err = capsys.readouterr()
        assert err.count("traceback") == 2
예제 #15
0
def test_clone():
    hello = "world"
    z = y.clone()
    z.configure(prefix="z| ")
    sy = y(hello, as_str=True)
    with y.preserve():
        y.configure(show_line_number=True)
        sz = z(hello, as_str=True)
        assert sy.replace("y", "z") == sz
예제 #16
0
def test_enabled3(capsys):
    with y.preserve():
        y.configure(enabled=[])
        y(2)
        with pytest.raises(TypeError):

            @y()
            def add2(x):
                return x + 2

        with pytest.raises(AttributeError):
            with y():
                pass

        @y(decorator=True)
        def add2(x):
            return x + 2

        with y(context_manager=True):
            pass
예제 #17
0
def test_depth_indent(capsys):
    s = "=============================================="
    a = [s + "1", [s + "2", [s + "3", [s + "4"]]], s + "1"]
    y(a, indent=4)
    y(a, depth=2, indent=4)
    out, err = capsys.readouterr()
    assert (err == """\
y|
    a:
        [   '==============================================1',
            [   '==============================================2',
                [   '==============================================3',
                    ['==============================================4']]],
            '==============================================1']
y|
    a:
        [   '==============================================1',
            ['==============================================2', [...]],
            '==============================================1']
""")
예제 #18
0
def test_multiple_as():
    with pytest.raises(TypeError):
        y(1, decorator=True, context_manager=True)
    with pytest.raises(TypeError):
        y(1, decorator=True, as_str=True)
    with pytest.raises(TypeError):
        y(1, context_manager=True, as_str=True)
예제 #19
0
def test_compact(capsys):
    if PY2:
        return
    a = 9 * ["0123456789"]
    y(a)
    y(a, compact=True)
    out, err = capsys.readouterr()
    assert (err == """\
y|
    a:
        ['0123456789',
         '0123456789',
         '0123456789',
         '0123456789',
         '0123456789',
         '0123456789',
         '0123456789',
         '0123456789',
         '0123456789']
y|
    a:
        ['0123456789', '0123456789', '0123456789', '0123456789', '0123456789',
         '0123456789', '0123456789', '0123456789', '0123456789']
""")
예제 #20
0
def test_enabled(capsys):
    with y.preserve():
        y("One")
        y.configure(enabled=False)
        y("Two")
        s = y("Two", as_str=True)
        assert s == "y| 'Two'\n"
        y.configure(enabled=True)
        y("Three")

    out, err = capsys.readouterr()
    assert (err == """\
y| 'One'
y| 'Three'
""")
예제 #21
0
def test_values_only():
    with y.preserve():
        y.configure(values_only=True)
        hello = "world"
        s = y(hello, as_str=True)
        assert s == "y| 'world'\n"
예제 #22
0
 def test_show_delta(capsys):
     hello = "world"
     y(hello, show_delta=True)
     out, err = capsys.readouterr()
     assert err.endswith("hello: 'world'\n")
     assert "delta=" in err
예제 #23
0
 def hello(val):
     y(val, show_line_number=True)
예제 #24
0
def test_no_arguments(capsys):
    result = y()
    out, err = capsys.readouterr()
    assert err.startswith(context_start)
    assert err.endswith(" in test_no_arguments()\n")
    assert result is None
예제 #25
0
def test_enabled2(capsys):
    with y.preserve():
        y.configure(enabled=False)
        line0 = y("line0")
        noline0 = y(prefix="no0")
        pair0 = y("p0", "p0")
        s0 = y("s0", as_str=True)
        y.configure(enabled=[])
        line1 = y("line1")
        noline1 = y(prefix="no1")
        pair1 = y("p1", "p1")
        s1 = y("s1", as_str=True)
        y.configure(enabled=True)
        line2 = y("line2")
        noline2 = y(prefix="no2")
        pair2 = y("p2", "p2")
        s2 = y("s2", as_str=True)
        out, err = capsys.readouterr()
        assert "line0" not in err and "p0" not in err and "no0" not in err
        assert "line1" not in err and "p1" not in err and "no1" not in err
        assert "line2" in err and "p2" in err and "no2" in err
        assert line0 == "line0"
        assert line1 == "line1"
        assert line2 == "line2"
        assert noline0 is None
        assert noline1 is None
        assert noline2 is None
        assert pair0 == ("p0", "p0")
        assert pair1 == ("p1", "p1")
        assert pair2 == ("p2", "p2")
        assert s0 == "y| 's0'\n"
        assert s1 == "y| 's1'\n"
        assert s2 == "y| 's2'\n"
예제 #26
0
def test_time(patch_datetime_now):
    hello = "world"
    s = y(hello, show_time=True, as_str=True)
    assert s == "y| @ 00:00:00.000000 ==> hello: 'world'\n"
예제 #27
0
def test_output(capsys):
    with tempfile.TemporaryDirectory(
    ) as tmpdir:  # we can't use tmpdir from pytest because of Python 2.7 compatibity

        g.result = ""

        def my_output(s):
            g.result += s + "\n"

        hello = "world"
        y(hello, output=print)
        out, err = capsys.readouterr()
        assert out == "y| hello: 'world'\n"
        assert err == ""
        y(hello, output=sys.stdout)
        out, err = capsys.readouterr()
        assert out == "y| hello: 'world'\n"
        assert err == ""
        y(hello, output="stdout")
        out, err = capsys.readouterr()
        assert out == "y| hello: 'world'\n"
        assert err == ""
        y(hello, output="")
        out, err = capsys.readouterr()
        assert out == ""
        assert err == ""
        y(hello, output="null")
        out, err = capsys.readouterr()
        assert out == ""
        assert err == ""
        y(hello, output=print)
        out, err = capsys.readouterr()
        assert out == "y| hello: 'world'\n"
        assert err == ""

        if True:
            path = Path(tmpdir) / "x0"
            y(hello, output=path)
            out, err = capsys.readouterr()
            assert out == ""
            assert err == ""
            with path.open("r") as f:
                assert f.read() == "y| hello: 'world'\n"

            path = Path(tmpdir) / "x1"
            y(hello, output=path)
            out, err = capsys.readouterr()
            assert out == ""
            assert err == ""
            with path.open("r") as f:
                assert f.read() == "y| hello: 'world'\n"

            path = Path(tmpdir) / "x2"
            with path.open("a+") as f:
                y(hello, output=f)
            with pytest.raises(TypeError):  # closed file
                y(hello, output=f)
            out, err = capsys.readouterr()
            assert out == ""
            assert err == ""
            with path.open("r") as f:
                assert f.read() == "y| hello: 'world'\n"

        with pytest.raises(TypeError):
            y(hello, output=1)

        y(hello, output=my_output)
        y(1, output=my_output)
        out, err = capsys.readouterr()
        assert out == ""
        assert err == ""
        assert g.result == "y| hello: 'world'\ny| 1\n"

    def test_serialize(capsys):
        def serialize(s):
            return repr(s) + " [len=" + str(len(s)) + "]"

        hello = "world"
        y(hello, serialize=serialize)
        out, err = capsys.readouterr()
        assert err == "y| hello: 'world' [len=5]\n"

    def test_show_time(capsys):
        hello = "world"
        y(hello, show_time=True)
        out, err = capsys.readouterr()
        assert err.endswith("hello: 'world'\n")
        assert "@ " in err

    def test_show_delta(capsys):
        hello = "world"
        y(hello, show_delta=True)
        out, err = capsys.readouterr()
        assert err.endswith("hello: 'world'\n")
        assert "delta=" in err

    def test_as_str(capsys):
        hello = "world"
        s = y(hello, as_str=True)
        y(hello)
        out, err = capsys.readouterr()
        assert err == s

        with pytest.raises(TypeError):

            @y(as_str=True)
            def add2(x):
                return x + 2

        with pytest.raises(TypeError):
            with y(as_str=True):
                pass
예제 #28
0
def test_prefix(capsys):
    hello = "world"
    y(hello, prefix="==> ")
    out, err = capsys.readouterr()
    assert err == "==> hello: 'world'\n"
예제 #29
0
def test_wrapping(capsys):
    if PY2:
        return

    l0 = "".join("         {c}".format(c=c)
                 for c in "12345678") + "\n" + "".join(".........0"
                                                       for c in "12345678")

    print(l0, file=sys.stderr)
    ccc = cccc = 3 * ["12345678123456789012"]
    ccc0 = [cccc[0] + "0"] + cccc[1:]
    y(ccc)
    y(cccc)
    y(ccc0)

    out, err = capsys.readouterr()
    assert (err == """\
         1         2         3         4         5         6         7         8
.........0.........0.........0.........0.........0.........0.........0.........0
y| ccc: ['12345678123456789012', '12345678123456789012', '12345678123456789012']
y|
    cccc:
        ['12345678123456789012', '12345678123456789012', '12345678123456789012']
y|
    ccc0:
        ['123456781234567890120',
         '12345678123456789012',
         '12345678123456789012']
""")
    a = "1234"
    b = bb = 9 * ["123"]
    print(l0, file=sys.stderr)
    y(a, b)
    y(a, bb)
    out, err = capsys.readouterr()
    assert (err == """\
         1         2         3         4         5         6         7         8
.........0.........0.........0.........0.........0.........0.........0.........0
y| a: '1234', b: ['123', '123', '123', '123', '123', '123', '123', '123', '123']
y|
    a: '1234'
    bb: ['123', '123', '123', '123', '123', '123', '123', '123', '123']
""")
    dddd = 10 * ["123"]
    dddd = ddddd = 10 * ["123"]
    e = "a\nb"
    print(l0, file=sys.stderr)
    y(a, dddd)
    y(a, ddddd)
    y(e)
    out, err = capsys.readouterr()
    assert (err == """\
         1         2         3         4         5         6         7         8
.........0.........0.........0.........0.........0.........0.........0.........0
y|
    a: '1234'
    dddd: ['123', '123', '123', '123', '123', '123', '123', '123', '123', '123']
y|
    a: '1234'
    ddddd:
        ['123', '123', '123', '123', '123', '123', '123', '123', '123', '123']
y|
    e:
        'a
        b'
""")
    a = aa = 2 * ["0123456789ABC"]
    print(l0, file=sys.stderr)
    y(a, line_length=40)
    y(aa, line_length=40)
    y(aa, line_length=41)
    out, err = capsys.readouterr()
    assert (err == """\
         1         2         3         4         5         6         7         8
.........0.........0.........0.........0.........0.........0.........0.........0
y| a: ['0123456789ABC', '0123456789ABC']
y|
    aa:
        ['0123456789ABC',
         '0123456789ABC']
y| aa: ['0123456789ABC', '0123456789ABC']
""")