testdata 0.3.8 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/testdata.rb +79 -166
- metadata +12 -10
data/lib/testdata.rb
CHANGED
@@ -2,212 +2,125 @@
|
|
2
2
|
|
3
3
|
# file: testdata.rb
|
4
4
|
|
5
|
-
require 'rexle'
|
6
|
-
require 'rscript'
|
7
|
-
require 'rexle-builder'
|
8
5
|
require 'rexml/document'
|
6
|
+
require 'app-routes'
|
9
7
|
|
10
|
-
|
11
|
-
class Path
|
8
|
+
class Testdata
|
12
9
|
include REXML
|
10
|
+
include AppRoutes
|
11
|
+
|
12
|
+
attr_accessor :debug
|
13
|
+
|
14
|
+
def initialize(s, options={})
|
15
|
+
@route = {}; @params = {} # used by app-routes
|
16
|
+
@success = [] # used by summary
|
17
|
+
@debug = false
|
13
18
|
|
14
|
-
|
15
|
-
|
19
|
+
# open the testdata document
|
20
|
+
buffer = self.send('read_' + (s[/https?:\/\//] ? 'url' : 'file'), s)
|
21
|
+
@doc = Document.new(buffer)
|
22
|
+
|
23
|
+
o = {log: false}.merge(options)
|
24
|
+
@log = o[:log] == true ? Document.new(tests) : nil
|
16
25
|
end
|
17
26
|
|
18
|
-
def
|
27
|
+
def testdata_values(id)
|
19
28
|
|
20
|
-
stringify = Proc.new
|
29
|
+
stringify = Proc.new do |x|
|
30
|
+
r = x.text.to_s.gsub(/^[\n\s]+/,'').length > 0 ? x.text : x.cdatas.join.strip
|
31
|
+
REXML::Text::unnormalize(r)
|
32
|
+
end
|
21
33
|
|
22
|
-
node = XPath.first(@doc.root, "records/test[summary/
|
34
|
+
node = XPath.first(@doc.root, "records/test[summary/path='#{id}']")
|
23
35
|
raise "Path error: node title not found" unless node
|
24
|
-
input_values = XPath.match(node, "records/io/summary[type='input']/*").map(&stringify)
|
25
|
-
output_values = XPath.match(node, "records/io/summary[type='output']/*").map(&stringify)
|
26
36
|
|
27
|
-
path_no = node.text('summary/path')
|
37
|
+
path_no = node.text('summary/path').to_s
|
28
38
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
39
|
+
xpath = "records/io/summary[type='input']/*"
|
40
|
+
input_values = XPath.match(node, xpath)[1..-1].map(&stringify)
|
41
|
+
|
42
|
+
# find the type or description
|
43
|
+
xpath = "summary/type/text() | summary/description/text()"
|
44
|
+
type_or_desc = XPath.match(node, xpath).map(&:to_s).find{|x| x != ''}
|
33
45
|
|
34
|
-
|
46
|
+
xpath = "records/io/summary[type='output']/*"
|
47
|
+
raw_output = XPath.match(node, xpath)
|
48
|
+
output_values = raw_output.length > 0 ? raw_output[1..-1].map(&stringify) : []
|
35
49
|
|
50
|
+
[path_no, input_values, type_or_desc, output_values]
|
51
|
+
end
|
52
|
+
|
53
|
+
def run(x=nil, debug2=nil)
|
54
|
+
@debug2 = debug2 ? true : false
|
55
|
+
@success = []
|
56
|
+
procs = {NilClass: :test_all, Range: :test_all, String: :test_id, Fixnum: :test_id}
|
57
|
+
method(procs[x.class.to_s.to_sym]).call(x)
|
58
|
+
summary()
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_all(x)
|
62
|
+
x ||=(0..-1)
|
63
|
+
|
64
|
+
XPath.match(@doc.root, "records/test/summary/path/text()")[x].each do |id|
|
65
|
+
puts 'id : ' + id.to_s
|
66
|
+
test_id(id)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_id(id='')
|
71
|
+
|
72
|
+
path_no, @inputs, tod, expected = testdata_values(id.to_s)
|
73
|
+
routes()
|
74
|
+
raw_actual = run_route tod
|
75
|
+
|
36
76
|
result = nil
|
37
77
|
@success << [nil, path_no.to_i]
|
38
|
-
|
39
|
-
begin
|
40
78
|
|
41
|
-
|
42
|
-
expected = (output_values - ['output'])
|
43
|
-
|
44
|
-
yield
|
45
|
-
|
46
|
-
raw_result = test(*values)
|
79
|
+
begin
|
47
80
|
|
48
|
-
if
|
49
|
-
a = [
|
81
|
+
if raw_actual then
|
82
|
+
a = [raw_actual].flatten.map(&:strip)
|
50
83
|
b = expected.map(&:strip)
|
51
84
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
85
|
+
if @debug == true or @debug2 == true then
|
86
|
+
puts "\n" + tod
|
87
|
+
puts "\nexpected : " + b.inspect
|
88
|
+
puts "\nactual : " + a.inspect + "\n"
|
89
|
+
end
|
57
90
|
|
58
91
|
result = a == b
|
59
92
|
else
|
60
|
-
result = [
|
93
|
+
result = [raw_actual].compact == expected
|
61
94
|
end
|
62
95
|
|
63
96
|
rescue Exception => e
|
64
97
|
err_label = e.message + " :: \n" + e.backtrace.join("\n")
|
65
98
|
|
66
99
|
puts err_label
|
67
|
-
XPath.first(node_result, "error").text = err_label
|
68
100
|
result = false
|
69
101
|
ensure
|
70
102
|
@success[-1][0] = result
|
71
103
|
end
|
72
104
|
end
|
73
105
|
|
74
|
-
def
|
75
|
-
|
76
|
-
end
|
77
|
-
|
78
|
-
class Testdata
|
79
|
-
include REXML
|
80
|
-
|
81
|
-
attr_reader :success
|
82
|
-
|
83
|
-
def initialize(s, options={})
|
84
|
-
#puts 'filex : ' + $0
|
85
|
-
#puts 'filez : ' + __FILE__
|
86
|
-
#puts 'pwd : ' + Dir.pwd
|
87
|
-
#exit
|
88
|
-
|
89
|
-
o = {log: false}.merge(options)
|
90
|
-
|
91
|
-
@filepath = File.dirname s
|
92
|
-
buffer = self.send('read_' + (s[/https?:\/\//] ? 'file' : 'url'), s)
|
93
|
-
@doc = Document.new(buffer)
|
94
|
-
raise "Testdata error: doc %s not found" % s unless @doc
|
95
|
-
@success = []
|
96
|
-
|
97
|
-
@log = o[:log] == true ? Document.new(tests) : nil
|
98
|
-
end
|
99
|
-
|
100
|
-
def paths()
|
101
|
-
yield(path = Path.new(@doc, @log, @success))
|
102
|
-
File.open(Dir.pwd + '/.test_log.xml','w'){|f| f.write @log } if @log
|
106
|
+
def routes(*args)
|
107
|
+
# override this method in the child class
|
103
108
|
end
|
104
109
|
|
105
110
|
def read_file(s) File.open(s, 'r').read end
|
106
111
|
def read_url(xml_url) open(xml_url, 'UserAgent' => 'S-Rscript').read end
|
107
112
|
|
108
|
-
def
|
109
|
-
|
110
|
-
|
111
|
-
a = @success.map(&:last).sort
|
112
|
-
{false: @success.select{|x| x[0] == false}.map(&:last).sort,
|
113
|
-
nil: ((a[0]..a[-1]).to_a - a)}
|
114
|
-
end
|
115
|
-
|
116
|
-
def find_by(s)
|
117
|
-
XPath.match(@doc.root, "records/test/summary[type='#{s}']/description/text()").map(&:to_s)
|
118
|
-
end
|
119
|
-
|
120
|
-
private
|
121
|
-
|
122
|
-
def tests()
|
123
|
-
script_file = XPath.first(@doc.root, "summary/script/text()").to_s
|
124
|
-
|
125
|
-
if script_file[/\/\/job:/] then
|
126
|
-
s = RScript.new.read(script_file.split(/\s/)).first
|
127
|
-
else
|
128
|
-
|
129
|
-
file_path = File.exists?(script_file) ? script_file : @filepath + '/' + script_file
|
130
|
-
s = File.open(file_path,'r').read
|
131
|
-
end
|
132
|
-
|
133
|
-
stringify = Proc.new {|x| x.text.to_s.gsub(/[\n\s]/,'').length > 0 ? x.text : x.cdatas.join.strip}
|
134
|
-
|
135
|
-
raw_paths = s[/testdata\.paths(.*)(?=end)/m,1].split(/(?=path\.tested\?)/)
|
136
|
-
raw_paths.shift
|
137
|
-
|
138
|
-
content = raw_paths.map do |x|
|
139
|
-
title = x[/path\.tested\?\s(.*)\sdo/,1][1..-2]
|
140
|
-
path_test = x[/def.*(?=end)/m]
|
141
|
-
raw_vars = path_test[/def path.test\(([^\)]+)/,1]
|
142
|
-
vars = raw_vars.split(/\s*,\s*/) if raw_vars
|
143
|
-
body = path_test[/def path\.test\([^\)]*\)(.*)(?=end)/m,1]
|
144
|
-
[title, vars, body]
|
145
|
-
end
|
146
|
-
|
147
|
-
r = (0..content.length - 1).map do |i|
|
148
|
-
node = XPath.first(@doc.root, "records/test[#{i+1}]")
|
149
|
-
input_values = XPath.match(node, "records/io/summary[type='input']/*").map(&stringify)
|
150
|
-
path_no = node.text('summary/path').to_s
|
151
|
-
output_values = XPath.match(node, "records/io/summary[type='output']/*").map(&stringify)
|
152
|
-
raw_output = (output_values - ['output'])
|
153
|
-
output = raw_output.join("\n")
|
154
|
-
|
155
|
-
values = input_values - ['input']
|
156
|
-
body = content[i][2].strip.gsub("\n ","\n")
|
157
|
-
if content[i][1] then
|
158
|
-
content[i][1].zip(values).reverse.each do |keyword, value|
|
159
|
-
body.gsub!(/#{keyword}(?!:)/, "'%s'" % value )
|
160
|
-
end
|
161
|
-
end
|
162
|
-
#puts 'xxxxx : ' + body.to_s
|
163
|
-
[path_no, content[i][0], body, output]
|
164
|
-
end
|
165
|
-
|
166
|
-
#r.each {|x| x.join("\n")}.join("\n")
|
167
|
-
tests_to_dynarex(r)
|
113
|
+
def get(s)
|
114
|
+
@inputs = @inputs.first if @inputs.length == 1
|
115
|
+
super(s){yield(@inputs)}
|
168
116
|
end
|
169
117
|
|
170
|
-
def
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
xml.format_mask '[!path_no] [!title] [!test] [!expected] [!actual]'
|
178
|
-
xml.schema 'results/result(path_no, title, test, expected, actual)'
|
179
|
-
end
|
180
|
-
xml.records do
|
181
|
-
tests.each do |path_no, title, testx, expected|
|
182
|
-
|
183
|
-
xml.result do
|
184
|
-
xml.path_no path_no
|
185
|
-
xml.title title
|
186
|
-
|
187
|
-
if testx[/[<>]/] then
|
188
|
-
xml.testx do
|
189
|
-
xml.cdata!("\n%s\n" % testx)
|
190
|
-
end
|
191
|
-
else
|
192
|
-
xml.testx testx
|
193
|
-
end
|
194
|
-
|
195
|
-
if expected[/</] then
|
196
|
-
xml.expected do
|
197
|
-
xml.cdata!("\n%s\n" % expected)
|
198
|
-
end
|
199
|
-
else
|
200
|
-
xml.expected expected
|
201
|
-
end
|
202
|
-
|
203
|
-
xml.actual
|
204
|
-
xml.error
|
205
|
-
end
|
206
|
-
end
|
207
|
-
end
|
208
|
-
end
|
209
|
-
|
210
|
-
Rexle.new(a).xml pretty: true
|
118
|
+
def summary()
|
119
|
+
success = @success.map(&:first)
|
120
|
+
a = @success.map(&:last).sort
|
121
|
+
{
|
122
|
+
score: [success.grep(true), success].map(&:length).join('/'),
|
123
|
+
failed: @success.select{|x| x[0] == false}.map(&:last).sort
|
124
|
+
}
|
211
125
|
end
|
212
|
-
|
213
126
|
end
|
metadata
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: testdata
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease:
|
5
|
+
version: 0.8.0
|
5
6
|
platform: ruby
|
6
7
|
authors: []
|
7
8
|
|
@@ -9,19 +10,20 @@ autorequire:
|
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
12
|
|
12
|
-
date: 2011-
|
13
|
+
date: 2011-03-31 00:00:00 +01:00
|
13
14
|
default_executable:
|
14
15
|
dependencies:
|
15
16
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
17
|
-
|
18
|
-
|
19
|
-
|
17
|
+
name: app-routes
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
20
21
|
requirements:
|
21
22
|
- - ">="
|
22
23
|
- !ruby/object:Gem::Version
|
23
24
|
version: "0"
|
24
|
-
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
25
27
|
description:
|
26
28
|
email:
|
27
29
|
executables: []
|
@@ -42,21 +44,21 @@ rdoc_options: []
|
|
42
44
|
require_paths:
|
43
45
|
- lib
|
44
46
|
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
45
48
|
requirements:
|
46
49
|
- - ">="
|
47
50
|
- !ruby/object:Gem::Version
|
48
51
|
version: "0"
|
49
|
-
version:
|
50
52
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
51
54
|
requirements:
|
52
55
|
- - ">="
|
53
56
|
- !ruby/object:Gem::Version
|
54
57
|
version: "0"
|
55
|
-
version:
|
56
58
|
requirements: []
|
57
59
|
|
58
60
|
rubyforge_project:
|
59
|
-
rubygems_version: 1.
|
61
|
+
rubygems_version: 1.5.2
|
60
62
|
signing_key:
|
61
63
|
specification_version: 3
|
62
64
|
summary: testdata
|