Skip to content

Commit

Permalink
working on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Jun 28, 2009
1 parent bf321fd commit cf16ef5
Show file tree
Hide file tree
Showing 21 changed files with 1,562 additions and 126 deletions.
6 changes: 3 additions & 3 deletions lib/erb/stripper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
module Erb
class Scanner < ERB::Compiler::Scanner
def scan
stag_reg = /(.*?)(^[ \t]*<%%|<%=|<%#|<%-|<%|\z)/m
stag_reg = /(.*?)(^[ \t]*<%%|<%%=|<%=|<%#|<%-|<%|\z)/m
etag_reg = /(.*?)(%%>|\-%>|%>|\z)/m
scanner = StringScanner.new(@src)
while !scanner.eos?
Expand All @@ -29,8 +29,8 @@ def to_ruby(source)
comment = true if token == '<%#'
if scanner.stag.nil?
result << to_whitespace(token)
scanner.stag = token if ['<%', '<%-', '<%=', '<%#'].include?(token)
elsif ['%>', '-%>'].include?(token)
scanner.stag = token if ['<%', '<%%', '<%-', '<%=', '<%%=', '<%#'].include?(token.strip)
elsif ['%>', '%%>', '-%>'].include?(token.strip)
result << to_whitespace(token.gsub(/>/, ';'))
scanner.stag = nil
else
Expand Down
1 change: 0 additions & 1 deletion lib/ripper/ruby_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def initialize(src, filename = nil, lineno = nil)
end

protected

def position
Ruby::Node::Position.new(lineno.to_i - 1, column)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ripper/ruby_builder/events/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def on_brace_block(params, statements)
end

def on_begin(body)
body = body.to_named_block unless body.is_a?(Ruby::NamedBlock)
body = body.to_chained_block unless body.is_a?(Ruby::ChainedBlock)
body.rdelim = pop_token(:@end)
body.identifier = pop_token(:@begin)
body
Expand Down
4 changes: 3 additions & 1 deletion lib/ripper/ruby_builder/events/call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ def on_command(identifier, args)
end

def on_command_call(target, separator, identifier, args)
# happens for identifiers that are operators, e.g. a.<<(foo)
identifier = pop_identifier(identifier.type) if identifier.try(:known?)
separator = pop_token(:@period, :"@::")
Ruby::Call.new(target, separator, identifier, args)
end

def on_call(target, separator, identifier)
# happens for symbols that are also keywords, e.g. :if
# happens for identifiers that are also keywords, e.g. :if
identifier = pop_identifier(identifier.type) if identifier.try(:known?)
separator = pop_token(:@period, :"@::", :left => target, :right => identifier)
Ruby::Call.new(target, separator, identifier)
Expand Down
5 changes: 5 additions & 0 deletions lib/ripper/ruby_builder/events/const.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def on_top_const_ref(const)
const.prolog.unshift(pop_token(:'@::'))
const
end

def on_top_const_field(const)
const.prolog.unshift(pop_token(:'@::'))
const
end
end
end
end
4 changes: 4 additions & 0 deletions lib/ripper/ruby_builder/events/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module Lexer
def on_parse_error(msg)
raise ParseError.new("#{filename}:#{position.row + 1}: #{msg}")
end

def on_param_error(param)
raise ParseError.new("#{filename}:#{position.row + 1}: syntax error, invalid parameter type: #{param.to_ruby}")
end

def on_ignored_nl(*args)
token = push(super)
Expand Down
2 changes: 1 addition & 1 deletion lib/ripper/ruby_builder/events/statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def on_program(statements)

def on_body_stmt(body, rescue_block, else_block, ensure_block)
statements = [rescue_block, else_block, ensure_block].compact
body = body.to_chained_block(nil, statements) if rescue_block || ensure_block
body = body.to_chained_block(nil, statements) unless statements.empty?
body
end

Expand Down
12 changes: 8 additions & 4 deletions lib/ripper/ruby_builder/events/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def on_string_content(*args)

def on_xstring_literal(string, type = :@tstring_end)
string_stack.pop if string == string_stack.last
string.rdelim = pop_token(type)
string.rdelim = pop_token(type) if string.respond_to?(:rdelim)
string
end

Expand All @@ -38,9 +38,13 @@ def on_regexp_literal(string, rdelim)
end

def on_xstring_new(*args)
ldelim = pop(:@symbeg, :@backtick, :@regexp_beg, :max => 1, :pass => true).first
string_stack << build_xstring(ldelim)
string_stack.last
if token = pop_token(:@heredoc_beg)
Ruby::HeredocBegin.new(token.token, token.position, token.prolog)
else
ldelim = pop(:@symbeg, :@backtick, :@regexp_beg, :max => 1, :pass => true).first
string_stack << build_xstring(ldelim)
string_stack.last
end
end

def on_xstring_add(string, content)
Expand Down
93 changes: 93 additions & 0 deletions notes/problems.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Do not use colons to separate if/unless/when conditions from expressions:

# e.g. adva_cms, ansuz, cruisecontrol, freemium, merb, paperclip, radiant,
# rbot, rfpdf, rghost, rstomp, shoulda, typus
case true; when TrueClass: true; end

# ruby 1.8.7
=> true

# ruby 1.9.1
syntax error, unexpected ':', expecting keyword_then or ',' or ';' or '\n'


Do not use instance_variables, constants or hash access as block parameters:

# e.g. in cruise_control, spree
lambda { |@foo| }

# ruby 1.8.7
=> #<Proc:0x00000000@(irb):1>

# ruby 1.9.1
formal argument cannot be an instance variable

# e.g. in cruisecontrol (frozen rails)
lambda { |foo[:bar]| }

# ruby 1.8.7
=> #<Proc:0x00000000@(irb):1>

# ruby 1.9.1
formal argument cannot be a constant

# e.g. in cruisecontrol (frozen rails)
lambda { |foo[:bar]| }

# ruby 1.8.7
=> #<Proc:0x00000000@(irb):1>

# ruby 1.9.1
syntax error, unexpected '[', expecting '|'


Do not use ...

# e.g. in cruisecontrol (frozen rails)
lambda { |(foo, )| }

# ruby 1.8.7
=> #<Proc:0x00000000@(irb):1>

# ruby 1.9.1
syntax error, unexpected ')'


Do not use commas to separate key and value in a Hash:

# e.g. cosy, streamlined, webrat
{1, 2}

# ruby 1.8.7
=> {1 => 2}

# ruby 1.9.1
syntax error, unexpected ',', expecting tASSOC


Do not put spaces in front of parentheses (method call arguments):

# e.g. in exception_logger (logged_exceptions_controller.rb:40, logged_exceptions_helper.rb:9)
A.foo (:bar)
syntax error, unexpected ',', expecting ')'


Do not put spaces in front of brackets (Hash access):

# e.g. in ultrasphinx (configure.rb:247)

entry ['association_name']
syntax error, unexpected tLBRACK, expecting keyword_do or '{' or '('


Put a space between the operator and number when calculating:

# e.g. in liquid (htmltags.rb:49, for.rb:104, paginate.rb:61)
a - b -1
syntax error, unexpected tUMINUS_NUM, expecting keyword_do or '{' or '('

# e.g. in prawn (span.rb:36)
a /2.0
syntax error, unexpected tREGEXP_BEG, expecting keyword_do or '{' or '('


6 changes: 3 additions & 3 deletions test/erb_stripper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_sexp_filename
ruby = Erb::Stripper.new.to_ruby(erb)
expected = <<-src
f.field_set do
column do
column do
[:foo].each do |foo|
t(:erb_1)
end
Expand All @@ -19,11 +19,11 @@ def test_sexp_filename
src
assert_equal erb.length, ruby.length
%w([:foo] erb_1 erb_2 foo.erb_3).each do |token|
assert ruby.index(token)
assert_not_nil ruby.index(token)
assert_equal erb.index(token), ruby.index(token)
end
expected.split("\n").each do |token|
assert ruby.index(token)
assert_not_nil ruby.index(token)
end
end
end
2 changes: 1 addition & 1 deletion test/fixtures/template.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html>
<ul>
<% f.field_set do %>
<% column do %>
<%% column do %%>
<% [:foo].each do |foo| %>
<li>

Expand Down
22 changes: 10 additions & 12 deletions test/fixtures/tmp.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
def _evaluate_condition(condition)
case condition
when Symbol : self.send(condition)
when Proc : self.instance_eval(&condition)
else
raise ArgumentError,
'Filter condtions need to be either a Symbol or a Proc'
end
end


add_tests("call_unary_neg",
"Ruby" => "-2**31",
"RawParseTree" => [:call,
[:call, [:lit, 2], :**, [:array, [:lit, 31]]],
:-@],
"ParseTree" => s(:call,
s(:call,
s(:lit, 2),
:**,
s(:arglist, s(:lit, 31))),
:-@, s(:arglist)),
"Ruby2Ruby" => "-(2 ** 31)")
Loading

0 comments on commit cf16ef5

Please sign in to comment.