require 'taptap/spec' module MyTests def equal_string(x) lambda { |s| x == s.to_s } end end describe "Bacon" do extend MyTests it "should have should.satisfy" do should.satisfy { 1 == 1 } should.satisfy { 1 } should.satisfy { true } should.not.satisfy { 1 == 2 } should.not.satisfy { false } should.not.satisfy { nil } 2.should.satisfy { |n| n % 2 == 0 } end it "should have should.equal" do "string1".should == "string1" "string1".should == "string1" "string1".should.equal "string1" "string1".should.not == "string2" "string1".should.not.equal "string2" end it "should have should.raise" do lambda { raise "Error" }.should.raise lambda { raise "Error" }.should.raise RuntimeError lambda { 1 + 1 }.should.not.raise lambda { 1 + 1 }.should.not.raise(Interrupt) lambda { lambda { Kernel.raise ZeroDivisionError.new("ArgumentError") }.should.not.raise(RuntimeError, Comparable) }.should.raise ZeroDivisionError end it "should have should.raise with a block" do should.raise { raise "Error" } should.raise(RuntimeError) { raise "Error" } end it "should have a should.raise should return the exception" do ex = lambda { raise "foo!" }.should.raise ex.should.be.kind_of RuntimeError ex.message.should =~ /foo/ end it "should have should.be.an.instance_of" do "string".should.be.instance_of String "string".should.be.an.instance_of String end it "should have should.be.nil" do nil.should.be.nil "foo".should.not.be.nil end it "should have should.include" do [1,2,3].should.include 2 [1,2,3].should.not.include 4 {1=>2, 3=>4}.should.include 1 {1=>2, 3=>4}.should.not.include 2 end it "should have should.be.a.kind_of" do Array.should.be.kind_of Module "string".should.be.kind_of Object 1.should.be.kind_of Comparable Array.should.be.a.kind_of Module "string".should.not.be.a.kind_of Class end it "should have should.match" do "string".should.match(/strin./) "string".should =~ /strin./ "string".should.not.match(/slin./) "string".should.not =~ /slin./ end it "should have should.throw" do lambda { throw :foo }.should.throw(:foo) lambda { :foo }.should.not.throw(:foo) should.throw(:foo) { throw :foo } end it "should have should.be.identical_to/same_as" do s = "string"; s.should.be.identical_to s "string".should.not.be.identical_to "string" s = "string"; s.should.be.same_as s "string".should.not.be.same_as "string" end it "should have should.respond_to" do "foo".should.respond_to :to_s 5.should.not.respond_to :to_str :foo.should.not.respond_to :nx end it "should have should.be.close" do 1.4.should.be.close 1.4, 0 0.4.should.be.close 0.5, 0.1 0.4.should.not.be.close 0.5, 0.05 0.4.should.not.be.close Object.new, 0.1 0.4.should.not.be.close 0.5, -0.1 end it "should support multiple negation" do 1.should.equal 1 1.should.not.not.equal 1 1.should.not.equal 2 1.should.not.not.not.equal 2 end it "should have should." do [].should.be.empty [1,2,3].should.not.be.empty {1=>2, 3=>4}.should.has_key 1 {1=>2, 3=>4}.should.not.has_key 2 lambda { nil.should.bla }.should.raise(NoMethodError) lambda { nil.should.not.bla }.should.raise(NoMethodError) end it "should have should (>, >=, <, <=, ===)" do 2.should.be > 1 1.should.not.be > 2 1.should.be < 2 2.should.be > 1 2.should.be >= 1 2.should.be >= 2 2.should.not.be >= 2.1 2.should.not.be <= 1 2.should.be <= 2 2.should.be <= 2.1 Array.should === [1,2,3] Integer.should.not === [1,2,3] /foo/.should === "foobar" "foobar".should.not === /foo/ end it "should allow for custom shoulds" do (1+1).should equal_string("2") (1+1).should.be equal_string("2") (1+2).should.not equal_string("2") (1+2).should.not.be equal_string("2") end end describe "before/after" do before do @a = 1 @b = 2 end before do @a = 2 end after do @a.should.equal 2 @a = 3 end after do @a.should.equal 3 end it "should run in the right order" do @a.should.equal 2 @b.should.equal 2 end end shared "a shared context" do it "gets called where it is included" do true.should.be.true end end shared "another shared context" do it "can access data" do @magic.should.be.equal 42 end end describe "shared/behaves_like" do behaves_like "a shared context" ctx = self it "raises NameError when the context is not found" do lambda { ctx.behaves_like "whoops" }.should.raise NameError end behaves_like "a shared context" before { @magic = 42 } behaves_like "another shared context" end