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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
describe Rouge::Lexer do
include Support::Lexing
it 'guesses the lexer with Lexer.guess' do
assert { Rouge::Lexer.guess(filename: 'foo.rb').tag == 'ruby' }
end
it 'guesses lexers with Lexer.guesses' do
assert { Rouge::Lexer.guesses(filename: 'foo.pl').map { |c| c.tag }.sort == ['perl', 'prolog'].sort }
end
it 'raises errors in .guess by default' do
assert { (Rouge::Lexer.guess(filename: 'foo.pl') rescue nil) == nil }
end
it 'customizes ambiguous cases in .guess' do
assert { Rouge::Lexer.guess(filename: 'foo.pl') { :fallback } == :fallback }
end
it 'makes a simple lexer' do
a_lexer = Class.new(Rouge::RegexLexer) do
state :root do
rule %r/a/, 'A'
rule %r/b/, 'B'
end
end
# consolidation
result = a_lexer.lex('aa').to_a
assert { result.size == 1 }
assert { result == [['A', 'aa']] }
result = a_lexer.lex('abab').to_a
assert { result.size == 4 }
assert { result == [['A', 'a'], ['B', 'b']] * 2 }
end
it 'pushes and pops states' do
a_lexer = Class.new(Rouge::RegexLexer) do
state :brace do
rule %r/b/, 'B'
rule %r/}/, 'Brace', :pop!
end
state :root do
rule %r/{/, 'Brace', :brace
rule %r/a/, 'A'
end
end
result = a_lexer.lex('a{b}a').to_a
assert { result.size == 5 }
# failed parses
t = Rouge::Token
assert {
a_lexer.lex('{a}').to_a ==
[['Brace', '{'], [t['Error'], 'a'], ['Brace', '}']]
}
assert { a_lexer.lex('b').to_a == [[t['Error'], 'b']] }
assert { a_lexer.lex('}').to_a == [[t['Error'], '}']] }
end
it 'does callbacks and grouping' do
callback_lexer = Class.new(Rouge::RegexLexer) do
state :root do
rule %r/(a)(b)/ do |s|
groups('A', 'B')
end
end
end
result = callback_lexer.lex('ab').to_a
assert { result.size == 2 }
assert { result[0] == ['A', 'a'] }
assert { result[1] == ['B', 'b'] }
end
it 'pops from the callback' do
callback_lexer = Class.new(Rouge::RegexLexer) do
state :root do
rule %r/a/, 'A', :a
rule %r/d/, 'D'
end
state :a do
rule %r/b/, 'B', :b
end
state :b do
rule %r/c/ do |ss|
token 'C'
pop!; pop! # go back to the root
end
end
end
assert_no_errors 'abcd', callback_lexer
end
it 'supports stateful lexes' do
stateful = Class.new(Rouge::RegexLexer) do
def incr
@count += 1
end
state :root do
rule %r/\d+/ do |ss|
token 'digit'
@count = ss[0].to_i
end
rule %r/\+/ do |ss|
incr
token(@count <= 5 ? 'lt' : 'gt')
end
end
end
result = stateful.lex('4++')
types = result.map { |(t,_)| t }
assert { types == %w(digit lt gt) }
end
it 'supports multiple states' do
lexer = Class.new(Rouge::RegexLexer) do
state :root do
rule %r/\w+/, 'text'
rule %r/(=)(\s*)/ do
groups 'operator', 'whitespace'
push :value
end
end
state :value do
rule %r/\d+/, 'digit', :pop!
rule %r/\[/, 'punctuation', [:pop!, :array]
end
state :array do
rule %r/\]/, 'punctuation', :pop!
rule %r//, 'token', :value
end
end
result = lexer.lex('numbers=[1]')
types = result.map { |(t,_)| t }
assert { types == %w(text operator punctuation digit punctuation) }
end
it 'delegates' do
class MasterLexer < Rouge::RegexLexer
state :root do
rule %r/a/, 'A'
rule %r/{(.*?)}/ do |m|
token 'brace', '{'
delegate BracesLexer.new, m[1]
token 'brace', '}'
end
end
end
class BracesLexer < Rouge::RegexLexer
state :root do
rule %r/b/, 'B'
end
end
assert_no_errors 'a{b}a', MasterLexer
end
it 'detects the beginnings of lines with ^ rules' do
class MyLexer < Rouge::RegexLexer
state :root do
rule %r/^a/, 'start'
rule %r/a/, 'not-start'
end
end
assert_has_token('start', 'a', MyLexer)
assert_has_token('start', "\na", MyLexer)
deny_has_token('not-start', 'a', MyLexer)
assert_has_token('not-start', 'aa', MyLexer)
end
it 'is undetectable by default' do
UndetectableLexer = Class.new(Rouge::Lexer)
refute { UndetectableLexer.methods(false).include?(:detect?) }
refute { UndetectableLexer.detectable? }
end
it 'can only be detectable within current scope' do
class DetectableLexer < Rouge::Lexer
def self.detect?
text.shebang?('foobar')
end
end
assert { DetectableLexer.methods(false).include?(:detect?) }
assert { DetectableLexer.detectable? }
NonDetectableLexer = Class.new(DetectableLexer)
refute { NonDetectableLexer.methods(false).include?(:detect?) }
refute { NonDetectableLexer.detectable? }
end
it 'handles boolean options' do
option_lexer = Class.new(Rouge::RegexLexer) do
option :bool_opt, 'An example boolean option'
def initialize(*)
super
@bool_opt = bool_option(:bool_opt) { nil }
end
end
assert_equal true, option_lexer.new({bool_opt: 'true'}).instance_variable_get(:@bool_opt)
assert_equal false, option_lexer.new({bool_opt: nil}).instance_variable_get(:@bool_opt)
assert_equal false, option_lexer.new({bool_opt: false}).instance_variable_get(:@bool_opt)
assert_equal false, option_lexer.new({bool_opt: 0}).instance_variable_get(:@bool_opt)
assert_equal false, option_lexer.new({bool_opt: '0'}).instance_variable_get(:@bool_opt)
assert_equal false, option_lexer.new({bool_opt: 'false'}).instance_variable_get(:@bool_opt)
assert_equal false, option_lexer.new({bool_opt: 'off'}).instance_variable_get(:@bool_opt)
end
it 'extends options with #with' do
php = Rouge::Lexers::PHP.new
assert { php.instance_variable_get(:@start_inline) == :guess }
inline_php = php.with(start_inline: true)
assert { inline_php.is_a?(Rouge::Lexers::PHP) }
assert { inline_php != php }
assert { php.instance_variable_get(:@start_inline) == :guess }
assert { inline_php.instance_variable_get(:@start_inline) == true }
end
end
|