1 require File.dirname(__FILE__) + '/../spec_helper'
2 require 'mspec/utils/name_map'
28 describe NameMap, "#exception?" do
33 it "returns true if the constant is Errno" do
34 @map.exception?("Errno").should == true
37 it "returns true if the constant is a kind of Exception" do
38 @map.exception?("Errno::EBADF").should == true
39 @map.exception?("LoadError").should == true
40 @map.exception?("SystemExit").should == true
43 it "returns false if the constant is not a kind of Exception" do
44 @map.exception?("NameMapSpecs::Error").should == false
45 @map.exception?("NameMapSpecs").should == false
48 it "returns false if the constant does not exist" do
49 @map.exception?("Nonexistent").should == false
53 describe NameMap, "#class_or_module" do
55 @map = NameMap.new true
58 it "returns the constant specified by the string" do
59 @map.class_or_module("NameMapSpecs").should == NameMapSpecs
62 it "returns the constant specified by the 'A::B' string" do
63 @map.class_or_module("NameMapSpecs::A").should == NameMapSpecs::A
66 it "returns nil if the constant is not a class or module" do
67 @map.class_or_module("Float::MAX").should == nil
70 it "returns nil if the constant is in the set of excluded constants" do
80 excluded.each do |const|
81 @map.class_or_module(const).should == nil
85 it "returns nil if the constant does not exist" do
86 @map.class_or_module("Heaven").should == nil
87 @map.class_or_module("Hell").should == nil
88 @map.class_or_module("Bush::Brain").should == nil
92 describe NameMap, "#dir_name" do
97 it "returns a directory name from the base name and constant" do
98 @map.dir_name("NameMapSpecs", 'spec/core').should == 'spec/core/namemapspecs'
101 it "returns a directory name from the components in the constants name" do
102 @map.dir_name("NameMapSpecs::A", 'spec').should == 'spec/namemapspecs/a'
103 @map.dir_name("NameMapSpecs::A::B", 'spec').should == 'spec/namemapspecs/a/b'
106 it "returns a directory name without 'class' for constants like TrueClass" do
107 @map.dir_name("TrueClass", 'spec').should == 'spec/true'
108 @map.dir_name("FalseClass", 'spec').should == 'spec/false'
111 it "returns 'exception' for the directory name of any Exception subclass" do
112 @map.dir_name("SystemExit", 'spec').should == 'spec/exception'
113 @map.dir_name("Errno::EBADF", 'spec').should == 'spec/exception'
116 it "returns 'class' for Class" do
117 @map.dir_name("Class", 'spec').should == 'spec/class'
121 # These specs do not cover all the mappings, but only describe how the
122 # name is derived when the hash item maps to a single value, a hash with
123 # a specific item, or a hash with a :default item.
124 describe NameMap, "#file_name" do
129 it "returns the name of the spec file based on the constant and method" do
130 @map.file_name("[]=", "Array").should == "element_set_spec.rb"
133 it "returns the name of the spec file based on the special entry for the method" do
134 @map.file_name("~", "Regexp").should == "match_spec.rb"
135 @map.file_name("~", "Fixnum").should == "complement_spec.rb"
138 it "returns the name of the spec file based on the default entry for the method" do
139 @map.file_name("<<", "NameMapSpecs").should == "append_spec.rb"
142 it "uses the last component of the constant to look up the method name" do
143 @map.file_name("^", "NameMapSpecs::Fixnum").should == "bit_xor_spec.rb"
147 describe NameMap, "#namespace" do
152 it "prepends the module to the constant name" do
153 @map.namespace("SubModule", Fixnum).should == "SubModule::Fixnum"
156 it "does not prepend Object, Class, or Module to the constant name" do
157 @map.namespace("Object", Fixnum).should == "Fixnum"
158 @map.namespace("Module", Integer).should == "Integer"
159 @map.namespace("Class", Float).should == "Float"
163 describe NameMap, "#map" do
168 it "flattens an object hierarchy into a single Hash" do
169 @map.map({}, [NameMapSpecs]).should == {
170 "NameMapSpecs." => ["n"],
171 "NameMapSpecs#" => ["n"],
172 "NameMapSpecs::A." => ["a"],
173 "NameMapSpecs::A#" => ["a", "c"],
174 "NameMapSpecs::A::B#" => ["b"],
175 "NameMapSpecs::Fixnum#" => ["f"]