diff options
Diffstat (limited to 'spec/mspec/spec/utils')
-rw-r--r-- | spec/mspec/spec/utils/deprecate_spec.rb | 17 | ||||
-rw-r--r-- | spec/mspec/spec/utils/name_map_spec.rb | 175 | ||||
-rw-r--r-- | spec/mspec/spec/utils/options_spec.rb | 1309 | ||||
-rw-r--r-- | spec/mspec/spec/utils/script_spec.rb | 473 | ||||
-rw-r--r-- | spec/mspec/spec/utils/version_spec.rb | 45 |
5 files changed, 2019 insertions, 0 deletions
diff --git a/spec/mspec/spec/utils/deprecate_spec.rb b/spec/mspec/spec/utils/deprecate_spec.rb new file mode 100644 index 0000000000..14e05c6f35 --- /dev/null +++ b/spec/mspec/spec/utils/deprecate_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' +require 'mspec/utils/deprecate' + +describe MSpec, "#deprecate" do + it "warns when using a deprecated method" do + warning = nil + $stderr.stub(:puts) { |str| warning = str } + MSpec.deprecate(:some_method, :other_method) + warning.should start_with(<<-EOS.chomp) + +some_method is deprecated, use other_method instead. +from +EOS + warning.should include(__FILE__) + warning.should include('8') + end +end diff --git a/spec/mspec/spec/utils/name_map_spec.rb b/spec/mspec/spec/utils/name_map_spec.rb new file mode 100644 index 0000000000..d38230ce06 --- /dev/null +++ b/spec/mspec/spec/utils/name_map_spec.rb @@ -0,0 +1,175 @@ +require 'spec_helper' +require 'mspec/utils/name_map' + +module NameMapSpecs + class A + A = self + + def self.a; end + def a; end + def c; end + + class B + def b; end + end + end + + class Error + end + + class Fixnum + def f; end + end + + def self.n; end + def n; end +end + +describe NameMap, "#exception?" do + before :each do + @map = NameMap.new + end + + it "returns true if the constant is Errno" do + @map.exception?("Errno").should == true + end + + it "returns true if the constant is a kind of Exception" do + @map.exception?("Errno::EBADF").should == true + @map.exception?("LoadError").should == true + @map.exception?("SystemExit").should == true + end + + it "returns false if the constant is not a kind of Exception" do + @map.exception?("NameMapSpecs::Error").should == false + @map.exception?("NameMapSpecs").should == false + end + + it "returns false if the constant does not exist" do + @map.exception?("Nonexistent").should == false + end +end + +describe NameMap, "#class_or_module" do + before :each do + @map = NameMap.new true + end + + it "returns the constant specified by the string" do + @map.class_or_module("NameMapSpecs").should == NameMapSpecs + end + + it "returns the constant specified by the 'A::B' string" do + @map.class_or_module("NameMapSpecs::A").should == NameMapSpecs::A + end + + it "returns nil if the constant is not a class or module" do + @map.class_or_module("Float::MAX").should == nil + end + + it "returns nil if the constant is in the set of excluded constants" do + excluded = %w[ + MSpecScript + MkSpec + NameMap + ] + + excluded.each do |const| + @map.class_or_module(const).should == nil + end + end + + it "returns nil if the constant does not exist" do + @map.class_or_module("Heaven").should == nil + @map.class_or_module("Hell").should == nil + @map.class_or_module("Bush::Brain").should == nil + end +end + +describe NameMap, "#dir_name" do + before :each do + @map = NameMap.new + end + + it "returns a directory name from the base name and constant" do + @map.dir_name("NameMapSpecs", 'spec/core').should == 'spec/core/namemapspecs' + end + + it "returns a directory name from the components in the constants name" do + @map.dir_name("NameMapSpecs::A", 'spec').should == 'spec/namemapspecs/a' + @map.dir_name("NameMapSpecs::A::B", 'spec').should == 'spec/namemapspecs/a/b' + end + + it "returns a directory name without 'class' for constants like TrueClass" do + @map.dir_name("TrueClass", 'spec').should == 'spec/true' + @map.dir_name("FalseClass", 'spec').should == 'spec/false' + end + + it "returns 'exception' for the directory name of any Exception subclass" do + @map.dir_name("SystemExit", 'spec').should == 'spec/exception' + @map.dir_name("Errno::EBADF", 'spec').should == 'spec/exception' + end + + it "returns 'class' for Class" do + @map.dir_name("Class", 'spec').should == 'spec/class' + end +end + +# These specs do not cover all the mappings, but only describe how the +# name is derived when the hash item maps to a single value, a hash with +# a specific item, or a hash with a :default item. +describe NameMap, "#file_name" do + before :each do + @map = NameMap.new + end + + it "returns the name of the spec file based on the constant and method" do + @map.file_name("[]=", "Array").should == "element_set_spec.rb" + end + + it "returns the name of the spec file based on the special entry for the method" do + @map.file_name("~", "Regexp").should == "match_spec.rb" + @map.file_name("~", "Fixnum").should == "complement_spec.rb" + end + + it "returns the name of the spec file based on the default entry for the method" do + @map.file_name("<<", "NameMapSpecs").should == "append_spec.rb" + end + + it "uses the last component of the constant to look up the method name" do + @map.file_name("^", "NameMapSpecs::Fixnum").should == "bit_xor_spec.rb" + end +end + +describe NameMap, "#namespace" do + before :each do + @map = NameMap.new + end + + it "prepends the module to the constant name" do + @map.namespace("SubModule", Integer).should == "SubModule::Integer" + end + + it "does not prepend Object, Class, or Module to the constant name" do + @map.namespace("Object", String).should == "String" + @map.namespace("Module", Integer).should == "Integer" + @map.namespace("Class", Float).should == "Float" + end +end + +describe NameMap, "#map" do + before :each do + @map = NameMap.new + end + + it "flattens an object hierarchy into a single Hash" do + @map.map({}, [NameMapSpecs]).should == { + "NameMapSpecs." => ["n"], + "NameMapSpecs#" => ["n"], + "NameMapSpecs::A." => ["a"], + "NameMapSpecs::A#" => ["a", "c"], + "NameMapSpecs::A::B#" => ["b"], + "NameMapSpecs::Fixnum#" => ["f"] + } + end +end diff --git a/spec/mspec/spec/utils/options_spec.rb b/spec/mspec/spec/utils/options_spec.rb new file mode 100644 index 0000000000..26c52bdbd0 --- /dev/null +++ b/spec/mspec/spec/utils/options_spec.rb @@ -0,0 +1,1309 @@ +require 'spec_helper' +require 'mspec/utils/options' +require 'mspec/version' +require 'mspec/guards/guard' +require 'mspec/runner/mspec' +require 'mspec/runner/formatters' + +describe MSpecOption, ".new" do + before :each do + @opt = MSpecOption.new("-a", "--bdc", "ARG", "desc", :block) + end + + it "sets the short attribute" do + @opt.short.should == "-a" + end + + it "sets the long attribute" do + @opt.long.should == "--bdc" + end + + it "sets the arg attribute" do + @opt.arg.should == "ARG" + end + + it "sets the description attribute" do + @opt.description.should == "desc" + end + + it "sets the block attribute" do + @opt.block.should == :block + end +end + +describe MSpecOption, "#arg?" do + it "returns true if arg attribute is not nil" do + MSpecOption.new(nil, nil, "ARG", nil, nil).arg?.should be_true + end + + it "returns false if arg attribute is nil" do + MSpecOption.new(nil, nil, nil, nil, nil).arg?.should be_false + end +end + +describe MSpecOption, "#match?" do + before :each do + @opt = MSpecOption.new("-a", "--bdc", "ARG", "desc", :block) + end + + it "returns true if the argument matches the short option" do + @opt.match?("-a").should be_true + end + + it "returns true if the argument matches the long option" do + @opt.match?("--bdc").should be_true + end + + it "returns false if the argument matches neither the short nor long option" do + @opt.match?("-b").should be_false + @opt.match?("-abdc").should be_false + end +end + +describe MSpecOptions, ".new" do + before :each do + @opt = MSpecOptions.new("cmd", 20, :config) + end + + it "sets the banner attribute" do + @opt.banner.should == "cmd" + end + + it "sets the config attribute" do + @opt.config.should == :config + end + + it "sets the width attribute" do + @opt.width.should == 20 + end + + it "sets the default width attribute" do + MSpecOptions.new.width.should == 30 + end +end + +describe MSpecOptions, "#on" do + before :each do + @opt = MSpecOptions.new + end + + it "adds a short option" do + @opt.should_receive(:add).with("-a", nil, nil, "desc", nil) + @opt.on("-a", "desc") + end + + it "adds a short option taking an argument" do + @opt.should_receive(:add).with("-a", nil, "ARG", "desc", nil) + @opt.on("-a", "ARG", "desc") + end + + it "adds a long option" do + @opt.should_receive(:add).with("-a", nil, nil, "desc", nil) + @opt.on("-a", "desc") + end + + it "adds a long option taking an argument" do + @opt.should_receive(:add).with("-a", nil, nil, "desc", nil) + @opt.on("-a", "desc") + end + + it "adds a short and long option" do + @opt.should_receive(:add).with("-a", nil, nil, "desc", nil) + @opt.on("-a", "desc") + end + + it "adds a short and long option taking an argument" do + @opt.should_receive(:add).with("-a", nil, nil, "desc", nil) + @opt.on("-a", "desc") + end + + it "raises MSpecOptions::OptionError if pass less than 2 arguments" do + lambda { @opt.on }.should raise_error(MSpecOptions::OptionError) + lambda { @opt.on "" }.should raise_error(MSpecOptions::OptionError) + end +end + +describe MSpecOptions, "#add" do + before :each do + @opt = MSpecOptions.new "cmd", 20 + @prc = lambda { } + end + + it "adds documentation for an option" do + @opt.should_receive(:doc).with(" -t, --typo ARG Correct typo ARG") + @opt.add("-t", "--typo", "ARG", "Correct typo ARG", @prc) + end + + it "leaves spaces in the documentation for a missing short option" do + @opt.should_receive(:doc).with(" --typo ARG Correct typo ARG") + @opt.add(nil, "--typo", "ARG", "Correct typo ARG", @prc) + end + + it "handles a short option with argument but no long argument" do + @opt.should_receive(:doc).with(" -t ARG Correct typo ARG") + @opt.add("-t", nil, "ARG", "Correct typo ARG", @prc) + end + + it "registers an option" do + option = MSpecOption.new "-t", "--typo", "ARG", "Correct typo ARG", @prc + MSpecOption.should_receive(:new).with( + "-t", "--typo", "ARG", "Correct typo ARG", @prc).and_return(option) + @opt.add("-t", "--typo", "ARG", "Correct typo ARG", @prc) + @opt.options.should == [option] + end +end + +describe MSpecOptions, "#match?" do + before :each do + @opt = MSpecOptions.new + end + + it "returns the MSpecOption instance matching the argument" do + @opt.on "-a", "--abdc", "desc" + option = @opt.match? "-a" + @opt.match?("--abdc").should be(option) + option.should be_kind_of(MSpecOption) + option.short.should == "-a" + option.long.should == "--abdc" + option.description.should == "desc" + end +end + +describe MSpecOptions, "#process" do + before :each do + @opt = MSpecOptions.new + ScratchPad.clear + end + + it "calls the on_extra block if the argument does not match any option" do + @opt.on_extra { ScratchPad.record :extra } + @opt.process ["-a"], "-a", "-a", nil + ScratchPad.recorded.should == :extra + end + + it "returns the matching option" do + @opt.on "-a", "ARG", "desc" + option = @opt.process [], "-a", "-a", "ARG" + option.should be_kind_of(MSpecOption) + option.short.should == "-a" + option.arg.should == "ARG" + option.description.should == "desc" + end + + it "raises an MSpecOptions::ParseError if arg is nil and there are no more entries in argv" do + @opt.on "-a", "ARG", "desc" + lambda { @opt.process [], "-a", "-a", nil }.should raise_error(MSpecOptions::ParseError) + end + + it "fetches the argument for the option from argv if arg is nil" do + @opt.on("-a", "ARG", "desc") { |o| ScratchPad.record o } + @opt.process ["ARG"], "-a", "-a", nil + ScratchPad.recorded.should == "ARG" + end + + it "calls the option's block" do + @opt.on("-a", "ARG", "desc") { ScratchPad.record :option } + @opt.process [], "-a", "-a", "ARG" + ScratchPad.recorded.should == :option + end + + it "does not call the option's block if it is nil" do + @opt.on "-a", "ARG", "desc" + lambda { @opt.process [], "-a", "-a", "ARG" }.should_not raise_error + end +end + +describe MSpecOptions, "#split" do + before :each do + @opt = MSpecOptions.new + end + + it "breaks a string at the nth character" do + opt, arg, rest = @opt.split "-bdc", 2 + opt.should == "-b" + arg.should == "dc" + rest.should == "dc" + end + + it "returns nil for arg if there are no characters left" do + opt, arg, rest = @opt.split "-b", 2 + opt.should == "-b" + arg.should == nil + rest.should == "" + end +end + +describe MSpecOptions, "#parse" do + before :each do + @opt = MSpecOptions.new + @prc = lambda { ScratchPad.record :parsed } + @arg_prc = lambda { |o| ScratchPad.record [:parsed, o] } + ScratchPad.clear + end + + it "parses a short option" do + @opt.on "-a", "desc", &@prc + @opt.parse ["-a"] + ScratchPad.recorded.should == :parsed + end + + it "parse a long option" do + @opt.on "--abdc", "desc", &@prc + @opt.parse ["--abdc"] + ScratchPad.recorded.should == :parsed + end + + it "parses a short option group" do + @opt.on "-a", "ARG", "desc", &@arg_prc + @opt.parse ["-a", "ARG"] + ScratchPad.recorded.should == [:parsed, "ARG"] + end + + it "parses a short option with an argument" do + @opt.on "-a", "ARG", "desc", &@arg_prc + @opt.parse ["-a", "ARG"] + ScratchPad.recorded.should == [:parsed, "ARG"] + end + + it "parses a short option with connected argument" do + @opt.on "-a", "ARG", "desc", &@arg_prc + @opt.parse ["-aARG"] + ScratchPad.recorded.should == [:parsed, "ARG"] + end + + it "parses a long option with an argument" do + @opt.on "--abdc", "ARG", "desc", &@arg_prc + @opt.parse ["--abdc", "ARG"] + ScratchPad.recorded.should == [:parsed, "ARG"] + end + + it "parses a long option with an '=' argument" do + @opt.on "--abdc", "ARG", "desc", &@arg_prc + @opt.parse ["--abdc=ARG"] + ScratchPad.recorded.should == [:parsed, "ARG"] + end + + it "parses a short option group with the final option taking an argument" do + ScratchPad.record [] + @opt.on("-a", "desc") { |o| ScratchPad << :a } + @opt.on("-b", "ARG", "desc") { |o| ScratchPad << [:b, o] } + @opt.parse ["-ab", "ARG"] + ScratchPad.recorded.should == [:a, [:b, "ARG"]] + end + + it "parses a short option group with a connected argument" do + ScratchPad.record [] + @opt.on("-a", "desc") { |o| ScratchPad << :a } + @opt.on("-b", "ARG", "desc") { |o| ScratchPad << [:b, o] } + @opt.on("-c", "desc") { |o| ScratchPad << :c } + @opt.parse ["-acbARG"] + ScratchPad.recorded.should == [:a, :c, [:b, "ARG"]] + end + + it "returns the unprocessed entries" do + @opt.on "-a", "ARG", "desc", &@arg_prc + @opt.parse(["abdc", "-a", "ilny"]).should == ["abdc"] + end + + it "calls the on_extra handler with unrecognized options" do + ScratchPad.record [] + @opt.on_extra { |o| ScratchPad << o } + @opt.on "-a", "desc" + @opt.parse ["-a", "-b"] + ScratchPad.recorded.should == ["-b"] + end + + it "does not attempt to call the block if it is nil" do + @opt.on "-a", "ARG", "desc" + @opt.parse(["-a", "ARG"]).should == [] + end + + it "raises MSpecOptions::ParseError if passed an unrecognized option" do + @opt.should_receive(:raise).with(MSpecOptions::ParseError, an_instance_of(String)) + @opt.stub(:puts) + @opt.stub(:exit) + @opt.parse "-u" + end +end + +describe MSpecOptions, "#banner=" do + before :each do + @opt = MSpecOptions.new + end + + it "sets the banner attribute" do + @opt.banner.should == "" + @opt.banner = "banner" + @opt.banner.should == "banner" + end +end + +describe MSpecOptions, "#width=" do + before :each do + @opt = MSpecOptions.new + end + + it "sets the width attribute" do + @opt.width.should == 30 + @opt.width = 20 + @opt.width.should == 20 + end +end + +describe MSpecOptions, "#config=" do + before :each do + @opt = MSpecOptions.new + end + + it "sets the config attribute" do + @opt.config.should be_nil + @opt.config = :config + @opt.config.should == :config + end +end + +describe MSpecOptions, "#doc" do + before :each do + @opt = MSpecOptions.new "command" + end + + it "adds text to be displayed with #to_s" do + @opt.doc "Some message" + @opt.doc "Another message" + @opt.to_s.should == <<-EOD +command + +Some message +Another message +EOD + end +end + +describe MSpecOptions, "#version" do + before :each do + @opt = MSpecOptions.new + ScratchPad.clear + end + + it "installs a basic -v, --version option" do + @opt.should_receive(:puts) + @opt.should_receive(:exit) + @opt.version "1.0.0" + @opt.parse "-v" + end + + it "accepts a block instead of using the default block" do + @opt.version("1.0.0") { |o| ScratchPad.record :version } + @opt.parse "-v" + ScratchPad.recorded.should == :version + end +end + +describe MSpecOptions, "#help" do + before :each do + @opt = MSpecOptions.new + ScratchPad.clear + end + + it "installs a basic -h, --help option" do + @opt.should_receive(:puts) + @opt.should_receive(:exit).with(1) + @opt.help + @opt.parse "-h" + end + + it "accepts a block instead of using the default block" do + @opt.help { |o| ScratchPad.record :help } + @opt.parse "-h" + ScratchPad.recorded.should == :help + end +end + +describe MSpecOptions, "#on_extra" do + before :each do + @opt = MSpecOptions.new + ScratchPad.clear + end + + it "registers a block to be called when an option is not recognized" do + @opt.on_extra { ScratchPad.record :extra } + @opt.parse "-g" + ScratchPad.recorded.should == :extra + end +end + +describe MSpecOptions, "#to_s" do + before :each do + @opt = MSpecOptions.new "command" + end + + it "returns the banner and descriptive strings for all registered options" do + @opt.on "-t", "--this ARG", "Adds this ARG to the list" + @opt.to_s.should == <<-EOD +command + + -t, --this ARG Adds this ARG to the list +EOD + end +end + +describe "The -B, --config FILE option" do + before :each do + @options, @config = new_option + end + + it "is enabled with #configure { }" do + @options.should_receive(:on).with("-B", "--config", "FILE", + an_instance_of(String)) + @options.configure {} + end + + it "calls the passed block" do + ["-B", "--config"].each do |opt| + ScratchPad.clear + + @options.configure { |x| ScratchPad.record x } + @options.parse [opt, "file"] + ScratchPad.recorded.should == "file" + end + end +end + +describe "The -C, --chdir DIR option" do + before :each do + @options, @config = new_option + @options.chdir + end + + it "is enabled with #chdir" do + @options.should_receive(:on).with("-C", "--chdir", "DIR", + an_instance_of(String)) + @options.chdir + end + + it "changes the working directory to DIR" do + Dir.should_receive(:chdir).with("dir").twice + ["-C", "--chdir"].each do |opt| + @options.parse [opt, "dir"] + end + end +end + +describe "The --prefix STR option" do + before :each do + @options, @config = new_option + end + + it "is enabled with #prefix" do + @options.should_receive(:on).with("--prefix", "STR", + an_instance_of(String)) + @options.prefix + end + + it "sets the prefix config value" do + @options.prefix + @options.parse ["--prefix", "some/dir"] + @config[:prefix].should == "some/dir" + end +end + +describe "The -n, --name RUBY_NAME option" do + before :each do + @verbose, $VERBOSE = $VERBOSE, nil + @options, @config = new_option + end + + after :each do + $VERBOSE = @verbose + end + + it "is enabled with #name" do + @options.should_receive(:on).with("-n", "--name", "RUBY_NAME", + an_instance_of(String)) + @options.name + end + + it "sets RUBY_NAME when invoked" do + Object.should_receive(:const_set).with(:RUBY_NAME, "name").twice + @options.name + @options.parse ["-n", "name"] + @options.parse ["--name", "name"] + end +end + +describe "The -t, --target TARGET option" do + before :each do + @options, @config = new_option + @options.targets + end + + it "is enabled with #targets" do + @options.stub(:on) + @options.should_receive(:on).with("-t", "--target", "TARGET", + an_instance_of(String)) + @options.targets + end + + it "sets the target to 'ruby' and flags to verbose with TARGET 'r' or 'ruby'" do + ["-t", "--target"].each do |opt| + ["r", "ruby"].each do |t| + @config[:target] = nil + @options.parse [opt, t] + @config[:target].should == "ruby" + end + end + end + + it "sets the target to 'jruby' with TARGET 'j' or 'jruby'" do + ["-t", "--target"].each do |opt| + ["j", "jruby"].each do |t| + @config[:target] = nil + @options.parse [opt, t] + @config[:target].should == "jruby" + end + end + end + + it "sets the target to 'shotgun/rubinius' with TARGET 'x' or 'rubinius'" do + ["-t", "--target"].each do |opt| + ["x", "rubinius"].each do |t| + @config[:target] = nil + @options.parse [opt, t] + @config[:target].should == "./bin/rbx" + end + end + end + + it "set the target to 'rbx' with TARGET 'rbx'" do + ["-t", "--target"].each do |opt| + ["X", "rbx"].each do |t| + @config[:target] = nil + @options.parse [opt, t] + @config[:target].should == "rbx" + end + end + end + + it "sets the target to 'maglev' with TARGET 'm' or 'maglev'" do + ["-t", "--target"].each do |opt| + ["m", "maglev"].each do |t| + @config[:target] = nil + @options.parse [opt, t] + @config[:target].should == "maglev-ruby" + end + end + end + + it "sets the target to 'topaz' with TARGET 't' or 'topaz'" do + ["-t", "--target"].each do |opt| + ["t", "topaz"].each do |t| + @config[:target] = nil + @options.parse [opt, t] + @config[:target].should == "topaz" + end + end + end + + it "sets the target to TARGET" do + ["-t", "--target"].each do |opt| + @config[:target] = nil + @options.parse [opt, "whateva"] + @config[:target].should == "whateva" + end + end +end + +describe "The -T, --target-opt OPT option" do + before :each do + @options, @config = new_option + @options.targets + end + + it "is enabled with #targets" do + @options.stub(:on) + @options.should_receive(:on).with("-T", "--target-opt", "OPT", + an_instance_of(String)) + @options.targets + end + + it "adds OPT to flags" do + ["-T", "--target-opt"].each do |opt| + @config[:flags].delete "--whateva" + @options.parse [opt, "--whateva"] + @config[:flags].should include("--whateva") + end + end +end + +describe "The -I, --include DIR option" do + before :each do + @options, @config = new_option + @options.targets + end + + it "is enabled with #targets" do + @options.stub(:on) + @options.should_receive(:on).with("-I", "--include", "DIR", + an_instance_of(String)) + @options.targets + end + + it "add DIR to the load path" do + ["-I", "--include"].each do |opt| + @config[:loadpath].delete "-Ipackage" + @options.parse [opt, "package"] + @config[:loadpath].should include("-Ipackage") + end + end +end + +describe "The -r, --require LIBRARY option" do + before :each do + @options, @config = new_option + @options.targets + end + + it "is enabled with #targets" do + @options.stub(:on) + @options.should_receive(:on).with("-r", "--require", "LIBRARY", + an_instance_of(String)) + @options.targets + end + + it "adds LIBRARY to the requires list" do + ["-r", "--require"].each do |opt| + @config[:requires].delete "-rlibrick" + @options.parse [opt, "librick"] + @config[:requires].should include("-rlibrick") + end + end +end + +describe "The -f, --format FORMAT option" do + before :each do + @options, @config = new_option + @options.formatters + end + + it "is enabled with #formatters" do + @options.stub(:on) + @options.should_receive(:on).with("-f", "--format", "FORMAT", + an_instance_of(String)) + @options.formatters + end + + it "sets the SpecdocFormatter with FORMAT 's' or 'specdoc'" do + ["-f", "--format"].each do |opt| + ["s", "specdoc"].each do |f| + @config[:formatter] = nil + @options.parse [opt, f] + @config[:formatter].should == SpecdocFormatter + end + end + end + + it "sets the HtmlFormatter with FORMAT 'h' or 'html'" do + ["-f", "--format"].each do |opt| + ["h", "html"].each do |f| + @config[:formatter] = nil + @options.parse [opt, f] + @config[:formatter].should == HtmlFormatter + end + end + end + + it "sets the DottedFormatter with FORMAT 'd', 'dot' or 'dotted'" do + ["-f", "--format"].each do |opt| + ["d", "dot", "dotted"].each do |f| + @config[:formatter] = nil + @options.parse [opt, f] + @config[:formatter].should == DottedFormatter + end + end + end + + it "sets the DescribeFormatter with FORMAT 'b' or 'describe'" do + ["-f", "--format"].each do |opt| + ["b", "describe"].each do |f| + @config[:formatter] = nil + @options.parse [opt, f] + @config[:formatter].should == DescribeFormatter + end + end + end + + it "sets the FileFormatter with FORMAT 'f', 'file'" do + ["-f", "--format"].each do |opt| + ["f", "file"].each do |f| + @config[:formatter] = nil + @options.parse [opt, f] + @config[:formatter].should == FileFormatter + end + end + end + + it "sets the UnitdiffFormatter with FORMAT 'u', 'unit', or 'unitdiff'" do + ["-f", "--format"].each do |opt| + ["u", "unit", "unitdiff"].each do |f| + @config[:formatter] = nil + @options.parse [opt, f] + @config[:formatter].should == UnitdiffFormatter + end + end + end + + it "sets the SummaryFormatter with FORMAT 'm' or 'summary'" do + ["-f", "--format"].each do |opt| + ["m", "summary"].each do |f| + @config[:formatter] = nil + @options.parse [opt, f] + @config[:formatter].should == SummaryFormatter + end + end + end + + it "sets the SpinnerFormatter with FORMAT 'a', '*', or 'spin'" do + ["-f", "--format"].each do |opt| + ["a", "*", "spin"].each do |f| + @config[:formatter] = nil + @options.parse [opt, f] + @config[:formatter].should == SpinnerFormatter + end + end + end + + it "sets the MethodFormatter with FORMAT 't' or 'method'" do + ["-f", "--format"].each do |opt| + ["t", "method"].each do |f| + @config[:formatter] = nil + @options.parse [opt, f] + @config[:formatter].should == MethodFormatter + end + end + end + + it "sets the YamlFormatter with FORMAT 'y' or 'yaml'" do + ["-f", "--format"].each do |opt| + ["y", "yaml"].each do |f| + @config[:formatter] = nil + @options.parse [opt, f] + @config[:formatter].should == YamlFormatter + end + end + end + + it "sets the JUnitFormatter with FORMAT 'j' or 'junit'" do + ["-f", "--format"].each do |opt| + ["j", "junit"].each do |f| + @config[:formatter] = nil + @options.parse [opt, f] + @config[:formatter].should == JUnitFormatter + end + end + end +end + +describe "The -o, --output FILE option" do + before :each do + @options, @config = new_option + @options.formatters + end + + it "is enabled with #formatters" do + @options.stub(:on) + @options.should_receive(:on).with("-o", "--output", "FILE", + an_instance_of(String)) + @options.formatters + end + + it "sets the output to FILE" do + ["-o", "--output"].each do |opt| + @config[:output] = nil + @options.parse [opt, "some/file"] + @config[:output].should == "some/file" + end + end +end + +describe "The -e, --example STR" do + before :each do + @options, @config = new_option + @options.filters + end + + it "is enabled with #filters" do + @options.stub(:on) + @options.should_receive(:on).with("-e", "--example", "STR", + an_instance_of(String)) + @options.filters + end + + it "adds STR to the includes list" do + ["-e", "--example"].each do |opt| + @config[:includes] = [] + @options.parse [opt, "this spec"] + @config[:includes].should include("this spec") + end + end +end + +describe "The -E, --exclude STR" do + before :each do + @options, @config = new_option + @options.filters + end + + it "is enabled with #filters" do + @options.stub(:on) + @options.should_receive(:on).with("-E", "--exclude", "STR", + an_instance_of(String)) + @options.filters + end + + it "adds STR to the excludes list" do + ["-E", "--exclude"].each do |opt| + @config[:excludes] = [] + @options.parse [opt, "this spec"] + @config[:excludes].should include("this spec") + end + end +end + +describe "The -p, --pattern PATTERN" do + before :each do + @options, @config = new_option + @options.filters + end + + it "is enabled with #filters" do + @options.stub(:on) + @options.should_receive(:on).with("-p", "--pattern", "PATTERN", + an_instance_of(String)) + @options.filters + end + + it "adds PATTERN to the included patterns list" do + ["-p", "--pattern"].each do |opt| + @config[:patterns] = [] + @options.parse [opt, "this spec"] + @config[:patterns].should include(/this spec/) + end + end +end + +describe "The -P, --excl-pattern PATTERN" do + before :each do + @options, @config = new_option + @options.filters + end + + it "is enabled with #filters" do + @options.stub(:on) + @options.should_receive(:on).with("-P", "--excl-pattern", "PATTERN", + an_instance_of(String)) + @options.filters + end + + it "adds PATTERN to the excluded patterns list" do + ["-P", "--excl-pattern"].each do |opt| + @config[:xpatterns] = [] + @options.parse [opt, "this spec"] + @config[:xpatterns].should include(/this spec/) + end + end +end + +describe "The -g, --tag TAG" do + before :each do + @options, @config = new_option + @options.filters + end + + it "is enabled with #filters" do + @options.stub(:on) + @options.should_receive(:on).with("-g", "--tag", "TAG", + an_instance_of(String)) + @options.filters + end + + it "adds TAG to the included tags list" do + ["-g", "--tag"].each do |opt| + @config[:tags] = [] + @options.parse [opt, "this spec"] + @config[:tags].should include("this spec") + end + end +end + +describe "The -G, --excl-tag TAG" do + before :each do + @options, @config = new_option + @options.filters + end + + it "is enabled with #filters" do + @options.stub(:on) + @options.should_receive(:on).with("-G", "--excl-tag", "TAG", + an_instance_of(String)) + @options.filters + end + + it "adds TAG to the excluded tags list" do + ["-G", "--excl-tag"].each do |opt| + @config[:xtags] = [] + @options.parse [opt, "this spec"] + @config[:xtags].should include("this spec") + end + end +end + +describe "The -w, --profile FILE option" do + before :each do + @options, @config = new_option + @options.filters + end + + it "is enabled with #filters" do + @options.stub(:on) + @options.should_receive(:on).with("-w", "--profile", "FILE", + an_instance_of(String)) + @options.filters + end + + it "adds FILE to the included profiles list" do + ["-w", "--profile"].each do |opt| + @config[:profiles] = [] + @options.parse [opt, "spec/profiles/rails.yaml"] + @config[:profiles].should include("spec/profiles/rails.yaml") + end + end +end + +describe "The -W, --excl-profile FILE option" do + before :each do + @options, @config = new_option + @options.filters + end + + it "is enabled with #filters" do + @options.stub(:on) + @options.should_receive(:on).with("-W", "--excl-profile", "FILE", + an_instance_of(String)) + @options.filters + end + + it "adds FILE to the excluded profiles list" do + ["-W", "--excl-profile"].each do |opt| + @config[:xprofiles] = [] + @options.parse [opt, "spec/profiles/rails.yaml"] + @config[:xprofiles].should include("spec/profiles/rails.yaml") + end + end +end + +describe "The -Z, --dry-run option" do + before :each do + @options, @config = new_option + @options.pretend + end + + it "is enabled with #pretend" do + @options.should_receive(:on).with("-Z", "--dry-run", an_instance_of(String)) + @options.pretend + end + + it "registers the MSpec pretend mode" do + MSpec.should_receive(:register_mode).with(:pretend).twice + ["-Z", "--dry-run"].each do |opt| + @options.parse opt + end + end +end + +describe "The --unguarded option" do + before :each do + @options, @config = new_option + @options.unguarded + end + + it "is enabled with #unguarded" do + @options.stub(:on) + @options.should_receive(:on).with("--unguarded", an_instance_of(String)) + @options.unguarded + end + + it "registers the MSpec unguarded mode" do + MSpec.should_receive(:register_mode).with(:unguarded) + @options.parse "--unguarded" + end +end + +describe "The --no-ruby_guard option" do + before :each do + @options, @config = new_option + @options.unguarded + end + + it "is enabled with #unguarded" do + @options.stub(:on) + @options.should_receive(:on).with("--no-ruby_bug", an_instance_of(String)) + @options.unguarded + end + + it "registers the MSpec no_ruby_bug mode" do + MSpec.should_receive(:register_mode).with(:no_ruby_bug) + @options.parse "--no-ruby_bug" + end +end + +describe "The -H, --random option" do + before :each do + @options, @config = new_option + @options.randomize + end + + it "is enabled with #randomize" do + @options.should_receive(:on).with("-H", "--random", an_instance_of(String)) + @options.randomize + end + + it "registers the MSpec randomize mode" do + MSpec.should_receive(:randomize).twice + ["-H", "--random"].each do |opt| + @options.parse opt + end + end +end + +describe "The -R, --repeat option" do + before :each do + @options, @config = new_option + @options.repeat + end + + it "is enabled with #repeat" do + @options.should_receive(:on).with("-R", "--repeat", "NUMBER", an_instance_of(String)) + @options.repeat + end + + it "registers the MSpec repeat mode" do + ["-R", "--repeat"].each do |opt| + MSpec.repeat = 1 + @options.parse [opt, "10"] + repeat_count = 0 + MSpec.repeat do + repeat_count += 1 + end + repeat_count.should == 10 + end + end +end + +describe "The -V, --verbose option" do + before :each do + @options, @config = new_option + @options.verbose + end + + it "is enabled with #verbose" do + @options.stub(:on) + @options.should_receive(:on).with("-V", "--verbose", an_instance_of(String)) + @options.verbose + end + + it "registers a verbose output object with MSpec" do + MSpec.should_receive(:register).with(:start, anything()).twice + MSpec.should_receive(:register).with(:load, anything()).twice + ["-V", "--verbose"].each do |opt| + @options.parse opt + end + end +end + +describe "The -m, --marker MARKER option" do + before :each do + @options, @config = new_option + @options.verbose + end + + it "is enabled with #verbose" do + @options.stub(:on) + @options.should_receive(:on).with("-m", "--marker", "MARKER", + an_instance_of(String)) + @options.verbose + end + + it "registers a marker output object with MSpec" do + MSpec.should_receive(:register).with(:load, anything()).twice + ["-m", "--marker"].each do |opt| + @options.parse [opt, ","] + end + end +end + +describe "The --int-spec option" do + before :each do + @options, @config = new_option + @options.interrupt + end + + it "is enabled with #interrupt" do + @options.should_receive(:on).with("--int-spec", an_instance_of(String)) + @options.interrupt + end + + it "sets the abort config option to false to only abort the running spec with ^C" do + @config[:abort] = true + @options.parse "--int-spec" + @config[:abort].should == false + end +end + +describe "The -Y, --verify option" do + before :each do + @options, @config = new_option + @options.verify + end + + it "is enabled with #interrupt" do + @options.stub(:on) + @options.should_receive(:on).with("-Y", "--verify", an_instance_of(String)) + @options.verify + end + + it "sets the MSpec mode to :verify" do + MSpec.should_receive(:register_mode).with(:verify).twice + ["-Y", "--verify"].each do |m| + @options.parse m + end + end +end + +describe "The -O, --report option" do + before :each do + @options, @config = new_option + @options.verify + end + + it "is enabled with #interrupt" do + @options.stub(:on) + @options.should_receive(:on).with("-O", "--report", an_instance_of(String)) + @options.verify + end + + it "sets the MSpec mode to :report" do + MSpec.should_receive(:register_mode).with(:report).twice + ["-O", "--report"].each do |m| + @options.parse m + end + end +end + +describe "The --report-on GUARD option" do + before :all do + MSpec.stub(:register_mode) + end + + before :each do + @options, @config = new_option + @options.verify + + SpecGuard.clear_guards + end + + after :each do + SpecGuard.clear_guards + end + + it "is enabled with #interrupt" do + @options.stub(:on) + @options.should_receive(:on).with("--report-on", "GUARD", an_instance_of(String)) + @options.verify + end + + it "sets the MSpec mode to :report_on" do + MSpec.should_receive(:register_mode).with(:report_on) + @options.parse ["--report-on", "ruby_bug"] + end + + it "converts the guard name to a symbol" do + name = double("ruby_bug") + name.should_receive(:to_sym) + @options.parse ["--report-on", name] + end + + it "saves the name of the guard" do + @options.parse ["--report-on", "ruby_bug"] + SpecGuard.guards.should == [:ruby_bug] + end +end + +describe "The -K, --action-tag TAG option" do + before :each do + @options, @config = new_option + @options.action_filters + end + + it "is enabled with #action_filters" do + @options.stub(:on) + @options.should_receive(:on).with("-K", "--action-tag", "TAG", + an_instance_of(String)) + @options.action_filters + end + + it "adds TAG to the list of tags that trigger actions" do + ["-K", "--action-tag"].each do |opt| + @config[:atags] = [] + @options.parse [opt, "action-tag"] + @config[:atags].should include("action-tag") + end + end +end + +describe "The -S, --action-string STR option" do + before :each do + @options, @config = new_option + @options.action_filters + end + + it "is enabled with #action_filters" do + @options.stub(:on) + @options.should_receive(:on).with("-S", "--action-string", "STR", + an_instance_of(String)) + @options.action_filters + end + + it "adds STR to the list of spec descriptions that trigger actions" do + ["-S", "--action-string"].each do |opt| + @config[:astrings] = [] + @options.parse [opt, "action-str"] + @config[:astrings].should include("action-str") + end + end +end + +describe "The -d, --debug option" do + before :each do + @options, @config = new_option + @options.debug + end + + after :each do + $MSPEC_DEBUG = nil + end + + it "is enabled with #debug" do + @options.stub(:on) + @options.should_receive(:on).with("-d", "--debug", an_instance_of(String)) + @options.debug + end + + it "sets $MSPEC_DEBUG to true" do + ["-d", "--debug"].each do |opt| + $MSPEC_DEBUG.should_not be_true + @options.parse opt + $MSPEC_DEBUG.should be_true + $MSPEC_DEBUG = nil + end + end +end diff --git a/spec/mspec/spec/utils/script_spec.rb b/spec/mspec/spec/utils/script_spec.rb new file mode 100644 index 0000000000..62f6787acd --- /dev/null +++ b/spec/mspec/spec/utils/script_spec.rb @@ -0,0 +1,473 @@ +require 'spec_helper' +require 'mspec/utils/script' +require 'mspec/runner/mspec' +require 'mspec/runner/filters' +require 'mspec/runner/actions/filter' + +describe MSpecScript, ".config" do + it "returns a Hash" do + MSpecScript.config.should be_kind_of(Hash) + end +end + +describe MSpecScript, ".set" do + it "sets the config hash key, value" do + MSpecScript.set :a, 10 + MSpecScript.config[:a].should == 10 + end +end + +describe MSpecScript, ".get" do + it "gets the config hash value for a key" do + MSpecScript.set :a, 10 + MSpecScript.get(:a).should == 10 + end +end + +describe MSpecScript, "#config" do + it "returns the MSpecScript config hash" do + MSpecScript.set :b, 5 + MSpecScript.new.config[:b].should == 5 + end + + it "returns the MSpecScript config hash from subclasses" do + class MSSClass < MSpecScript; end + MSpecScript.set :b, 5 + MSSClass.new.config[:b].should == 5 + end +end + +describe MSpecScript, "#load_default" do + before :all do + @verbose = $VERBOSE + $VERBOSE = nil + end + + after :all do + $VERBOSE = @verbose + end + + before :each do + @version = RUBY_VERSION + if Object.const_defined? :RUBY_ENGINE + @engine = Object.const_get :RUBY_ENGINE + end + @script = MSpecScript.new + MSpecScript.stub(:new).and_return(@script) + end + + after :each do + Object.const_set :RUBY_VERSION, @version + Object.const_set :RUBY_ENGINE, @engine if @engine + end + + it "attempts to load 'default.mspec'" do + @script.stub(:try_load) + @script.should_receive(:try_load).with('default.mspec').and_return(true) + @script.load_default + end + + it "attempts to load a config file based on RUBY_ENGINE and RUBY_VERSION" do + Object.const_set :RUBY_ENGINE, "ybur" + Object.const_set :RUBY_VERSION, "1.8.9" + default = "ybur.1.8.mspec" + @script.should_receive(:try_load).with('default.mspec').and_return(false) + @script.should_receive(:try_load).with(default) + @script.should_receive(:try_load).with('ybur.mspec') + @script.load_default + end +end + +describe MSpecScript, ".main" do + before :each do + @script = double("MSpecScript").as_null_object + MSpecScript.stub(:new).and_return(@script) + # Do not require full mspec as it would conflict with RSpec + MSpecScript.should_receive(:require).with('mspec') + end + + it "creates an instance of MSpecScript" do + MSpecScript.should_receive(:new).and_return(@script) + MSpecScript.main + end + + it "attempts to load the default config" do + @script.should_receive(:load_default) + MSpecScript.main + end + + it "attempts to load the '~/.mspecrc' script" do + @script.should_receive(:try_load).with('~/.mspecrc') + MSpecScript.main + end + + it "calls the #options method on the script" do + @script.should_receive(:options) + MSpecScript.main + end + + it "calls the #signals method on the script" do + @script.should_receive(:signals) + MSpecScript.main + end + + it "calls the #register method on the script" do + @script.should_receive(:register) + MSpecScript.main + end + + it "calls the #setup_env method on the script" do + @script.should_receive(:setup_env) + MSpecScript.main + end + + it "calls the #run method on the script" do + @script.should_receive(:run) + MSpecScript.main + end +end + +describe MSpecScript, "#initialize" do + before :each do + @config = MSpecScript.new.config + end + + it "sets the default config values" do + @config[:formatter].should == nil + @config[:includes].should == [] + @config[:excludes].should == [] + @config[:patterns].should == [] + @config[:xpatterns].should == [] + @config[:tags].should == [] + @config[:xtags].should == [] + @config[:atags].should == [] + @config[:astrings].should == [] + @config[:abort].should == true + @config[:config_ext].should == '.mspec' + end +end + +describe MSpecScript, "#load" do + before :each do + File.stub(:exist?).and_return(false) + @script = MSpecScript.new + @file = "default.mspec" + @base = "default" + end + + it "attempts to locate the file through the expanded path name" do + File.should_receive(:expand_path).with(@file, ".").and_return(@file) + File.should_receive(:exist?).with(@file).and_return(true) + Kernel.should_receive(:load).with(@file).and_return(:loaded) + @script.load(@file).should == :loaded + end + + it "appends config[:config_ext] to the name and attempts to locate the file through the expanded path name" do + File.should_receive(:expand_path).with(@base, ".").and_return(@base) + File.should_receive(:expand_path).with(@base, "spec").and_return(@base) + File.should_receive(:expand_path).with(@file, ".").and_return(@file) + File.should_receive(:exist?).with(@base).and_return(false) + File.should_receive(:exist?).with(@file).and_return(true) + Kernel.should_receive(:load).with(@file).and_return(:loaded) + @script.load(@base).should == :loaded + end + + it "attemps to locate the file in '.'" do + path = File.expand_path @file, "." + File.should_receive(:exist?).with(path).and_return(true) + Kernel.should_receive(:load).with(path).and_return(:loaded) + @script.load(@file).should == :loaded + end + + it "appends config[:config_ext] to the name and attempts to locate the file in '.'" do + path = File.expand_path @file, "." + File.should_receive(:exist?).with(path).and_return(true) + Kernel.should_receive(:load).with(path).and_return(:loaded) + @script.load(@base).should == :loaded + end + + it "attemps to locate the file in 'spec'" do + path = File.expand_path @file, "spec" + File.should_receive(:exist?).with(path).and_return(true) + Kernel.should_receive(:load).with(path).and_return(:loaded) + @script.load(@file).should == :loaded + end + + it "appends config[:config_ext] to the name and attempts to locate the file in 'spec'" do + path = File.expand_path @file, "spec" + File.should_receive(:exist?).with(path).and_return(true) + Kernel.should_receive(:load).with(path).and_return(:loaded) + @script.load(@base).should == :loaded + end + + it "loads a given file only once" do + path = File.expand_path @file, "spec" + File.should_receive(:exist?).with(path).and_return(true) + Kernel.should_receive(:load).once.with(path).and_return(:loaded) + @script.load(@base).should == :loaded + @script.load(@base).should == true + end +end + +describe MSpecScript, "#custom_options" do + before :each do + @script = MSpecScript.new + end + + after :each do + end + + it "prints 'None'" do + options = double("options") + options.should_receive(:doc).with(" No custom options registered") + @script.custom_options options + end +end + +describe MSpecScript, "#register" do + before :each do + @script = MSpecScript.new + + @formatter = double("formatter").as_null_object + @script.config[:formatter] = @formatter + end + + it "creates and registers the formatter" do + @formatter.should_receive(:new).and_return(@formatter) + @formatter.should_receive(:register) + @script.register + end + + it "does not register the formatter if config[:formatter] is false" do + @script.config[:formatter] = false + @script.register + end + + it "calls #custom_register" do + @script.should_receive(:custom_register) + @script.register + end + + it "registers :formatter with the formatter instance" do + @formatter.stub(:new).and_return(@formatter) + MSpec.should_receive(:store).with(:formatter, @formatter) + @script.register + end + + it "does not register :formatter if config[:formatter] is false" do + @script.config[:formatter] = false + MSpec.should_not_receive(:store) + @script.register + end +end + +describe MSpecScript, "#register" do + before :each do + @script = MSpecScript.new + + @formatter = double("formatter").as_null_object + @script.config[:formatter] = @formatter + + @filter = double("filter") + @filter.should_receive(:register) + + @ary = ["some", "spec"] + end + + it "creates and registers a MatchFilter for include specs" do + MatchFilter.should_receive(:new).with(:include, *@ary).and_return(@filter) + @script.config[:includes] = @ary + @script.register + end + + it "creates and registers a MatchFilter for excluded specs" do + MatchFilter.should_receive(:new).with(:exclude, *@ary).and_return(@filter) + @script.config[:excludes] = @ary + @script.register + end + + it "creates and registers a RegexpFilter for include specs" do + RegexpFilter.should_receive(:new).with(:include, *@ary).and_return(@filter) + @script.config[:patterns] = @ary + @script.register + end + + it "creates and registers a RegexpFilter for excluded specs" do + RegexpFilter.should_receive(:new).with(:exclude, *@ary).and_return(@filter) + @script.config[:xpatterns] = @ary + @script.register + end + + it "creates and registers a TagFilter for include specs" do + TagFilter.should_receive(:new).with(:include, *@ary).and_return(@filter) + @script.config[:tags] = @ary + @script.register + end + + it "creates and registers a TagFilter for excluded specs" do + TagFilter.should_receive(:new).with(:exclude, *@ary).and_return(@filter) + @script.config[:xtags] = @ary + @script.register + end + + it "creates and registers a ProfileFilter for include specs" do + ProfileFilter.should_receive(:new).with(:include, *@ary).and_return(@filter) + @script.config[:profiles] = @ary + @script.register + end + + it "creates and registers a ProfileFilter for excluded specs" do + ProfileFilter.should_receive(:new).with(:exclude, *@ary).and_return(@filter) + @script.config[:xprofiles] = @ary + @script.register + end +end + +describe MSpecScript, "#signals" do + before :each do + @script = MSpecScript.new + @abort = @script.config[:abort] + end + + after :each do + @script.config[:abort] = @abort + end + + it "traps the INT signal if config[:abort] is true" do + Signal.should_receive(:trap).with("INT") + @script.config[:abort] = true + @script.signals + end + + it "does not trap the INT signal if config[:abort] is not true" do + Signal.should_not_receive(:trap).with("INT") + @script.config[:abort] = false + @script.signals + end +end + +describe MSpecScript, "#entries" do + before :each do + @script = MSpecScript.new + + File.stub(:expand_path).and_return("name") + File.stub(:file?).and_return(false) + File.stub(:directory?).and_return(false) + end + + it "returns the pattern in an array if it is a file" do + File.should_receive(:expand_path).with("file").and_return("file/expanded") + File.should_receive(:file?).with("file/expanded").and_return(true) + @script.entries("file").should == ["file/expanded"] + end + + it "returns Dir['pattern/**/*_spec.rb'] if pattern is a directory" do + File.should_receive(:directory?).with("name").and_return(true) + File.stub(:expand_path).and_return("name","name/**/*_spec.rb") + Dir.should_receive(:[]).with("name/**/*_spec.rb").and_return(["dir1", "dir2"]) + @script.entries("name").should == ["dir1", "dir2"] + end + + it "aborts if pattern cannot be resolved to a file nor a directory" do + @script.should_receive(:abort) + @script.entries("pattern") + end + + describe "with config[:prefix] set" do + before :each do + prefix = "prefix/dir" + @script.config[:prefix] = prefix + @name = prefix + "/name" + end + + it "returns the pattern in an array if it is a file" do + File.should_receive(:expand_path).with(@name).and_return(@name) + File.should_receive(:file?).with(@name).and_return(true) + @script.entries("name").should == [@name] + end + + it "returns Dir['pattern/**/*_spec.rb'] if pattern is a directory" do + File.stub(:expand_path).and_return(@name, @name+"/**/*_spec.rb") + File.should_receive(:directory?).with(@name).and_return(true) + Dir.should_receive(:[]).with(@name + "/**/*_spec.rb").and_return(["dir1", "dir2"]) + @script.entries("name").should == ["dir1", "dir2"] + end + + it "aborts if pattern cannot be resolved to a file nor a directory" do + @script.should_receive(:abort) + @script.entries("pattern") + end + end +end + +describe MSpecScript, "#files" do + before :each do + @script = MSpecScript.new + end + + it "accumlates the values returned by #entries" do + @script.should_receive(:entries).and_return(["file1"], ["file2"]) + @script.files(["a", "b"]).should == ["file1", "file2"] + end + + it "strips a leading '^' and removes the values returned by #entries" do + @script.should_receive(:entries).and_return(["file1"], ["file2"], ["file1"]) + @script.files(["a", "b", "^a"]).should == ["file2"] + end + + it "processes the array elements in order" do + @script.should_receive(:entries).and_return(["file1"], ["file1"], ["file2"]) + @script.files(["^a", "a", "b"]).should == ["file1", "file2"] + end +end + +describe MSpecScript, "#files" do + before :each do + MSpecScript.set :files, ["file1", "file2"] + + @script = MSpecScript.new + end + + after :each do + MSpecScript.config.delete :files + end + + it "looks up items with leading ':' in the config object" do + @script.should_receive(:entries).and_return(["file1"], ["file2"]) + @script.files([":files"]).should == ["file1", "file2"] + end + + it "returns an empty list if the config key is not set" do + @script.files([":all_files"]).should == [] + end +end + +describe MSpecScript, "#setup_env" do + before :each do + @script = MSpecScript.new + @options, @config = new_option + @script.stub(:config).and_return(@config) + end + + after :each do + end + + it "sets MSPEC_RUNNER = '1' in the environment" do + ENV["MSPEC_RUNNER"] = "0" + @script.setup_env + ENV["MSPEC_RUNNER"].should == "1" + end + + it "sets RUBY_EXE = config[:target] in the environment" do + ENV["RUBY_EXE"] = nil + @script.setup_env + ENV["RUBY_EXE"].should == @config[:target] + end + + it "sets RUBY_FLAGS = config[:flags] in the environment" do + ENV["RUBY_FLAGS"] = nil + @config[:flags] = ["-w", "-Q"] + @script.setup_env + ENV["RUBY_FLAGS"].should == "-w -Q" + end +end diff --git a/spec/mspec/spec/utils/version_spec.rb b/spec/mspec/spec/utils/version_spec.rb new file mode 100644 index 0000000000..0b2d383c6d --- /dev/null +++ b/spec/mspec/spec/utils/version_spec.rb @@ -0,0 +1,45 @@ +require 'spec_helper' +require 'mspec/utils/version' + +describe SpecVersion, "#to_s" do + it "returns the string with which it was initialized" do + SpecVersion.new("1.8").to_s.should == "1.8" + SpecVersion.new("2.118.9").to_s.should == "2.118.9" + end +end + +describe SpecVersion, "#to_str" do + it "returns the same string as #to_s" do + version = SpecVersion.new("2.118.9") + version.to_str.should == version.to_s + end +end + +describe SpecVersion, "#to_i with ceil = false" do + it "returns an integer representation of the version string" do + SpecVersion.new("2.23.10").to_i.should == 1022310 + end + + it "replaces missing version parts with zeros" do + SpecVersion.new("1.8").to_i.should == 1010800 + SpecVersion.new("1.8.6").to_i.should == 1010806 + end +end + +describe SpecVersion, "#to_i with ceil = true" do + it "returns an integer representation of the version string" do + SpecVersion.new("1.8.6", true).to_i.should == 1010806 + end + + it "fills in 9s for missing tiny values" do + SpecVersion.new("1.8", true).to_i.should == 1010899 + SpecVersion.new("1.8.6", true).to_i.should == 1010806 + end +end + +describe SpecVersion, "#to_int" do + it "returns the same value as #to_i" do + version = SpecVersion.new("4.16.87") + version.to_int.should == version.to_i + end +end |