forked from pygments/pygments.rb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pygments.rb
142 lines (125 loc) · 4.04 KB
/
test_pygments.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
132
133
134
135
136
137
138
139
140
141
142
# coding: utf-8
require 'test/unit'
require 'pygments'
P = Pygments
class PygmentsLexerTest < Test::Unit::TestCase
RUBY_CODE = "#!/usr/bin/ruby\nputs 'foo'"
def test_lexer_by_content
assert_equal 'rb', P.lexer_name_for(RUBY_CODE)
end
def test_lexer_by_mimetype
assert_equal 'rb', P.lexer_name_for(:mimetype => 'text/x-ruby')
end
def test_lexer_by_filename
assert_equal 'rb', P.lexer_name_for(:filename => 'test.rb')
end
def test_lexer_by_name
assert_equal 'rb', P.lexer_name_for(:lexer => 'ruby')
end
def test_lexer_by_filename_and_content
assert_equal 'rb', P.lexer_name_for(RUBY_CODE, :filename => 'test.rb')
end
def test_lexer_by_nothing
assert_equal nil, P.lexer_name_for(:invalid => true)
end
end
class PygmentsLexerClassTest < Test::Unit::TestCase
def test_find
assert_equal 'Ruby', P::Lexer['Ruby'].name
assert_equal 'Ruby', P::Lexer['ruby'].name
assert_equal 'Ruby', P::Lexer['rb'].name
assert_equal 'Ruby', P::Lexer['rake'].name
assert_equal 'Ruby', P::Lexer['gemspec'].name
end
def test_find_by_name
assert_equal P::Lexer['Ruby'], P::Lexer.find_by_name('Ruby')
end
def test_find_by_alias
assert_equal P::Lexer['Ruby'], P::Lexer.find_by_alias('rb')
assert_equal P::Lexer['Ruby'], P::Lexer.find_by_alias('ruby')
end
def test_find_lexer_by_extname
assert_equal P::Lexer['Ruby'], P::Lexer.find_by_extname('.rb')
assert_equal P::Lexer['PHP'], P::Lexer.find_by_extname('.php4')
assert_equal P::Lexer['PHP'], P::Lexer.find_by_extname('.php5')
assert_equal P::Lexer['Groff'], P::Lexer.find_by_extname('.1')
assert_equal P::Lexer['Groff'], P::Lexer.find_by_extname('.3')
end
def test_find_lexer_by_mimetype
assert_equal P::Lexer['Ruby'], P::Lexer.find_by_mimetype('text/x-ruby')
end
end
class PygmentsCssTest < Test::Unit::TestCase
include Pygments
def test_css
assert_match /^\.err \{/, P.css
end
def test_css_prefix
assert_match /^\.highlight \.err \{/, P.css('.highlight')
end
def test_css_options
assert_match /^\.codeerr \{/, P.css(:classprefix => 'code')
end
def test_css_prefix_and_options
assert_match /^\.mycode \.codeerr \{/, P.css('.mycode', :classprefix => 'code')
end
def test_css_default
assert_match '.c { color: #408080; font-style: italic }', P.css
end
def test_css_colorful
assert_match '.c { color: #888888 }', css(:style => 'colorful')
end
end
class PygmentsConfigTest < Test::Unit::TestCase
def test_styles
assert P.styles.include?('colorful')
end
def test_filters
assert P.filters.include?('codetagify')
end
def test_lexers
list = P.lexers
assert list.has_key?('Ruby')
assert list['Ruby'][:aliases].include?('duby')
end
def test_formatters
list = P.formatters
assert list.has_key?('Html')
assert list['Html'][:aliases].include?('html')
end
end
class PygmentsHighlightTest < Test::Unit::TestCase
RUBY_CODE = "#!/usr/bin/ruby\nputs 'foo'"
def test_highlight_empty
P.highlight('')
P.highlight(nil)
end
def test_highlight_defaults_to_html
code = P.highlight(RUBY_CODE)
assert_match '<span class="c1">#!/usr/bin/ruby</span>', code
end
def test_highlight_markdown_compatible_html
code = P.highlight(RUBY_CODE)
assert_no_match %r{</pre></div>\Z}, code
end
def test_highlight_works_with_null_bytes
code = P.highlight("\0hello", :lexer => 'rb')
assert_match "hello", code
end
def test_highlight_works_on_utf8
code = P.highlight('# ø', :lexer => 'rb', :options => {:encoding => 'utf-8'})
assert_match '<span class="c1"># ø</span>', code
end
def test_highlight_formatter_bbcode
code = P.highlight(RUBY_CODE, :formatter => 'bbcode')
assert_match '[i]#!/usr/bin/ruby[/i]', code
end
def test_highlight_formatter_terminal
code = P.highlight(RUBY_CODE, :formatter => 'terminal')
assert_match "\e[37m#!/usr/bin/ruby\e[39;49;00m", code
end
def test_highlight_options
code = P.highlight(RUBY_CODE, :options => {:full => true, :title => 'test'})
assert_match '<title>test</title>', code
end
end