assert_value 1.0 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,7 +4,9 @@ Checks that two values are same and "magically" replaces expected value
4
4
  with the actual in case the new behavior (and new actual value) is correct.
5
5
  Support two kind of arguments: string and code block.
6
6
 
7
- ## String Example:
7
+ This check works with both Test/Unit and RSpec. See documentation and examples below:
8
+
9
+ ## Testing String Values
8
10
 
9
11
  It is better to start with no expected value
10
12
 
@@ -24,9 +26,9 @@ If you accept the new value your test will be automatically modified to
24
26
  foo
25
27
  END
26
28
 
27
- ## Block Example:
29
+ ## Testing Block Values
28
30
 
29
- assert_value supports code block as argument. If executed block raises exception then
31
+ assert_value can take code block as an argument. If executed block raises exception then
30
32
  exception message is returned as actual value:
31
33
 
32
34
  assert_value do
@@ -48,7 +50,13 @@ After the new value is accepted you get
48
50
  nil + 1
49
51
  end
50
52
 
51
- ## Options:
53
+ ## Testing Values Stored in Files
54
+
55
+ Sometimes test string is too large to be inlined into the test source. Put it into the file instead:
56
+
57
+ assert_value "foo", log: 'test/log/reference.txt'
58
+
59
+ ## Additional Test/Unit Options:
52
60
 
53
61
  --no-interactive skips all questions and just reports failures
54
62
  --autoaccept prints diffs and automatically accepts all new actual values
@@ -66,6 +74,29 @@ In Ruby 1.9:
66
74
  ruby test/unit/foo_test.rb --autoaccept
67
75
  rake test TESTOPTS="--autoaccept"
68
76
 
77
+ ## RSpec
78
+
79
+ In specs you can either call assert_value directly or use "be_same_value_as" matcher:
80
+
81
+ describe "spec with be_same_value_as matcher" do
82
+ it "compares with inline value" do
83
+ expect("foo").to be_same_value_as <<-END
84
+ foo
85
+ END
86
+ end
87
+
88
+ it "compares with inline block" do
89
+ expect { "foo" }.to be_same_value_as <<-END
90
+ foo
91
+ END
92
+ end
93
+
94
+ it "compares with value in file" do
95
+ expect("foo").to be_same_value_as(:log => 'test/logs/assert_value_with_files.log.ref')
96
+ end
97
+ end
98
+
99
+
69
100
  ## Canonicalization:
70
101
 
71
102
  Before comparing expected and actual strings, assert_value canonicalizes both using these rules:
@@ -90,6 +121,7 @@ In Ruby 1.9:
90
121
 
91
122
  ## Changelog
92
123
 
124
+ - 1.1: RSpec support
93
125
  - 1.0: Rename to assert_value
94
126
  - 0.7: Support Ruby 1.9's MiniTest
95
127
  - 0.6: Support test execution on Mac
data/assert_value.gemspec CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
 
3
3
  SPEC = Gem::Specification.new do |s|
4
4
  s.name = "assert_value"
5
- s.version = "1.0"
5
+ s.version = "1.1"
6
6
  s.author = "Pluron, Inc."
7
7
  s.email = "support@pluron.com"
8
8
  s.homepage = "http://github.com/acunote/assert_value"
data/lib/assert_value.rb CHANGED
@@ -60,7 +60,7 @@ end
60
60
  module AssertValueAssertion
61
61
 
62
62
  def file_offsets
63
- @file_offsets ||= Hash.new { |hash, key| hash[key] = {} }
63
+ @@file_offsets ||= Hash.new { |hash, key| hash[key] = {} }
64
64
  end
65
65
 
66
66
  # assert_value: assert which checks that two strings (expected and actual) are same
@@ -167,7 +167,9 @@ module AssertValueAssertion
167
167
  # assert_value, run tests and they will fill expected values for you automatically
168
168
  def assert_value(*args)
169
169
  if block_given?
170
- mode = :block
170
+ # rspec passes block to the expect() function, not to the matcher
171
+ # so string substitution should work as if assert_value is called with a string
172
+ mode = @rspec_matcher ? :scalar : :block
171
173
  expected = args[0]
172
174
  actual = ""
173
175
  begin
@@ -250,6 +252,10 @@ module AssertValueAssertion
250
252
  end
251
253
  end
252
254
 
255
+ def be_same_value_as(expected = nil)
256
+ BeSameValueAs.new(expected)
257
+ end
258
+
253
259
  private
254
260
 
255
261
  def succeed
@@ -282,7 +288,8 @@ private
282
288
  # change - what to do with expected value (:create_expected_string or :update_expected_string)
283
289
  # mode - describes signature of assert_value call by type of main argument (:block or :scalar)
284
290
  def accept_string(actual, change, mode)
285
- file, method, line = get_caller_location(:depth => 3)
291
+ depth = @rspec_matcher ? 6 : 3
292
+ file, method, line = get_caller_location(:depth => depth)
286
293
 
287
294
  # read source file, construct the new source, replacing everything
288
295
  # between "do" and "end" in assert_value's block
@@ -315,7 +322,8 @@ private
315
322
  if change == :create_expected_string
316
323
  if mode == :scalar
317
324
  # add second argument to assert_value if it's omitted
318
- source[expected_text_start_line-1] = "#{source[expected_text_start_line-1].chop}, <<-END\n"
325
+ comma = "," unless @rspec_matcher
326
+ source[expected_text_start_line-1] = "#{source[expected_text_start_line-1].chop}#{comma} <<-END\n"
319
327
  elsif mode == :block
320
328
  # add expected value as argument to assert_value before block call
321
329
  source[expected_text_start_line-1] = source[expected_text_start_line-1].sub(/assert_value(\(.*?\))*/, "assert_value(<<-END)")
@@ -407,6 +415,33 @@ else
407
415
  end
408
416
  end
409
417
 
418
+ # RSpec matcher for assert_value
419
+ class BeSameValueAs
420
+ include AssertValueAssertion
421
+
422
+ def initialize(expected)
423
+ @expected = expected
424
+ @rspec_matcher = true
425
+ end
426
+
427
+ def matches?(target)
428
+ if target.is_a? Proc
429
+ assert_value @expected, &target
430
+ else
431
+ assert_value target, @expected
432
+ end
433
+ end
434
+
435
+ def failure_message_for_should
436
+ "expected to be the same"
437
+ end
438
+
439
+ def failure_message_for_should_not
440
+ "expected not to be the same"
441
+ end
442
+ end
443
+
444
+
410
445
  if defined?(RSpec)
411
446
  RSpec.configure do |c|
412
447
  c.include AssertValueAssertion
@@ -2,27 +2,42 @@ require 'assert_value'
2
2
 
3
3
  describe "Assert Value" do
4
4
 
5
- it "compares with value in file" do
6
- assert_value("foo", :log => 'test/logs/assert_value_with_files.log.ref')
7
- expect(assert_value("foo", :log => 'test/logs/assert_value_with_files.log.ref')).to eq(true)
8
- end
9
-
10
- it "compares with value inlined in source file" do
11
- assert_value "foo", <<-END
12
- foo
13
- END
14
-
15
- expect(
16
- assert_value "foo", <<-END
17
- foo
18
- END
19
- ).to eq(true)
20
-
21
- expect(assert_value(<<-END) do
22
- foo
23
- END
24
- "foo"
25
- end).to eq(true)
26
- end
5
+ describe "true rspec blessed way" do
6
+ it "compares with inline value" do
7
+ expect("foo").to be_same_value_as <<-END
8
+ foo
9
+ END
10
+ end
11
+
12
+ it "compares with inline block" do
13
+ expect { "foo" }.to be_same_value_as <<-END
14
+ foo
15
+ END
16
+ end
17
+
18
+ it "compares with value in file" do
19
+ expect("foo").to be_same_value_as(:log => 'test/logs/assert_value_with_files.log.ref')
20
+ end
21
+ end
22
+
23
+ describe "test/unit way is also supported" do
24
+ it "compares with inline value" do
25
+ assert_value "foo", <<-END
26
+ foo
27
+ END
28
+ end
29
+
30
+ it "compares with inline block" do
31
+ assert_value(<<-END) do
32
+ foo
33
+ END
34
+ "foo"
35
+ end
36
+ end
37
+
38
+ it "compares with value in file" do
39
+ assert_value("foo", :log => 'test/logs/assert_value_with_files.log.ref')
40
+ end
41
+ end
27
42
 
28
43
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assert_value
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: '1.1'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-24 00:00:00.000000000 Z
12
+ date: 2013-10-28 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: assert_value assertion
15
15
  email: support@pluron.com