Permalink
Browse files

Example groups

  • Loading branch information...
1 parent 4cc2a04 commit 18606926c185ca687d028d3782a0cf19fd56d70e @txus committed Oct 29, 2010
View
@@ -1,44 +0,0 @@
-Feature: DSL
-
- Spec-ish Domain Specific Language
-
- Scenario: declare an example
- Given a directory named "rspec_project"
- When I cd to "rspec_project"
- Given a file named "sample_spec.rb" with:
- """
- it "does something" do
- assert true
- end
- """
- When I run "bundle exec stendhal sample_spec.rb"
- Then the exit status should be 0
- And the output should contain "1 example, 0 failures"
-
- Scenario: declare a failing example
- Given a directory named "rspec_project"
- When I cd to "rspec_project"
- Given a file named "sample_spec.rb" with:
- """
- it "does something" do
- assert false
- end
- """
- When I run "bundle exec stendhal sample_spec.rb"
- Then the exit status should be 0
- And the output should contain "* does something"
- And the output should contain "1 example, 1 failure"
-
- Scenario: declare a pending example
- Given a directory named "rspec_project"
- When I cd to "rspec_project"
- Given a file named "sample_spec.rb" with:
- """
- pending "does something" do
- assert false
- end
- """
- When I run "bundle exec stendhal sample_spec.rb"
- Then the exit status should be 0
- And the output should contain "* does something"
- And the output should contain "1 example, 0 failures, 1 pending"
View
@@ -0,0 +1,101 @@
+Feature: Examples and example groups
+
+ Spec-ish Domain Specific Language
+
+ Scenario: declare an example
+ Given a directory named "rspec_project"
+ When I cd to "rspec_project"
+ Given a file named "sample_spec.rb" with:
+ """
+ describe "something" do
+ it "does something" do
+ assert true
+ end
+ end
+ """
+ When I run "stendhal sample_spec.rb"
+ Then the exit status should be 0
+ And the output should contain "1 example, 0 failures"
+
+ Scenario: declare a failing example
+ Given a directory named "rspec_project"
+ When I cd to "rspec_project"
+ Given a file named "sample_spec.rb" with:
+ """
+ describe "something" do
+ it "does something" do
+ assert false
+ end
+ end
+ """
+ When I run "stendhal sample_spec.rb"
+ Then the exit status should be 0
+ And the output should contain "* does something"
+ And the output should contain "1 example, 1 failure"
+
+ Scenario: declare a pending example
+ Given a directory named "rspec_project"
+ When I cd to "rspec_project"
+ Given a file named "sample_spec.rb" with:
+ """
+ describe "something" do
+ pending "does something" do
+ assert false
+ end
+ end
+ """
+ When I run "stendhal sample_spec.rb"
+ Then the exit status should be 0
+ And the output should contain "* does something"
+ And the output should contain "1 example, 0 failures, 1 pending"
+
+ Scenario: declare an example in nested groups
+ Given a directory named "rspec_project"
+ When I cd to "rspec_project"
+ Given a file named "sample_spec.rb" with:
+ """
+ describe "something" do
+ describe "inside another thing" do
+ it "does this" do
+ assert true
+ end
+ end
+ end
+ """
+ When I run "stendhal sample_spec.rb"
+ Then the exit status should be 0
+ And the output should contain "something"
+ And the output should contain "inside another thing"
+ And the output should contain "* does this"
+ And the output should contain "1 example, 0 failures"
+
+ Scenario: many examples in different example groups
+ Given a directory named "rspec_project"
+ When I cd to "rspec_project"
+ Given a file named "sample_spec.rb" with:
+ """
+ describe "something" do
+ describe "inside another thing" do
+ it "does this" do
+ assert true
+ end
+ end
+ describe "pending" do
+ pending "todo" do
+ assert false
+ end
+ it "fails" do
+ assert false
+ end
+ end
+ end
+ """
+ When I run "stendhal sample_spec.rb"
+ Then the exit status should be 0
+ And the output should contain "something"
+ And the output should contain "inside another thing"
+ And the output should contain "* does this"
+ And the output should contain "pending"
+ And the output should contain "* todo"
+ And the output should contain "* fails"
+ And the output should contain "3 examples, 1 failure, 1 pending"
View
@@ -1,17 +0,0 @@
-Feature: Command line runner
-
- Command line runner to run spec files.
-
- Scenario: Runs the spec and prints docstrings
- Given a directory named "rspec_project"
- When I cd to "rspec_project"
- Given a file named "sample_spec.rb" with:
- """
- it "does something" do
- puts "indeed"
- end
- """
- When I run "bundle exec stendhal sample_spec.rb"
- Then the exit status should be 0
- And the output should contain "* does something"
- And the output should contain "indeed"
View
@@ -1,4 +1,6 @@
require 'stendhal/assertions'
require 'stendhal/example'
+require 'stendhal/example_group'
require 'stendhal/core_ext/kernel'
+require 'stendhal/dsl'
View
@@ -4,7 +4,7 @@
require File.join('.',file)
end
-examples, failures, pending = Stendhal::Example.run_all
+examples, failures, pending = Stendhal::ExampleGroup.run_all
report = "\n#{examples} #{examples != 1 ? 'examples' : 'example'}, #{failures} #{failures != 1 ? 'failures' : 'failure'}"
report += ", #{pending} pending" if pending > 0
puts report
@@ -1,13 +1,5 @@
module Kernel
- def it(*args,&blk)
- Stendhal::Example.new(*args,&blk)
- end
- def pending(*args,&blk)
- if args.last.is_a? Hash
- args.last.update(:pending => true)
- else
- args << {:pending => true}
- end
- Stendhal::Example.new(*args,&blk)
+ def describe(*args,&blk)
+ Stendhal::ExampleGroup.new(*args,&blk)
end
end
View
@@ -0,0 +1,20 @@
+module Stendhal
+ module DSL
+ module Example
+ def it(*args,&blk)
+ self.add_example Stendhal::Example.new(*args,&blk)
+ end
+ def pending(*args,&blk)
+ if args.last.is_a? Hash
+ options = args.pop
+ options.update(:pending => true)
+ else
+ options = {:pending => true}
+ end
+ self.add_example Stendhal::Example.new(*args,options,&blk)
+ end
+ end
+ end
+end
+
+Stendhal::ExampleGroup.send(:include, Stendhal::DSL::Example)
View
@@ -43,29 +43,16 @@ def destroy
@@examples.delete self
end
- def self.run_all
- failures = pending = 0
- @@examples.reject do |example|
- if example.pending?
- pending += 1
- $stdout.print "* #{example.description}\n"
- true
- else
- false
- end
- end.each do |example|
- failures += example.run
- $stdout.print "* #{example.description}\n"
+ class << self
+
+ def destroy_all
+ @@examples = []
end
- [Example.count, failures, pending]
- end
- def self.destroy_all
- @@examples = []
- end
+ def count
+ @@examples.count
+ end
- def self.count
- @@examples.count
end
end
@@ -0,0 +1,61 @@
+module Stendhal
+ class ExampleGroup
+ @@example_groups = []
+
+ attr_reader :description
+ attr_reader :block
+ attr_reader :examples
+
+ def initialize(docstring, options = {}, &block)
+ @description = docstring
+ @block = block
+ @examples = []
+ instance_exec(&@block) if block_given?
+ @@example_groups << self
+ end
+
+ def run
+ failures = pending = 0
+ examples.reject do |example|
+ if example.pending?
+ pending += 1
+ $stdout.print "* #{example.description}\n"
+ true
+ else
+ false
+ end
+ end.each do |example|
+ failures += example.run
+ $stdout.print "* #{example.description}\n"
+ end
+ [examples.count, failures, pending]
+ end
+
+ def add_example(example)
+ examples << example
+ end
+
+ class << self
+ attr_reader :example_groups
+
+ def destroy_all
+ @@example_groups = []
+ end
+
+ def run_all
+ result = [0,0,0]
+ @@example_groups.each do |g|
+ puts g.description
+ group_result = g.run
+ result = result.zip(group_result).map{ |pair| pair[0] + pair[1] }
+ end
+ result
+ end
+
+ def count
+ @@example_groups.count
+ end
+ end
+
+ end
+end
Oops, something went wrong.

0 comments on commit 1860692

Please sign in to comment.