##// END OF EJS Templates
Sort out the different implementations of EvalFormatter, so we usually use the simple one.
Thomas Kluyver -
Show More
@@ -1,113 +1,115 b''
1 1 # encoding: utf-8
2 2 """Tests for IPython.utils.text"""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2011 The IPython Development Team
6 6 #
7 7 # Distributed under the terms of the BSD License. The full license is in
8 8 # the file COPYING, distributed as part of this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 15 import os
16 16 import math
17 17
18 18 import nose.tools as nt
19 19
20 20 from nose import with_setup
21 21
22 22 from IPython.testing import decorators as dec
23 23 from IPython.utils import text
24 24
25 25 #-----------------------------------------------------------------------------
26 26 # Globals
27 27 #-----------------------------------------------------------------------------
28 28
29 29 def test_columnize():
30 30 """Basic columnize tests."""
31 31 size = 5
32 32 items = [l*size for l in 'abc']
33 33 out = text.columnize(items, displaywidth=80)
34 34 nt.assert_equals(out, 'aaaaa bbbbb ccccc\n')
35 35 out = text.columnize(items, displaywidth=10)
36 36 nt.assert_equals(out, 'aaaaa ccccc\nbbbbb\n')
37 37
38 38
39 39 def test_columnize_long():
40 40 """Test columnize with inputs longer than the display window"""
41 41 text.columnize(['a'*81, 'b'*81], displaywidth=80)
42 42 size = 11
43 43 items = [l*size for l in 'abc']
44 44 out = text.columnize(items, displaywidth=size-1)
45 45 nt.assert_equals(out, '\n'.join(items+['']))
46 46
47 47 def eval_formatter_check(f):
48 48 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os, u=u"cafΓ©", b="cafΓ©")
49 49 s = f.format("{n} {n//4} {stuff.split()[0]}", **ns)
50 50 nt.assert_equals(s, "12 3 hello")
51 51 s = f.format(' '.join(['{n//%i}'%i for i in range(1,8)]), **ns)
52 52 nt.assert_equals(s, "12 6 4 3 2 2 1")
53 53 s = f.format('{[n//i for i in range(1,8)]}', **ns)
54 54 nt.assert_equals(s, "[12, 6, 4, 3, 2, 2, 1]")
55 55 s = f.format("{stuff!s}", **ns)
56 56 nt.assert_equals(s, ns['stuff'])
57 57 s = f.format("{stuff!r}", **ns)
58 58 nt.assert_equals(s, repr(ns['stuff']))
59 59
60 60 # Check with unicode:
61 61 s = f.format("{u}", **ns)
62 62 nt.assert_equals(s, ns['u'])
63 63 # This decodes in a platform dependent manner, but it shouldn't error out
64 64 s = f.format("{b}", **ns)
65 65
66 66 nt.assert_raises(NameError, f.format, '{dne}', **ns)
67 67
68 68 def eval_formatter_slicing_check(f):
69 69 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
70 70 s = f.format(" {stuff.split()[:]} ", **ns)
71 71 nt.assert_equals(s, " ['hello', 'there'] ")
72 72 s = f.format(" {stuff.split()[::-1]} ", **ns)
73 73 nt.assert_equals(s, " ['there', 'hello'] ")
74 74 s = f.format("{stuff[::2]}", **ns)
75 75 nt.assert_equals(s, ns['stuff'][::2])
76 76
77 77 nt.assert_raises(SyntaxError, f.format, "{n:x}", **ns)
78
79 78
80 79 def eval_formatter_no_slicing_check(f):
81 80 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
82 81
83 82 s = f.format('{n:x} {pi**2:+f}', **ns)
84 83 nt.assert_equals(s, "c +9.869604")
85 84
85 s = f.format('{stuff[slice(1,4)]}', **ns)
86 nt.assert_equals(s, 'ell')
87
86 88 nt.assert_raises(SyntaxError, f.format, "{a[:]}")
87 89
88 90 def test_eval_formatter():
89 91 f = text.EvalFormatter()
90 92 eval_formatter_check(f)
91 93 eval_formatter_no_slicing_check(f)
92 94
93 95 def test_full_eval_formatter():
94 96 f = text.FullEvalFormatter()
95 97 eval_formatter_check(f)
96 98 eval_formatter_slicing_check(f)
97 99
98 100 def test_dollar_formatter():
99 101 f = text.DollarFormatter()
100 102 eval_formatter_check(f)
101 103 eval_formatter_slicing_check(f)
102 104
103 105 ns = dict(n=12, pi=math.pi, stuff='hello there', os=os)
104 106 s = f.format("$n", **ns)
105 107 nt.assert_equals(s, "12")
106 108 s = f.format("$n.real", **ns)
107 109 nt.assert_equals(s, "12")
108 110 s = f.format("$n/{stuff[:5]}", **ns)
109 111 nt.assert_equals(s, "12/hello")
110 112 s = f.format("$n $$HOME", **ns)
111 113 nt.assert_equals(s, "12 $HOME")
112 114 s = f.format("${foo}", foo="HOME")
113 115 nt.assert_equals(s, "$HOME")
General Comments 0
You need to be logged in to leave comments. Login now