How I use the testdata gem

The testdata gem is a test framework for unit testing. It uses a separate file for test data to keep things as simple as possible. Here's an example:

#!/usr/bin/env ruby

# file: test_gtk2html.rb

require 'testdata'
require 'requestor'

eval Requestor.read('http://rorbuilder.info/r/ruby') do |x|
  x.require 'gtk2html'
end

class TestGtk2HTML < Testdata::Base

  def tests()

    test 'Render to_a' do |html, width, height|
      
      doc = Htmle.new(html)
      Gtk2HTML::Render.new(doc, width, height).to_a.inspect
        
    end

  end
end


url = 'http://rorbuilder.info/r/gemtest/gtk2html/testdata_gtk2html.xml'

TestGtk2HTML.new(url).run
#=> {:passed=>true, :score=>"2/2", :failed=>[]}


To run a specific test, we enter the path number into the run method e.g.

TestGtk2HTML.new(url).run '1'

Note: The path number is simply the identifier for a specific test.

If the test failed, we can examine the expected results with the actual results using the debug flag e.g.

TestGtk2HTML.new(url).run '1', debug=true

Output:

inputs: 
  html: <html style="background-color:white;margin:0;padding:0;font-size:1.3em;color:red"><div style="background-color:white;margin:0;padding:0;font-size:1.3em;color:red"></div></html>
  width: 320
  height: 240

type or description:
 Render to_a: 

expected : 
  ["[[:draw_box, [0.0, 0.0, 0.0, 0.0], [0, 0, 320, 240], [0.0, 0.0, 0.0, 0.0], {:\"background-color\"=>\"white\", :margin=>{:top=>0.0, :right=>0.0, :bottom=>0.0, :left=>0.0}, :padding=>{:top=>0.0, :right=>0.0, :bottom=>0.0, :left=>0.0}, :\"font-size\"=>\"1.3em\", :color=>\"red\"}], [[:draw_box, [0.0, 0.0, 0.0, 0.0], [nil, nil, nil, nil], [0.0, 0.0, 0.0, 0.0], {:\"background-color\"=>\"white\", :margin=>{:top=>0.0, :right=>0.0, :bottom=>0.0, :left=>0.0}, :padding=>{:top=>0.0, :right=>0.0, :bottom=>0.0, :left=>0.0}, :\"font-size\"=>\"1.3em\", :color=>\"red\"}]]]"]

Resources