require File.dirname(__FILE__) + '/test_helper' class TestFileStatistics < Test::Unit::TestCase def test_trailing_end_is_inferred verify_everything_marked "trailing end", <<-EOF 1 class X 1 def foo 2 "foo" 0 end 0 end EOF verify_everything_marked "trailing end with comments", <<-EOF 1 class X 1 def foo 2 "foo" 0 end 0 # foo bar 0 =begin 0 .... 0 =end 0 end EOF end def test_begin_ensure_else_case_are_inferred verify_everything_marked "begin ensure else case", <<-EOF 0 begin # bleh 2 puts a 0 begin 2 raise "foo" 0 rescue Exception => e 2 puts b 0 ensure 2 puts c 0 end 2 if a() 1 b 0 else 1 c 0 end 0 case 2 when bar =~ /foo/ 1 puts "bar" 0 else 1 puts "baz" 0 end 0 end EOF end def test_rescue_is_inferred verify_everything_marked "rescue", <<-EOF 0 begin 1 foo 0 rescue 1 puts "bar" 0 end EOF end def test_code_metrics_are_computed_correctly lines, coverage, counts = code_info_from_string <<-EOF 1 a = 1 0 # this is a comment 1 if bar 0 b = 2 0 end 0 =begin 0 this too 0 bleh 0 =end 0 puts < and do not contain the same elements 0 EOT EOF end def test_heredocs_multiple verify_everything_marked "multiple-unquoted", <<-EOF 1 puts < and do not contain the same elements 0 EOT 0 and are foo bar baz 0 EOT EOF end def test_ignore_non_heredocs verify_marked_exactly "bitshift-numeric", [0], <<-EOF 1 puts 1<<2 0 return if foo 0 do_stuff() 0 2 EOF verify_marked_exactly "bitshift-symbolic", [0], <<-EOF 1 puts 1< 0 4 % 0 3 && 0 true || 0 foo << 0 bar( 0 baz[ 0 { 0 1,2}] = 0 1 ) EOF verify_everything_marked "boolean expression", <<-EOF 1 x = (foo and 0 bar) or 0 baz EOF verify_marked_exactly "code blocks", [0, 3, 6], <<-EOF 1 x = foo do # stuff 0 baz 0 end 1 bar do |x| 0 baz 0 end 1 bar {|a, b| # bleh | +1 0 baz 0 } EOF verify_everything_marked "escaped linebreaks", <<-EOF 1 def t2 0 puts \\ 1 "foo" 0 end 0 end EOF end def test_handle_multiline_blocks_first_not_marked verify_everything_marked "multiline block first not marked", <<-'EOF' 1 blah = Array.new 1 10.times do 0 blah << lambda do |f| 1 puts "I should say #{f}!" 0 end 0 end EOF end def test_handle_multiline_blocks_last_line_not_marked verify_everything_marked "multiline block last not marked", <<-'EOF' 1 blee = [1, 2, 3] 0 blee.map! do |e| 1 [e, e] 0 end.flatten! 1 p blee EOF end def test_handle_multiline_data_with_trailing_stuff_on_last_line verify_everything_marked "multiline data hash", <<-'EOF' 1 @review = Review.new({ 0 :product_id => params[:id], 0 :user => current_user 0 }.merge(params[:review])) #red 1 @review.save EOF end def test_handle_multiline_expression_1st_line_ends_in_block_header # excerpt taken from mongrel/handlers.rb verify_everything_marked "multiline with block starting on 1st", <<-EOF 1 uris = listener.classifier.handler_map 0 results << table("handlers", uris.map {|uri,handlers| 1 [uri, 0 "
" +
    1    handlers.map {|h| h.class.to_s }.join("\n") +
    0    "
" 0 ] 1 }) EOF end def test_handle_multiple_block_end_delimiters_in_empty_line verify_everything_marked "multiline with }) delimiter, forward", <<-EOF 1 assert(@c.config == { 0 'host' => 'myhost.tld', 0 'port' => 1234 0 }) EOF verify_everything_marked "multiline with }) delimiter, backwards", <<-EOF 0 assert(@c.config == { 0 'host' => 'myhost.tld', 0 'port' => 1234 1 }) EOF end STRING_DELIMITER_PAIRS = [ %w-%{ }-, %w-%q{ }-, %w-%Q{ }-, %w{%[ ]}, %w{%q[ ]}, %w{%( )}, %w{%Q( )}, %w{%Q[ ]}, %w{%q! !}, %w{%! !}, ] def test_multiline_strings_basic STRING_DELIMITER_PAIRS.each do |s_delim, e_delim| verify_everything_marked "multiline strings, basic #{s_delim}", <<-EOF 1 PATTERN_TEXT = #{s_delim} 0 NUMBERS = 'one|two|three|four|five' 0 ON_OFF = 'on|off' 0 #{e_delim} EOF end STRING_DELIMITER_PAIRS.each do |s_delim, e_delim| verify_marked_exactly "multiline strings, escaped #{s_delim}", [0], <<-EOF 1 PATTERN_TEXT = #{s_delim} foooo bar baz \\#{e_delim} baz #{e_delim} 0 NUMBERS = 'one|two|three|four|five' 0 ON_OFF = 'on|off' EOF verify_marked_exactly "multiline strings, #{s_delim}, interpolation", [0], <<-EOF 1 PATTERN_TEXT = #{s_delim} \#{#{s_delim} foo #{e_delim}} \\#{e_delim} baz #{e_delim} 0 NUMBERS = 'one|two|three|four|five' 0 ON_OFF = 'on|off' EOF end end def test_multiline_strings_escaped_delimiter STRING_DELIMITER_PAIRS.each do |s_delim, e_delim| verify_everything_marked "multiline, escaped #{s_delim}", <<-EOF 1 PATTERN_TEXT = #{s_delim} foo \\#{e_delim} 0 NUMBERS = 'one|two|three|four|five' 0 ON_OFF = 'on|off' 0 #{e_delim} EOF end end def test_handle_multiline_expressions_with_heredocs verify_everything_marked "multiline and heredocs", <<-EOF 1 puts < 0}, str.map{|line| line[/^\d+/].to_i } ] end end