kronk 1.5.4 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc CHANGED
@@ -1,3 +1,17 @@
1
+ === 1.6.0 / 2011-09-11
2
+
3
+ * Major Enhancements:
4
+
5
+ * Diff output limited context with a default of 3 lines.
6
+
7
+ * Enhancements:
8
+
9
+ * Parsed headers output is a Hash of Strings instead of Arrays.
10
+
11
+ * Bugfixes:
12
+
13
+ * Path transaction fixes for accessing and yielding already removed data.
14
+
1
15
  === 1.5.4 / 2011-09-02
2
16
 
3
17
  * Bugfixes:
data/Manifest.txt CHANGED
@@ -12,7 +12,10 @@ lib/kronk/data_renderer.rb
12
12
  lib/kronk/diff.rb
13
13
  lib/kronk/diff/ascii_format.rb
14
14
  lib/kronk/diff/color_format.rb
15
+ lib/kronk/diff/output.rb
15
16
  lib/kronk/path.rb
17
+ lib/kronk/path/matcher.rb
18
+ lib/kronk/path/path_match.rb
16
19
  lib/kronk/path/transaction.rb
17
20
  lib/kronk/player.rb
18
21
  lib/kronk/player/benchmark.rb
@@ -38,14 +41,17 @@ test/mocks/302_response.txt
38
41
  test/mocks/cookies.yml
39
42
  test/mocks/get_request.txt
40
43
  test/test_assertions.rb
41
- test/test_core_ext.rb
42
44
  test/test_cmd.rb
43
- test/test_helper_methods.rb
45
+ test/test_core_ext.rb
46
+ test/test_data_string.rb
44
47
  test/test_diff.rb
45
48
  test/test_helper.rb
49
+ test/test_helper_methods.rb
46
50
  test/test_input_reader.rb
47
51
  test/test_kronk.rb
48
52
  test/test_path.rb
53
+ test/test_path_match.rb
54
+ test/test_path_matcher.rb
49
55
  test/test_player.rb
50
56
  test/test_request.rb
51
57
  test/test_request_parser.rb
data/README.rdoc CHANGED
@@ -89,6 +89,12 @@ a regexp and values are evaluated by const_get.
89
89
  plist: PlistParser
90
90
  json: JSON
91
91
 
92
+ Number of lines of context to use for diff. Full diff is returned when
93
+ set to false:
94
+
95
+ :context: 5 # show 5 lines before and after diff
96
+ :context: false # show the full file
97
+
92
98
  How to format the diff output. Supports the "special" values
93
99
  :ascii_diff and :color_diff or any string that will correctly
94
100
  resolve to a constant:
data/TODO.rdoc ADDED
@@ -0,0 +1,12 @@
1
+ = TODO
2
+
3
+ * Support data diffing for arrays (including data struct).
4
+
5
+ * Allow for showing diffed sections of a diff only.
6
+ Do it by sorted paths? Example:
7
+ - "/path/foo": "value1"
8
+ + "/path/bar": "value1"
9
+
10
+ * Should these Path case be handled as looking for a blank node or any node?
11
+ "/path/foo="
12
+ "/path/=foo"
data/lib/kronk.rb CHANGED
@@ -14,7 +14,7 @@ require 'yaml'
14
14
  class Kronk
15
15
 
16
16
  # This gem's version.
17
- VERSION = '1.5.4'
17
+ VERSION = '1.6.0'
18
18
 
19
19
  require 'kronk/constants'
20
20
  require 'kronk/player'
@@ -26,10 +26,13 @@ class Kronk
26
26
  require 'kronk/player/input_reader'
27
27
  require 'kronk/cmd'
28
28
  require 'kronk/path'
29
+ require 'kronk/path/path_match'
30
+ require 'kronk/path/matcher'
29
31
  require 'kronk/path/transaction'
30
32
  require 'kronk/data_renderer'
31
33
  require 'kronk/diff/ascii_format'
32
34
  require 'kronk/diff/color_format'
35
+ require 'kronk/diff/output'
33
36
  require 'kronk/diff'
34
37
  require 'kronk/response'
35
38
  require 'kronk/request'
@@ -277,7 +280,8 @@ class Kronk
277
280
  @responses = [res1, res2]
278
281
  @response = res2
279
282
 
280
- @diff = Diff.new str1, str2
283
+ opts = {:labels => [res1.uri, res2.uri]}.merge @options
284
+ @diff = Diff.new str1, str2, opts
281
285
  end
282
286
 
283
287
 
data/lib/kronk/cmd.rb CHANGED
@@ -115,6 +115,12 @@ Parse and run diffs against data from live and cached http responses.
115
115
  end
116
116
 
117
117
 
118
+ opt.on('--context [NUM]', Integer,
119
+ 'Show NUM context lines for diff') do |value|
120
+ options[:context] = value || Kronk.config[:context] || 3
121
+ end
122
+
123
+
118
124
  opt.on('-q', '--brief', 'Output only whether URI responses differ') do
119
125
  Kronk.config[:brief] = true
120
126
  end
@@ -126,6 +132,11 @@ Parse and run diffs against data from live and cached http responses.
126
132
  end
127
133
 
128
134
 
135
+ opt.on('--full', 'Show the full diff') do
136
+ options[:context] = false
137
+ end
138
+
139
+
129
140
  opt.on('-i', '--include [HEADER1,HEADER2]', Array,
130
141
  'Include all or given headers in response') do |value|
131
142
  options[:with_headers] ||= []
@@ -161,7 +172,7 @@ Parse and run diffs against data from live and cached http responses.
161
172
  end
162
173
 
163
174
 
164
- opt.on('--irb', 'Start an IRB console') do
175
+ opt.on('--irb', 'Start an IRB console with the response') do
165
176
  options[:irb] = true
166
177
  end
167
178
 
@@ -182,6 +193,11 @@ Parse and run diffs against data from live and cached http responses.
182
193
  end
183
194
 
184
195
 
196
+ opt.on('--paths', 'Render data as path value pairs') do
197
+ Kronk.config[:render_paths] = true
198
+ end
199
+
200
+
185
201
  opt.on('--prev', 'Use last response to diff against') do
186
202
  options[:uris].unshift Kronk.config[:cache_file]
187
203
  end
@@ -280,7 +296,7 @@ Parse and run diffs against data from live and cached http responses.
280
296
  'Header to pass to the server request') do |value|
281
297
  options[:headers] ||= {}
282
298
 
283
- key, value = value.split /:\s*/, 2
299
+ key, value = value.split(/:\s*/, 2)
284
300
  options[:headers][key] = value.to_s.strip
285
301
  end
286
302
 
@@ -80,6 +80,7 @@ class Kronk
80
80
  DEFAULT_CONFIG = {
81
81
  :content_types => DEFAULT_CONTENT_TYPES.dup,
82
82
  :cache_file => DEFAULT_CACHE_FILE,
83
+ :context => 3,
83
84
  :cookies_file => DEFAULT_COOKIES_FILE,
84
85
  :default_host => "http://localhost:3000",
85
86
  :diff_format => :ascii_diff,
@@ -1,5 +1,62 @@
1
1
  class Kronk
2
2
 
3
+ ##
4
+ # A String with per-line metadata.
5
+
6
+ class DataString < String
7
+
8
+ attr_accessor :meta
9
+
10
+ def initialize str="", metadata=nil
11
+ @meta = [metadata].compact * str.length
12
+ super str
13
+ end
14
+
15
+
16
+ ##
17
+ # Add a string with metadata to the data string.
18
+
19
+ def append str, metadata=nil
20
+ dstr = self.class.new str
21
+ dstr.meta = [metadata] * str.length
22
+ self << dstr
23
+ end
24
+
25
+
26
+ def << str
27
+ if str.class == self.class
28
+ @meta.concat str.meta
29
+ else
30
+ @meta.concat([@meta.last] * str.length)
31
+ end
32
+ super str
33
+ end
34
+
35
+
36
+ def [] arg
37
+ dstr = self.class.new super
38
+ dstr.meta = @meta[arg]
39
+ dstr
40
+ end
41
+
42
+
43
+ def split pattern=$;, *more
44
+ arr = super
45
+ i = 0
46
+ interval = 0
47
+ interval = (self.length - arr.join.length) / (arr.length - 1) if
48
+ arr.length > 1
49
+
50
+ arr.map do |str|
51
+ ds = self.class.new str
52
+ ds.meta = @meta[i,str.length]
53
+ i += str.length + interval
54
+ ds
55
+ end
56
+ end
57
+ end
58
+
59
+
3
60
  ##
4
61
  # Creates ordered data strings for rendering to the output.
5
62
 
@@ -21,6 +78,7 @@ class Kronk
21
78
  end
22
79
  end
23
80
 
81
+
24
82
  ##
25
83
  # Returns a json data string that is diff-able, meaning sorted by
26
84
  # Hash keys when available.
@@ -40,52 +98,67 @@ class Kronk
40
98
  end
41
99
 
42
100
 
43
- def self.ordered_data_string data, struct_only=false, indent=nil, &block
101
+ ##
102
+ # Turns a data set into an ordered string output for diff-ing.
103
+
104
+ def self.ordered_data_string data, struct_only=false, path=[], &block
44
105
  i_width = Kronk.config[:indentation] || 1
45
- indent ||= 0
46
- indent += i_width
106
+ indent = (path.length + 1) * i_width
107
+ pad = " " * indent
108
+ path_str = Path.join path
47
109
 
48
110
  case data
49
111
 
50
112
  when Hash
51
- return "{}" if data.empty?
52
-
53
- output = "{\n"
113
+ return DataString.new("{}", path_str) if data.empty?
114
+ output = DataString.new "{\n", path_str
54
115
 
55
116
  sorted_keys = sort_any data.keys
56
117
 
57
118
  data_values =
58
119
  sorted_keys.map do |key|
59
- value = data[key]
60
- pad = " " * indent
61
- subdata = ordered_data_string value, struct_only, indent, &block
62
- "#{pad}#{ yield(:key, key) }#{ yield(:key_assign) } #{subdata}"
120
+ value = data[key]
121
+ new_path = path.dup << key
122
+ subdata = ordered_data_string value, struct_only, new_path, &block
123
+ line = "#{pad}#{ yield(:key, key) }#{ yield(:key_assign) } "
124
+ line = DataString.new line, path_str
125
+ line << subdata
63
126
  end
64
127
 
65
- output << data_values.join(",\n") << "\n"
66
- output << "#{" " * (indent - i_width)}}"
128
+ data_values.each_with_index do |val, i|
129
+ val << "," unless i == data_values.length - 1
130
+ output << val << "\n"
131
+ end
67
132
 
68
- when Array
69
- return "[]" if data.empty?
133
+ output.append(("#{" " * (indent - i_width)}}"), path_str)
70
134
 
71
- output = "[\n"
135
+ when Array
136
+ return DataString.new("[]", path_str) if data.empty?
137
+ output = DataString.new "[\n", path_str
72
138
 
73
139
  data_values =
74
- data.map do |value|
75
- pad = " " * indent
76
- "#{pad}#{ordered_data_string value, struct_only, indent, &block}"
140
+ (0...data.length).map do |key|
141
+ value = data[key]
142
+ new_path = path.dup << key
143
+ subdata = ordered_data_string value, struct_only, new_path, &block
144
+ line = DataString.new pad, path_str
145
+ line << subdata
77
146
  end
78
147
 
79
- output << data_values.join(",\n") << "\n"
80
- output << "#{" " * (indent - i_width)}]"
148
+ data_values.each_with_index do |val, i|
149
+ val << "," unless i == data_values.length - 1
150
+ output << val << "\n"
151
+ end
152
+
153
+ output.append(("#{" " * (indent - i_width)}]"), path_str)
81
154
 
82
155
  else
83
- struct_only ? yield(:struct, data) : yield(:value, data)
156
+ output = struct_only ? yield(:struct, data) : yield(:value, data)
157
+ DataString.new(output.to_s, path_str)
84
158
  end
85
159
  end
86
160
 
87
161
 
88
-
89
162
  ##
90
163
  # Sorts an array of any combination of string, integer, or symbols.
91
164
 
data/lib/kronk/diff.rb CHANGED
@@ -9,9 +9,9 @@ class Kronk
9
9
  ##
10
10
  # Creates a new diff from two data objects.
11
11
 
12
- def self.new_from_data data1, data2, options={}
13
- new ordered_data_string(data1, options[:struct]),
14
- ordered_data_string(data2, options[:struct])
12
+ def self.new_from_data data1, data2, opts={}
13
+ new ordered_data_string(data1, opts[:struct]),
14
+ ordered_data_string(data2, opts[:struct]), opts
15
15
  end
16
16
 
17
17
 
@@ -20,6 +20,8 @@ class Kronk
20
20
  # Hash keys when available.
21
21
 
22
22
  def self.ordered_data_string data, struct_only=false
23
+ data = Kronk::Path.pathed(data) if Kronk.config[:render_paths]
24
+
23
25
  case Kronk.config[:render_lang].to_s
24
26
  when 'ruby' then DataRenderer.ruby(data, struct_only)
25
27
  else
@@ -32,7 +34,7 @@ class Kronk
32
34
  # Adds line numbers to each lines of a String.
33
35
 
34
36
  def self.insert_line_nums str, formatter=nil
35
- format = Diff.formatter formatter || Kronk.config[:diff_format]
37
+ format = Diff::Output.formatter formatter || Kronk.config[:diff_format]
36
38
 
37
39
  out = ""
38
40
  width = str.lines.count.to_s.length
@@ -45,26 +47,14 @@ class Kronk
45
47
  end
46
48
 
47
49
 
48
- ##
49
- # Returns a formatter from a symbol or string. Returns nil if not found.
50
-
51
- def self.formatter name
52
- return AsciiFormat if name == :ascii_diff
53
- return ColorFormat if name == :color_diff
54
- Kronk.find_const name rescue name
55
- end
56
-
50
+ attr_accessor :str1, :str2, :char, :output
57
51
 
58
- attr_accessor :str1, :str2, :char, :formatter, :show_lines
59
-
60
- def initialize str1, str2, char=/\r?\n/
52
+ def initialize str1, str2, opts={}
61
53
  @str1 = str1
62
54
  @str2 = str2
63
- @char = char
64
55
  @diff_ary = nil
65
- @show_lines = Kronk.config[:show_lines]
66
- @formatter =
67
- self.class.formatter(Kronk.config[:diff_format]) || AsciiFormat
56
+ @char = opts[:char] || /\r?\n/
57
+ @output = Output.new self, opts
68
58
  end
69
59
 
70
60
 
@@ -81,6 +71,7 @@ class Kronk
81
71
 
82
72
  def create_diff
83
73
  diff_ary = []
74
+ return diff_ary if @str1.empty? && @str2.empty?
84
75
 
85
76
  arr1 = @str1.split @char
86
77
  arr2 = @str2.split @char
@@ -207,54 +198,17 @@ class Kronk
207
198
 
208
199
  ##
209
200
  # Returns a formatted output as a string.
210
- # Supported options are:
211
- # :join_char:: String - The string used to join lines; default "\n"
212
- # :show_lines:: Boolean - Insert line numbers or not; default @show_lines
213
- # :formatter:: Object - The formatter to use; default @formatter
214
-
215
- def formatted options={}
216
- options = {
217
- :join_char => "\n",
218
- :show_lines => @show_lines,
219
- :formatter => @formatter
220
- }.merge options
221
-
222
- format = options[:formatter]
223
-
224
- line1 = line2 = 0
225
201
 
226
- lines1 = @str1.lines.count
227
- lines2 = @str2.lines.count
228
-
229
- width = (lines1 > lines2 ? lines1 : lines2).to_s.length
230
-
231
- diff_array.map do |item|
232
- case item
233
- when String
234
- line1 = line1.next
235
- line2 = line2.next
236
-
237
- lines = format.lines [line1, line2], width if options[:show_lines]
238
- "#{lines}#{format.common item}"
202
+ def formatted opts={}
203
+ @output.render if any?
204
+ end
239
205
 
240
- when Array
241
- item = item.dup
242
206
 
243
- item[0] = item[0].map do |str|
244
- line1 = line1.next
245
- lines = format.lines [line1, nil], width if options[:show_lines]
246
- "#{lines}#{format.deleted str}"
247
- end
248
-
249
- item[1] = item[1].map do |str|
250
- line2 = line2.next
251
- lines = format.lines [nil, line2], width if options[:show_lines]
252
- "#{lines}#{format.added str}"
253
- end
207
+ ##
208
+ # Returns true if any diff is found.
254
209
 
255
- item
256
- end
257
- end.flatten.join options[:join_char]
210
+ def any?
211
+ !!diff_array.find{|i| Array === i }
258
212
  end
259
213
 
260
214