wycats / merb-extlib

Ruby core extensions library extracted from Merb core.

This URL has Read+Write access

commit  13e1ce21ffb627294e934725d7fadbe6f7c557ae
tree    eaaab6a1a840a7b1186bf253e4c0b53167cdd488
parent  ab24c8eb3a5ce560434a131046cd096cbb22adb7
merb-extlib / spec / object_spec.rb
100644 164 lines (127 sloc) 3.982 kb
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
require File.dirname(__FILE__) + '/spec_helper'
class Foo
end
 
 
describe Object do
  it "should treat an empty string as blank" do
    "".should be_blank
  end
 
  it "should treat a string with just spaces as blank" do
    " ".should be_blank
  end
 
  it "should treat an empty array as blank" do
    [].should be_blank
  end
 
  it "should treat boolean false as blank" do
    false.should be_blank
  end
end
 
 
 
describe Object, "#quacks_like" do
  it "use respond_to? to determine quacks_like :symbol" do
    "Foo".should be_quacks_like(:strip)
  end
 
  it "should return true if any of the elements in the Array are true" do
    "Foo".should be_quacks_like([String, Array])
  end
 
  it "should return false if an invalid value is passed in" do
    "Foo".should_not be_quacks_like({})
  end
end
 
 
 
describe Object, "#full_const_get" do
  class April
    class In
      class Paris
        PERFORMER = "Ella Fitzgerald"
      end
    end
  end
 
  module Succubus
    module In
      module Rapture
        PERFORMER = "Dimmu Borgir"
      end
    end
  end
 
  it "returns constant corresponding to the name" do
    Object.full_const_get("April").should == April
  end
 
  it "handles nested classes" do
    Object.full_const_get("April::In::Paris").should == April::In::Paris
  end
 
  it "handles nested modules" do
    Object.full_const_get("Succubus::In::Rapture").should == Succubus::In::Rapture
  end
  
  it "handles top level constant naming" do
    Object.full_const_get("::Succubus::In::Rapture").should == Succubus::In::Rapture
  end
 
  it "handles in-scoped constants in modules" do
    Object.full_const_get("Succubus::In::Rapture::PERFORMER").should == "Dimmu Borgir"
  end
 
  it "handles in-scoped constants in classes" do
    Object.full_const_get("April::In::Paris::PERFORMER").should == "Ella Fitzgerald"
  end
 
  it "acts as a global function" do
    lambda { self.full_const_get("April::In::Paris::PERFORMER") }.should raise_error(NameError)
  end
 
  it "raises an exception if constant is undefined" do
    lambda { Object.full_const_get("We::May::Never::Meet::Again") }.should raise_error(NameError)
  end
end
 
describe Object, "#full_const_set" do
  class April
    class In
      class Paris
      end
    end
  end
  
  module Succubus
    module In
      module Rapture
      end
    end
  end
 
  it "should assign top level constants" do
    Object.full_const_set('May', 5).should == 5
    Object.full_const_get('May').should == 5
  end
  
  it "handles top level constant naming" do
    Object.full_const_set('::June', 6).should == 6
    Object.full_const_get('::June').should == 6
  end
  
  it "handles in-scoped constants in classes" do
    Object.full_const_set("April::In::Paris::COUNTRY", "USA").should == "USA"
    Object.full_const_get("April::In::Paris::COUNTRY").should == "USA"
  end
 
  it "handles in-scoped constants in modules" do
    Object.full_const_set("Succubus::In::Rapture::COUNTRY", "Norway").should == "Norway"
    Object.full_const_get("Succubus::In::Rapture::COUNTRY").should == "Norway"
  end
 
  it "raises an exception if constant is undefined" do
    lambda { Object.full_const_set("We::May::Never::Meet", "Again") }.should raise_error(NameError)
  end
end
 
describe Object, "#make_module" do
  it "defines module from a string name" do
    Object.make_module("Cant::Take::That::Away::From::Me")
 
    defined?(Cant::Take::That::Away::From::Me).should == "constant"
  end
 
  it "is OK if module already defined" do
    module Merb
      module Is
        module Modular
        end
      end
    end
 
    lambda { Object.make_module("Merb::Is::Modular") }.should_not raise_error
  end
end
 
describe Object, "#in?" do
  it "should be true if the argument includes self" do
    4.in?([1,2,4,5]).should be_true
  end
  
  it "should be false if the argument does not include self" do
    4.in?([1,2,3,5]).should be_false
  end
  
  it "should splat the args so [] are not required" do
    4.in?(1,2,3,4,5).should be_true
  end
end