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