rspec-power_assert 0.0.2 → 0.0.3

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: f985fde5f03e7787b91ad69cf8849ceb7083d227
4
- data.tar.gz: fe7c3b67f117838f0e1d0abae1367a5aa2fc5108
3
+ metadata.gz: 5693810a622bd1930d357d53d757219f3f1f0659
4
+ data.tar.gz: b31892819f2ad71874546590b8c856b51e9a0cd1
5
5
  SHA512:
6
- metadata.gz: f2e0b10de6fc623cf9e4ac01a42eeee3f524a0e5774b0eacd4093237686d2fa5947591bdc7509593a918797ce6e30791b06db6026d38df29f3ca344215b17205
7
- data.tar.gz: 1801ce6325718bfd027579ee8514ee784d79fe137aa4efbad5aa943bd27d86dc317e499de68f2a75356693106a4c15358d32ef95de5a61cba2e5c7d1e2b8d706
6
+ metadata.gz: f9756ee18ab14800437c0fad0e43f9aa08a089d74760a5b711efa6480c8e69567107262a94f820e91e3bc4d3a81e096611cd9208417fd257fa5bab7b7bd15432
7
+ data.tar.gz: db669065a59ce0894ee10f9dcdf99c0551824cb0af74dc0cafbbcd8ac4a1efc184250c3361082d633f437794c111b91dd01b9fab92647be2a47f6a5e921541ee
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
+ -r power_assert
1
2
  --format documentation
2
3
  --color
data/README.md CHANGED
@@ -1,4 +1,8 @@
1
1
  # Rspec::PowerAssert
2
+ [![Gem Version](https://badge.fury.io/rb/rspec-power_assert.svg)](http://badge.fury.io/rb/rspec-power_assert)
3
+
4
+ ## Support
5
+ - RSpec 2.14 or later
2
6
 
3
7
  ## Installation
4
8
 
@@ -18,53 +22,137 @@ Or install it yourself as:
18
22
 
19
23
  ## Usage
20
24
 
21
- ### spec
25
+ First, add require option to `.rspec`
26
+
27
+ ```
28
+ -r power_assert
29
+ ```
30
+
31
+ Use `ExampleGroup.it_is_asserted_by` or `ExampleGroup#is_asserted_by`
32
+
22
33
  ```ruby
23
- describe Array do
24
- describe "#map" do
25
- let(:array) { [1, 2, 3] }
26
- subject { %w(a b c) }
34
+ describe Object do
35
+ it_is_asserted_by { a == b }
27
36
 
28
- before do
29
- @array = [1, 2, 3]
30
- end
37
+ it do
38
+ is_asserted_by { a == b }
39
+ end
40
+ end
41
+ ```
31
42
 
32
- it do
33
- is_asserted_by { subject.map(&:upcase) == array }
34
- end
43
+ ## Config
35
44
 
36
- it do
37
- upcased = subject.map(&:upcase)
38
- upcased.pop
39
- is_asserted_by { upcased == @array }
40
- end
45
+ ### verbose_successful_report
41
46
 
42
- it do
43
- is_expected.to eq %w(a b c)
44
- is_asserted_by { subject.map(&:upcase) == %w(A B C) }
45
- end
47
+ ```ruby
48
+ RSpec::PowerAssert.verbose_successful_report = true
49
+ ```
50
+
51
+ If it set true, Successful Example reports with `power_assert` outputs.
52
+ If it set false, Successful Example reports without `power_assert` outputs.
53
+
54
+ Default is *true*.
46
55
 
47
- it "should transform array" do
48
- is_expected.to eq %w(a b c)
49
- is_asserted_by { subject.map(&:upcase) == %w(A B C) }
56
+ ## Sample
57
+ ### spec sample
58
+ ```ruby
59
+ describe Rspec::PowerAssert do
60
+ describe Array do
61
+ describe "#map" do
62
+ let(:array) { [1, 2, 3] }
63
+ subject { %w(a b c) }
64
+
65
+ before do
66
+ @array = [1, 2, 3]
67
+ end
68
+
69
+ it do
70
+ is_asserted_by { subject.map(&:upcase) == array }
71
+ end
72
+
73
+ it do
74
+ is_asserted_by {
75
+ subject.map(&:upcase) == array
76
+ }
77
+ end
78
+
79
+ it do
80
+ is_asserted_by { subject.map(&:upcase) == %w(A B C) }
81
+ end
82
+
83
+ it do
84
+ is_asserted_by {
85
+ subject.map(&:upcase) == %w(A B C)
86
+ }
87
+ end
88
+
89
+ it do
90
+ upcased = subject.map(&:upcase)
91
+ upcased.pop
92
+ is_asserted_by { upcased == @array }
93
+ end
94
+
95
+ it "should transform array" do
96
+ is_expected.to eq %w(a b c)
97
+ is_asserted_by { subject.map(&:upcase) == %w(A B C) }
98
+ end
99
+
100
+ it "should transform array (failed)" do
101
+ is_asserted_by { subject.map(&:upcase) == %w(A B) }
102
+ end
103
+
104
+ it_is_asserted_by { subject.map(&:upcase) == %w(A B C) }
105
+
106
+ it_is_asserted_by { subject.map(&:upcase) == %w(A B) }
107
+
108
+ it_is_asserted_by do
109
+ subject.map(&:upcase) == %w(A B C)
110
+ end
111
+
112
+ it_is_asserted_by "succ each element" do
113
+ subject.map(&:succ) == ["b", "c", "e"] + @array
114
+ end
50
115
  end
51
116
  end
52
117
  end
53
118
  ```
54
119
 
55
- ### output
120
+ ### output sample
56
121
  ```
57
122
  Rspec::PowerAssert
58
123
  Array
59
124
  #map
60
125
  example at ./spec/rspec/power_assert_spec.rb:13 (FAILED - 1)
61
126
  example at ./spec/rspec/power_assert_spec.rb:17 (FAILED - 2)
62
- is_asserted_by { subject.map(&:upcase) == %w(A B C) }
127
+ should
128
+ be asserted by { subject.map(&:upcase) == %w(A B C) }
129
+ | | |
130
+ | | true
131
+ | ["A", "B", "C"]
132
+ ["a", "b", "c"]
133
+ should
134
+ be asserted by { subject.map(&:upcase) == %w(A B C) }
63
135
  | | |
64
136
  | | true
65
137
  | ["A", "B", "C"]
66
138
  ["a", "b", "c"]
139
+ example at ./spec/rspec/power_assert_spec.rb:33 (FAILED - 3)
67
140
  should transform array
141
+ should transform array (failed) (FAILED - 4)
142
+ should
143
+ be asserted by { subject.map(&:upcase) == %w(A B C) }
144
+ | | |
145
+ | | true
146
+ | ["A", "B", "C"]
147
+ ["a", "b", "c"]
148
+ example at ./spec/rspec/power_assert_spec.rb:50 (FAILED - 5)
149
+ should
150
+ be asserted by { subject.map(&:upcase) == %w(A B C) }
151
+ | | |
152
+ | | true
153
+ | ["A", "B", "C"]
154
+ ["a", "b", "c"]
155
+ succ each element (FAILED - 6)
68
156
 
69
157
  Failures:
70
158
 
@@ -76,26 +164,78 @@ Failures:
76
164
  | | false
77
165
  | ["A", "B", "C"]
78
166
  ["a", "b", "c"]
79
- # ./lib/rspec/power_assert.rb:19:in `is_asserted_by'
167
+ # ./lib/rspec/power_assert.rb:57:in `handle_result_and_message'
168
+ # ./lib/rspec/power_assert.rb:31:in `is_asserted_by'
80
169
  # ./spec/rspec/power_assert_spec.rb:14:in `block (4 levels) in <top (required)>'
81
170
 
82
171
  2) Rspec::PowerAssert Array#map
172
+ Failure/Error: is_asserted_by {
173
+ subject.map(&:upcase) == array
174
+ | | | |
175
+ | | | [1, 2, 3]
176
+ | | false
177
+ | ["A", "B", "C"]
178
+ ["a", "b", "c"]
179
+ # ./lib/rspec/power_assert.rb:57:in `handle_result_and_message'
180
+ # ./lib/rspec/power_assert.rb:31:in `is_asserted_by'
181
+ # ./spec/rspec/power_assert_spec.rb:18:in `block (4 levels) in <top (required)>'
182
+
183
+ 3) Rspec::PowerAssert Array#map
83
184
  Failure/Error: is_asserted_by { upcased == @array }
84
185
  is_asserted_by { upcased == @array }
85
186
  | | |
86
187
  | | [1, 2, 3]
87
188
  | false
88
189
  ["A", "B"]
89
- # ./lib/rspec/power_assert.rb:19:in `is_asserted_by'
90
- # ./spec/rspec/power_assert_spec.rb:20:in `block (4 levels) in <top (required)>'
91
-
92
- Finished in 0.00455 seconds (files took 0.10298 seconds to load)
93
- 4 examples, 2 failures
190
+ # ./lib/rspec/power_assert.rb:57:in `handle_result_and_message'
191
+ # ./lib/rspec/power_assert.rb:31:in `is_asserted_by'
192
+ # ./spec/rspec/power_assert_spec.rb:36:in `block (4 levels) in <top (required)>'
193
+
194
+ 4) Rspec::PowerAssert Array#map should transform array (failed)
195
+ Failure/Error: is_asserted_by { subject.map(&:upcase) == %w(A B) }
196
+ is_asserted_by { subject.map(&:upcase) == %w(A B) }
197
+ | | |
198
+ | | false
199
+ | ["A", "B", "C"]
200
+ ["a", "b", "c"]
201
+ # ./lib/rspec/power_assert.rb:57:in `handle_result_and_message'
202
+ # ./lib/rspec/power_assert.rb:31:in `is_asserted_by'
203
+ # ./spec/rspec/power_assert_spec.rb:45:in `block (4 levels) in <top (required)>'
204
+
205
+ 5) Rspec::PowerAssert Array#map
206
+ Failure/Error: it_is_asserted_by { subject.map(&:upcase) == %w(A B) }
207
+ it_is_asserted_by { subject.map(&:upcase) == %w(A B) }
208
+ | | |
209
+ | | false
210
+ | ["A", "B", "C"]
211
+ ["a", "b", "c"]
212
+ # ./lib/rspec/power_assert.rb:57:in `handle_result_and_message'
213
+ # ./lib/rspec/power_assert.rb:44:in `evaluate_example'
214
+ # ./spec/rspec/power_assert_spec.rb:50:in `block in it_is_asserted_by'
215
+
216
+ 6) Rspec::PowerAssert Array#map succ each element
217
+ Failure/Error: it_is_asserted_by "succ each element" do
218
+ subject.map(&:succ) == ["b", "c", "e"] + @array
219
+ | | | |
220
+ | | | [1, 2, 3]
221
+ | | false
222
+ | ["b", "c", "d"]
223
+ ["a", "b", "c"]
224
+ # ./lib/rspec/power_assert.rb:57:in `handle_result_and_message'
225
+ # ./lib/rspec/power_assert.rb:44:in `evaluate_example'
226
+ # ./spec/rspec/power_assert_spec.rb:56:in `block in it_is_asserted_by'
227
+
228
+ Finished in 0.01725 seconds (files took 0.12235 seconds to load)
229
+ 11 examples, 6 failures
94
230
 
95
231
  Failed examples:
96
232
 
97
233
  rspec ./spec/rspec/power_assert_spec.rb:13 # Rspec::PowerAssert Array#map
98
234
  rspec ./spec/rspec/power_assert_spec.rb:17 # Rspec::PowerAssert Array#map
235
+ rspec ./spec/rspec/power_assert_spec.rb:33 # Rspec::PowerAssert Array#map
236
+ rspec ./spec/rspec/power_assert_spec.rb:44 # Rspec::PowerAssert Array#map should transform array (failed)
237
+ rspec ./spec/rspec/power_assert_spec.rb:50 # Rspec::PowerAssert Array#map
238
+ rspec ./spec/rspec/power_assert_spec.rb:56 # Rspec::PowerAssert Array#map succ each element
99
239
  ```
100
240
 
101
241
  ## Contributing
@@ -2,21 +2,64 @@ require "rspec/power_assert/version"
2
2
  require "rspec/core"
3
3
  require "power_assert"
4
4
 
5
+ module PowerAssert
6
+ class << self
7
+ def rspec_start(assertion_proc, assertion_method: nil)
8
+ yield RSpecContext.new(assertion_proc, assertion_method)
9
+ end
10
+ end
11
+
12
+ class RSpecContext < Context
13
+ # hack for context changing
14
+ def do_yield
15
+ @trace.enable do
16
+ @base_caller_length = caller_locations.length + 2
17
+ yield
18
+ end
19
+ end
20
+ end
21
+ private_constant :RSpecContext
22
+ end
23
+
5
24
  module RSpec
6
25
  module PowerAssert
26
+ @verbose_successful_report = true
27
+
28
+ def self.verbose_successful_report
29
+ !!@verbose_successful_report
30
+ end
31
+
32
+ def self.verbose_successful_report=(verbose)
33
+ @verbose_successful_report = verbose
34
+ end
35
+
7
36
  def is_asserted_by(&blk)
8
- result = nil
9
- msg = nil
10
- ::PowerAssert.start(blk, assertion_method: __method__) do |tp|
11
- result = tp.yield
12
- msg = tp.message_proc.call
37
+ result, msg = ::PowerAssert.start(blk, assertion_method: __method__) do |tp|
38
+ [tp.yield, tp.message_proc.call]
39
+ end
40
+
41
+ handle_result_and_message(result, msg, __method__)
42
+ end
43
+
44
+ private
45
+
46
+ def evaluate_example(method_name, &blk)
47
+ # hack for context changing
48
+ pr = -> { self.instance_exec(&blk) }
49
+
50
+ result, msg = ::PowerAssert.rspec_start(pr, assertion_method: method_name) do |tp|
51
+ [tp.yield, tp.message_proc.call]
13
52
  end
14
53
 
54
+ handle_result_and_message(result, msg, method_name)
55
+ end
56
+
57
+ def handle_result_and_message(result, msg, method_name)
15
58
  if result
16
- RSpec::Matchers.last_matcher = DummyAssertionMatcher.new(msg)
59
+ RSpec::Matchers.last_matcher = DummyAssertionMatcher.new(msg, method_name.to_s)
17
60
 
18
61
  if RSpec::Matchers.respond_to?(:last_should=)
19
- RSpec::Matchers.last_should = "" # for RSpec 2
62
+ RSpec::Matchers.last_should = :should # for RSpec 2
20
63
  else
21
64
  RSpec::Matchers.last_expectation_handler = DummyExpectationHandler # for RSpec 3
22
65
  end
@@ -28,23 +71,66 @@ module RSpec
28
71
  # for generating description
29
72
  class DummyExpectationHandler
30
73
  def self.verb
31
- ""
74
+ "should"
32
75
  end
33
76
  end
77
+ private_constant :DummyExpectationHandler
34
78
 
35
- # for generating description
36
79
  class DummyAssertionMatcher
37
- def initialize(msg)
80
+ INDENT_LEVEL = 12
81
+
82
+ def initialize(msg, method_name)
38
83
  @msg = msg
84
+ @assertion_method = method_name
39
85
  end
40
86
 
41
87
  def description
42
- "\n#{@msg}"
88
+ if @msg =~ /^(\s*)#{@assertion_method}/
89
+ output = @msg.sub(/^(\s*#{@assertion_method})/, "#{display_message}")
90
+ offset = display_message.length - $1.length
91
+ else
92
+ output = @msg.sub(/^(\s*)(\S.*?)$/, "#{display_message} { \\2 }")
93
+ offset = display_message.length + 3 - $1.length
94
+ end
95
+
96
+ "#{$/}#{build_message(output, offset)}"
97
+ end
98
+
99
+ private
100
+
101
+ def build_message(output, offset)
102
+ if RSpec::PowerAssert.verbose_successful_report
103
+ output.each_line.with_index.map do |l, idx|
104
+ next l if idx == 0
105
+
106
+ if offset > 1
107
+ " " * offset + l
108
+ else
109
+ l.each_char.drop(offset.abs).join
110
+ end
111
+ end.join
112
+ else
113
+ output.each_line.first
114
+ end
43
115
  end
116
+
117
+ def display_message
118
+ " " * INDENT_LEVEL + "be asserted by"
119
+ end
120
+ end
121
+ private_constant :DummyAssertionMatcher
122
+ end
123
+
124
+ module PowerAssertExtensions
125
+ def it_is_asserted_by(description = nil, &blk)
126
+ file, lineno = blk.source_location
127
+ cmd = description ? "it(description)" : "specify"
128
+ eval %{#{cmd} do evaluate_example("#{__method__}", &blk) end}, binding, file, lineno
44
129
  end
45
130
  end
46
131
  end
47
132
 
48
133
  RSpec.configure do |config|
134
+ config.extend RSpec::PowerAssertExtensions
49
135
  config.include RSpec::PowerAssert
50
136
  end
@@ -1,5 +1,5 @@
1
1
  module Rspec
2
2
  module PowerAssert
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -15,20 +15,47 @@ describe Rspec::PowerAssert do
15
15
  end
16
16
 
17
17
  it do
18
- upcased = subject.map(&:upcase)
19
- upcased.pop
20
- is_asserted_by { upcased == @array }
18
+ is_asserted_by {
19
+ subject.map(&:upcase) == array
20
+ }
21
21
  end
22
22
 
23
23
  it do
24
- is_expected.to eq %w(a b c)
25
24
  is_asserted_by { subject.map(&:upcase) == %w(A B C) }
26
25
  end
27
26
 
27
+ it do
28
+ is_asserted_by {
29
+ subject.map(&:upcase) == %w(A B C)
30
+ }
31
+ end
32
+
33
+ it do
34
+ upcased = subject.map(&:upcase)
35
+ upcased.pop
36
+ is_asserted_by { upcased == @array }
37
+ end
38
+
28
39
  it "should transform array" do
29
40
  is_expected.to eq %w(a b c)
30
41
  is_asserted_by { subject.map(&:upcase) == %w(A B C) }
31
42
  end
43
+
44
+ it "should transform array (failed)" do
45
+ is_asserted_by { subject.map(&:upcase) == %w(A B) }
46
+ end
47
+
48
+ it_is_asserted_by { subject.map(&:upcase) == %w(A B C) }
49
+
50
+ it_is_asserted_by { subject.map(&:upcase) == %w(A B) }
51
+
52
+ it_is_asserted_by do
53
+ subject.map(&:upcase) == %w(A B C)
54
+ end
55
+
56
+ it_is_asserted_by "succ each element" do
57
+ subject.map(&:succ) == ["b", "c", "e"] + @array
58
+ end
32
59
  end
33
60
  end
34
61
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-power_assert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - joker1007
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-27 00:00:00.000000000 Z
11
+ date: 2014-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: power_assert