kronk 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/yzma DELETED
@@ -1,13 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- begin
4
- require 'yzma'
5
-
6
- rescue LoadError => e
7
- raise unless e.message =~ %r{no such file to load -- yzma}
8
-
9
- $: << File.join(File.dirname(__FILE__), "../lib")
10
- require 'yzma'
11
- end
12
-
13
- Yzma.run
@@ -1,144 +0,0 @@
1
- class Kronk
2
-
3
- ##
4
- # THIS CLASS IS DEPRECATED AND WILL BE REMOVED IN KRONK-1.5.x
5
- #
6
- # Wraps a complex data structure to provide a search-driven interface.
7
-
8
- class DataSet
9
-
10
- # Deep merge proc for recursive Hash merging.
11
- DEEP_MERGE =
12
- proc do |key,v1,v2|
13
- Hash === v1 && Hash === v2 ? v1.merge(v2,&DEEP_MERGE) : v2
14
- end
15
-
16
-
17
- attr_accessor :data
18
-
19
- def initialize data
20
- @data = data
21
- end
22
-
23
-
24
- ##
25
- # Modify the data object by passing inclusive or exclusive data paths.
26
- # Supports the following options:
27
- # :only_data:: String/Array - keep data that matches the paths
28
- # :ignore_data:: String/Array - remove data that matches the paths
29
- #
30
- # Deprecated options:
31
- # :only_data_with:: String/Array - keep data with a matched child
32
- # :ignore_data_with:: String/Array - remove data with a matched child
33
- #
34
- # Note: the data is processed in the following order:
35
- # * only_data
36
- # * ignore_data
37
-
38
- def modify options
39
- warn_path_deprecation! if options[:ignore_data_with] ||
40
- options[:only_data_with]
41
-
42
- collect_data_points options[:only_data_with], true if
43
- options[:only_data_with]
44
-
45
- delete_data_points options[:ignore_data_with], true if
46
- options[:ignore_data_with]
47
-
48
- collect_data_points options[:only_data] if options[:only_data]
49
-
50
- delete_data_points options[:ignore_data] if options[:ignore_data]
51
-
52
- @data
53
- end
54
-
55
-
56
- ##
57
- # New implementation of DataSet#modify
58
-
59
- def fetch options
60
- warn_path_deprecation! if options[:ignore_data_with] ||
61
- options[:only_data_with]
62
-
63
- options[:only_data] = [*options[:only_data]].compact
64
- options[:ignore_data] = [*options[:ignore_data]].compact
65
-
66
- options[:only_data].concat(
67
- [*options[:only_data_with]].map!{|path| path << "/.."}
68
- ) if options[:only_data_with]
69
-
70
- options[:ignore_data].concat(
71
- [*options[:ignore_data_with]].map!{|path| path << "/.."}
72
- ) if options[:ignore_data_with]
73
-
74
- Path::Transaction.run @data, options do |t|
75
- t.select(*options[:only_data])
76
- t.delete(*options[:ignore_data])
77
- end
78
- end
79
-
80
-
81
- ##
82
- # Keep only specific data points from the data structure.
83
-
84
- def collect_data_points data_paths, affect_parent=false
85
- Kronk::Cmd.warn "DataSet#collect_data_points deprecated. "+
86
- "Use Path::Transaction"
87
-
88
- new_data = @data.class.new
89
-
90
- [*data_paths].each do |data_path|
91
- opts = Path.parse_regex_opts! data_path
92
- data_path << "/.." if affect_parent
93
-
94
- Path.find data_path, @data, opts do |data, k, path|
95
- curr_data = @data
96
- new_curr_data = new_data
97
-
98
- path.each_with_index do |key, i|
99
- if i == path.length - 1
100
- new_curr_data[key] = curr_data[key]
101
- else
102
- new_curr_data[key] ||= curr_data[key].class.new
103
- new_curr_data = new_curr_data[key]
104
- curr_data = curr_data[key]
105
- end
106
- end
107
- end
108
- end
109
-
110
- @data.replace new_data
111
- end
112
-
113
-
114
- ##
115
- # Remove specific data points from the data structure.
116
-
117
- def delete_data_points data_paths, affect_parent=false
118
- Kronk::Cmd.warn "DataSet#delete_data_points deprecated. "+
119
- "Use Path::Transaction"
120
-
121
- [*data_paths].each do |data_path|
122
- opts = Path.parse_regex_opts! data_path
123
- data_path << "/.." if affect_parent
124
-
125
- Path.find data_path, @data, opts do |obj, k, path|
126
- next unless obj.respond_to? :[]
127
-
128
- del_method = Array === obj ? :delete_at : :delete
129
- obj.send del_method, k
130
- end
131
- end
132
-
133
- @data
134
- end
135
-
136
-
137
- private
138
-
139
- def warn_path_deprecation!
140
- Kronk::Cmd.warn "The :ignore_data_with and :only_data_with options "+
141
- "are deprecated. Use the '/..' path notation."
142
- end
143
- end
144
- end
data/lib/yzma.rb DELETED
@@ -1,174 +0,0 @@
1
- require 'rubygems'
2
- require 'kronk'
3
-
4
- ##
5
- # Yzma bosses Kronk around to give you meaningful diff variation
6
- # statistical data:
7
- # Yzma.report "My Report" do
8
- # compare uri1, uri2, :count => 100 do
9
- # randomize_param :limit, 20..200, :optional => true, :allow_blank => true
10
- #
11
- # randomize_param :q, %w{pizza restaurant flowers}
12
- #
13
- # randomize_param :g, %w{91106 91203}
14
- # end
15
- #
16
- # compare uri3, uri4, :count => 10,
17
- # :title => "Second Compare"
18
- # end
19
-
20
- class Yzma
21
-
22
- require 'yzma/report'
23
- require 'yzma/randomizer'
24
-
25
-
26
- ##
27
- # Parses ARGV for Yzma.
28
-
29
- def self.parse_args argv
30
- options = {:files => []}
31
-
32
- opts = OptionParser.new do |opt|
33
- opt.program_name = File.basename $0
34
- opt.version = Kronk::VERSION
35
- opt.release = nil
36
-
37
- opt.banner = <<-STR
38
-
39
- #{opt.program_name} #{opt.version}
40
-
41
- Run diff reports between two URI requests.
42
-
43
- Usage:
44
- #{opt.program_name} --help
45
- #{opt.program_name} --version
46
- #{opt.program_name} file1 [file2 ...]
47
- STR
48
- end
49
-
50
- opts.parse! argv
51
-
52
- options[:files] = argv.dup
53
-
54
- if options[:files].empty?
55
- $stderr << "\nError: At least one report file must be specified.\n"
56
- $stderr << "See 'yzma --help' for usage\n\n"
57
- exit 1
58
- end
59
-
60
- options
61
- end
62
-
63
-
64
- ##
65
- # Construct and run an Yzma report.
66
-
67
- def self.report name_or_report=nil, &block
68
- yzma = new name_or_report
69
-
70
- yzma.instance_eval(&block)
71
-
72
- yzma.report.header << "Ran #{yzma.comparisons} URI comparison(s)"
73
- yzma.report.header << "Iterated a total of #{yzma.iterations} case(s)"
74
-
75
- curr_req = nil
76
-
77
- yzma.report.write do |req, data|
78
- next unless data[:diff] > 0
79
- "\n#{data[:diff]} avg diffs:\n#{req[0]} - #{req[1]}\n"
80
- end
81
- end
82
-
83
-
84
- ##
85
- # Run the Yzma command.
86
-
87
- def self.run argv=ARGV
88
- options = parse_args argv
89
- options[:files].each do |file|
90
- self.instance_eval File.read(file)
91
- end
92
-
93
- exit 2 if self.diffs > 0
94
- end
95
-
96
-
97
- class << self
98
- attr_accessor :diffs
99
- end
100
-
101
- self.diffs = 0
102
-
103
-
104
- attr_reader :report, :comparisons, :iterations
105
-
106
- ##
107
- # Initialize Izma with optional name.
108
-
109
- def initialize name_or_report=nil
110
- case name_or_report
111
- when String
112
- @name = name_or_report
113
- @report = Report.new @name
114
-
115
- when Report
116
- @report = name_or_report
117
- @name = @report.name
118
-
119
- else
120
- @name = 'Yzma Report'
121
- @report = Report.new @name
122
- end
123
-
124
- @comparisons = 0
125
- @iterations = 0
126
- end
127
-
128
-
129
- ##
130
- # Compare two paths or uris. Second uri may be omitted.
131
- # Supports all Kronk.compare options, plus:
132
- # :count:: Integer - number of times to run the endpoint; default 1.
133
- # :title:: String - title to display when running compares.
134
-
135
- def compare uri1, uri2, options={}, &block
136
- options = options.dup
137
- count = options.delete(:count) || 1
138
- title = options.delete(:title) || "#{uri1} --- #{uri2}"
139
-
140
- @comparisons = @comparisons.next
141
-
142
- diff_avg = 0
143
- diff_cnt = 0
144
-
145
- puts title
146
- 1.upto(count) do |i|
147
- randomizer = Randomizer.new
148
- randomizer.instance_eval &block if block_given?
149
-
150
- randomized_opts = options.merge randomizer.to_options
151
-
152
- begin
153
- diff = Kronk.compare uri1, uri2, randomized_opts
154
-
155
- diff_avg = (diff_avg * diff_cnt + diff.count) / (diff_cnt + 1)
156
- diff_cnt = diff_cnt.next
157
-
158
- @iterations = @iterations.next
159
-
160
- $stdout << (diff.count > 0 ? "D" : ".")
161
- self.class.diffs = self.class.diffs + diff.count
162
-
163
- rescue Kronk::Request::NotFoundError
164
- $stdout << "E"
165
- end
166
-
167
- $stdout.flush
168
- end
169
-
170
- @report.add [uri1, uri2], :diff => diff_avg
171
-
172
- $stdout << "\n\n"
173
- end
174
- end
@@ -1,54 +0,0 @@
1
- class Yzma
2
-
3
- ##
4
- # Randomizes params, data, and headers for Yzma requests.
5
-
6
- class Randomizer
7
-
8
- def initialize
9
- @options = {}
10
- end
11
-
12
-
13
- def to_options
14
- @options.dup
15
- end
16
-
17
-
18
- def randomize_param name, values, options={}
19
- @options[:query] ||= {}
20
- assign_random @options[:query], name, values, options
21
- end
22
-
23
-
24
- def randomize_data name, values, options={}
25
- @options[:data] ||= {}
26
- assign_random @options[:data], name, values, options
27
- end
28
-
29
-
30
- def randomize_header name, values, options={}
31
- @options[:headers] ||= {}
32
- assign_random @options[:headers], name, values, options
33
- end
34
-
35
-
36
- def assign_random obj, key, values, options={}
37
- random_value = pick_random values, options
38
- obj[key] = random_value if random_value
39
- end
40
-
41
-
42
- def pick_random val, options={}
43
- val = File.readlines val if String === val
44
- val = val.to_a if Range === val
45
-
46
- val << "" if options[:allow_blank]
47
- val << nil if options[:optional]
48
-
49
- return if val.empty?
50
-
51
- val[rand(val.length)]
52
- end
53
- end
54
- end
data/lib/yzma/report.rb DELETED
@@ -1,47 +0,0 @@
1
- class Yzma
2
-
3
- ##
4
- # Used to build and display Yzma reports.
5
- # Other report types can be created by inheriting this class and
6
- # overridding the 'write' method to use @data as desired.
7
-
8
- class Report
9
-
10
- attr_accessor :data, :header, :footer, :name
11
-
12
- def initialize name
13
- @name = name
14
- @data = []
15
- @header = ["#{@name} #{Time.now}"]
16
- @footer = ["\n"]
17
- end
18
-
19
-
20
- ##
21
- # Adds data and a related piece of data identification to
22
- # the report.
23
-
24
- def add identifier, data
25
- @data << [identifier, data]
26
- end
27
-
28
-
29
- ##
30
- # Generates and writes the report to the given IO instance.
31
- # Defaults to STDOUT.
32
-
33
- def write io=$stdout
34
- io << @header.join("\n")
35
-
36
- @data.each do |identifier, data|
37
- if block_given?
38
- io << yield(identifier, data)
39
- else
40
- io << "\n#{identifier.inspect}\n#{data.inspect}\n"
41
- end
42
- end
43
-
44
- io << @footer.join("\n")
45
- end
46
- end
47
- end
@@ -1,213 +0,0 @@
1
- require 'test/test_helper'
2
-
3
- class TestDataSet < Test::Unit::TestCase
4
-
5
- def setup
6
- @data = {
7
- :key1 => {
8
- :key1a => [
9
- "foo",
10
- "bar",
11
- "foobar",
12
- {:findme => "thing"}
13
- ],
14
- 'key1b' => "findme"
15
- },
16
- 'findme' => [
17
- 123,
18
- 456,
19
- {:findme => 123456}
20
- ],
21
- :key2 => "foobar",
22
- :key3 => {
23
- :key3a => ["val1", "val2", "val3"]
24
- }
25
- }
26
-
27
- @dataset = Kronk::DataSet.new @data
28
-
29
- @dataset_mock = Kronk::DataSet.new mock_data
30
-
31
- Kronk::Cmd.expects :warn
32
- end
33
-
34
-
35
- def test_modify_only_data
36
- data = @dataset_mock.modify :only_data => "subs/1"
37
- assert_equal({"subs" => [nil, "b"]}, data)
38
- end
39
-
40
-
41
- def test_modify_ignore_data
42
- data = @dataset_mock.modify :ignore_data => "subs/1"
43
-
44
- expected = mock_data
45
- expected['subs'].delete_at 1
46
-
47
- assert_equal expected, data
48
- end
49
-
50
-
51
- def test_modify_only_data_with
52
- Kronk::Cmd.expects :warn
53
- data = @dataset_mock.modify :only_data_with => "subs/1"
54
- assert_equal({"subs" => ["a", "b"]}, data)
55
- end
56
-
57
-
58
- def test_modify_only_and_ignored_data
59
- Kronk::Cmd.expects :warn
60
- data = @dataset_mock.modify :ignore_data => "subs/1", :only_data => "subs/1"
61
- assert_equal({"subs" => [nil]}, data)
62
- end
63
-
64
-
65
- def test_collect_data_points_affect_parent_array
66
- data = @dataset_mock.collect_data_points "**=(A|a)?", true
67
-
68
- expected = {
69
- "root" => [nil, ["A1", "A2"]],
70
- "subs" => ["a", "b"]
71
- }
72
-
73
- assert_equal expected, data
74
- end
75
-
76
-
77
- def test_collect_data_points_affect_parent_hash
78
- data = @dataset_mock.collect_data_points "**=bar?", true
79
- assert_equal({"tests"=>{:foo=>:bar, "test"=>[[1, 2], 2.123]}}, data)
80
- end
81
-
82
-
83
- def test_collect_data_points_single
84
- data = @dataset_mock.collect_data_points "subs/1"
85
- assert_equal({"subs" => [nil, "b"]}, data)
86
- end
87
-
88
-
89
- def test_collect_data_points_single_wildcard
90
- data = @dataset_mock.collect_data_points "root/*/tests"
91
- assert_equal({"root"=>[nil, nil, nil, {:tests=>["D3a", "D3b"]}]}, data)
92
- end
93
-
94
-
95
- def test_collect_data_points_recursive_wildcard
96
- data = @dataset_mock.collect_data_points "**/test?"
97
-
98
- expected = {
99
- "tests"=>{:foo=>:bar, "test"=>[[1, 2], 2.123]},
100
- "root"=>[nil, nil, nil, {
101
- :tests=>["D3a", "D3b"],
102
- "test"=>[["D1a\nContent goes here", "D1b"], "D2"]}]
103
- }
104
-
105
- assert_equal expected, data
106
- end
107
-
108
-
109
- def test_collect_data_points_recursive_wildcard_value
110
- data = @dataset_mock.collect_data_points "**=(A|a)?"
111
-
112
- expected = {
113
- "root" => [nil, ["A1", "A2"]],
114
- "subs" => ["a"]
115
- }
116
-
117
- assert_equal expected, data
118
- end
119
-
120
-
121
- def test_delete_data_points_affect_parent_array
122
- data = @dataset_mock.delete_data_points "**/test/0/*", true
123
-
124
- expected = mock_data
125
- expected['root'][3]['test'].delete_at 0
126
- expected['tests']['test'].delete_at 0
127
-
128
- assert_equal expected, data
129
- end
130
-
131
-
132
- def test_delete_data_points_affect_parent_array_value
133
- data = @dataset_mock.delete_data_points "**/test/0/*=D*", true
134
-
135
- expected = mock_data
136
- expected['root'][3]['test'].delete_at 0
137
-
138
- assert_equal expected, data
139
- end
140
-
141
-
142
- def test_delete_data_points_affect_parent_hash
143
- data = @dataset_mock.delete_data_points "subs/1", true
144
-
145
- expected = mock_data
146
- expected.delete 'subs'
147
-
148
- assert_equal expected, data
149
- end
150
-
151
-
152
- def test_delete_data_points_affect_parent_hash_value
153
- data = @dataset_mock.delete_data_points "**/*=a", true
154
-
155
- expected = mock_data
156
- expected.delete 'subs'
157
-
158
- assert_equal expected, data
159
- end
160
-
161
-
162
- def test_delete_data_points_single
163
- data = @dataset_mock.delete_data_points "subs/1"
164
-
165
- expected = mock_data
166
- expected['subs'].delete_at 1
167
-
168
- assert_equal expected, data
169
- end
170
-
171
-
172
- def test_delete_data_points_single_wildcard
173
- data = @dataset_mock.delete_data_points "root/*/tests"
174
-
175
- expected = mock_data
176
- expected['root'][3].delete :tests
177
-
178
- assert_equal expected, data
179
- end
180
-
181
-
182
- def test_delete_data_points_single_wildcard_qmark
183
- data = @dataset_mock.delete_data_points "subs/?"
184
-
185
- expected = mock_data
186
- expected['subs'].clear
187
-
188
- assert_equal expected, data
189
- end
190
-
191
-
192
- def test_delete_data_points_recursive_wildcard
193
- data = @dataset_mock.delete_data_points "**/test?"
194
-
195
- expected = mock_data
196
- expected['root'][3].delete :tests
197
- expected['root'][3].delete 'test'
198
- expected.delete "tests"
199
-
200
- assert_equal expected, data
201
- end
202
-
203
-
204
- def test_delete_data_points_recursive_wildcard_value
205
- data = @dataset_mock.delete_data_points "**=A?|a?"
206
-
207
- expected = mock_data
208
- expected['root'][1].clear
209
- expected['subs'] = ["b"]
210
-
211
- assert_equal expected, data
212
- end
213
- end