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
2031fd5e » cheind 2008-12-18 added simple unit tests 1 #
2 # Project:: Ruby-Snippets
3 #
4 # Author:: Christoph Heindl (mailto:christoph.heindl@gmail.com)
5 # Homepage:: http://cheind.blogspot.com
6
7 require 'test/unit'
8 require 'dependencies/walker'
9
10 class TrueTest < Test::Unit::TestCase
11
12 def test_should_index_correctly
13 walker = Dependencies::Walker.new(Logger.new('walker.log'))
14 path_to_files = 'tests/dependencies/files'
15 walker.index(path_to_files, '*.txt') do |path|
16 File.basename(path, '.txt')
17 end
18 assert_not_nil(walker.resolve(path_to_files + '/a.txt'))
19 assert_not_nil(walker.resolve(path_to_files + '/b.txt'))
20 assert_not_nil(walker.resolve(path_to_files + '/c.txt'))
21 assert_not_nil(walker.resolve(path_to_files + '/d.txt'))
22 end
23
24 def test_should_parse_correctly
25 walker = Dependencies::Walker.new(Logger.new('walker.log'))
26 path_to_files = 'tests/dependencies/files'
27 walker.index(path_to_files, '*.txt') do |path|
28 File.basename(path, '.txt')
29 end
30 walker.parse(path_to_files, '*.txt') do |path, file|
31 dependencies = []
32 while (line = file.gets)
33 if line =~ /^->\s*(\w+)/
34 dependencies << File.basename($1, '.txt')
35 end
36 end
37 dependencies
38 end
39 assert_equal(true, walker.graph.has_edge?('a', 'b'))
40 assert_equal(true, walker.graph.has_edge?('b', 'c'))
41 assert_equal(false, walker.graph.has_edge?('c', 'd') && walker.graph.has_edge?('d', 'b'))
42 end
43
44 def test_should_parse_correctly_with_cycle
45 walker = Dependencies::Walker.new(Logger.new('walker.log'))
46 # Explicitely allow cycles
47 walker.on_cycle do |path, from, to|
48 walker.graph.add_edge(from, to)
49 end
50
51 path_to_files = 'tests/dependencies/files'
52 walker.index(path_to_files, '*.txt') do |path|
53 File.basename(path, '.txt')
54 end
55 walker.parse(path_to_files, '*.txt') do |path, file|
56 dependencies = []
57 while (line = file.gets)
58 if line =~ /^->\s*(\w+)/
59 dependencies << File.basename($1, '.txt')
60 end
61 end
62 dependencies
63 end
64 assert_equal(true, walker.graph.has_edge?('a', 'b'))
65 assert_equal(true, walker.graph.has_edge?('b', 'c'))
66 assert_equal(true, walker.graph.has_edge?('c', 'd'))
67 assert_equal(true, walker.graph.has_edge?('d', 'b'))
68 end
69 end