spec/lib/hotcell/lexer_spec.rb in hotcell-0.0.1 vs spec/lib/hotcell/lexer_spec.rb in hotcell-0.1.0

- old
+ new

@@ -1,11 +1,11 @@ # encoding: UTF-8 require 'spec_helper' describe Hotcell::Lexer do def scan template - described_class.new(template).tokens + described_class.new(template).tokens.map { |token| [token[0], token[1][0]] } end def expression template scan("{{ #{template} }}")[1..-2] end @@ -63,20 +63,22 @@ end context 'numeric' do context 'integer' do specify { expression('42').should == [[:INTEGER, 42]] } - specify { expression('-42').should == [[:MINUS, '-'], [:INTEGER, 42]] } + specify { expression('-42').should == [[:INTEGER, -42]] } + specify { expression('- 42').should == [[:MINUS, '-'], [:INTEGER, 42]] } specify { expression('!42').should == [[:NOT, '!'], [:INTEGER, 42]] } specify { expression('42.').should == [[:INTEGER, 42], [:PERIOD, '.']] } specify { expression('42.foo').should == [[:INTEGER, 42], [:PERIOD, '.'], [:IDENTIFER, 'foo']] } specify { expression('42foo').should == [[:INTEGER, 42], [:IDENTIFER, 'foo']] } end context 'float' do specify { expression('42.42').should == [[:FLOAT, 42.42]] } - specify { expression('-42.42').should == [[:MINUS, '-'], [:FLOAT, 42.42]] } + specify { expression('-42.42').should == [[:FLOAT, -42.42]] } + specify { expression('- 42.42').should == [[:MINUS, '-'], [:FLOAT, 42.42]] } specify { expression('!42.42').should == [[:NOT, '!'], [:FLOAT, 42.42]] } specify { expression('0.42').should == [[:FLOAT, 0.42]] } specify { expression('.42').should == [[:FLOAT, 0.42]] } specify { expression('.42.').should == [[:FLOAT, 0.42], [:PERIOD, '.']] } specify { expression('.42.foo').should == [[:FLOAT, 0.42], [:PERIOD, '.'], [:IDENTIFER, 'foo']] } @@ -144,11 +146,12 @@ specify { expression(%q{"fo\'o"}).should == [[:STRING, "fo\'o"]] } specify { expression(%q{"fo\no"}).should == [[:STRING, "fo\no"]] } specify { expression(%q{"fo\mo"}).should == [[:STRING, "fo\mo"]] } specify { expression(%q{"foo42"}).should == [[:STRING, "foo42"]] } specify { expression(%q{"привет"}).should == [[:STRING, "привет"]] } - specify { expression(%q{"при\вет"}).should == [[:STRING, "при\вет"]] } + # RBX can not handle this + # specify { expression(%q{"при\вет"}).should == [[:STRING, "при\вет"]] } context do let(:strings) { data 'dstrings' } specify { expression(strings).delete_if { |token| token.first == :NEWLINE }.should == [ @@ -166,12 +169,15 @@ specify { expression('/regexp/i').should == [[:REGEXP, /regexp/i]] } specify { expression('/regexp/m').should == [[:REGEXP, /regexp/m]] } specify { expression('/regexp/x').should == [[:REGEXP, /regexp/x]] } specify { expression('/regexp/sdmfri').should == [[:REGEXP, /regexp/im]] } specify { expression('/\.*/').should == [[:REGEXP, /\.*/]] } - specify { expression('/\//').should == [[:REGEXP, /\//]] } - specify { expression('/\//ix').should == [[:REGEXP, /\//ix]] } + # Funny ruby 2.0 bug. regexp1.to_s == regexp2.to_s, but regexp1 != regexp2 + # specify { expression('/\//').should == [[:REGEXP, /\//]] } + # specify { expression('/\//ix').should == [[:REGEXP, /\//ix]] } + specify { expression('/\//').to_s.should == [[:REGEXP, /\//]].to_s } + specify { expression('/\//ix').to_s.should == [[:REGEXP, /\//ix]].to_s } specify { expression('/регексп/').should == [[:REGEXP, /регексп/]] } specify { expression('/регексп/m').should == [[:REGEXP, /регексп/m]] } context 'ambiguity' do specify { expression('hello /regexp/').should == [ @@ -217,36 +223,36 @@ specify { scan("{{ #hel{#\n#lo#}}").should == [[:TOPEN, "{{"], [:COMMENT, "#hel{#"], [:NEWLINE, "\n"], [:COMMENT, "#lo#"], [:TCLOSE, "}}"]] } end context 'errors' do - describe Hotcell::Errors::UnexpectedSymbol do - let(:error) { Hotcell::Errors::UnexpectedSymbol } + describe Hotcell::UnexpectedSymbol do + let(:error) { Hotcell::UnexpectedSymbol } - specify { expect { expression("hello @world") }.to raise_error(error, /1:10/) } - specify { expect { expression("@hello world") }.to raise_error(error, /1:4/) } - specify { expect { expression("hello world@") }.to raise_error(error, /1:15/) } - specify { expect { expression("hello\n@ world") }.to raise_error(error, /2:1/) } - specify { expect { expression("hello\n @world") }.to raise_error(error, /2:2/) } - specify { expect { expression("hello\n world@") }.to raise_error(error, /2:7/) } - specify { expect { expression("hello@\n world") }.to raise_error(error, /1:9/) } - specify { expect { expression("@hello\n world") }.to raise_error(error, /1:4/) } - specify { expect { expression("'привет' @ 'мир'") }.to raise_error(error, /1:13/) } + specify { expect { expression("hello @world") }.to raise_error(error, /`@`.*1:10/) } + specify { expect { expression("@hello world") }.to raise_error(error, /`@`.*1:4/) } + specify { expect { expression("hello world@") }.to raise_error(error, /`@`.*1:15/) } + specify { expect { expression("hello\n@ world") }.to raise_error(error, /`@`.*2:1/) } + specify { expect { expression("hello\n @world") }.to raise_error(error, /`@`.*2:2/) } + specify { expect { expression("hello\n world@") }.to raise_error(error, /`@`.*2:7/) } + specify { expect { expression("hello@\n world") }.to raise_error(error, /`@`.*1:9/) } + specify { expect { expression("@hello\n world") }.to raise_error(error, /`@`.*1:4/) } + specify { expect { expression("'привет' @ 'мир'") }.to raise_error(error, /`@`.*1:13/) } end - describe Hotcell::Errors::UnterminatedString do - let(:error) { Hotcell::Errors::UnterminatedString } + describe Hotcell::UnterminatedString do + let(:error) { Hotcell::UnterminatedString } - specify { expect { expression("hello 'world") }.to raise_error(error, /1:10/) } - specify { expect { expression("hello\nwor'ld") }.to raise_error(error, /2:4/) } - specify { expect { expression("hello 'world\\'") }.to raise_error(error, /1:10/) } - specify { expect { expression("hello 'wor\\'ld") }.to raise_error(error, /1:10/) } - specify { expect { expression("\"hello world") }.to raise_error(error, /1:4/) } - specify { expect { expression("he\"llo\\\" world") }.to raise_error(error, /1:6/) } - specify { expect { expression("he\"llo\\\" \nworld") }.to raise_error(error, /1:6/) } - specify { expect { expression("\"hello\\\"\n world") }.to raise_error(error, /1:4/) } - specify { expect { expression("'привет' 'мир") }.to raise_error(error, /1:13/) } + specify { expect { expression("hello 'world") }.to raise_error(error, /`'world }}`.*1:10/) } + specify { expect { expression("hello\nwor'ld") }.to raise_error(error, /`'ld }}`.*2:4/) } + specify { expect { expression("hello 'world\\'") }.to raise_error(error, /`'world\\' }}`.*1:10/) } + specify { expect { expression("hello 'wor\\'ld") }.to raise_error(error, /`'wor\\'ld }}`.*1:10/) } + specify { expect { expression("\"hello world") }.to raise_error(error, /`"hello world }}`.*1:4/) } + specify { expect { expression("he\"llo\\\" world") }.to raise_error(error, /`"llo\\" world }}`.*1:6/) } + specify { expect { expression("he\"llo\\\" \nworld") }.to raise_error(error, /`"llo\\" \nworld }}`.*1:6/) } + specify { expect { expression("\"hello\\\"\n world") }.to raise_error(error, /`"hello\\"\n world }}`.*1:4/) } + specify { expect { expression("'привет' 'мир") }.to raise_error(error, /`'мир }}`.*1:13/) } end end context 'complex expressions' do specify { expression('3+2 * 9').should == [ @@ -324,9 +330,12 @@ end end context 'template comments' do specify { scan('{{#').should == [[:COMMENT, "{{#"]] } + specify { scan('{{{#').should == [[:TOPEN, "{{"], [:HOPEN, "{"], [:COMMENT, "#"]] } + specify { scan('{{{{#').should == [[:TOPEN, "{{"], [:HOPEN, "{"], [:HOPEN, "{"], [:COMMENT, "#"]] } + specify { scan('{#').should == [[:TEMPLATE, "{#"]] } specify { scan('{{# }}').should == [[:COMMENT, "{{# }}"]] } specify { scan('{{##}}').should == [[:COMMENT, "{{##}}"]] } specify { scan('{{###}}').should == [[:COMMENT, "{{###}}"]] } specify { scan('{{# {{# blabla').should == [[:COMMENT, "{{# {{# blabla"]] } specify { scan('{{# {{# }} blabla').should == [[:COMMENT, "{{# {{# }} blabla"]] }