cheind / ruby-snippets

Handy ruby snippets to get the job done quickly!

This URL has Read+Write access

ruby-snippets / tests / dependencies / test_walker.rb
100644 69 lines (63 sloc) 2.273 kb
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#
# Project:: Ruby-Snippets
#
# Author:: Christoph Heindl (mailto:christoph.heindl@gmail.com)
# Homepage:: http://cheind.blogspot.com
 
require 'test/unit'
require 'dependencies/walker'
 
class TrueTest < Test::Unit::TestCase
 
  def test_should_index_correctly
    walker = Dependencies::Walker.new(Logger.new('walker.log'))
    path_to_files = 'tests/dependencies/files'
    walker.index(path_to_files, '*.txt') do |path|
      File.basename(path, '.txt')
    end
    assert_not_nil(walker.resolve(path_to_files + '/a.txt'))
    assert_not_nil(walker.resolve(path_to_files + '/b.txt'))
    assert_not_nil(walker.resolve(path_to_files + '/c.txt'))
    assert_not_nil(walker.resolve(path_to_files + '/d.txt'))
  end
  
  def test_should_parse_correctly
    walker = Dependencies::Walker.new(Logger.new('walker.log'))
    path_to_files = 'tests/dependencies/files'
    walker.index(path_to_files, '*.txt') do |path|
      File.basename(path, '.txt')
    end
    walker.parse(path_to_files, '*.txt') do |path, file|
      dependencies = []
      while (line = file.gets)
        if line =~ /^->\s*(\w+)/
          dependencies << File.basename($1, '.txt')
        end
      end
      dependencies
    end
    assert_equal(true, walker.graph.has_edge?('a', 'b'))
    assert_equal(true, walker.graph.has_edge?('b', 'c'))
    assert_equal(false, walker.graph.has_edge?('c', 'd') && walker.graph.has_edge?('d', 'b'))
  end
  
  def test_should_parse_correctly_with_cycle
    walker = Dependencies::Walker.new(Logger.new('walker.log'))
    # Explicitely allow cycles
    walker.on_cycle do |path, from, to|
      walker.graph.add_edge(from, to)
    end
    
    path_to_files = 'tests/dependencies/files'
    walker.index(path_to_files, '*.txt') do |path|
      File.basename(path, '.txt')
    end
    walker.parse(path_to_files, '*.txt') do |path, file|
      dependencies = []
      while (line = file.gets)
        if line =~ /^->\s*(\w+)/
          dependencies << File.basename($1, '.txt')
        end
      end
      dependencies
    end
    assert_equal(true, walker.graph.has_edge?('a', 'b'))
    assert_equal(true, walker.graph.has_edge?('b', 'c'))
    assert_equal(true, walker.graph.has_edge?('c', 'd'))
    assert_equal(true, walker.graph.has_edge?('d', 'b'))
  end
end