spec/spec/runner/options_spec.rb in rspec-1.0.8 vs spec/spec/runner/options_spec.rb in rspec-1.1.0
- old
+ new
@@ -1,175 +1,294 @@
require File.dirname(__FILE__) + '/../../spec_helper.rb'
module Spec
module Runner
describe Options do
- before do
+ before(:each) do
@err = StringIO.new('')
@out = StringIO.new('')
@options = Options.new(@err, @out)
end
- it "instantiates empty arrays" do
- @options.examples.should == []
- @options.formatters.should == []
+ after(:each) do
+ Spec::Expectations.differ = nil
end
- it "defaults to QuietBacktraceTweaker" do
- @options.backtrace_tweaker.class.should == QuietBacktraceTweaker
+ describe "#examples" do
+ it "should default to empty array" do
+ @options.examples.should == []
+ end
end
- it "defaults to no dry_run" do
- @options.dry_run.should == false
+ describe "#backtrace_tweaker" do
+ it "should default to QuietBacktraceTweaker" do
+ @options.backtrace_tweaker.class.should == QuietBacktraceTweaker
+ end
end
- it "parse_diff sets context_lines" do
- @options.parse_diff nil
- @options.context_lines.should == 3
+ describe "#dry_run" do
+ it "should default to false" do
+ @options.dry_run.should == false
+ end
end
- it "defaults diff to unified" do
- @options.parse_diff nil
- @options.diff_format.should == :unified
+ describe "#context_lines" do
+ it "should default to 3" do
+ @options.context_lines.should == 3
+ end
end
- it "should use unified diff format option when format is unified" do
- @options.parse_diff 'unified'
- @options.diff_format.should == :unified
- @options.differ_class.should equal(Spec::Expectations::Differs::Default)
- end
+ describe "#parse_diff with nil" do
+ before(:each) do
+ @options.parse_diff nil
+ end
- it "should use context diff format option when format is context" do
- @options.parse_diff 'context'
- @options.diff_format.should == :context
- @options.differ_class.should == Spec::Expectations::Differs::Default
- end
+ it "should make diff_format unified" do
+ @options.diff_format.should == :unified
+ end
- it "should use custom diff format option when format is a custom format" do
- @options.parse_diff "Custom::Formatter"
- @options.diff_format.should == :custom
- @options.differ_class.should == Custom::Formatter
+ it "should set Spec::Expectations.differ to be a default differ" do
+ Spec::Expectations.differ.class.should ==
+ ::Spec::Expectations::Differs::Default
+ end
end
- it "should print instructions about how to fix missing differ" do
- lambda { @options.parse_diff "Custom::MissingDiffer" }.should raise_error(NameError)
- @err.string.should match(/Couldn't find differ class Custom::MissingDiffer/n)
- end
+ describe "#parse_diff with 'unified'" do
+ before(:each) do
+ @options.parse_diff 'unified'
+ end
- it "should print instructions about how to fix bad formatter" do
- lambda do
- @options.parse_format "Custom::BadFormatter"
- end.should raise_error(NameError, /undefined local variable or method `bad_method'/)
- end
+ it "should make diff_format unified and uses default differ_class" do
+ @options.diff_format.should == :unified
+ @options.differ_class.should equal(Spec::Expectations::Differs::Default)
+ end
- it "parse_example sets single example when argument not a file" do
- example = "something or other"
- File.file?(example).should == false
- @options.parse_example example
- @options.examples.should eql(["something or other"])
+ it "should set Spec::Expectations.differ to be a default differ" do
+ Spec::Expectations.differ.class.should ==
+ ::Spec::Expectations::Differs::Default
+ end
end
- it "parse_example sets examples to contents of file" do
- example = "#{File.dirname(__FILE__)}/examples.txt"
- File.should_receive(:file?).with(example).and_return(true)
- file = StringIO.new("Sir, if you were my husband, I would poison your drink.\nMadam, if you were my wife, I would drink it.")
- File.should_receive(:open).with(example).and_return(file)
-
- @options.parse_example example
- @options.examples.should eql([
- "Sir, if you were my husband, I would poison your drink.",
- "Madam, if you were my wife, I would drink it."
- ])
- end
- end
+ describe "#parse_diff with 'context'" do
+ before(:each) do
+ @options.parse_diff 'context'
+ end
- describe Options, "splitting class names and args" do
- before do
- @err = StringIO.new('')
- @out = StringIO.new('')
- @options = Options.new(@err, @out)
- end
-
- it "should split class names with args" do
- @options.split_at_colon('Foo').should == ['Foo', nil]
- @options.split_at_colon('Foo:arg').should == ['Foo', 'arg']
- @options.split_at_colon('Foo::Bar::Zap:arg').should == ['Foo::Bar::Zap', 'arg']
- @options.split_at_colon('Foo:arg1,arg2').should == ['Foo', 'arg1,arg2']
- @options.split_at_colon('Foo::Bar::Zap:arg1,arg2').should == ['Foo::Bar::Zap', 'arg1,arg2']
- @options.split_at_colon('Foo::Bar::Zap:drb://foo,drb://bar').should == ['Foo::Bar::Zap', 'drb://foo,drb://bar']
- end
+ it "should make diff_format context and uses default differ_class" do
+ @options.diff_format.should == :context
+ @options.differ_class.should == Spec::Expectations::Differs::Default
+ end
- it "should raise error when splitting something starting with a number" do
- lambda { @options.split_at_colon('') }.should raise_error("Couldn't parse \"\"")
+ it "should set Spec::Expectations.differ to be a default differ" do
+ Spec::Expectations.differ.class.should ==
+ ::Spec::Expectations::Differs::Default
+ end
end
- it "should raise error when not class name" do
- lambda do
- @options.load_class('foo', 'fruit', '--food')
- end.should raise_error('"foo" is not a valid class name')
+ describe "#parse_diff with Custom::Differ" do
+ before(:each) do
+ @options.parse_diff 'Custom::Differ'
+ end
+
+ it "should use custom differ_class" do
+ @options.diff_format.should == :custom
+ @options.differ_class.should == Custom::Differ
+ Spec::Expectations.differ.should be_instance_of(Custom::Differ)
+ end
+
+ it "should set Spec::Expectations.differ to be a default differ" do
+ Spec::Expectations.differ.class.should ==
+ ::Custom::Differ
+ end
end
- end
- describe Options, "receiving create_behaviour_runner" do
- before do
- @err = StringIO.new
- @out = StringIO.new
- @options = Options.new(@err, @out)
+ describe "#parse_diff with missing class name" do
+ it "should raise error" do
+ lambda { @options.parse_diff "Custom::MissingDiffer" }.should raise_error(NameError)
+ @err.string.should match(/Couldn't find differ class Custom::MissingDiffer/n)
+ end
end
- it "should fail when custom runner not found" do
- @options.runner_arg = "Whatever"
- lambda { @options.create_behaviour_runner }.should raise_error(NameError)
- @err.string.should match(/Couldn't find behaviour runner class/)
+ describe "#parse_example" do
+ it "with argument thats not a file path, sets argument as the example" do
+ example = "something or other"
+ File.file?(example).should == false
+ @options.parse_example example
+ @options.examples.should eql(["something or other"])
+ end
+
+ it "with argument that is a file path, sets examples to contents of the file" do
+ example = "#{File.dirname(__FILE__)}/examples.txt"
+ File.should_receive(:file?).with(example).and_return(true)
+ file = StringIO.new("Sir, if you were my husband, I would poison your drink.\nMadam, if you were my wife, I would drink it.")
+ File.should_receive(:open).with(example).and_return(file)
+
+ @options.parse_example example
+ @options.examples.should eql([
+ "Sir, if you were my husband, I would poison your drink.",
+ "Madam, if you were my wife, I would drink it."
+ ])
+ end
end
- it "should fail when custom runner not valid class name" do
- @options.runner_arg = "whatever"
- lambda { @options.create_behaviour_runner }.should raise_error('"whatever" is not a valid class name')
- @err.string.should match(/"whatever" is not a valid class name/)
+ describe "#examples_should_not_be_run" do
+ it "should cause #run_examples to return true and do nothing" do
+ @options.examples_should_not_be_run
+ ExampleGroupRunner.should_not_receive(:new)
+
+ @options.run_examples.should be_true
+ end
end
- it "returns nil when generate is true" do
- @options.generate = true
- @options.create_behaviour_runner.should == nil
+ describe "#load_class" do
+ it "should raise error when not class name" do
+ lambda do
+ @options.send(:load_class, 'foo', 'fruit', '--food')
+ end.should raise_error('"foo" is not a valid class name')
+ end
end
- it "returns a BehaviourRunner by default" do
- runner = @options.create_behaviour_runner
- runner.class.should == BehaviourRunner
+ describe "#reporter" do
+ it "returns a Reporter" do
+ @options.reporter.should be_instance_of(Reporter)
+ @options.reporter.options.should === @options
+ end
end
- it "does not set Expectations differ when differ_class is not set" do
- @options.differ_class = nil
- Spec::Expectations.should_not_receive(:differ=)
- @options.create_behaviour_runner
+ describe "#add_example_group affecting passed in example_group" do
+ it "runs all examples when options.examples is nil" do
+ example_1_has_run = false
+ example_2_has_run = false
+ @example_group = Class.new(::Spec::Example::ExampleGroup).describe("Some Examples") do
+ it "runs 1" do
+ example_1_has_run = true
+ end
+ it "runs 2" do
+ example_2_has_run = true
+ end
+ end
+
+ @options.examples = nil
+
+ @options.add_example_group @example_group
+ @options.run_examples
+ example_1_has_run.should be_true
+ example_2_has_run.should be_true
+ end
+
+ it "keeps all example_definitions when options.examples is empty" do
+ example_1_has_run = false
+ example_2_has_run = false
+ @example_group = Class.new(::Spec::Example::ExampleGroup).describe("Some Examples") do
+ it "runs 1" do
+ example_1_has_run = true
+ end
+ it "runs 2" do
+ example_2_has_run = true
+ end
+ end
+
+ @options.examples = []
+
+ @options.add_example_group @example_group
+ @options.run_examples
+ example_1_has_run.should be_true
+ example_2_has_run.should be_true
+ end
end
- it "sets Expectations differ when differ_class is set" do
- @options.differ_class = Spec::Expectations::Differs::Default
- Spec::Expectations.should_receive(:differ=).with(anything()).and_return do |arg|
- arg.class.should == Spec::Expectations::Differs::Default
+ describe "#add_example_group affecting example_group" do
+ it "adds example_group when example_group has example_definitions and is not shared" do
+ @example_group = Class.new(::Spec::Example::ExampleGroup).describe("Some Examples") do
+ it "uses this example_group" do
+ end
+ end
+
+ @options.number_of_examples.should == 0
+ @options.add_example_group @example_group
+ @options.number_of_examples.should == 1
+ @options.example_groups.length.should == 1
end
- @options.configure
end
- it "creates a Reporter" do
- formatter = ::Spec::Runner::Formatter::BaseFormatter.new(:somewhere)
- @options.formatters << formatter
- reporter = Reporter.new(@formatters, @backtrace_tweaker)
- Reporter.should_receive(:new).with(@options.formatters, @options.backtrace_tweaker).and_return(reporter)
- @options.configure
- @options.reporter.should === reporter
+ describe "#remove_example_group" do
+ it "should remove the ExampleGroup from the list of ExampleGroups" do
+ @example_group = Class.new(::Spec::Example::ExampleGroup).describe("Some Examples") do
+ end
+ @options.add_example_group @example_group
+ @options.example_groups.should include(@example_group)
+
+ @options.remove_example_group @example_group
+ @options.example_groups.should_not include(@example_group)
+ end
end
- it "sets colour and dry_run on the formatters" do
- @options.colour = true
- @options.dry_run = true
- formatter = ::Spec::Runner::Formatter::BaseTextFormatter.new(:somewhere)
- formatter.should_receive(:colour=).with(true)
- formatter.should_receive(:dry_run=).with(true)
- @options.formatters << formatter
- @options.configure
+ describe "#run_examples" do
+ it "should use the standard runner by default" do
+ runner = ::Spec::Runner::ExampleGroupRunner.new(@options)
+ ::Spec::Runner::ExampleGroupRunner.should_receive(:new).
+ with(@options).
+ and_return(runner)
+ @options.user_input_for_runner = nil
+
+ @options.run_examples
+ end
+
+ it "should use a custom runner when given" do
+ runner = Custom::ExampleGroupRunner.new(@options, nil)
+ Custom::ExampleGroupRunner.should_receive(:new).
+ with(@options, nil).
+ and_return(runner)
+ @options.user_input_for_runner = "Custom::ExampleGroupRunner"
+
+ @options.run_examples
+ end
+
+ it "should use a custom runner with extra options" do
+ runner = Custom::ExampleGroupRunner.new(@options, 'something')
+ Custom::ExampleGroupRunner.should_receive(:new).
+ with(@options, 'something').
+ and_return(runner)
+ @options.user_input_for_runner = "Custom::ExampleGroupRunner:something"
+
+ @options.run_examples
+ end
+
+ describe "#run_examples when there are example_group" do
+ before(:each) do
+ @options.add_example_group Class.new(::Spec::Example::ExampleGroup)
+ @options.formatters << Formatter::BaseTextFormatter.new(@options, @out)
+ end
+
+ it "runs the Examples and outputs the result" do
+ @options.run_examples
+ @out.string.should include("0 examples, 0 failures")
+ end
+
+ it "sets #examples_run? to true" do
+ @options.examples_run?.should be_false
+ @options.run_examples
+ @options.examples_run?.should be_true
+ end
+ end
+
+ describe "#run_examples when there are no example_group" do
+ before(:each) do
+ @options.formatters << Formatter::BaseTextFormatter.new(@options, @out)
+ end
+
+ it "does not run Examples and does not output a result" do
+ @options.run_examples
+ @out.string.should_not include("examples")
+ @out.string.should_not include("failures")
+ end
+
+ it "sets #examples_run? to false" do
+ @options.examples_run?.should be_false
+ @options.run_examples
+ @options.examples_run?.should be_false
+ end
+ end
end
end
end
end