summaryrefslogtreecommitdiff
path: root/test/unit/lines_of_code.rb
blob: e2c0caf2fa0e0e9e138fae9445d4eb3e385211f8 (plain)
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
require 'test/unit'
require 'coderay'
$VERBOSE = true

require File.expand_path('../../lib/assert_warning', __FILE__)

class LinesOfCodeTest < Test::Unit::TestCase
  
  def test_creation
    assert CodeRay::Encoders::LinesOfCode < CodeRay::Encoders::Encoder
    filter = nil
    assert_nothing_raised do
      filter = CodeRay.encoder :loc
    end
    assert_kind_of CodeRay::Encoders::LinesOfCode, filter
    assert_nothing_raised do
      filter = CodeRay.encoder :lines_of_code
    end
    assert_kind_of CodeRay::Encoders::LinesOfCode, filter
  end
  
  def test_lines_of_code
    tokens = CodeRay.scan <<-RUBY, :ruby
#!/usr/bin/env ruby

# a minimal Ruby program
puts "Hello world!"
    RUBY
    assert_equal 1, CodeRay::Encoders::LinesOfCode.new.encode_tokens(tokens)
    assert_equal 1, tokens.lines_of_code
    assert_equal 1, tokens.loc
  end
  
  class ScannerMockup
    KINDS_NOT_LOC = [:space]
  end
  
  def test_filtering_block_tokens
    tokens = CodeRay::Tokens.new
    tokens.concat ["Hello\n", :world]
    tokens.concat ["\n", :space]
    tokens.concat ["Hello\n", :comment]
    
    assert_warning 'Tokens have no associated scanner, counting all nonempty lines.' do
      assert_equal 1, tokens.lines_of_code
    end
    
    tokens.scanner = ScannerMockup.new
    assert_equal 2, tokens.lines_of_code
  end
  
end