summaryrefslogtreecommitdiff
path: root/tests/test.py
blob: 6b2c5daad3b2d690ae76d00650c1aa95168a859e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
# Copyright (c) 2013 Yu-Jie Lin
# Licensed under the BSD License, for detailed license information, see COPYING

import doctest
import unittest

import smartypants
from smartypants import Attr, smartypants as sp


class SmartyPantsTestCase(unittest.TestCase):

    def test_change_default_attr(self):

        TEXT = '"foo" -- bar'

        attr = Attr.q
        Attr.default = attr
        self.assertEqual(Attr.default, attr)

        T = sp(TEXT)
        E = '“foo” -- bar'
        self.assertEquals(T, E)

        attr = Attr.q | Attr.d
        Attr.default = attr
        self.assertEqual(Attr.default, attr)

        T = sp(TEXT)
        E = '“foo” — bar'
        self.assertEquals(T, E)

    def test_dates(self):

        self.assertEqual(sp("1440-80's"), "1440-80’s")
        self.assertEqual(sp("1440-'80s"), "1440-‘80s")
        self.assertEqual(sp("1440--'80s"), "1440—‘80s")
        self.assertEqual(sp("1960s"), "1960s")  # no effect.
        self.assertEqual(sp("1960's"), "1960’s")
        self.assertEqual(sp("one two '60s"), "one two ‘60s")
        self.assertEqual(sp("'60s"), "‘60s")

    def test_skip_tags(self):

        T = sp('''<script type="text/javascript">
<!--
var href = "http://www.google.com";
var linktext = "google";
document.write('<a href="' + href + '">' + linktext + "</a>");
//-->
</script>''')
        E = '''<script type="text/javascript">
<!--
var href = "http://www.google.com";
var linktext = "google";
document.write('<a href="' + href + '">' + linktext + "</a>");
//-->
</script>'''
        self.assertEqual(T, E)

    def test_convert_quot(self):

        TEXT = (
            "<p>He said &quot;Let's write some code.&quot; "
            "This code here <code>if True:\n\tprint &quot;Okay&quot;</code> "
            "is python code.</p>")

        T = sp(TEXT)
        E = ("<p>He said &quot;Let&#8217;s write some code.&quot; "
             "This code here <code>if True:\n\tprint &quot;Okay&quot;</code> "
             "is python code.</p>")
        self.assertEqual(T, E)

        T = sp(TEXT, Attr.w)
        E = ('<p>He said "Let\'s write some code." '
             "This code here <code>if True:\n\tprint &quot;Okay&quot;</code> "
             "is python code.</p>")
        self.assertEqual(T, E)

        T = sp(TEXT, Attr.q | Attr.w)
        E = ("<p>He said &#8220;Let&#8217;s write some code.&#8221; "
             "This code here <code>if True:\n\tprint &quot;Okay&quot;</code> "
             "is python code.</p>")
        self.assertEqual(T, E)

    def test_comments(self):

        self.assertEqual(sp("--"), "&#8212;")
        self.assertEqual(sp("-->"), "&#8212;>")
        self.assertEqual(sp("-- \t  >"), "&#8212; \t  >")

        TEXT = '<!-- "foo" --> blah--blah <!-- "bar" -->'
        T = sp(TEXT)
        E = '<!-- "foo" --> blah&#8212;blah <!-- "bar" -->'
        self.assertEqual(T, E)

        TEXT = (
            '<p>foo -- "bar"<!-- foo-bar\n'
            '<p>blah "this"</p>\n'
            '-->\n'
            '</p>'
        )

        T = sp(TEXT)
        E = (
            '<p>foo &#8212; &#8220;bar&#8221;<!-- foo-bar\n'
            '<p>blah "this"</p>\n'
            '-->\n'
            '</p>'
        )
        self.assertEqual(T, E)

        # nothing should be converted
        for TEXT in ('<!-- comment -->',
                     '<!-- <li>Fee-fi-of-fum</li> -->',
                     '<!-- "foo" --> <!-- "bar" -->'):
            self.assertEqual(sp(TEXT), TEXT)

        # not comments
        self.assertEqual(sp('<!-- -- -->'), '<!&#8212; &#8212; &#8212;>')
        self.assertEqual(sp('<!-- -- -- \t >'),
                         '<!&#8212; &#8212; &#8212; \t >')

    def test_ordinal_numbers(self):

        self.assertEqual(sp("21st century"), "21st century")  # no effect.
        self.assertEqual(sp("3rd"), "3rd")  # no effect.

    def test_educated_quotes(self):

        self.assertEqual(sp('"Isn\'t this fun?"'),
                         '&#8220;Isn&#8217;t this fun?&#8221;')


def load_tests(loader, tests, pattern):

    tests.addTests(doctest.DocTestSuite(smartypants))
    return tests