Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / conch / test / test_text.py
1 # -*- test-case-name: twisted.conch.test.test_text -*-
2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
4
5 from twisted.trial import unittest
6
7 from twisted.conch.insults import helper, text
8
9 A = text.attributes
10
11 class Serialization(unittest.TestCase):
12     def setUp(self):
13         self.attrs = helper.CharacterAttribute()
14
15     def testTrivial(self):
16         self.assertEqual(
17             text.flatten(A.normal['Hello, world.'], self.attrs),
18             'Hello, world.')
19
20     def testBold(self):
21         self.assertEqual(
22             text.flatten(A.bold['Hello, world.'], self.attrs),
23             '\x1b[1mHello, world.')
24
25     def testUnderline(self):
26         self.assertEqual(
27             text.flatten(A.underline['Hello, world.'], self.attrs),
28             '\x1b[4mHello, world.')
29
30     def testBlink(self):
31         self.assertEqual(
32             text.flatten(A.blink['Hello, world.'], self.attrs),
33             '\x1b[5mHello, world.')
34
35     def testReverseVideo(self):
36         self.assertEqual(
37             text.flatten(A.reverseVideo['Hello, world.'], self.attrs),
38             '\x1b[7mHello, world.')
39
40     def testMinus(self):
41         self.assertEqual(
42             text.flatten(
43                 A.bold[A.blink['Hello', -A.bold[' world'], '.']],
44                 self.attrs),
45             '\x1b[1;5mHello\x1b[0;5m world\x1b[1;5m.')
46
47     def testForeground(self):
48         self.assertEqual(
49             text.flatten(
50                 A.normal[A.fg.red['Hello, '], A.fg.green['world!']],
51                 self.attrs),
52             '\x1b[31mHello, \x1b[32mworld!')
53
54     def testBackground(self):
55         self.assertEqual(
56             text.flatten(
57                 A.normal[A.bg.red['Hello, '], A.bg.green['world!']],
58                 self.attrs),
59             '\x1b[41mHello, \x1b[42mworld!')
60
61
62 class EfficiencyTestCase(unittest.TestCase):
63     todo = ("flatten() isn't quite stateful enough to avoid emitting a few extra bytes in "
64             "certain circumstances, so these tests fail.  The failures take the form of "
65             "additional elements in the ;-delimited character attribute lists.  For example, "
66             "\\x1b[0;31;46m might be emitted instead of \\x[46m, even if 31 has already been "
67             "activated and no conflicting attributes are set which need to be cleared.")
68
69     def setUp(self):
70         self.attrs = helper.CharacterAttribute()
71
72     def testComplexStructure(self):
73         output = A.normal[
74             A.bold[
75                 A.bg.cyan[
76                     A.fg.red[
77                         "Foreground Red, Background Cyan, Bold",
78                         A.blink[
79                             "Blinking"],
80                         -A.bold[
81                             "Foreground Red, Background Cyan, normal"]],
82                     A.fg.green[
83                         "Foreground Green, Background Cyan, Bold"]]]]
84
85         self.assertEqual(
86             text.flatten(output, self.attrs),
87             "\x1b[1;31;46mForeground Red, Background Cyan, Bold"
88             "\x1b[5mBlinking"
89             "\x1b[0;31;46mForeground Red, Background Cyan, normal"
90             "\x1b[1;32;46mForeground Green, Background Cyan, Bold")
91
92     def testNesting(self):
93         self.assertEqual(
94             text.flatten(A.bold['Hello, ', A.underline['world.']], self.attrs),
95             '\x1b[1mHello, \x1b[4mworld.')
96
97         self.assertEqual(
98             text.flatten(
99                 A.bold[A.reverseVideo['Hello, ', A.normal['world'], '.']],
100                 self.attrs),
101             '\x1b[1;7mHello, \x1b[0mworld\x1b[1;7m.')