/
test_typogruby.rb
131 lines (109 loc) · 7.48 KB
/
test_typogruby.rb
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
# -*- encoding: utf-8 -*-
require 'minitest/autorun'
require File.expand_path('../../lib/typogruby', __FILE__)
class TypogrubyTest < Minitest::Test
include Typogruby
def test_should_replace_amps
assert_equal 'One <span class="amp">&</span> two', amp('One & two')
assert_equal 'One <span class="amp">&</span> two', amp('One & two')
assert_equal 'One <span class="amp">&</span> two', amp('One & two')
assert_equal 'One <span class="amp">&</span> two', amp('One & two')
end
def test_should_ignore_special_amps
assert_equal 'One <span class="amp">&</span> two', amp('One <span class="amp">&</span> two')
assert_equal '“this” <span class="amp">&</span> <a href="/?that&test">that</a>', amp('“this” & <a href="/?that&test">that</a>')
assert_equal %Q{<script>\nvar x = "FOO & BAR";\n</script>"}, initial_quotes(%Q{<script>\nvar x = "FOO & BAR";\n</script>"})
end
def test_should_ignore_standalone_amps_in_attributes
assert_equal '<link href="xyz.html" title="One & Two">xyz</link>', amp('<link href="xyz.html" title="One & Two">xyz</link>')
end
def test_should_replace_caps
assert_equal 'A message from <span class="caps">KU</span>', caps("A message from KU")
assert_equal 'Replace text <a href="."><span class="caps">IN</span></a> tags', caps('Replace text <a href=".">IN</a> tags')
assert_equal 'Replace text <i><span class="caps">IN</span></i> tags', caps('Replace text <i>IN</i> tags')
end
def test_should_ignore_special_case_caps
assert_equal 'It should ignore just numbers like 1234.', caps('It should ignore just numbers like 1234.')
assert_equal '<pre>CAPS</pre> more <span class="caps">CAPS</span>', caps("<pre>CAPS</pre> more CAPS")
assert_equal '<Pre>CAPS</PRE> with odd tag names <span class="caps">CAPS</span>', caps("<Pre>CAPS</PRE> with odd tag names CAPS")
assert_equal 'A message from <span class="caps">2KU2</span> with digits', caps("A message from 2KU2 with digits")
assert_equal 'Dotted caps followed by spaces should never include them in the wrap <span class="caps">D.O.T.</span> like so.', caps("Dotted caps followed by spaces should never include them in the wrap D.O.T. like so.")
assert_equal 'Caps in attributes (<span title="Example CAPS">example</span>) should be ignored', caps('Caps in attributes (<span title="Example CAPS">example</span>) should be ignored')
assert_equal '<head><title>CAPS Example</title></head>', caps('<head><title>CAPS Example</title></head>')
end
def test_should_not_break_caps_with_apostrophes
assert_equal '<span class="caps">JIMMY\'S</span>', caps("JIMMY'S")
assert_equal '<i><span class="caps">D.O.T.</span></i><span class="caps">HE34T</span><b><span class="caps">RFID</span></b>', caps("<i>D.O.T.</i>HE34T<b>RFID</b>")
end
def test_should_not_break_caps_with_ampersands
assert_equal '<span class="caps">AT&T</span>', caps("AT&T")
assert_equal '<span class="caps">AT&T</span>', caps("AT&T")
assert_equal '<span class="caps">AT&T</span>', caps("AT&T")
end
def test_should_replace_quotes
assert_equal '<span class="dquo">"</span>With primes"', initial_quotes('"With primes"')
assert_equal '<span class="quo">\'</span>With single primes\'', initial_quotes("'With single primes'")
assert_equal '<a href="#"><span class="dquo">"</span>With primes and a link"</a>', initial_quotes('<a href="#">"With primes and a link"</a>')
assert_equal '<span class="dquo">“</span>With smartypanted quotes”', initial_quotes('“With smartypanted quotes”')
assert_equal '<span class="quo">‘</span>With manual quotes’', initial_quotes('‘With manual quotes’')
assert_equal %Q{<script>\nvar x = "FOO BAR and BAZ";\n</script>"}, initial_quotes(%Q{<script>\nvar x = "FOO BAR and BAZ";\n</script>"})
end
def test_should_not_replace_quotes_in_scripts
assert_equal %Q{<script>\nvar x = "foo" +\n"bar";\n</script>"}, initial_quotes(%Q{<script>\nvar x = "foo" +\n"bar";\n</script>"})
end
def test_should_apply_smartypants
assert_equal 'The “Green” man', smartypants('The "Green" man')
end
def test_should_apply_all_filters
assert_equal '<h2><span class="dquo">“</span>Jäyhawks” <span class="amp">&</span> <span class="caps">KU</span> fans act extremely obnoxiously</h2>', improve('<h2>"Jäyhawks" & KU fans act extremely obnoxiously</h2>')
end
def test_should_prevent_widows
assert_equal 'A very simple test', widont('A very simple test')
end
def test_should_not_change_single_word_items
assert_equal 'Test', widont('Test')
assert_equal ' Test', widont(' Test')
assert_equal '<ul><li>Test</p></li><ul>', widont('<ul><li>Test</p></li><ul>')
assert_equal '<ul><li> Test</p></li><ul>', widont('<ul><li> Test</p></li><ul>')
assert_equal '<p>In a couple of paragraphs</p><p>paragraph two</p>', widont('<p>In a couple of paragraphs</p><p>paragraph two</p>')
assert_equal '<h1><a href="#">In a link inside a heading</i> </a></h1>', widont('<h1><a href="#">In a link inside a heading</i> </a></h1>')
assert_equal '<h1><a href="#">In a link</a> followed by other text</h1>', widont('<h1><a href="#">In a link</a> followed by other text</h1>')
end
def test_should_not_add_nbsp_before_another
assert_equal 'Sentence with one nbsp', widont('Sentence with one nbsp')
end
def test_should_not_error_on_empty_html
assert_equal '<h1><a href="#"></a></h1>', widont('<h1><a href="#"></a></h1>')
end
def test_should_ignore_caps_in_special_tags
assert_equal '<title>I like JSON</title>', caps('<title>I like JSON</title>')
end
def test_should_ignore_widows_in_special_tags
assert_equal '<div>Divs get no love!</div>', widont('<div>Divs get no love!</div>')
assert_equal '<pre>Neither do PREs</pre>', widont('<pre>Neither do PREs</pre>')
assert_equal '<title>or titles</title>', widont('<title>or titles</title>')
assert_equal '<textarea>nor text in textarea</textarea>', widont('<textarea>nor text in textarea</textarea>')
assert_equal "<script>\nreturn window;\n</script>", widont("<script>\nreturn window;\n</script>")
assert_equal '<div><p>But divs with paragraphs do!</p></div>', widont('<div><p>But divs with paragraphs do!</p></div>')
end
def test_should_ignore_attributes_across_mutliple_lines_when_preventing_widows
assert_equal %Q{<div id="aniver" style="\nbackground-image:url('/static/colorfulbackground.png');\nbackground-repeat:no-repeat;\ntext-align:center;\nmargin:0;\npadding:0;\nbackground-size: 100% 100%;">},
widont(%Q{<div id="aniver" style="\nbackground-image:url('/static/colorfulbackground.png');\nbackground-repeat:no-repeat;\ntext-align:center;\nmargin:0;\npadding:0;\nbackground-size: 100% 100%;">})
end
def test_should_convert_entities
assert_equal "Variëren…", entities('Variëren…')
assert_equal "<p>Olé</p>", entities('<p>Olé</p>')
end
def test_should_leave_sensitive_tags_alone
%w{script pre code kbd math}.each do |tag_name|
test_string = "<#{tag_name}>“CAPS & chårs” and widows</#{tag_name}>"
assert_equal test_string, improve(test_string)
end
end
def test_should_not_forget_about_duplicate_sensitive_tags
%w{script pre code kbd math}.each do |tag_name|
test_string = "<#{tag_name}>this</#{tag_name}>==<#{tag_name}>this</#{tag_name}>"
assert_equal test_string, improve(test_string)
end
end
end