wycats / merb-extlib

Ruby core extensions library extracted from Merb core.

commit  13e1ce21ffb627294e934725d7fadbe6f7c557ae
tree    eaaab6a1a840a7b1186bf253e4c0b53167cdd488
parent  ab24c8eb3a5ce560434a131046cd096cbb22adb7
merb-extlib / spec / string_spec.rb
100644 168 lines (120 sloc) 4.213 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
165
166
167
168
require File.dirname(__FILE__) + '/spec_helper'
 
describe String, "#to_const_string" do
  it "swaps slashes with ::" do
    "foo/bar".to_const_string.should == "Foo::Bar"
  end
 
  it "replaces snake_case with CamelCase" do
    "foo/bar/baz_bat".to_const_string.should == "Foo::Bar::BazBat"
  end
 
  it "leaves constant string as is" do
    "Merb::Test".to_const_string.should == "Merb::Test"
  end
end
 
 
 
describe String, "#to_const_path" do
  it "swaps :: with slash" do
    "Foo::Bar".to_const_path.should == "foo/bar"
  end
 
  it "snake_cases string" do
    "Merb::Test::ViewHelper".to_const_path.should == "merb/test/view_helper"
  end
 
  it "leaves slash-separated snake case string as is" do
    "merb/test/view_helper".to_const_path.should == "merb/test/view_helper"
  end
end
 
 
 
describe String, "#camel_case" do
  it "handles lowercase without underscore" do
    "merb".camel_case.should == "Merb"
  end
 
  it "handles lowercase with 1 underscore" do
    "merb_core".camel_case.should == "MerbCore"
  end
 
  it "handles lowercase with more than 1 underscore" do
    "so_you_want_contribute_to_merb_core".camel_case.should == "SoYouWantContributeToMerbCore"
  end
 
  it "handles lowercase with more than 1 underscore in a row" do
    "__python__is__like__this".camel_case.should == "PythonIsLikeThis"
  end
 
  it "handle first capital letter with underscores" do
    "Python__Is__Like__This".camel_case.should == "PythonIsLikeThis"
  end
 
  it "leaves CamelCase as is" do
    "TestController".camel_case.should == "TestController"
  end
end
 
 
 
describe String, "#snake_case" do
  it "lowercases one word CamelCase" do
    "Merb".snake_case.should == "merb"
  end
 
  it "makes one underscore snake_case two word CamelCase" do
    "MerbCore".snake_case.should == "merb_core"
  end
 
  it "handles CamelCase with more than 2 words" do
    "SoYouWantContributeToMerbCore".snake_case.should == "so_you_want_contribute_to_merb_core"
  end
 
  it "handles CamelCase with more than 2 capital letter in a row" do
    "CNN".snake_case.should == "cnn"
    "CNNNews".snake_case.should == "cnn_news"
    "HeadlineCNNNews".snake_case.should == "headline_cnn_news"
  end
 
  it "does NOT change one word lowercase" do
    "merb".snake_case.should == "merb"
  end
 
  it "leaves snake_case as is" do
    "merb_core".snake_case.should == "merb_core"
  end
end
 
 
 
describe String, "#escape_regexp" do
  it "escapes all * in a string" do
    "*and*".escape_regexp.should == "\\*and\\*"
  end
 
  it "escapes all ? in a string" do
    "?and?".escape_regexp.should == "\\?and\\?"
  end
 
  it "escapes all { in a string" do
    "{and{".escape_regexp.should == "\\{and\\{"
  end
 
  it "escapes all } in a string" do
    "}and}".escape_regexp.should == "\\}and\\}"
  end
 
  it "escapes all . in a string" do
    ".and.".escape_regexp.should == "\\.and\\."
  end
 
  it "escapes all regexp special characters used in a string" do
    "*?{}.".escape_regexp.should == "\\*\\?\\{\\}\\."
  end
end
 
 
 
describe String, "#unescape_regexp" do
  it "unescapes all \\* in a string" do
    "\\*and\\*".unescape_regexp.should == "*and*"
  end
 
  it "unescapes all \\? in a string" do
    "\\?and\\?".unescape_regexp.should == "?and?"
  end
 
  it "unescapes all \\{ in a string" do
    "\\{and\\{".unescape_regexp.should == "{and{"
  end
 
  it "unescapes all \\} in a string" do
    "\\}and\\}".unescape_regexp.should == "}and}"
  end
 
  it "unescapes all \\. in a string" do
    "\\.and\\.".unescape_regexp.should == ".and."
  end
 
  it "unescapes all regexp special characters used in a string" do
    "\\*\\?\\{\\}\\.".unescape_regexp.should == "*?{}."
  end
end
 
 
 
describe String, "#/" do
  it "concanates operands with File::SEPARATOR" do
    ("merb" / "core").should == "merb#{File::SEPARATOR}core"
  end
end
 
 
require 'rbconfig'
describe String, "#relative_path_from" do
  it "uses other operand as base for path calculation" do
    site_dir = Config::CONFIG["sitedir"]
 
    two_levels_up = site_dir.split(File::SEPARATOR)
    2.times { two_levels_up.pop } # remove two deepest directories
    two_levels_up = File::SEPARATOR + two_levels_up.join(File::SEPARATOR)
 
    two_levels_up.relative_path_from(site_dir).should == "../.."
  end
end