| 1 |
# encoding: utf-8 |
| 2 |
"""Tests for IPython.utils.text""" |
| 3 |
|
| 4 |
# ----------------------------------------------------------------------------- |
| 5 |
# Copyright (C) 2011 The IPython Development Team |
| 6 |
# |
| 7 |
# Distributed under the terms of the BSD License. The full license is in |
| 8 |
# the file COPYING, distributed as part of this software. |
| 9 |
# ----------------------------------------------------------------------------- |
| 10 |
|
| 11 |
# ----------------------------------------------------------------------------- |
| 12 |
# Imports |
| 13 |
# ----------------------------------------------------------------------------- |
| 14 |
|
| 15 |
import os |
| 16 |
import sys |
| 17 |
import math |
| 18 |
import random |
| 19 |
|
| 20 |
from pathlib import Path |
| 21 |
|
| 22 |
import pytest |
| 23 |
|
| 24 |
from IPython.utils import text |
| 25 |
|
| 26 |
# ----------------------------------------------------------------------------- |
| 27 |
# Globals |
| 28 |
# ----------------------------------------------------------------------------- |
| 29 |
|
| 30 |
|
| 31 |
def eval_formatter_list_comprehension_check(f): |
| 32 |
ns = dict(n=12) |
| 33 |
s = f.format("{[n//i for i in range(1,8)]}", **ns) |
| 34 |
assert s == "[12, 6, 4, 3, 2, 2, 1]" |
| 35 |
|
| 36 |
|
| 37 |
def eval_formatter_check(f): |
| 38 |
ns = dict(n=12, pi=math.pi, stuff="hello there", os=os, u="café", b="café") |
| 39 |
s = f.format("{n} {n//4} {stuff.split()[0]}", **ns) |
| 40 |
assert s == "12 3 hello" |
| 41 |
s = f.format(" ".join(["{n//%i}" % i for i in range(1, 8)]), **ns) |
| 42 |
assert s == "12 6 4 3 2 2 1" |
| 43 |
s = f.format("{list(n//i for i in range(1,8))}", **ns) |
| 44 |
assert s == "[12, 6, 4, 3, 2, 2, 1]" |
| 45 |
s = f.format("{stuff!s}", **ns) |
| 46 |
assert s == ns["stuff"] |
| 47 |
s = f.format("{stuff!r}", **ns) |
| 48 |
assert s == repr(ns["stuff"]) |
| 49 |
|
| 50 |
# Check with unicode: |
| 51 |
s = f.format("{u}", **ns) |
| 52 |
assert s == ns["u"] |
| 53 |
# This decodes in a platform dependent manner, but it shouldn't error out |
| 54 |
s = f.format("{b}", **ns) |
| 55 |
|
| 56 |
pytest.raises(NameError, f.format, "{dne}", **ns) |
| 57 |
|
| 58 |
|
| 59 |
def eval_formatter_slicing_check(f): |
| 60 |
ns = dict(n=12, pi=math.pi, stuff="hello there", os=os) |
| 61 |
s = f.format(" {stuff.split()[:]} ", **ns) |
| 62 |
assert s == " ['hello', 'there'] " |
| 63 |
s = f.format(" {stuff.split()[::-1]} ", **ns) |
| 64 |
assert s == " ['there', 'hello'] " |
| 65 |
s = f.format("{stuff[::2]}", **ns) |
| 66 |
assert s == ns["stuff"][::2] |
| 67 |
|
| 68 |
pytest.raises(SyntaxError, f.format, "{n:x}", **ns) |
| 69 |
|
| 70 |
|
| 71 |
def eval_formatter_no_slicing_check(f): |
| 72 |
ns = dict(n=12, pi=math.pi, stuff="hello there", os=os) |
| 73 |
|
| 74 |
s = f.format("{n:x} {pi**2:+f}", **ns) |
| 75 |
assert s == "c +9.869604" |
| 76 |
|
| 77 |
s = f.format("{stuff[slice(1,4)]}", **ns) |
| 78 |
assert s == "ell" |
| 79 |
|
| 80 |
s = f.format("{a[:]}", a=[1, 2]) |
| 81 |
assert s == "[1, 2]" |
| 82 |
|
| 83 |
|
| 84 |
def test_eval_formatter(): |
| 85 |
f = text.EvalFormatter() |
| 86 |
eval_formatter_check(f) |
| 87 |
eval_formatter_no_slicing_check(f) |
| 88 |
if sys.version_info < (3, 14): |
| 89 |
eval_formatter_list_comprehension_check(f) |
| 90 |
|
| 91 |
|
| 92 |
def test_full_eval_formatter(): |
| 93 |
f = text.FullEvalFormatter() |
| 94 |
eval_formatter_check(f) |
| 95 |
eval_formatter_slicing_check(f) |
| 96 |
eval_formatter_list_comprehension_check(f) |
| 97 |
|
| 98 |
|
| 99 |
def test_dollar_formatter(): |
| 100 |
f = text.DollarFormatter() |
| 101 |
eval_formatter_check(f) |
| 102 |
eval_formatter_slicing_check(f) |
| 103 |
|
| 104 |
ns = dict(n=12, pi=math.pi, stuff="hello there", os=os) |
| 105 |
s = f.format("$n", **ns) |
| 106 |
assert s == "12" |
| 107 |
s = f.format("$n.real", **ns) |
| 108 |
assert s == "12" |
| 109 |
s = f.format("$n/{stuff[:5]}", **ns) |
| 110 |
assert s == "12/hello" |
| 111 |
s = f.format("$n $$HOME", **ns) |
| 112 |
assert s == "12 $HOME" |
| 113 |
s = f.format("${foo}", foo="HOME") |
| 114 |
assert s == "$HOME" |
| 115 |
|
| 116 |
|
| 117 |
def test_strip_email(): |
| 118 |
src = """\ |
| 119 |
>> >>> def f(x): |
| 120 |
>> ... return x+1 |
| 121 |
>> ... |
| 122 |
>> >>> zz = f(2.5)""" |
| 123 |
cln = """\ |
| 124 |
>>> def f(x): |
| 125 |
... return x+1 |
| 126 |
... |
| 127 |
>>> zz = f(2.5)""" |
| 128 |
assert text.strip_email_quotes(src) == cln |
| 129 |
|
| 130 |
|
| 131 |
def test_strip_email2(): |
| 132 |
src = "> > > list()" |
| 133 |
cln = "list()" |
| 134 |
assert text.strip_email_quotes(src) == cln |
| 135 |
|
| 136 |
|
| 137 |
def test_LSString(): |
| 138 |
lss = text.LSString("abc\ndef") |
| 139 |
assert lss.l == ["abc", "def"] |
| 140 |
assert lss.s == "abc def" |
| 141 |
lss = text.LSString(os.getcwd()) |
| 142 |
assert isinstance(lss.p[0], Path) |
| 143 |
|
| 144 |
|
| 145 |
def test_SList(): |
| 146 |
sl = text.SList(["a 11", "b 1", "a 2"]) |
| 147 |
assert sl.n == "a 11\nb 1\na 2" |
| 148 |
assert sl.s == "a 11 b 1 a 2" |
| 149 |
assert sl.grep(lambda x: x.startswith("a")) == text.SList(["a 11", "a 2"]) |
| 150 |
assert sl.fields(0) == text.SList(["a", "b", "a"]) |
| 151 |
assert sl.sort(field=1, nums=True) == text.SList(["b 1", "a 2", "a 11"]) |