robsharp-extlib 0.9.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. data/.autotest +21 -0
  2. data/.document +5 -0
  3. data/.gitignore +22 -0
  4. data/LICENSE +47 -0
  5. data/README.rdoc +17 -0
  6. data/Rakefile +28 -0
  7. data/VERSION +1 -0
  8. data/extlib.gemspec +147 -0
  9. data/lib/extlib.rb +50 -0
  10. data/lib/extlib/array.rb +38 -0
  11. data/lib/extlib/assertions.rb +8 -0
  12. data/lib/extlib/blank.rb +89 -0
  13. data/lib/extlib/boolean.rb +11 -0
  14. data/lib/extlib/byte_array.rb +6 -0
  15. data/lib/extlib/class.rb +179 -0
  16. data/lib/extlib/datetime.rb +29 -0
  17. data/lib/extlib/dictionary.rb +433 -0
  18. data/lib/extlib/hash.rb +450 -0
  19. data/lib/extlib/hook.rb +407 -0
  20. data/lib/extlib/inflection.rb +442 -0
  21. data/lib/extlib/lazy_array.rb +453 -0
  22. data/lib/extlib/lazy_module.rb +18 -0
  23. data/lib/extlib/logger.rb +198 -0
  24. data/lib/extlib/mash.rb +157 -0
  25. data/lib/extlib/module.rb +51 -0
  26. data/lib/extlib/nil.rb +5 -0
  27. data/lib/extlib/numeric.rb +5 -0
  28. data/lib/extlib/object.rb +178 -0
  29. data/lib/extlib/object_space.rb +13 -0
  30. data/lib/extlib/pathname.rb +20 -0
  31. data/lib/extlib/pooling.rb +235 -0
  32. data/lib/extlib/rubygems.rb +38 -0
  33. data/lib/extlib/simple_set.rb +66 -0
  34. data/lib/extlib/string.rb +176 -0
  35. data/lib/extlib/struct.rb +17 -0
  36. data/lib/extlib/symbol.rb +21 -0
  37. data/lib/extlib/time.rb +44 -0
  38. data/lib/extlib/try_dup.rb +44 -0
  39. data/lib/extlib/virtual_file.rb +10 -0
  40. data/spec/array_spec.rb +40 -0
  41. data/spec/blank_spec.rb +86 -0
  42. data/spec/byte_array_spec.rb +8 -0
  43. data/spec/class_spec.rb +158 -0
  44. data/spec/datetime_spec.rb +22 -0
  45. data/spec/hash_spec.rb +536 -0
  46. data/spec/hook_spec.rb +1235 -0
  47. data/spec/inflection/plural_spec.rb +565 -0
  48. data/spec/inflection/singular_spec.rb +498 -0
  49. data/spec/inflection_extras_spec.rb +111 -0
  50. data/spec/lazy_array_spec.rb +1961 -0
  51. data/spec/lazy_module_spec.rb +38 -0
  52. data/spec/mash_spec.rb +312 -0
  53. data/spec/module_spec.rb +71 -0
  54. data/spec/object_space_spec.rb +10 -0
  55. data/spec/object_spec.rb +114 -0
  56. data/spec/pooling_spec.rb +511 -0
  57. data/spec/rcov.opts +6 -0
  58. data/spec/simple_set_spec.rb +58 -0
  59. data/spec/spec.opts +4 -0
  60. data/spec/spec_helper.rb +7 -0
  61. data/spec/string_spec.rb +222 -0
  62. data/spec/struct_spec.rb +13 -0
  63. data/spec/symbol_spec.rb +9 -0
  64. data/spec/time_spec.rb +31 -0
  65. data/spec/try_call_spec.rb +74 -0
  66. data/spec/try_dup_spec.rb +46 -0
  67. data/spec/virtual_file_spec.rb +22 -0
  68. data/tasks/ci.rake +1 -0
  69. data/tasks/metrics.rake +36 -0
  70. data/tasks/spec.rake +25 -0
  71. data/tasks/yard.rake +9 -0
  72. data/tasks/yardstick.rake +19 -0
  73. metadata +198 -0
@@ -0,0 +1,6 @@
1
+ --exclude "spec"
2
+ --sort coverage
3
+ --callsites
4
+ --xrefs
5
+ --profile
6
+ --text-summary
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+ require 'extlib/simple_set'
3
+
4
+ describe Extlib::SimpleSet do
5
+
6
+ before do
7
+ @s = Extlib::SimpleSet.new("Initial")
8
+ end
9
+
10
+ describe "#initialize" do
11
+ it 'adds passed value to the set' do
12
+ @new_generation_vms = Extlib::SimpleSet.new(["Rubinius", "PyPy", "Parrot"])
13
+
14
+ @new_generation_vms.should have_key("Rubinius")
15
+ @new_generation_vms.should have_key("PyPy")
16
+ @new_generation_vms.should have_key("Parrot")
17
+ end
18
+ end
19
+
20
+ describe "#<<" do
21
+ it "adds value to the set" do
22
+ @s << "Hello"
23
+ @s.to_a.should be_include("Hello")
24
+ end
25
+
26
+ it 'sets true mark on the key' do
27
+ @s << "Fun"
28
+ @s["Fun"].should be_true
29
+ end
30
+ end
31
+
32
+ describe "#merge(other)" do
33
+ it "preserves old values when values do not overlap" do
34
+ @s.should have_key("Initial")
35
+ end
36
+
37
+ it 'adds new values from merged set' do
38
+ @t = @s.merge(["Merged value"])
39
+ @t.should have_key("Merged value")
40
+ end
41
+
42
+ it 'returns a SimpleSet instance' do
43
+ @s.merge(["Merged value"]).should be_kind_of(Extlib::SimpleSet)
44
+ end
45
+ end
46
+
47
+ describe "#inspect" do
48
+ it "lists set values" do
49
+ @s.inspect.should == "#<SimpleSet: {\"Initial\"}>"
50
+ end
51
+ end
52
+
53
+ describe "#keys" do
54
+ it 'is aliased as to_a' do
55
+ @s.to_a.should === @s.keys
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --loadby random
3
+ --format profile
4
+ --backtrace
@@ -0,0 +1,7 @@
1
+ $TESTING=true
2
+ require "rubygems"
3
+ require "spec"
4
+ require "yaml"
5
+
6
+ lib_path = File.expand_path('../lib', __FILE__)
7
+ $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
@@ -0,0 +1,222 @@
1
+ require 'spec_helper'
2
+ require 'extlib/string'
3
+
4
+ describe String, "#to_const_string" do
5
+ it "swaps slashes with ::" do
6
+ "foo/bar".to_const_string.should == "Foo::Bar"
7
+ end
8
+
9
+ it "replaces snake_case with CamelCase" do
10
+ "foo/bar/baz_bat".to_const_string.should == "Foo::Bar::BazBat"
11
+ end
12
+
13
+ it "leaves constant string as is" do
14
+ "Merb::Test".to_const_string.should == "Merb::Test"
15
+ end
16
+ end
17
+
18
+
19
+
20
+ describe String, "#to_const_path" do
21
+ it "swaps :: with slash" do
22
+ "Foo::Bar".to_const_path.should == "foo/bar"
23
+ end
24
+
25
+ it "snake_cases string" do
26
+ "Merb::Test::ViewHelper".to_const_path.should == "merb/test/view_helper"
27
+ end
28
+
29
+ it "leaves slash-separated snake case string as is" do
30
+ "merb/test/view_helper".to_const_path.should == "merb/test/view_helper"
31
+ end
32
+ end
33
+
34
+
35
+
36
+ describe String, "#camel_case" do
37
+ it "handles lowercase without underscore" do
38
+ "merb".camel_case.should == "Merb"
39
+ end
40
+
41
+ it "handles lowercase with 1 underscore" do
42
+ "merb_core".camel_case.should == "MerbCore"
43
+ end
44
+
45
+ it "handles lowercase with more than 1 underscore" do
46
+ "so_you_want_contribute_to_merb_core".camel_case.should == "SoYouWantContributeToMerbCore"
47
+ end
48
+
49
+ it "handles lowercase with more than 1 underscore in a row" do
50
+ "__python__is__like__this".camel_case.should == "PythonIsLikeThis"
51
+ end
52
+
53
+ it "handle first capital letter with underscores" do
54
+ "Python__Is__Like__This".camel_case.should == "PythonIsLikeThis"
55
+ end
56
+
57
+ it "leaves CamelCase as is" do
58
+ "TestController".camel_case.should == "TestController"
59
+ end
60
+ end
61
+
62
+
63
+
64
+ describe String, "#snake_case" do
65
+ it "lowercases one word CamelCase" do
66
+ "Merb".snake_case.should == "merb"
67
+ end
68
+
69
+ it "makes one underscore snake_case two word CamelCase" do
70
+ "MerbCore".snake_case.should == "merb_core"
71
+ end
72
+
73
+ it "handles CamelCase with more than 2 words" do
74
+ "SoYouWantContributeToMerbCore".snake_case.should == "so_you_want_contribute_to_merb_core"
75
+ end
76
+
77
+ it "handles CamelCase with more than 2 capital letter in a row" do
78
+ "CNN".snake_case.should == "cnn"
79
+ "CNNNews".snake_case.should == "cnn_news"
80
+ "HeadlineCNNNews".snake_case.should == "headline_cnn_news"
81
+ "NameACRONYM".snake_case.should == "name_acronym"
82
+ end
83
+
84
+ it "does NOT change one word lowercase" do
85
+ "merb".snake_case.should == "merb"
86
+ end
87
+
88
+ it "leaves snake_case as is" do
89
+ "merb_core".snake_case.should == "merb_core"
90
+ end
91
+ end
92
+
93
+
94
+
95
+ describe String, "#escape_regexp" do
96
+ it "escapes all * in a string" do
97
+ "*and*".escape_regexp.should == "\\*and\\*"
98
+ end
99
+
100
+ it "escapes all ? in a string" do
101
+ "?and?".escape_regexp.should == "\\?and\\?"
102
+ end
103
+
104
+ it "escapes all { in a string" do
105
+ "{and{".escape_regexp.should == "\\{and\\{"
106
+ end
107
+
108
+ it "escapes all } in a string" do
109
+ "}and}".escape_regexp.should == "\\}and\\}"
110
+ end
111
+
112
+ it "escapes all . in a string" do
113
+ ".and.".escape_regexp.should == "\\.and\\."
114
+ end
115
+
116
+ it "escapes all regexp special characters used in a string" do
117
+ "*?{}.".escape_regexp.should == "\\*\\?\\{\\}\\."
118
+ end
119
+ end
120
+
121
+
122
+
123
+ describe String, "#unescape_regexp" do
124
+ it "unescapes all \\* in a string" do
125
+ "\\*and\\*".unescape_regexp.should == "*and*"
126
+ end
127
+
128
+ it "unescapes all \\? in a string" do
129
+ "\\?and\\?".unescape_regexp.should == "?and?"
130
+ end
131
+
132
+ it "unescapes all \\{ in a string" do
133
+ "\\{and\\{".unescape_regexp.should == "{and{"
134
+ end
135
+
136
+ it "unescapes all \\} in a string" do
137
+ "\\}and\\}".unescape_regexp.should == "}and}"
138
+ end
139
+
140
+ it "unescapes all \\. in a string" do
141
+ "\\.and\\.".unescape_regexp.should == ".and."
142
+ end
143
+
144
+ it "unescapes all regexp special characters used in a string" do
145
+ "\\*\\?\\{\\}\\.".unescape_regexp.should == "*?{}."
146
+ end
147
+ end
148
+
149
+
150
+
151
+ describe String, "#/" do
152
+ it "concanates operands with File::SEPARATOR" do
153
+ ("merb" / "core").should == "merb#{File::SEPARATOR}core"
154
+ end
155
+ end
156
+
157
+
158
+ require 'rbconfig'
159
+ describe String, "#relative_path_from" do
160
+ it "uses other operand as base for path calculation" do
161
+ site_dir = Config::CONFIG["sitedir"]
162
+
163
+ two_levels_up = site_dir.split(File::SEPARATOR)
164
+ 2.times { two_levels_up.pop } # remove two deepest directories
165
+ two_levels_up = two_levels_up.join(File::SEPARATOR)
166
+
167
+ two_levels_up.relative_path_from(site_dir).should == "../.."
168
+ end
169
+ end
170
+
171
+
172
+ describe String, ".translate" do
173
+ before(:each) do
174
+ String.stub!(:translations).and_return({ "on snakes and rubies" => "a serpenti e rubini" })
175
+ end
176
+
177
+ it 'looks up for translation in translations dictionary' do
178
+ String.translate("on snakes and rubies").should == "a serpenti e rubini"
179
+ end
180
+
181
+ it 'returns string that has no translations as it is' do
182
+ String.translate("shapes").should == "shapes"
183
+ String.translate("kalopsia").should == "kalopsia"
184
+ String.translate("holding on to nothing").should == "holding on to nothing"
185
+ end
186
+ end
187
+
188
+ describe String, ".t" do
189
+ before(:each) do
190
+ String.stub!(:translations).and_return({ '%s must not be blank' => "%s moet ingevuld worden",
191
+ 'username' => 'gebruikersnaam',
192
+ '%s must be between %s and %s characters long' => '%s moet tussen %s en %s tekens lang zijn'})
193
+ end
194
+
195
+ it 'looks up for translation in translations dictionary and translates parameters as well' do
196
+ "%s must not be blank".t(:username).should == "gebruikersnaam moet ingevuld worden"
197
+ "%s must not be blank".t('username').should == "gebruikersnaam moet ingevuld worden"
198
+ "%s must be between %s and %s characters long".t(:password, 5, 9).should == "password moet tussen 5 en 9 tekens lang zijn"
199
+ end
200
+
201
+ it 'returns string that has no translations as it is' do
202
+ "password".t.should == "password"
203
+ end
204
+
205
+ it 'should not translate when freezed' do
206
+ "%s must not be blank".t('username'.freeze).should == "username moet ingevuld worden"
207
+ end
208
+ end
209
+
210
+ describe String, ".translations" do
211
+ before(:each) do
212
+
213
+ end
214
+
215
+ it 'returns empty hash by default' do
216
+ String.translations.should == {}
217
+ end
218
+
219
+ it 'returns @translations if set' do
220
+ pending "is it @translations on metaclass or @@translations? leaving it out for now"
221
+ end
222
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+ require 'extlib/struct'
3
+
4
+ describe Struct do
5
+
6
+ it "should have attributes" do
7
+
8
+ s = Struct.new(:name).new('bob')
9
+ s.attributes.should == { :name => 'bob' }
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+ require 'extlib/symbol'
3
+
4
+ describe Symbol, "#/" do
5
+ it "concanates operands with File::SEPARATOR" do
6
+ (:merb / "core").should == "merb#{File::SEPARATOR}core"
7
+ (:merb / :core).should == "merb#{File::SEPARATOR}core"
8
+ end
9
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ require 'extlib/time'
3
+
4
+ require 'json'
5
+
6
+ describe Time, "#to_json" do
7
+
8
+ before do
9
+ @expected = "\"2008-03-28T22:54:20Z\""
10
+ end
11
+
12
+ it "should transform itself into a ISO 8601 compatible string" do
13
+ Time.utc(2008, 3, 28, 22, 54, 20).to_json.should == @expected
14
+ Time.xmlschema("2008-03-28T22:54:20Z").to_json.should == @expected
15
+ Time.xmlschema("2008-03-28T17:54:20-05:00").to_json.should == @expected
16
+ end
17
+ end
18
+
19
+ describe Time, "#to_time" do
20
+ it "should return a copy of its self" do
21
+ time = Time.now
22
+ time.to_time.should == time
23
+ end
24
+ end
25
+
26
+ describe Time, "#to_datetime" do
27
+ it "should return an equivalent DateTime" do
28
+ time = Time.now
29
+ time.to_datetime.should == DateTime.parse(time.to_s)
30
+ end
31
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+ require 'extlib'
3
+
4
+ describe "#try_call" do
5
+ describe "with an Object" do
6
+ before :all do
7
+ @receiver = Object.new
8
+ end
9
+
10
+ it "returns receiver itself" do
11
+ @receiver.try_call.should == @receiver
12
+ end
13
+ end
14
+
15
+ describe "with a number" do
16
+ before :all do
17
+ @receiver = 42
18
+ end
19
+
20
+ it "returns receiver itself" do
21
+ @receiver.try_call.should == @receiver
22
+ end
23
+ end
24
+
25
+ describe "with a String" do
26
+ before :all do
27
+ @receiver = "Ruby, programmer's best friend"
28
+ end
29
+
30
+ it "returns receiver itself" do
31
+ @receiver.try_call.should == @receiver
32
+ end
33
+ end
34
+
35
+ describe "with a hash" do
36
+ before :all do
37
+ @receiver = { :functional_programming => "FTW" }
38
+ end
39
+
40
+ it "returns receiver itself" do
41
+ @receiver.try_call.should == @receiver
42
+ end
43
+ end
44
+
45
+ describe "with a Proc" do
46
+ before :all do
47
+ @receiver = Proc.new { 5 * 7 }
48
+ end
49
+
50
+ it "returns result of calling of a proc" do
51
+ @receiver.try_call.should == 35
52
+ end
53
+ end
54
+
55
+ describe "with a Proc that takes 2 arguments" do
56
+ before :all do
57
+ @receiver = Proc.new { |one, other| one + other }
58
+ end
59
+
60
+ it "passes arguments to #call, returns result of calling of a proc" do
61
+ @receiver.try_call(10, 20).should == 30
62
+ end
63
+ end
64
+
65
+ describe "with a Proc that takes 3 arguments" do
66
+ before :all do
67
+ @receiver = Proc.new { |a, b, c| (a + b) * c }
68
+ end
69
+
70
+ it "passes arguments to #call, returns result of calling of a proc" do
71
+ @receiver.try_call(10, 20, 3).should == 90
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+ require 'extlib/try_dup'
3
+
4
+ describe "try_dup" do
5
+ it "returns a duplicate version on regular objects" do
6
+ obj = Object.new
7
+ oth = obj.try_dup
8
+ obj.should_not === oth
9
+ end
10
+
11
+ it "returns self on Numerics" do
12
+ obj = 12
13
+ oth = obj.try_dup
14
+ obj.should === oth
15
+ end
16
+
17
+ it "returns self on Symbols" do
18
+ obj = :test
19
+ oth = obj.try_dup
20
+ obj.should === oth
21
+ end
22
+
23
+ it "returns self on true" do
24
+ obj = true
25
+ oth = obj.try_dup
26
+ obj.should === oth
27
+ end
28
+
29
+ it "returns self on false" do
30
+ obj = false
31
+ oth = obj.try_dup
32
+ obj.should === oth
33
+ end
34
+
35
+ it "returns self on nil" do
36
+ obj = nil
37
+ oth = obj.try_dup
38
+ obj.should === oth
39
+ end
40
+
41
+ it "returns self on modules" do
42
+ obj = Module.new
43
+ oth = obj.try_dup
44
+ obj.object_id.should == oth.object_id
45
+ end
46
+ end