expectations 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/expectations.rb CHANGED
@@ -2,13 +2,12 @@ module Expectations
2
2
  end
3
3
 
4
4
  def Expectations(&block)
5
- Expectations::SuiteRunner.suite_eval &block
5
+ Expectations::SuiteRunner.suite_eval(&block)
6
6
  rescue
7
7
  Expectations::SuiteRunner.do_not_run
8
8
  raise
9
9
  end
10
10
 
11
- require 'rubygems'
12
11
  require 'mocha'
13
12
  require 'mocha/standalone'
14
13
  require 'singleton'
@@ -16,6 +15,7 @@ require 'benchmark'
16
15
  require 'erb'
17
16
  require 'fileutils'
18
17
  require File.expand_path(File.dirname(__FILE__) + '/expectations/object')
18
+ require File.expand_path(File.dirname(__FILE__) + '/expectations/blank_slate')
19
19
  require File.expand_path(File.dirname(__FILE__) + '/expectations/recorder')
20
20
  require File.expand_path(File.dirname(__FILE__) + '/expectations/delegate_recorder')
21
21
  require File.expand_path(File.dirname(__FILE__) + '/expectations/recorded_expectation')
@@ -0,0 +1,113 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Copyright 2004, 2006 by Jim Weirich (jim@weirichhouse.org).
4
+ # All rights reserved.
5
+
6
+ # Permission is granted for use, copying, modification, distribution,
7
+ # and distribution of modified versions of this work as long as the
8
+ # above copyright notice is included.
9
+ #++
10
+
11
+ ######################################################################
12
+ # BlankSlate provides an abstract base class with no predefined
13
+ # methods (except for <tt>\_\_send__</tt> and <tt>\_\_id__</tt>).
14
+ # BlankSlate is useful as a base class when writing classes that
15
+ # depend upon <tt>method_missing</tt> (e.g. dynamic proxies).
16
+ #
17
+ class BlankSlate
18
+ class << self
19
+
20
+ # Hide the method named +name+ in the BlankSlate class. Don't
21
+ # hide +instance_eval+ or any method beginning with "__".
22
+ def hide(name)
23
+ if instance_methods.include?(name.to_s) and
24
+ name !~ /^(__|instance_eval|extend|is_a?)/
25
+ @hidden_methods ||= {}
26
+ @hidden_methods[name.to_sym] = instance_method(name)
27
+ undef_method name
28
+ end
29
+ end
30
+
31
+ def find_hidden_method(name)
32
+ @hidden_methods ||= {}
33
+ @hidden_methods[name] || superclass.find_hidden_method(name)
34
+ end
35
+
36
+ # Redefine a previously hidden method so that it may be called on a blank
37
+ # slate object.
38
+ def reveal(name)
39
+ bound_method = nil
40
+ unbound_method = find_hidden_method(name)
41
+ fail "Don't know how to reveal method '#{name}'" unless unbound_method
42
+ define_method(name) do |*args|
43
+ bound_method ||= unbound_method.bind(self)
44
+ bound_method.call(*args)
45
+ end
46
+ end
47
+ end
48
+
49
+ instance_methods.each { |m| hide(m) }
50
+ end
51
+
52
+ ######################################################################
53
+ # Since Ruby is very dynamic, methods added to the ancestors of
54
+ # BlankSlate <em>after BlankSlate is defined</em> will show up in the
55
+ # list of available BlankSlate methods. We handle this by defining a
56
+ # hook in the Object and Kernel classes that will hide any method
57
+ # defined after BlankSlate has been loaded.
58
+ #
59
+ module Kernel
60
+ class << self
61
+ alias_method :blank_slate_method_added, :method_added
62
+
63
+ # Detect method additions to Kernel and remove them in the
64
+ # BlankSlate class.
65
+ def method_added(name)
66
+ result = blank_slate_method_added(name)
67
+ return result if self != Kernel
68
+ BlankSlate.hide(name)
69
+ result
70
+ end
71
+ end
72
+ end
73
+
74
+ ######################################################################
75
+ # Same as above, except in Object.
76
+ #
77
+ class Object
78
+ class << self
79
+ alias_method :blank_slate_method_added, :method_added
80
+
81
+ # Detect method additions to Object and remove them in the
82
+ # BlankSlate class.
83
+ def method_added(name)
84
+ result = blank_slate_method_added(name)
85
+ return result if self != Object
86
+ BlankSlate.hide(name)
87
+ result
88
+ end
89
+
90
+ def find_hidden_method(name)
91
+ nil
92
+ end
93
+ end
94
+ end
95
+
96
+ ######################################################################
97
+ # Also, modules included into Object need to be scanned and have their
98
+ # instance methods removed from blank slate. In theory, modules
99
+ # included into Kernel would have to be removed as well, but a
100
+ # "feature" of Ruby prevents late includes into modules from being
101
+ # exposed in the first place.
102
+ #
103
+ class Module
104
+ alias blankslate_original_append_features append_features
105
+ def append_features(mod)
106
+ result = blankslate_original_append_features(mod)
107
+ return result if mod != Object
108
+ instance_methods.each do |name|
109
+ BlankSlate.hide(name)
110
+ end
111
+ result
112
+ end
113
+ end
@@ -2,7 +2,7 @@ module Expectations::RecordedExpectation
2
2
  def execute
3
3
  begin
4
4
  mocha_setup
5
- instance_exec expected.subject!, &block if block
5
+ instance_exec(expected.subject!, &block) if block
6
6
  if expected.verify!
7
7
  self.extend(Expectations::Results::Fulfilled)
8
8
  else
@@ -21,4 +21,4 @@ module Expectations::RecordedExpectation
21
21
  self
22
22
  end
23
23
 
24
- end
24
+ end
@@ -1,5 +1,5 @@
1
- class Expectations::Recorder
2
-
1
+ class Expectations::Recorder < BlankSlate
2
+
3
3
  attr_reader :subject
4
4
  def initialize(subject)
5
5
  @subject = subject
@@ -2,6 +2,10 @@ class Expectations::Suite
2
2
 
3
3
  include Mocha::Standalone
4
4
 
5
+ def initialize
6
+ @do_not_run = false
7
+ end
8
+
5
9
  def xml(string)
6
10
  Expectations::XmlString.new(string)
7
11
  end
@@ -18,7 +22,7 @@ class Expectations::Suite
18
22
  end
19
23
 
20
24
  def expect(expected, &block)
21
- expectations << Expectations::Expectation.new(expected, *caller.first.split(/:/), &block)
25
+ expectations << Expectations::Expectation.new(expected, *caller.first.split(/:/)[0..1], &block)
22
26
  end
23
27
 
24
28
  def do_not_run
@@ -14,7 +14,7 @@ class Expectations::SuiteRunner
14
14
  end
15
15
 
16
16
  def self.suite_eval(&block)
17
- self.instance.suite.instance_eval &block
17
+ self.instance.suite.instance_eval(&block)
18
18
  end
19
19
 
20
20
  end
data/rakefile.rb CHANGED
@@ -46,7 +46,7 @@ specification = Gem::Specification.new do |s|
46
46
  expect NoMethodError do
47
47
  Object.invalid_method_call
48
48
  end."
49
- s.version = "0.2.3"
49
+ s.version = "0.2.4"
50
50
  s.author = 'Jay Fields'
51
51
  s.description = "A lightweight unit testing framework. Tests (expectations) will be written as follows
52
52
  expect 2 do
data/test/test_helper.rb CHANGED
@@ -1,2 +1,3 @@
1
+ require 'rubygems'
1
2
  require File.dirname(__FILE__) + '/silent'
2
3
  require File.dirname(__FILE__) + '/../lib/expectations'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: expectations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jay Fields
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-03-07 00:00:00 -08:00
12
+ date: 2008-03-27 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -30,6 +30,7 @@ extensions: []
30
30
  extra_rdoc_files:
31
31
  - README
32
32
  files:
33
+ - lib/expectations/blank_slate.rb
33
34
  - lib/expectations/delegate_recorder.rb
34
35
  - lib/expectations/expectation.rb
35
36
  - lib/expectations/mock_recorder.rb