PageRenderTime 65ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_examples.rb

http://github.com/glejeune/Ruby-Graphviz
Ruby | 137 lines | 96 code | 21 blank | 20 comment | 5 complexity | a148b2f7fde18c970097c207e552ae4a MD5 | raw file
Possible License(s): GPL-2.0
  1. require 'open3'
  2. class GraphVizTest < Test::Unit::TestCase
  3. # you can run a subset of all the samples like this:
  4. # ruby test/test_examples.rb --name='/sample3[6-9]/'
  5. #
  6. # The above will run samples 36, 37, 38, and 39
  7. #
  8. RootDir = File.expand_path('../..', __FILE__)
  9. ExampleDir = File.join(RootDir,'examples')
  10. OutputDir = File.join(File.dirname(__FILE__),'output')
  11. # the below tests write to stdout. the other tests write to filesystem
  12. Skips = {
  13. #'35' => 'hanging for me',
  14. '33' => 'FamilyTree is broken',
  15. '36' => 'hangs for me',
  16. '53' => 'FamilyTree is broken',
  17. '57' => 'will not be able to find the graphml script',
  18. '98' => 'This test is just for debug',
  19. '99' => 'FamilyTree is broken'
  20. }
  21. def test_sample07
  22. assert_output_pattern(/\Adigraph structs \{.+\}\n\Z/m, '07')
  23. end
  24. def test_sample22
  25. assert_output_pattern(/\Adigraph mainmap \{.+\}\n\Z/m, '22')
  26. end
  27. def test_sample23
  28. assert_output_pattern(%r{\A<map.+</map>\n\Z}m, '23')
  29. end
  30. def test_sample27
  31. assert_output_pattern(/\Adigraph G \{.*\}\n\Z/m, '27')
  32. end
  33. def test_sample38
  34. assert_output_pattern(/\Adigraph G \{.*\}\n\Z/m, '38')
  35. end
  36. def test_sample40
  37. assert_output_pattern(/\Adigraph G \{.*\}\n\Z/m, '40')
  38. end
  39. def test_sample41
  40. assert_output_pattern(/\A.*\Z/m, '40')
  41. end
  42. def test_sample55
  43. assert_output_pattern(/\Agraph G \{.*\}\n\Z/m, '55')
  44. end
  45. def test_sample62
  46. assert_output_pattern(/\ANode.*\n\Z/m, '62')
  47. end
  48. def test_sample70
  49. assert_output_pattern(/\Agraph G \{.*\}\n\Z/m, '70')
  50. end
  51. #
  52. # for every sample file in the examples directory that matches the
  53. # pattern ("sample01.rb, sample02.rb, etc) make a corresponding
  54. # test method: test_sample01(), test_sample02(), etc. To actually define
  55. # this methods in this way instead of just iterating over the list of files
  56. # will make it easier to use command-line options to isolate certain
  57. # tests,
  58. # (for example: ruby test/test_examples.rb --name '/sample0[1-5]/' )
  59. # and to integrate better with certain kinds of test output and
  60. # reporting tools.
  61. #
  62. # we will skip over any methods already defined
  63. #
  64. @last_image_path = nil
  65. @number_to_path = {}
  66. class << self
  67. def make_sample_test_method path
  68. fail("failed match: #{path}") unless
  69. matches = %r{/(sample(\d\d))\.rb\Z}.match(path)
  70. basename, number = matches.captures
  71. number_to_path[number] = path
  72. meth = "test_#{basename}"
  73. return if method_defined?(meth) # if we hand-write one
  74. if Skips[number]
  75. puts "skipping #{basename} - #{Skips[number]}"
  76. return
  77. end
  78. define_method(meth){ assert_sample_file_has_no_output(path) }
  79. end
  80. attr_accessor :last_image_path, :number_to_path
  81. end
  82. if File.directory? OutputDir
  83. FileUtils.rm_rf OutputDir
  84. end
  85. FileUtils.cp_r ExampleDir, OutputDir
  86. samples = Dir[File.join(OutputDir,'sample*.rb')].sort
  87. samples.each {|path| make_sample_test_method(path) }
  88. private
  89. def assert_output_pattern tgt_regexp, number
  90. path = self.class.number_to_path[number]
  91. out, err, _ = Open3.capture3("ruby #{path}")
  92. assert_equal "", err, "no errors"
  93. assert_match tgt_regexp, out.gsub(/\r\n/, "\n"), "output for sample#{number} should match regexp"
  94. end
  95. def assert_sample_file_has_no_output path
  96. begin
  97. out, err, _ = Open3.capture3("ruby #{path}")
  98. assert_equal(0, out.length, "expecting empty output got [#{out}]")
  99. assert_equal(0, err.length, "expecting empty errorput got [#{err}]")
  100. rescue Exception => e
  101. assert(false, "got exception on #{File.basename(path)}: #{e.message}")
  102. end
  103. end
  104. def setup_sample path
  105. unless File.directory? OutputDir
  106. FileUtils.mkdir_p(OutputDir, :verbose => true)
  107. end
  108. ARGV[0] = nil if ARGV.any? # hack trigger searching for 'dot' executable
  109. end
  110. def run_sample(path)
  111. run_path = File.join(OutputDir, File.basename(path))
  112. FileUtils.cp path, run_path
  113. out, err, _ = Open3.capture3("ruby #{run_path}")
  114. return out, err
  115. end
  116. end