summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/utils/name_map_spec.rb
blob: d5d2cca84aa67c370f016df5d86e62e1535bf6b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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("~", "Integer").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::Integer").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