Skip to content

Commit

Permalink
Allow should to be called from the context.
Browse files Browse the repository at this point in the history
This returns an instance of Peck::Should::Specification. In itself this
doesn't do anything, but it allows you to add methods to
Peck::Should::Specification so you can make stuff happen.

The most obvious thing is to create a specification like displayed in the
example (006).
  • Loading branch information
Manfred committed Sep 12, 2012
1 parent 2f3d65e commit b5078c4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
27 changes: 27 additions & 0 deletions README.md
Expand Up @@ -100,6 +100,33 @@ created.
emd
end

Except extension you can also add should macros which defined one or more
specifications:

class Peck::Should::Specification
class Disallow < Peck::Should::Proxy
def get(action)
context.it("disallows GET on `#{action}'") do
get action
response.should == :unauthorized
end
end
end

def disallow
Peck::Should::Specification::Disallow.new(context)
end
end

describe CertificatesController, "when accessed by a regular user" do
before do
login :regular_user
end

should.disallow.get :index
should.disallow.get :show
end

## Documentation

Peck is still very much in flux and will probably change a lot in the coming
Expand Down
24 changes: 24 additions & 0 deletions examples/006_should_in_context_spec.rb
@@ -0,0 +1,24 @@
require 'preamble'

class Peck
class Should
class Specification
def handle_the_truth
context.it('handles the truth') do
true.should == true
end
end
end
end
end

describe "The test framework" do
should.handle_the_truth
end

Peck.run

require 'assert'

assert(Peck.counter.ran == 1,
"Expected 1 specification to have been run")
24 changes: 23 additions & 1 deletion lib/peck/expectations.rb
Expand Up @@ -2,6 +2,24 @@

class Peck
class Should
class Proxy
attr_accessor :negated
attr_reader :context, :block

def initialize(context, &block)
@context = context
@block = block
@negated = false
end
end

class Specification < Proxy
def not
@negated = !@negated
self
end
end

# Kills ==, ===, =~, eql?, equal?, frozen?, instance_of?, is_a?,
# kind_of?, nil?, respond_to?, tainted?
KILL_METHODS_RE = /\?|^\W+$/
Expand Down Expand Up @@ -110,6 +128,10 @@ def method_missing(name, *args, &block)

class Object
def should(*args, &block)
Peck::Should.new(self).be(*args, &block)
if self.kind_of?(Class) && (self < Peck::Context)
Peck::Should::Specification.new(self)
else
Peck::Should.new(self).be(*args, &block)
end
end
end

0 comments on commit b5078c4

Please sign in to comment.