wrong 0.5.4 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -16,7 +16,7 @@ Inspired by [assert { 2.0 }](http://assert2.rubyforge.org/) but rewritten from s
16
16
 
17
17
  We have deployed gems for both Ruby and JRuby; if you get dependency issues on your platform, please let us know what Ruby interpreter and version you're using and what errors you get, and we'll try to track it down.
18
18
 
19
- ## Usage ##
19
+ ## Usage
20
20
 
21
21
  Wrong provides a simple assert method that takes a block:
22
22
 
@@ -56,6 +56,16 @@ And a companion, 'deny':
56
56
 
57
57
  deny{'abc'.include?('bc')}
58
58
  ==> Didn't expect "abc".include?("bc")
59
+
60
+ More examples are in the file `examples.rb` <http://github.com/alexch/wrong/blob/master/examples.rb>
61
+
62
+ There's also a spreadsheet showing a translation from Test::Unit and RSpec to Wrong, with notes, at [this Google Doc](https://spreadsheets.google.com/pub?key=0AouPn6oLrimWdE0tZDVOWnFGMzVPZy0tWHZwdnhFYkE&hl=en&output=html). (Ask <alexch@gmail.com> if you want editing privileges.)
63
+
64
+ And don't miss the [slideshare presentation](http://www.slideshare.net/alexchaffee/wrong-5069976).
65
+
66
+ ## Helper methods
67
+
68
+ ### rescuing
59
69
 
60
70
  There's also a convenience method for catching errors:
61
71
 
@@ -66,6 +76,8 @@ There's also a convenience method for catching errors:
66
76
  rescuing { raise("vanilla") } is #<RuntimeError: vanilla>
67
77
  raise("vanilla") raises RuntimeError: vanilla
68
78
 
79
+ ### capturing
80
+
69
81
  And one for capturing output streams:
70
82
 
71
83
  assert { capturing { puts "hi" } == "hi\n" }
@@ -75,12 +87,16 @@ And one for capturing output streams:
75
87
  assert { out == "something standard\n" }
76
88
  assert { err =~ /something erroneous/ }
77
89
 
90
+ ### close_to?
91
+
78
92
  If you want to compare floats, try this:
79
93
 
80
94
  assert { 5.0.close_to?(5.0001) } # default tolerance = 0.001
81
95
  assert { 5.0.close_to?(5.1, 0.5) } # optional tolerance parameter
82
96
 
83
- (If you don't want `close_to?` cluttering up `Float` in your test runs then use `include Wrong::Assert` instead of `include Wrong`.)
97
+ If you don't want `close_to?` cluttering up `Float` in your test runs then use `include Wrong::Assert` instead of `include Wrong`.
98
+
99
+ ### d
84
100
 
85
101
  We also implement the most amazing debugging method ever, `d`, which gives you a sort of mini-wrong wherever you want it
86
102
  , even in production code at runtime:
@@ -90,13 +106,9 @@ We also implement the most amazing debugging method ever, `d`, which gives you a
90
106
  d { x } # => prints "x is 7" to the console
91
107
  d { x * 2 } # => prints "(x * 2) is 14" to the console
92
108
 
93
- (`d` was originally implemented by Rob Sanheim in LogBuddy; as with Assert2 this version is a rewrite and homage.) Remember, if you want `d` to work at runtime (e.g. in a webapp) then you must `include Wrong::D` inside your app, e.g. in your `environment.rb` file.
109
+ `d` was originally implemented by Rob Sanheim in LogBuddy; as with Assert2 this version is a rewrite and homage. You may also enjoy [g](https://github.com/jugyo/g) by [jugyo](http://jugyo.org/).
94
110
 
95
- More examples are in the file `examples.rb` <http://github.com/alexch/wrong/blob/master/examples.rb>
96
-
97
- There's also a spreadsheet showing a translation from Test::Unit and RSpec to Wrong, with notes, at [this Google Doc](https://spreadsheets.google.com/pub?key=0AouPn6oLrimWdE0tZDVOWnFGMzVPZy0tWHZwdnhFYkE&hl=en&output=html). (Ask <alexch@gmail.com> if you want editing privileges.)
98
-
99
- And don't miss the [slideshare presentation](http://www.slideshare.net/alexchaffee/wrong-5069976).
111
+ Remember, if you want `d` to work at runtime (e.g. in a webapp) then you must `include Wrong::D` inside your app, e.g. in your `environment.rb` file.
100
112
 
101
113
  ## Test Framework Adapters ##
102
114
 
@@ -250,7 +262,6 @@ Before you get your knickers in a twist about how this is totally unacceptable b
250
262
  * Beware of Side Effects! (See discussion elsewhere in this document.)
251
263
  * "Doesn't all this parsing slow down my test run"? No - this applies to failure cases only. If the assert block returns true then Wrong simply moves on.
252
264
 
253
-
254
265
  ## Explanations ##
255
266
 
256
267
  `assert` and `deny` can take an optional explanation, e.g.
@@ -286,6 +297,26 @@ We hope this structure lets your eyes focus on the meaningful values and differe
286
297
 
287
298
  Wrong tries to maintain indentation to improve readability. If the inspected VALUE contains newlines, or is longer than will fit on your console, the succeeding lines will be indented to a pleasant level.
288
299
 
300
+ Sometimes Wrong will not be able to evaluate a detail without raising an exception. This exception will be duly noted, which might be misleading. For example,
301
+
302
+ a = [1,2,3,4]
303
+ assert { a.all? {|i| i<4} }
304
+
305
+ would fail, since on the final pass, `(i < 4)` is false. But the error message is a bit vague:
306
+
307
+ Wrong::Assert::AssertionFailedError: Expected a.all? { |i| (i < 4) }, but
308
+ i raises NameError: undefined local variable or method `i' for main:Object
309
+
310
+ In evaluating the inner expression, Wrong does not have access to the block parameter `i`, since `i` is not in the scope of the outer expression. A better way to write the assertion would be
311
+
312
+ a = [1,2,3,4]
313
+ a.each {|i| assert {i < 4}}
314
+
315
+ which gives
316
+
317
+ Wrong::Assert::AssertionFailedError: Expected (i < 4), but
318
+ i is 4
319
+
289
320
  ## Formatters ##
290
321
 
291
322
  Enhancements for error messages sit under wrong/message.
data/lib/wrong/assert.rb CHANGED
@@ -20,7 +20,7 @@ module Wrong
20
20
  # Actual signature: assert(explanation = nil, depth = 0, &block)
21
21
  def assert(*args, &block)
22
22
  # to notice (and fail fast from) odd recursion problem
23
- raise "Reentry bug while trying to assert(#{args.join(', ')})" if @_inside_wrong_assert
23
+ raise "Reentry bug while trying to assert(#{args.join(', ')})" if (@_inside_wrong_assert ||= nil)
24
24
  @_inside_wrong_assert = true
25
25
 
26
26
  if block.nil?
data/lib/wrong/chunk.rb CHANGED
@@ -77,18 +77,7 @@ module Wrong
77
77
  end
78
78
 
79
79
  def read_source_file(file)
80
- Chunk.read_here_or_higher(file)
81
- end
82
-
83
- def self.read_here_or_higher(file, dir = ".")
84
- File.read "#{dir}/#{file}"
85
- rescue Errno::ENOENT, Errno::EACCES => e
86
- # we may be in a chdir underneath where the file is, so move up one level and try again
87
- parent = "#{dir}/..".gsub(/^(\.\/)*/, '')
88
- if File.expand_path(dir) == File.expand_path(parent)
89
- raise Errno::ENOENT, "couldn't find #{file}"
90
- end
91
- read_here_or_higher(file, parent)
80
+ Config.read_here_or_higher(file)
92
81
  end
93
82
 
94
83
  # Algorithm:
@@ -294,7 +283,7 @@ public # don't know exactly why this needs to be public but eval'ed code can't f
294
283
  end
295
284
 
296
285
  def self.terminal_width
297
- @terminal_width || (terminal_size && terminal_size.first) || 80
286
+ (@terminal_width ||= nil) || (terminal_size && terminal_size.first) || 80
298
287
  end
299
288
 
300
289
  def self.terminal_width= forced_with
data/lib/wrong/config.rb CHANGED
@@ -1,17 +1,16 @@
1
- require "wrong/chunk"
2
1
 
3
2
  module Wrong
4
3
 
5
4
  def self.load_config
6
5
  settings = begin
7
- Chunk.read_here_or_higher(".wrong")
6
+ Config.read_here_or_higher(".wrong")
8
7
  rescue Errno::ENOENT => e
9
8
  # couldn't find it
10
9
  nil # In Ruby 1.8, "e" would be returned here otherwise
11
10
  end
12
11
  Config.new settings
13
12
  end
14
-
13
+
15
14
  def self.config
16
15
  @config ||= load_config
17
16
  end
@@ -25,6 +24,17 @@ module Wrong
25
24
  class ConfigError < RuntimeError
26
25
  end
27
26
 
27
+ def self.read_here_or_higher(file, dir = ".")
28
+ File.read "#{dir}/#{file}"
29
+ rescue Errno::ENOENT, Errno::EACCES => e
30
+ # we may be in a chdir underneath where the file is, so move up one level and try again
31
+ parent = "#{dir}/..".gsub(/^(\.\/)*/, '')
32
+ if File.expand_path(dir) == File.expand_path(parent)
33
+ raise Errno::ENOENT, "couldn't find #{file}"
34
+ end
35
+ read_here_or_higher(file, parent)
36
+ end
37
+
28
38
  def initialize(string = nil)
29
39
  self[:aliases] = {:assert => [:assert], :deny => [:deny]}
30
40
  if string
data/lib/wrong/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Wrong
2
- VERSION = "0.5.4" unless defined?(Wrong::VERSION)
2
+ VERSION = "0.5.6" unless defined?(Wrong::VERSION)
3
3
  end
data/test/assert_test.rb CHANGED
@@ -10,7 +10,7 @@ describe "basic assert features" do
10
10
  end
11
11
 
12
12
  describe "pass/fail basics" do
13
- it "passes when the result is true. deny does the reverse" do
13
+ it "assert passes when the result is true. deny does the reverse" do
14
14
  @m.assert { true }
15
15
  @m.assert { 1==1 }
16
16
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrong
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.5.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-07-19 00:00:00.000000000Z
13
+ date: 2011-08-24 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: predicated
17
- requirement: &2169343480 !ruby/object:Gem::Requirement
17
+ requirement: &70326965279440 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 0.2.3
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2169343480
25
+ version_requirements: *70326965279440
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: ruby_parser
28
- requirement: &2169342940 !ruby/object:Gem::Requirement
28
+ requirement: &70326965278120 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 2.0.4
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *2169342940
36
+ version_requirements: *70326965278120
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: ruby2ruby
39
- requirement: &2169342460 !ruby/object:Gem::Requirement
39
+ requirement: &70326965276680 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '1.2'
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *2169342460
47
+ version_requirements: *70326965276680
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: sexp_processor
50
- requirement: &2169341960 !ruby/object:Gem::Requirement
50
+ requirement: &70326965275820 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '3.0'
56
56
  type: :runtime
57
57
  prerelease: false
58
- version_requirements: *2169341960
58
+ version_requirements: *70326965275820
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: diff-lcs
61
- requirement: &2169341460 !ruby/object:Gem::Requirement
61
+ requirement: &70326965274860 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ~>
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: 1.1.2
67
67
  type: :runtime
68
68
  prerelease: false
69
- version_requirements: *2169341460
69
+ version_requirements: *70326965274860
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: ParseTree
72
- requirement: &2169340960 !ruby/object:Gem::Requirement
72
+ requirement: &70326965274080 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ~>
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: '3.0'
78
78
  type: :runtime
79
79
  prerelease: false
80
- version_requirements: *2169340960
80
+ version_requirements: *70326965274080
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: sourcify
83
- requirement: &2169340440 !ruby/object:Gem::Requirement
83
+ requirement: &70326965273080 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: 0.3.0
89
89
  type: :runtime
90
90
  prerelease: false
91
- version_requirements: *2169340440
91
+ version_requirements: *70326965273080
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: file-tail
94
- requirement: &2169339940 !ruby/object:Gem::Requirement
94
+ requirement: &70326965272260 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ~>
@@ -99,7 +99,7 @@ dependencies:
99
99
  version: '1.0'
100
100
  type: :runtime
101
101
  prerelease: false
102
- version_requirements: *2169339940
102
+ version_requirements: *70326965272260
103
103
  description: ! 'Wrong provides a general assert method that takes a predicate block.
104
104
  Assertion failure
105
105
 
@@ -197,7 +197,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
197
197
  version: '0'
198
198
  segments:
199
199
  - 0
200
- hash: 488446624914030243
200
+ hash: -1102052589902211149
201
201
  required_rubygems_version: !ruby/object:Gem::Requirement
202
202
  none: false
203
203
  requirements:
@@ -206,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
206
206
  version: '0'
207
207
  requirements: []
208
208
  rubyforge_project: wrong
209
- rubygems_version: 1.8.5
209
+ rubygems_version: 1.8.6
210
210
  signing_key:
211
211
  specification_version: 3
212
212
  summary: Wrong provides a general assert method that takes a predicate block. Assertion