power_assert 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ac2d67b15f362a642ebe57434047e526ddc386dc
4
- data.tar.gz: dc515760f9b1fa257188a4745a2a713d6522be6d
3
+ metadata.gz: 1afb2223a3e268c8295564a49a13c7feff085da7
4
+ data.tar.gz: d37fc4cfd92f123df5863ce90ed70e68da566a34
5
5
  SHA512:
6
- metadata.gz: 956fc1290880bf653dc70c59b35216db33e8bbe09517a04639b47c1d3e81c642fe682d829e1d71ecb36fc2e3f7e3c4bb04727ffa6a1ea8814b7b4621a437dd28
7
- data.tar.gz: b18c7f57d7cbe043fa4b621914a8426569f81654ccec756054d8806749f78d467d5b830776411088ea7ac839d4e09f65f9d3be31783bcd31c829f9dc647e3f18
6
+ metadata.gz: 72b22a8fe4dcf9905a19b22b6081af7273fdba4ddfdea9b7cca07c852ed3c6a5bfb5ab2db6042079a95e4e78f18736d0a034a7384aec170eab6ca16b32a4cfa5
7
+ data.tar.gz: 4946106cb77a3f81a457a8585f63e1c58ae81a2773d9065e58dce7e95b1a422cfa549668f38b4e5e5af1e2eb064b17bfa4abe63d60fe6a8d99d7abde86e7c33e
data/.travis.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 2.0.0
4
- - 2.1.2
4
+ - 2.1
5
5
  - ruby-head
data/LEGAL ADDED
File without changes
data/README.rdoc CHANGED
@@ -3,7 +3,13 @@
3
3
  Power Assert for Ruby.
4
4
 
5
5
  == Related Projects
6
+ * {test-unit}[https://github.com/test-unit/test-unit](>= 3.0.0)
6
7
  * {test-unit-power_assert}[https://github.com/k-tsj/test-unit-power_assert]
7
8
  * {minitest-power_assert}[https://github.com/hsbt/minitest-power_assert]
9
+ * {pry-power_assert}[https://github.com/yui-knk/pry-power_assert]
10
+ * {power_p}[https://github.com/k-tsj/power_p]
11
+
12
+ == Reference
13
+ * {Power Assert in Ruby (at RubyKaigi 2014) // Speaker Deck}[https://speakerdeck.com/k_tsj/power-assert-in-ruby]
8
14
 
9
15
  == Travis Build Status {<img src="https://secure.travis-ci.org/k-tsj/power_assert.png"/>}[http://travis-ci.org/k-tsj/power_assert]
data/lib/power_assert.rb CHANGED
@@ -9,18 +9,43 @@ rescue
9
9
  end
10
10
 
11
11
  require 'power_assert/version'
12
-
13
12
  require 'ripper'
14
13
 
15
- # NB: API is not fixed
16
-
17
14
  module PowerAssert
15
+ class << self
16
+ def configuration
17
+ @configuration ||= Configuration[false]
18
+ end
19
+
20
+ def configure
21
+ yield configuration
22
+ end
23
+
24
+ def start(assertion_proc, assertion_method: nil)
25
+ yield Context.new(assertion_proc, assertion_method)
26
+ end
27
+ end
28
+
29
+ Configuration = Struct.new(:lazy_inspection)
30
+ private_constant :Configuration
31
+
32
+ class InspectedValue
33
+ def initialize(value)
34
+ @value = value
35
+ end
36
+
37
+ def inspect
38
+ @value
39
+ end
40
+ end
41
+ private_constant :InspectedValue
42
+
18
43
  class Context
19
44
  Value = Struct.new(:name, :value, :column)
20
45
  Ident = Struct.new(:type, :name, :column)
21
46
 
22
47
  TARGET_CALLER_DIFF = {return: 5, c_return: 4}
23
- TARGET_CALLER_INDEX = {return: 3, c_return: 2}
48
+ TARGET_INDEX_OFFSET = {bmethod: 3, method: 2}
24
49
 
25
50
  attr_reader :message_proc
26
51
 
@@ -36,20 +61,19 @@ module PowerAssert
36
61
  @assertion_proc = assertion_proc
37
62
  @assertion_method_name = assertion_method.to_s
38
63
  @message_proc = -> {
39
- @assertion_message ||=
40
- @base_caller_length > 0 ? assertion_message(@line || '',
41
- methods || [],
42
- return_values,
43
- refs || [],
44
- assertion_proc.binding).freeze :
45
- nil
64
+ return nil if @base_caller_length < 0
65
+ @message ||= build_assertion_message(@line || '', methods || [], return_values, refs || [], assertion_proc.binding).freeze
46
66
  }
47
67
  @proc_local_variables = assertion_proc.binding.eval('local_variables').map(&:to_s)
68
+ target_thread = Thread.current
48
69
  @trace = TracePoint.new(:return, :c_return) do |tp|
49
70
  next if method_ids and ! method_ids.include?(tp.method_id)
50
71
  locs = tp.binding.eval('caller_locations')
51
- if locs.length - @base_caller_length == TARGET_CALLER_DIFF[tp.event]
52
- idx = TARGET_CALLER_INDEX[tp.event]
72
+ current_diff = locs.length - @base_caller_length
73
+ target_diff = TARGET_CALLER_DIFF[tp.event]
74
+ is_target_bmethod = current_diff < target_diff
75
+ if (is_target_bmethod or current_diff == target_diff) and Thread.current == target_thread
76
+ idx = target_diff - TARGET_INDEX_OFFSET[is_target_bmethod ? :bmethod : :method]
53
77
  unless path
54
78
  path = locs[idx].path
55
79
  lineno = locs[idx].lineno
@@ -59,7 +83,8 @@ module PowerAssert
59
83
  method_ids = methods.map(&:name).map(&:to_sym).uniq
60
84
  end
61
85
  if path == locs[idx].path and lineno == locs[idx].lineno
62
- return_values << Value[tp.method_id.to_s, tp.return_value, nil]
86
+ val = PowerAssert.configuration.lazy_inspection ? tp.return_value : InspectedValue.new(tp.return_value.inspect)
87
+ return_values << Value[tp.method_id.to_s, val, nil]
63
88
  end
64
89
  end
65
90
  end
@@ -78,8 +103,8 @@ module PowerAssert
78
103
  end
79
104
  end
80
105
 
81
- def assertion_message(line, methods, return_values, refs, proc_binding)
82
- set_column(line, methods, return_values)
106
+ def build_assertion_message(line, methods, return_values, refs, proc_binding)
107
+ set_column(methods, return_values)
83
108
  ref_values = refs.map {|i| Value[i.name, proc_binding.eval(i.name), i.column] }
84
109
  vals = (return_values + ref_values).find_all(&:column).sort_by(&:column).reverse
85
110
  if vals.empty?
@@ -98,7 +123,7 @@ module PowerAssert
98
123
  ret.join("\n")
99
124
  end
100
125
 
101
- def set_column(line, methods, return_values)
126
+ def set_column(methods, return_values)
102
127
  methods = methods.dup
103
128
  return_values.each do |val|
104
129
  idx = methods.index {|method| method.name == val.name }
@@ -125,7 +150,11 @@ module PowerAssert
125
150
  when :binary
126
151
  handle_columnless_ident(extract_idents(sexp[1]), sexp[2], extract_idents(sexp[3]))
127
152
  when :call
128
- [sexp[1], sexp[3]].flat_map {|s| extract_idents(s) }
153
+ if sexp[3] == :call
154
+ handle_columnless_ident(extract_idents(sexp[1]), :call, [])
155
+ else
156
+ [sexp[1], sexp[3]].flat_map {|s| extract_idents(s) }
157
+ end
129
158
  when :array
130
159
  sexp[1] ? sexp[1].flat_map {|s| extract_idents(s) } : []
131
160
  when :command_call
@@ -134,7 +163,12 @@ module PowerAssert
134
163
  handle_columnless_ident(extract_idents(sexp[1]), :[], extract_idents(sexp[2]))
135
164
  when :method_add_arg
136
165
  idents = extract_idents(sexp[1])
137
- idents[0..-2] + extract_idents(sexp[2]) + [idents[-1]]
166
+ if idents.empty?
167
+ # idents may be empty(e.g. ->{}.())
168
+ extract_idents(sexp[2])
169
+ else
170
+ idents[0..-2] + extract_idents(sexp[2]) + [idents[-1]]
171
+ end
138
172
  when :args_add_block
139
173
  _, (tag, ss0, *ss1), _ = sexp
140
174
  if tag == :args_add_star
@@ -192,7 +226,8 @@ module PowerAssert
192
226
  MID2SRCTXT = {
193
227
  :[] => '[',
194
228
  :+@ => '+',
195
- :-@ => '-'
229
+ :-@ => '-',
230
+ :call => '('
196
231
  }
197
232
 
198
233
  def handle_columnless_ident(left_idents, mid, right_idents)
@@ -218,11 +253,6 @@ module PowerAssert
218
253
  end
219
254
  end
220
255
  private_constant :Context
221
-
222
- def start(assertion_proc, assertion_method: nil)
223
- yield Context.new(assertion_proc, assertion_method)
224
- end
225
- module_function :start
226
256
  end
227
257
 
228
258
  if defined? RubyVM
@@ -1,3 +1,3 @@
1
1
  module PowerAssert
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/power_assert.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
8
8
  s.email = ['kazuki@callcc.net']
9
9
  s.homepage = 'https://github.com/k-tsj/power_assert'
10
10
  s.summary = %q{Power Assert for Ruby}
11
- s.description = %q{Power Assert for Ruby}
11
+ s.description = %q{Power Assert for Ruby. Power Assert shows each value of variables and method calls in the expression. It is useful for testing, providing which value wasn't correct when the condition is not satisfied.}
12
12
 
13
13
  s.files = `git ls-files`.split("\n")
14
14
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -106,6 +106,13 @@ class TestPowerAssert < Test::Unit::TestCase
106
106
 
107
107
  [[[:method, "b", 5]],
108
108
  ':"a#{b}c"'],
109
+
110
+ # not supported
111
+ [[],
112
+ '->{}.()'],
113
+
114
+ [[[:method, "a", 0], [:method, "b", 3], [:method, "call", 2]],
115
+ 'a.(b)'],
109
116
  ]
110
117
 
111
118
  EXTRACT_METHODS_TEST.each_with_index do |(expect, source), idx|
@@ -124,6 +131,10 @@ class TestPowerAssert < Test::Unit::TestCase
124
131
  end
125
132
  end
126
133
 
134
+ define_method(:bmethod) do
135
+ false
136
+ end
137
+
127
138
  def test_assertion_message
128
139
  a = 0
129
140
  @b = 1
@@ -214,5 +225,45 @@ END
214
225
  END
215
226
  ! a != (+a == -a)
216
227
  }
228
+
229
+ assert_equal <<END.chomp, assertion_message {
230
+ bmethod
231
+ |
232
+ false
233
+ END
234
+ bmethod
235
+ }
236
+ end
237
+
238
+ def test_lazy_inspection
239
+ PowerAssert.configure do |c|
240
+ assert !c.lazy_inspection
241
+ end
242
+ assert_equal <<END.chomp, assertion_message {
243
+ 'a'.sub(/./, 'b').sub!(/./, 'c')
244
+ | |
245
+ | "c"
246
+ "b"
247
+ END
248
+ 'a'.sub(/./, 'b').sub!(/./, 'c')
249
+ }
250
+
251
+ PowerAssert.configure do |c|
252
+ c.lazy_inspection = true
253
+ end
254
+ begin
255
+ assert_equal <<END.chomp, assertion_message {
256
+ 'a'.sub(/./, 'b').sub!(/./, 'c')
257
+ | |
258
+ | "c"
259
+ "c"
260
+ END
261
+ 'a'.sub(/./, 'b').sub!(/./, 'c')
262
+ }
263
+ ensure
264
+ PowerAssert.configure do |c|
265
+ c.lazy_inspection = false
266
+ end
267
+ end
217
268
  end
218
269
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: power_assert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kazuki Tsujimoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-05 00:00:00.000000000 Z
11
+ date: 2014-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test-unit
@@ -52,7 +52,9 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: Power Assert for Ruby
55
+ description: Power Assert for Ruby. Power Assert shows each value of variables and
56
+ method calls in the expression. It is useful for testing, providing which value
57
+ wasn't correct when the condition is not satisfied.
56
58
  email:
57
59
  - kazuki@callcc.net
58
60
  executables: []
@@ -65,6 +67,7 @@ files:
65
67
  - BSDL
66
68
  - COPYING
67
69
  - Gemfile
70
+ - LEGAL
68
71
  - README.rdoc
69
72
  - Rakefile
70
73
  - lib/power_assert.rb