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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
require File.expand_path('support.rb', File.dirname(__FILE__))
class GraphVizTest < Test::Unit::TestCase
#
# you can run a subset of all the samples like this:
# ruby test/test_examples.rb --name='/sample3[6-9]/'
#
# The above will run samples 36, 37, 38, and 39
#
include IoHack
RootDir = File.expand_path('../..', __FILE__)
ExampleDir = File.join(RootDir,'examples')
OutputDir = File.join(File.dirname(__FILE__),'output')
# OutputDir = File.join(RootDir,'test','output')
# the below tests write to stdout. the other tests write to filesystem
Skips = {
#'35' => 'hanging for me',
'33' => 'FamilyTree is broken',
'36' => 'hangs for me',
'53' => 'FamilyTree is broken',
'57' => 'will not be able to find the graphml script',
'99' => 'FamilyTree is broken'
}
def test_sample07
assert_output_pattern(/\Adigraph structs \{.+\}\n\Z/m, '07')
end
def test_sample22
assert_output_pattern(/\Adigraph mainmap \{.+\}\n\Z/m, '22')
end
def test_sample23
assert_output_pattern(%r{\A<map.+</map>\n\Z}m, '23')
end
def test_sample27
assert_output_pattern(/\Adigraph G \{.*\}\n\Z/m, '27')
end
def test_sample38
assert_output_pattern(/\Adigraph G \{.*\}\n\Z/m, '38')
end
def test_sample40
assert_output_pattern(/\Adigraph G \{.*\}\n\Z/m, '40')
end
def test_sample41
assert_output_pattern(/\A.*\Z/m, '40')
end
def test_sample55
assert_output_pattern(/\Agraph G \{.*\}\n\Z/m, '55')
end
def test_sample62
assert_output_pattern(/\ANode.*\n\Z/m, '62')
end
#
# for every sample file in the examples directory that matches the
# pattern ("sample01.rb, sample02.rb, etc) make a corresponding
# test method: test_sample01(), test_sample02(), etc. To actually define
# this methods in this way instead of just iterating over the list of files
# will make it easier to use command-line options to isolate certain
# tests,
# (for example: ruby test/test_examples.rb --name '/sample0[1-5]/' )
# and to integrate better with certain kinds of test output and
# reporting tools.
#
# we will skip over any methods already defined
#
@last_image_path = nil
@number_to_path = {}
class << self
def make_sample_test_method path
fail("failed match: #{path}") unless
matches = %r{/(sample(\d\d))\.rb\Z}.match(path)
basename, number = matches.captures
number_to_path[number] = path
meth = "test_#{basename}"
return if method_defined?(meth) # if we hand-write one
if Skips[number]
puts "skipping #{basename} - #{Skips[number]}"
return
end
define_method(meth){ assert_sample_file_has_no_output(path) }
end
attr_accessor :last_image_path, :number_to_path
end
samples = Dir[File.join(ExampleDir,'sample*.rb')].sort
samples.each {|path| make_sample_test_method(path) }
private
def assert_output_pattern tgt_regexp, number
path = self.class.number_to_path[number]
setup_sample path
out, err = fake_popen2(path)
assert_equal "", err, "no errors"
assert_match tgt_regexp, out.gsub(/\r\n/, "\n"), "output for sample#{number} should match regexp"
end
def assert_sample_file_has_no_output path
setup_sample(path)
begin
out, err = fake_popen2(path)
assert_equal(0, out.length, "expecting empty output")
assert_equal(0, err.length, "expecting empty errorput")
msg = "maybe generated #{self.class.last_image_path}"
print "\n", msg
rescue Exception => e
assert(false, "got exception on #{File.basename(path)}: #{e.message}")
puts "out: ", out.inspect, "err:", err.inspect
end
end
def setup_sample path
unless File.directory? OutputDir
FileUtils.mkdir_p(OutputDir, :verbose => true)
end
ARGV[0] = nil if ARGV.any? # hack trigger searching for 'dot' executable
hack_output_path path
end
def hack_output_path path
# hack $0 to change where the output image is written to
fake_example_path = File.join(OutputDir, File.basename(path))
$program_name = fake_example_path.dup
alias $0 $program_name
self.class.last_image_path = "#{$0}.png"
end
end
|