expectations 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/expectations/expectation.rb +28 -2
- data/lib/expectations/mocha.rb +5 -0
- data/lib/expectations/object.rb +30 -0
- data/lib/expectations/results.rb +12 -1
- data/lib/expectations/suite.rb +6 -1
- data/lib/expectations/suite_results.rb +5 -3
- data/lib/expectations.rb +5 -0
- data/rakefile.rb +2 -2
- data/test/expectations/expectation_test.rb +17 -1
- data/test/expectations/results_test.rb +10 -2
- data/test/expectations/suite_results_test.rb +7 -1
- data/test/sample_expecations_test.rb +22 -0
- metadata +4 -2
@@ -1,4 +1,6 @@
|
|
1
1
|
class Expectations::Expectation
|
2
|
+
include Mocha::Standalone
|
3
|
+
|
2
4
|
attr_accessor :expected, :block, :file, :line, :actual
|
3
5
|
def initialize(expected, &block)
|
4
6
|
self.expected, self.block = expected, block
|
@@ -6,15 +8,39 @@ class Expectations::Expectation
|
|
6
8
|
end
|
7
9
|
|
8
10
|
def execute
|
11
|
+
expected.is_a?(Mocha::Expectation) ? behavior_based_execute : state_based_execute
|
12
|
+
end
|
13
|
+
|
14
|
+
def behavior_based_execute
|
15
|
+
begin
|
16
|
+
mock = expected.instance_variable_get(:@mock)
|
17
|
+
mocha_setup
|
18
|
+
instance_exec mock, &block
|
19
|
+
mock.verify
|
20
|
+
self.extend(Expectations::Results::Fulfilled)
|
21
|
+
rescue Mocha::ExpectationError => ex
|
22
|
+
self.extend(Expectations::Results::BehaviorFailure)
|
23
|
+
self.message = ex.message.gsub(/:/,";")
|
24
|
+
rescue Exception => ex
|
25
|
+
self.extend(Expectations::Results::Error)
|
26
|
+
self.exception = ex
|
27
|
+
ensure
|
28
|
+
mocha_teardown
|
29
|
+
end
|
30
|
+
self
|
31
|
+
end
|
32
|
+
|
33
|
+
def state_based_execute
|
9
34
|
begin
|
10
35
|
self.actual = block.call
|
11
36
|
return self.extend(Expectations::Results::Fulfilled) if expected == actual
|
12
37
|
rescue Exception => ex
|
13
38
|
return self.extend(Expectations::Results::Fulfilled) if expected == ex.class
|
14
39
|
self.extend(Expectations::Results::Error)
|
15
|
-
self.exception
|
40
|
+
self.exception = ex
|
41
|
+
self.actual = ex.class if expected.is_a?(Class) && expected < StandardError
|
16
42
|
return self
|
17
43
|
end
|
18
|
-
self.extend(Expectations::Results::
|
44
|
+
self.extend(Expectations::Results::StateBasedFailure)
|
19
45
|
end
|
20
46
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class Object
|
2
|
+
alias to_receive expects
|
3
|
+
|
4
|
+
unless defined? instance_exec # 1.9
|
5
|
+
module InstanceExecMethods #:nodoc:
|
6
|
+
end
|
7
|
+
include InstanceExecMethods
|
8
|
+
|
9
|
+
# Evaluate the block with the given arguments within the context of
|
10
|
+
# this object, so self is set to the method receiver.
|
11
|
+
#
|
12
|
+
# From Mauricio's http://eigenclass.org/hiki/bounded+space+instance_exec
|
13
|
+
def instance_exec(*args, &block)
|
14
|
+
begin
|
15
|
+
old_critical, Thread.critical = Thread.critical, true
|
16
|
+
n = 0
|
17
|
+
n += 1 while respond_to?(method_name = "__instance_exec#{n}")
|
18
|
+
InstanceExecMethods.module_eval { define_method(method_name, &block) }
|
19
|
+
ensure
|
20
|
+
Thread.critical = old_critical
|
21
|
+
end
|
22
|
+
|
23
|
+
begin
|
24
|
+
send(method_name, *args)
|
25
|
+
ensure
|
26
|
+
InstanceExecMethods.module_eval { remove_method(method_name) } rescue nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/expectations/results.rb
CHANGED
@@ -21,7 +21,18 @@ module Expectations::Results
|
|
21
21
|
end
|
22
22
|
|
23
23
|
module Expectations::Results
|
24
|
-
module
|
24
|
+
module StateBasedFailure
|
25
|
+
include Expectations::Results
|
26
|
+
char "F"
|
27
|
+
def message
|
28
|
+
"expected: <#{expected.inspect}> got: <#{actual.inspect}>"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module Expectations::Results
|
34
|
+
module BehaviorFailure
|
35
|
+
attr_accessor :message
|
25
36
|
include Expectations::Results
|
26
37
|
char "F"
|
27
38
|
end
|
data/lib/expectations/suite.rb
CHANGED
@@ -1,4 +1,9 @@
|
|
1
1
|
class Expectations::Suite
|
2
|
+
include Mocha::Standalone
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
mocha_setup
|
6
|
+
end
|
2
7
|
|
3
8
|
def execute(out=STDOUT)
|
4
9
|
suite_result = Expectations::SuiteResults.new(out)
|
@@ -10,7 +15,7 @@ class Expectations::Suite
|
|
10
15
|
suite_result.print_results(benchmark)
|
11
16
|
suite_result.result
|
12
17
|
end
|
13
|
-
|
18
|
+
|
14
19
|
def expectations
|
15
20
|
@expectations ||= []
|
16
21
|
end
|
@@ -22,7 +22,9 @@ class Expectations::SuiteResults
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def failures
|
25
|
-
expectations.select
|
25
|
+
expectations.select do |expectation|
|
26
|
+
expectation.is_a?(Expectations::Results::StateBasedFailure) || expectation.is_a?(Expectations::Results::BehaviorFailure)
|
27
|
+
end
|
26
28
|
end
|
27
29
|
|
28
30
|
def print_results(benchmark)
|
@@ -37,14 +39,14 @@ class Expectations::SuiteResults
|
|
37
39
|
errors.each do |error|
|
38
40
|
out.puts " #{error.file}:#{error.line}:in `expect'"
|
39
41
|
out.puts " line <#{error.line}>"
|
40
|
-
out.puts " error <#{error.exception.message}>"
|
42
|
+
out.puts " error <#{error.exception.message.gsub(/:/,";")}>"
|
41
43
|
out.puts " expected <#{error.expected.inspect}> got <#{error.actual.inspect}>"
|
42
44
|
end
|
43
45
|
out.puts "\nFailures:" if failures.any?
|
44
46
|
failures.each do |failure|
|
45
47
|
out.puts " #{failure.file}:#{failure.line}:in `expect'"
|
46
48
|
out.puts " line <#{failure.line}>"
|
47
|
-
out.puts "
|
49
|
+
out.puts " #{failure.message}"
|
48
50
|
end
|
49
51
|
end
|
50
52
|
end
|
data/lib/expectations.rb
CHANGED
@@ -5,8 +5,13 @@ def Expectations(&block)
|
|
5
5
|
Expectations::SuiteRunner.instance.suite_eval &block
|
6
6
|
end
|
7
7
|
|
8
|
+
require 'rubygems'
|
9
|
+
require 'mocha'
|
10
|
+
require 'mocha/standalone'
|
8
11
|
require 'singleton'
|
9
12
|
require 'benchmark'
|
13
|
+
require File.expand_path(File.dirname(__FILE__) + '/expectations/object')
|
14
|
+
require File.expand_path(File.dirname(__FILE__) + '/expectations/mocha')
|
10
15
|
require File.expand_path(File.dirname(__FILE__) + '/expectations/suite')
|
11
16
|
require File.expand_path(File.dirname(__FILE__) + '/expectations/suite_runner')
|
12
17
|
require File.expand_path(File.dirname(__FILE__) + '/expectations/suite_results')
|
data/rakefile.rb
CHANGED
@@ -33,7 +33,7 @@ specification = Gem::Specification.new do |s|
|
|
33
33
|
expect NoMethodError do
|
34
34
|
Object.invalid_method_call
|
35
35
|
end."
|
36
|
-
s.version = "0.0.
|
36
|
+
s.version = "0.0.2"
|
37
37
|
s.author = 'Jay Fields'
|
38
38
|
s.description = "A lightweight unit testing framework. Tests (expectations) will be written as follows
|
39
39
|
expect 2 do
|
@@ -50,7 +50,7 @@ specification = Gem::Specification.new do |s|
|
|
50
50
|
s.extra_rdoc_files = ['README']
|
51
51
|
s.rdoc_options << '--title' << 'expectations' << '--main' << 'README' << '--line-numbers'
|
52
52
|
|
53
|
-
s.autorequire = '
|
53
|
+
s.autorequire = 'expectations'
|
54
54
|
s.files = FileList['{lib,test}/**/*.rb', '[A-Z]*$', 'rakefile.rb'].to_a
|
55
55
|
s.test_file = "test/all_tests.rb"
|
56
56
|
end
|
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + "/../test_helper"
|
|
2
2
|
|
3
3
|
Expectations do
|
4
4
|
expect true do
|
5
|
-
Expectations::Expectation.new(1) { 2 }.execute.is_a?(Expectations::Results::
|
5
|
+
Expectations::Expectation.new(1) { 2 }.execute.is_a?(Expectations::Results::StateBasedFailure)
|
6
6
|
end
|
7
7
|
|
8
8
|
expect true do
|
@@ -13,6 +13,22 @@ Expectations do
|
|
13
13
|
Expectations::Expectation.new(1) { raise }.execute.is_a?(Expectations::Results::Error)
|
14
14
|
end
|
15
15
|
|
16
|
+
expect NoMethodError do
|
17
|
+
Expectations::Expectation.new(ArgumentError) { Object.no_method }.execute.actual
|
18
|
+
end
|
19
|
+
|
20
|
+
expect "undefined method `no_method' for Object:Class" do
|
21
|
+
Expectations::Expectation.new(ArgumentError) { Object.no_method }.execute.exception.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
expect true do
|
25
|
+
Expectations::Expectation.new(NoMethodError) { Object.no_method }.execute.is_a?(Expectations::Results::Fulfilled)
|
26
|
+
end
|
27
|
+
|
28
|
+
expect nil do
|
29
|
+
Expectations::Expectation.new(String) { Object.no_method }.execute.actual
|
30
|
+
end
|
31
|
+
|
16
32
|
expect __LINE__ + 1 do
|
17
33
|
Expectations::Expectation.new(1) { 2 }.execute.line
|
18
34
|
end
|
@@ -7,7 +7,11 @@ Expectations do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
expect "F" do
|
10
|
-
Object.new.extend(Expectations::Results::
|
10
|
+
Object.new.extend(Expectations::Results::StateBasedFailure).char
|
11
|
+
end
|
12
|
+
|
13
|
+
expect "F" do
|
14
|
+
Object.new.extend(Expectations::Results::BehaviorFailure).char
|
11
15
|
end
|
12
16
|
|
13
17
|
expect "E" do
|
@@ -19,7 +23,11 @@ Expectations do
|
|
19
23
|
end
|
20
24
|
|
21
25
|
expect false do
|
22
|
-
Object.new.extend(Expectations::Results::
|
26
|
+
Object.new.extend(Expectations::Results::StateBasedFailure).fulfilled?
|
27
|
+
end
|
28
|
+
|
29
|
+
expect false do
|
30
|
+
Object.new.extend(Expectations::Results::BehaviorFailure).fulfilled?
|
23
31
|
end
|
24
32
|
|
25
33
|
expect false do
|
@@ -21,7 +21,13 @@ Expectations do
|
|
21
21
|
|
22
22
|
expect false do
|
23
23
|
results = Expectations::SuiteResults.new(Silent)
|
24
|
-
results << Object.new.extend(Expectations::Results::
|
24
|
+
results << Object.new.extend(Expectations::Results::StateBasedFailure)
|
25
|
+
results.result
|
26
|
+
end
|
27
|
+
|
28
|
+
expect false do
|
29
|
+
results = Expectations::SuiteResults.new(Silent)
|
30
|
+
results << Object.new.extend(Expectations::Results::BehaviorFailure)
|
25
31
|
results.result
|
26
32
|
end
|
27
33
|
end
|
@@ -1,6 +1,10 @@
|
|
1
1
|
require File.dirname(__FILE__) + "/test_helper"
|
2
2
|
|
3
|
+
class Phone
|
4
|
+
end
|
5
|
+
|
3
6
|
Expectations do
|
7
|
+
|
4
8
|
expect 2 do
|
5
9
|
1 + 1
|
6
10
|
end
|
@@ -9,4 +13,22 @@ Expectations do
|
|
9
13
|
Object.no_method
|
10
14
|
end
|
11
15
|
|
16
|
+
expect mock.to_receive(:redial) do |phone|
|
17
|
+
phone.redial(10)
|
18
|
+
end
|
19
|
+
|
20
|
+
expect mock.to_receive(:dial).with("2125551212") do |phone|
|
21
|
+
phone.dial("2125551212")
|
22
|
+
end
|
23
|
+
|
24
|
+
expect mock.to_receive(:dial).times(2) do |phone|
|
25
|
+
phone.dial
|
26
|
+
phone.dial
|
27
|
+
end
|
28
|
+
|
29
|
+
expect Object.to_receive(:foo).with(1).times(2) do
|
30
|
+
Object.foo(1)
|
31
|
+
Object.foo(1)
|
32
|
+
end
|
33
|
+
|
12
34
|
end
|
metadata
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: expectations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jay Fields
|
8
|
-
autorequire:
|
8
|
+
autorequire: expectations
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
@@ -23,6 +23,8 @@ extra_rdoc_files:
|
|
23
23
|
- README
|
24
24
|
files:
|
25
25
|
- lib/expectations/expectation.rb
|
26
|
+
- lib/expectations/mocha.rb
|
27
|
+
- lib/expectations/object.rb
|
26
28
|
- lib/expectations/results.rb
|
27
29
|
- lib/expectations/suite.rb
|
28
30
|
- lib/expectations/suite_results.rb
|