-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathspec.rake
192 lines (155 loc) · 4.9 KB
/
spec.rake
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
require 'fileutils'
require 'whois'
require 'whois/parser'
namespace :spec do
ROOT_DIR = File.expand_path('..', __dir__)
TARGET_DIR = File.join(ROOT_DIR, %w[spec whois parsers responses])
SOURCE_DIR = File.join(ROOT_DIR, %w[spec fixtures responses])
SOURCE_PARTS = SOURCE_DIR.split("/")
TPL_DESCRIBE = <<-RUBY.chomp!
# This file is autogenerated. Do not edit it manually.
# If you want change the content of this file, edit
#
# %{sfile}
#
# and regenerate the tests with the following rake task
#
# $ rake spec:generate
#
require 'spec_helper'
require 'whois/parsers/%{khost}.rb'
describe %{described_class}, "%{descr}" do
subject do
file = fixture("responses", "%{fixture}")
part = Whois::Record::Part.new(body: File.read(file))
described_class.new(part)
end
%{contexts}
end
RUBY
TPL_CONTEXT = <<-RUBY.chomp!
describe "#%{descr}" do
it do
%{examples}
end
end
RUBY
TPL_MATCH = <<-RUBY.chomp!
expect(subject.%{attribute}).to %{match}
RUBY
TPL_MATCH_SIZE = <<-RUBY.chomp!
expect(subject.%{attribute}.size).to eq(%{size})
RUBY
TPL_MATCH_RAISE = <<-RUBY.chomp!
expect { subject.%{attribute} }.to %{match}
RUBY
def relativize(path)
path.gsub(ROOT_DIR, "")
end
task :generate => :generate_parsers
task :generate_parsers do
Dir["#{SOURCE_DIR}/**/*.expected"].each do |source_path|
# Generate the filename and described_class name from the test file.
parts = (source_path.split("/") - SOURCE_PARTS)
khost = parts.first
kfile = parts.last
described_class = Whois::Parser.parser_klass(khost)
target_path = File.join(TARGET_DIR, *parts).gsub(".expected", "_spec.rb")
# Extract the tests from the test file
# and generates a Hash.
#
# {
# "domain" => [
# ["%s", "== \"google.biz\""]
# ],
# "created_on" => [
# ["%s", "be_a(Time)"],
# ["%s", "== Time.parse(\"2002-03-27 00:01:00 UTC\")"]
# ]
# }
#
tests = {}
match = nil
lines = File.open(source_path, "r:UTF-8")
lines.each do |line|
line.chomp!
case line
when ""
# skip empty line
when /^\s*$/, %r{^\s*//}
# skip comment line
when /^#([^\s]+)/
tests[match = Regexp.last_match(1)] = []
when /^\s+(.+?) (.+)/
tests[match] << _parse_assertion(Regexp.last_match(1), Regexp.last_match(2))
else
raise "Invalid Line `#{line}' in `#{source_path}'"
end
end
# Generate the RSpec content and
# write one file for every test.
contexts = tests.map do |attr, specs|
matches = specs.map do |method, condition|
attribute = method % attr
case condition
when /raise_error/
format(TPL_MATCH_RAISE, attribute: attribute, match: condition)
when /^%SIZE\{(\d+)\}$/
format(TPL_MATCH_SIZE, attribute: attribute, size: Regexp.last_match(1))
else
format(TPL_MATCH, attribute: attribute, match: condition)
end
end.join("\n")
format(TPL_CONTEXT, descr: attr, examples: matches)
end.join("\n")
describe = <<-RUBY
#{format(TPL_DESCRIBE, :described_class => described_class, :khost => khost, :descr => kfile, :sfile => relativize(source_path), :fixture => parts.join('/').gsub('.expected', '.txt'), :contexts => contexts)}
RUBY
print "Generating #{relativize(target_path)}... "
File.dirname(target_path).tap { |d| File.exist?(d) || FileUtils.mkdir_p(d) }
File.write(target_path, describe)
print "done!\n"
end
end
def _parse_assertion(method, condition)
m = method
c = condition.strip
case
# %s %CLASS{time} -> %s be_a(time)
when c =~ /^%CLASS\{(.+)\}$/
c = "be_a(#{_build_condition_typeof(Regexp.last_match(1))})"
# %s %TIME{...} -> %s Time.parse(...)
when c =~ /^%TIME\{(.+)\}$/
c = "eq(Time.parse(\"#{Regexp.last_match(1)}\"))"
# %s %ERROR{...} -> %s raise_error(...)
when c =~ /^%ERROR\{(.+)\}$/
c = "raise_error(Whois::#{Regexp.last_match(1)})"
# %s =~ "foo"
when c =~ /^%MATCH\{(.+)\}$/
c = "match(/#{Regexp.last_match(1)}/)"
# %s == "foo"
when c =~ /^== (.+)$/
c = "eq(#{Regexp.last_match(1)})"
end
[m, c]
end
def _build_condition_typeof(described_class)
case described_class
when "array" then "Array"
when "time" then "Time"
when "contact" then "Whois::Parser::Contact"
when "registrar" then "Whois::Parser::Registrar"
when "nameserver" then "Whois::Parser::Nameserver"
else
raise "Unknown class `#{described_class}'"
end
end
def _build_condition_typecast(described_class, value)
case described_class
when "time"
%{Time.parse("#{value}")}
else
raise "Unknown class `#{described_class}'"
end
end
end