Refactor tokenizer

This commit is contained in:
Vitalii Elenhaupt 2017-11-04 17:38:04 +02:00
parent fedc29ceb6
commit e383ec17c2
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
3 changed files with 45 additions and 47 deletions

View file

@ -1,17 +1,17 @@
require "../../spec_helper"
private def it_transforms(number, expected)
it "transforms large number #{number}" do
s = Ameba::Source.new number
Ameba::Rules::LargeNumbers.new.catch(s).should_not be_valid
s.errors.first.message.should contain expected
module Ameba
subject = Rules::LargeNumbers.new
private def it_transforms(number, expected)
it "transforms large number #{number}" do
s = Source.new number
Rules::LargeNumbers.new.catch(s).should_not be_valid
s.errors.first.message.should contain expected
end
end
end
module Ameba::Rules
subject = LargeNumbers.new
describe LargeNumbers do
describe Rules::LargeNumbers do
it "passes if large number does not require underscore" do
s = Source.new %q(
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

View file

@ -1,42 +1,44 @@
require "../spec_helper"
private def it_tokenizes(str, expected)
it "tokenizes #{str}" do
([] of Symbol).tap do |token_types|
Ameba::Tokenizer.new(Ameba::Source.new str).run do |token|
token_types << token.type
end.should be_true
end.should eq expected
end
end
module Ameba
private def it_tokenizes(str, expected)
it "tokenizes #{str}" do
([] of Symbol).tap do |token_types|
Tokenizer.new(Source.new str)
.run { |token| token_types << token.type }
.should be_true
end.should eq expected
end
end
describe Tokenizer do
describe "#run" do
it_tokenizes %("string"), %i(STRING)
it_tokenizes %(100), %i(NUMBER)
it_tokenizes %('a'), %i(CHAR)
it_tokenizes %([]), %i([])
it_tokenizes %([] of String), %i([] SPACE IDENT SPACE CONST)
it_tokenizes %q("str #{3}"), %i(STRING NUMBER)
it_tokenizes %("string"), %i(DELIMITER_START STRING DELIMITER_END EOF)
it_tokenizes %(100), %i(NUMBER EOF)
it_tokenizes %('a'), %i(CHAR EOF)
it_tokenizes %([]), %i([] EOF)
it_tokenizes %([] of String), %i([] SPACE IDENT SPACE CONST EOF)
it_tokenizes %q("str #{3}"), %i(
DELIMITER_START STRING INTERPOLATION_START NUMBER } DELIMITER_END EOF
)
it_tokenizes %(%w(1 2)),
%i(STRING_ARRAY_START STRING STRING STRING_ARRAY_END)
%i(STRING_ARRAY_START STRING STRING STRING_ARRAY_END EOF)
it_tokenizes %(%i(one two)),
%i(SYMBOL_ARRAY_START STRING STRING STRING_ARRAY_END)
%i(SYMBOL_ARRAY_START STRING STRING STRING_ARRAY_END EOF)
it_tokenizes %(
class A
def method
puts "hello"
class A
def method
puts "hello"
end
end
end
), [
:NEWLINE, :SPACE, :IDENT, :SPACE, :CONST, :NEWLINE, :SPACE, :IDENT,
:SPACE, :IDENT, :NEWLINE, :SPACE, :IDENT, :SPACE, :STRING, :NEWLINE,
:SPACE, :IDENT, :NEWLINE, :SPACE, :IDENT, :NEWLINE, :SPACE,
]
), %i(
NEWLINE SPACE IDENT SPACE CONST NEWLINE SPACE IDENT SPACE IDENT
NEWLINE SPACE IDENT SPACE DELIMITER_START STRING DELIMITER_END
NEWLINE SPACE IDENT NEWLINE SPACE IDENT NEWLINE SPACE EOF
)
end
end
end