The following test framework was intended to simplify testing by storing the data in an XML (Polyrex) file.


Notes:
4 paths are to be tested for this class (Dynarex), these are:

1) create a new dynarex file from the schema
2) output a dynarex plain text view
2.5) create a new dynarex file from a dynarex plain text view
3) apply CRUD to an existing file and save it.

inputs outputs
------- -----------
1) schema_txt schema_xml
2) credits_xml credits_txt
2.5) credits_txt credits_xml
3) contacts_xml contacts_xml


Out of these 4 paths I'm only testing the 1st path as follows:



require 'dynarex'

Testdata.new('testdata.xml').paths do |path|
path.tested? 'create a new dynarex file from the schema' do |input,output|
result = input.data('schema_txt') {|schema_txt| Dynarex.new(schema_txt).to_xml}
expected = output.data('schema_xml') {|schema_xml| File.open(schema_xml,'r').read}
result == expected
end
end
#=> [true]


Here's the test framework for the above:

require 'rexml/document'

class Select
include REXML

def initialize(type, node) @node, @type = node, type end

def data(x)
procs = {}
procs[:String] = Proc.new {|type,x| r = XPath.first(@node, "records/io/summary[type='#{type}']/#{x}/text()").to_s; puts r; r}
procs[:Array] = Proc.new do |type,a|
xpath = a.map {|x| "io/summary[type='#{type}']/#{x}/text()"}.join(' | ')
XPath.match(@node, xpath).map &:to_s
end
yield procs[x.class.to_s.to_sym].call(@type, x)
end
end

class Path
include REXML

def initialize(doc) @doc = doc end

def tested? description
node = XPath.first(@doc.root, "records/test[summary/description='#{description}']")
raise "Path error: node not found" unless node
yield *%w(input output).map {|x| Select.new(x,node)}
end
end

class Testdata
include REXML

attr_reader :success

def initialize(s)
buffer = self.send('read_' + (s[/https?:\/\//] ? 'file' : 'url'), s)
@doc = Document.new(buffer)
raise "Testdata error: doc %s not found" % s unless @doc
@success = []
end

def paths() @success << yield(path = Path.new(@doc)) end
def read_file(s) File.open(s, 'r').read end
def read_url(xml_url) open(xml_url, 'UserAgent' => 'S-Rscript').read end
end


file: testdata.xml






1
create a new dynarex file from the schema




input
contacts[title,description]/contact(name,age,dob)




output
contacts.xml






Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/Gre6p4toVZI/11341