quickie 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,2 +1,6 @@
1
+ 0.2.0
2
+ - Improved actual vs. expected reporting.
3
+ - Improved user experience when running within IRB or Pry.
4
+
1
5
  0.1.0
2
6
  - Initial Release.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- quickie (0.1.0)
4
+ quickie (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -6,7 +6,7 @@
6
6
  module Quickie
7
7
  class Hell < RuntimeError
8
8
  def oops
9
- puts "\n#{message.chomp} in #{backtrace[2].sub(':', ', line ').sub(':', ' ')}"
9
+ "#{message.chomp} in #{backtrace[2].sub(':', ', line ').sub(':', ' ')}"
10
10
  end
11
11
  end
12
12
 
@@ -14,13 +14,11 @@ module Quickie
14
14
  def initialize(object, verb)
15
15
  @object = object
16
16
  @should = (verb == :should)
17
- %w[ == === =~ > >= < <= => ].each do |operator|
18
- self.class.override operator
19
- end
20
17
  end
21
18
 
22
19
  private
23
20
 
21
+ # Override an operator to be able to tell whether it succeeded or not.
24
22
  #--------------------------------------------------------------------------
25
23
  def self.override(operator)
26
24
  define_method(operator) do |expected|
@@ -40,6 +38,8 @@ module Quickie
40
38
  end
41
39
  end
42
40
 
41
+ # Note that we always evaluate positive operators, and then flip the actual
42
+ # result based on should/should_not request.
43
43
  #--------------------------------------------------------------------------
44
44
  def evaluate(operator, negative_operator, expected)
45
45
  actual = !!@object.__send__(operator, expected)
@@ -48,14 +48,14 @@ module Quickie
48
48
  if actual
49
49
  report :success
50
50
  else
51
- report :failure
52
51
  raise Hell, lyrics(negative_operator || operator, expected)
53
52
  end
54
53
 
55
54
  rescue Hell => e
56
- e.oops
55
+ report :failure, e.oops
57
56
  end
58
57
 
58
+ # Format actual vs. expected message.
59
59
  #--------------------------------------------------------------------------
60
60
  def lyrics(operator, expected)
61
61
  format = "expected: %s %s\n actual: %s %s"
@@ -63,10 +63,23 @@ module Quickie
63
63
  format % [ operator, expected.inspect, ' ' * operator.size, @object.inspect ]
64
64
  end
65
65
 
66
+ # Report test success and/or failure. When running within IRB or Pry the
67
+ # message gets displayed immediately, otherwise all the messages are shown
68
+ # by the Runner in at_exit block.
69
+ #--------------------------------------------------------------------------
70
+ def report(status, message = nil)
71
+ print(status == :success ? '.' : 'F')
72
+ if !defined?(::IRB) && !defined?(::Pry)
73
+ Runner.update(status, message)
74
+ else
75
+ puts "\n\n#{message}"
76
+ end
77
+ end
78
+
79
+ # The matcher magic starts here ;-)
66
80
  #--------------------------------------------------------------------------
67
- def report(status)
68
- print '.' if status == :success
69
- Runner.update(status)
81
+ %w[ == === =~ > >= < <= => ].each do |operator|
82
+ override operator
70
83
  end
71
84
  end
72
85
  end
@@ -5,14 +5,18 @@
5
5
  #------------------------------------------------------------------------------
6
6
  module Quickie
7
7
  class Runner
8
+ @@trace = []
8
9
  @@stats = Hash.new(0)
9
10
 
10
- def self.update(status)
11
+ def self.update(status, message = nil)
11
12
  at_exit {
12
- puts "\n\nPassed: #{@@stats[:success]}, not quite: #{@@stats[:failure]}, total tests: #{@@stats.values.inject(:+)}."
13
+ puts
14
+ puts "\n" << @@trace.join("\n\n") unless @@trace.empty?
15
+ puts "\nPassed: #{@@stats[:success]}, not quite: #{@@stats[:failure]}, total tests: #{@@stats.values.inject(:+)}."
13
16
  } if @@stats.empty?
14
17
 
15
18
  @@stats[status] += 1
19
+ @@trace << message if message
16
20
  end
17
21
  end
18
22
  end
@@ -5,6 +5,6 @@
5
5
  #------------------------------------------------------------------------------
6
6
  module Quickie
7
7
  def self.version
8
- '0.1.0'
8
+ '0.2.0'
9
9
  end
10
10
  end
data/test/quickie_test.rb CHANGED
@@ -12,33 +12,24 @@ require File.expand_path(File.dirname(__FILE__) + "/../lib/quickie")
12
12
  # 2. Capture the output of the test.
13
13
  # 3. Make sure captured output matches the expectation.
14
14
  #
15
- # In addition, we hack the Quickie stats so that captured tests are not
16
- # counted in the actual results.
15
+ # In addition, we hack the Quickie trace/stats so that failed captured
16
+ # tests are not shown/counted in the actual results.
17
17
  #--------------------------------------------------------------------------
18
18
  def capture
19
- stats = Quickie::Runner.class_variable_get('@@stats')
20
- captured = StringIO.new
21
- standard, $stdout = $stdout, captured
19
+ stats = Quickie::Runner.class_variable_get(:@@stats)
20
+ trace = Quickie::Runner.class_variable_get(:@@trace)
21
+
22
+ standard, $stdout = $stdout, StringIO.new
22
23
  yield
23
- captured.string
24
+ $stdout.string
24
25
  ensure
25
- $stdout = standard
26
- if captured.string == '.'
26
+ if $stdout.string == '.'
27
27
  stats[:success] -= 1
28
28
  else
29
29
  stats[:failure] -= 1
30
+ trace.pop
30
31
  end
31
- Quickie::Runner.class_variable_set('@@stats', stats)
32
- end
33
-
34
- #--------------------------------------------------------------------------
35
- class String
36
- def fix(line)
37
- self.sub!(/^/, "\n") # Insert newline.
38
- self.sub!("?", line.to_s) # Insert actual line number.
39
- self.sub!(/\n+Passed.+$/, "") # Ignore the stats.
40
- self
41
- end
32
+ $stdout = standard
42
33
  end
43
34
 
44
35
  # Should - passing specs.
@@ -59,54 +50,16 @@ capture { 1234567.should_not_be < 0 }.should == "."
59
50
 
60
51
  # Should - failing specs.
61
52
  #--------------------------------------------------------------------------
62
- capture { "abc".should != "abc" }.should == <<-EOS.fix(__LINE__)
63
- expected: != "abc"
64
- actual: "abc" in test/quickie_test.rb, line ? in `block in <main>'
65
- EOS
66
-
67
- capture { "abc".should == "xyz" }.should == <<-EOS.fix(__LINE__)
68
- expected: == "xyz"
69
- actual: "abc" in test/quickie_test.rb, line ? in `block in <main>'
70
- EOS
71
-
72
- capture { "abc".should !~ /AB/i }.should == <<-EOS.fix(__LINE__)
73
- expected: !~ /AB/i
74
- actual: "abc" in test/quickie_test.rb, line ? in `block in <main>'
75
- EOS
76
-
77
- capture { "abc".should =~ /XY/i }.should == <<-EOS.fix(__LINE__)
78
- expected: =~ /XY/i
79
- actual: "abc" in test/quickie_test.rb, line ? in `block in <main>'
80
- EOS
81
-
82
- capture { 1234567.should_be < 0 }.should == <<-EOS.fix(__LINE__)
83
- expected: < 0
84
- actual: 1234567 in test/quickie_test.rb, line ? in `block in <main>'
85
- EOS
53
+ capture { "abc".should != "abc" }.should == "F"
54
+ capture { "abc".should == "xyz" }.should == "F"
55
+ capture { "abc".should !~ /AB/i }.should == "F"
56
+ capture { "abc".should =~ /XY/i }.should == "F"
57
+ capture { 1234567.should_be < 0 }.should == "F"
86
58
 
87
59
  # Should Not - failing specs.
88
60
  #--------------------------------------------------------------------------
89
- capture { "abc".should_not == "abc" }.should == <<-EOS.fix(__LINE__)
90
- expected not: == "abc"
91
- actual: "abc" in test/quickie_test.rb, line ? in `block in <main>'
92
- EOS
93
-
94
- capture { "abc".should_not != "xyz" }.should == <<-EOS.fix(__LINE__)
95
- expected not: != "xyz"
96
- actual: "abc" in test/quickie_test.rb, line ? in `block in <main>'
97
- EOS
98
-
99
- capture { "abc".should_not =~ /AB/i }.should == <<-EOS.fix(__LINE__)
100
- expected not: =~ /AB/i
101
- actual: "abc" in test/quickie_test.rb, line ? in `block in <main>'
102
- EOS
103
-
104
- capture { "abc".should_not !~ /XY/i }.should == <<-EOS.fix(__LINE__)
105
- expected not: !~ /XY/i
106
- actual: "abc" in test/quickie_test.rb, line ? in `block in <main>'
107
- EOS
108
-
109
- capture { 1234567.should_not_be > 0 }.should == <<-EOS.fix(__LINE__)
110
- expected not: > 0
111
- actual: 1234567 in test/quickie_test.rb, line ? in `block in <main>'
112
- EOS
61
+ capture { "abc".should_not == "abc" }.should == "F"
62
+ capture { "abc".should_not != "xyz" }.should == "F"
63
+ capture { "abc".should_not =~ /AB/i }.should == "F"
64
+ capture { "abc".should_not !~ /XY/i }.should == "F"
65
+ capture { 1234567.should_not_be > 0 }.should == "F"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quickie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
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: 2012-01-03 00:00:00.000000000 Z
12
+ date: 2012-01-06 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Quickie adds Object#should and Object#should_not methods for quick testing
15
15
  of your Ruby code