rubyexts 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/CHANGELOG +2 -0
  2. data/LICENSE +20 -0
  3. data/README.textile +0 -0
  4. data/Rakefile +79 -0
  5. data/TODO.txt +6 -0
  6. data/lib/rubyexts.rb +102 -0
  7. data/lib/rubyexts/array.rb +20 -0
  8. data/lib/rubyexts/attribute.rb +151 -0
  9. data/lib/rubyexts/capture_io.rb +32 -0
  10. data/lib/rubyexts/class.rb +177 -0
  11. data/lib/rubyexts/colored_string.rb +103 -0
  12. data/lib/rubyexts/enumerable.rb +30 -0
  13. data/lib/rubyexts/file.rb +84 -0
  14. data/lib/rubyexts/hash.rb +180 -0
  15. data/lib/rubyexts/kernel.rb +91 -0
  16. data/lib/rubyexts/module.rb +53 -0
  17. data/lib/rubyexts/object.rb +56 -0
  18. data/lib/rubyexts/object_space.rb +16 -0
  19. data/lib/rubyexts/os.rb +89 -0
  20. data/lib/rubyexts/platform.rb +27 -0
  21. data/lib/rubyexts/random.rb +25 -0
  22. data/lib/rubyexts/string.rb +92 -0
  23. data/lib/rubyexts/time.rb +15 -0
  24. data/lib/rubyexts/time_dsl.rb +62 -0
  25. data/lib/rubyexts/try_dup.rb +43 -0
  26. data/lib/rubyexts/unique_array.rb +16 -0
  27. data/rubyexts.gemspec +36 -0
  28. data/script/spec +12 -0
  29. data/spec/rubyexts/array_spec.rb +19 -0
  30. data/spec/rubyexts/attribute_spec.rb +37 -0
  31. data/spec/rubyexts/class_spec.rb +1 -0
  32. data/spec/rubyexts/cli_spec.rb +0 -0
  33. data/spec/rubyexts/colored_string_spec.rb +9 -0
  34. data/spec/rubyexts/enumerable_spec.rb +52 -0
  35. data/spec/rubyexts/file_spec.rb +78 -0
  36. data/spec/rubyexts/hash_spec.rb +91 -0
  37. data/spec/rubyexts/kernel_spec.rb +9 -0
  38. data/spec/rubyexts/module_spec.rb +0 -0
  39. data/spec/rubyexts/object_space_spec.rb +17 -0
  40. data/spec/rubyexts/object_spec.rb +57 -0
  41. data/spec/rubyexts/os_spec.rb +121 -0
  42. data/spec/rubyexts/platform_spec.rb +0 -0
  43. data/spec/rubyexts/random_spec.rb +31 -0
  44. data/spec/rubyexts/string_spec.rb +23 -0
  45. data/spec/rubyexts/time_dsl_spec.rb +88 -0
  46. data/spec/rubyexts/time_spec.rb +11 -0
  47. data/spec/rubyexts/unique_array_spec.rb +0 -0
  48. data/spec/rubyexts_spec.rb +182 -0
  49. data/spec/spec.opts +5 -0
  50. data/spec/spec_helper.rb +16 -0
  51. metadata +104 -0
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative "../../lib/rubyexts/object"
4
+
5
+ class ObjectStub
6
+ def generator
7
+ define_instance_method(:new_method) { "return value" }
8
+ end
9
+
10
+ def proxy(*args, &block)
11
+ return {args: args, block: block}
12
+ end
13
+ end
14
+
15
+ describe Object do
16
+ before(:each) do
17
+ @stub = ObjectStub.new
18
+ end
19
+
20
+ describe "#try" do
21
+ it "should returns nil if given method doesn't exist" do
22
+ @stub.try(:a_method).should be_nil
23
+ end
24
+
25
+ it "can takes arguments" do
26
+ returned = @stub.try(:proxy, :one, :two, :three)
27
+ returned[:args].should eql([:one, :two, :three])
28
+ end
29
+
30
+ it "can takes block" do
31
+ returned = @stub.try(:proxy) { [:one, :two, :three] }
32
+ returned[:block].call.should eql([:one, :two, :three])
33
+ end
34
+ end
35
+
36
+ describe "#not_nil?" do
37
+ it "should be true if object isn't nil" do
38
+ Object.new.not_nil?.should be_true
39
+ end
40
+
41
+ it "should be false if object is nil" do
42
+ nil.not_nil?.should be_false
43
+ end
44
+ end
45
+
46
+ describe "#define_instance_method" do
47
+ before(:each) do
48
+ @stub = ObjectStub.new
49
+ end
50
+
51
+ it "should define instance method" do
52
+ @stub.generator
53
+ methods = ObjectStub.instance_methods
54
+ methods.should include(:new_method)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,121 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative "../../lib/rubyexts/os"
4
+
5
+ describe OS do
6
+ before(:each) do
7
+ @os = OS.new(home: "/Users/botanicus", path: "/bin:/sbin")
8
+ end
9
+
10
+ describe ".parse" do
11
+ it "should transform 'KEY' => value syntax into key: value" do
12
+ @os = OS.parse("HOME" => "/Users/botanicus", "PATH" => "/bin:/sbin")
13
+ end
14
+
15
+ it "should use ENV by default" do
16
+ ENV.should eql(OS.parse.original_env)
17
+ end
18
+
19
+ it "should save original ENV" do
20
+ OS.parse.original_env.should eql(ENV)
21
+ end
22
+ end
23
+
24
+ describe "basic variables" do
25
+ before(:each) do
26
+ @os = OS.new(@os.env.merge(empty: "", digit: "123"))
27
+ end
28
+
29
+ it "should be shortcut for ENV['variable']" do
30
+ @os.home.should eql("/Users/botanicus")
31
+ end
32
+
33
+ it "should returns array of paths if key ends with PATH or LIB" do
34
+ @os.path.should eql(["/bin", "/sbin"])
35
+ end
36
+
37
+ it "should convert empty string to nil" do
38
+ @os.empty.should be_nil
39
+ end
40
+
41
+ it "should convert string '123' to integer 123" do
42
+ @os.digit.should eql(123)
43
+ end
44
+ end
45
+
46
+ describe "#[]" do
47
+ it "should returns value for given key if key exist" do
48
+ @os[:home].should eql("/Users/botanicus")
49
+ end
50
+
51
+ it "should returns nil if given key doesn't exist" do
52
+ @os[:xpath].should be_nil
53
+ end
54
+ end
55
+
56
+ describe "#==" do
57
+ it "should be equal if keys and values are equal" do
58
+ one = OS.new(home: "/Users/botanicus", path: "/bin:/sbin")
59
+ two = OS.new(home: "/Users/botanicus", path: "/bin:/sbin")
60
+ one.should == two
61
+ end
62
+
63
+ it "should be equal if keys and sorted values are equal" do
64
+ one = OS.new(home: "/Users/botanicus", path: "/sbin:/bin")
65
+ two = OS.new(home: "/Users/botanicus", path: "/bin:/sbin")
66
+ one.should == two
67
+ end
68
+
69
+ it "should not be equal if keys are equal but values not" do
70
+ one = OS.new(home: "/Users/one", path: "/bin:/sbin")
71
+ two = OS.new(home: "/Users/two", path: "/bin:/sbin")
72
+ one.should_not == two
73
+ end
74
+
75
+ it "should not be equal if keys are different" do
76
+ one = OS.new(home: "/Users/one")
77
+ two = OS.new(home: "/Users/two", path: "/bin:/sbin")
78
+ one.should_not == two
79
+ end
80
+ end
81
+
82
+ describe "#keys" do
83
+ it "should returns array of ENV keys which are upcased strings" do
84
+ @os.keys.should be_kind_of(Array)
85
+ @os.keys.all? { |key| key.should be_kind_of(Symbol) }
86
+ @os.keys.all? { |key| key.should match(/^[a-z_]+$/) }
87
+ end
88
+ end
89
+
90
+ describe "#inspect" do
91
+ it "should show all the keys" do
92
+ [:home, :path].each do |key|
93
+ @os.inspect.should match(%r[#{key}])
94
+ end
95
+ end
96
+ end
97
+
98
+ describe "#root?" do
99
+ it "should returns false if user isn't root" do
100
+ @os.should_not be_root
101
+ end
102
+
103
+ it "should returns true if user is root" do
104
+ pending do
105
+ @os.should be_root
106
+ end
107
+ end
108
+ end
109
+
110
+ describe "#method_missing" do
111
+ it "should returns nil if method doesn't exist and no arguments or block given" do
112
+ @os.i_do_not_exist.should be_nil
113
+ end
114
+
115
+ it "should raise NoMethodError nil if method doesn't exist and no arguments or block given" do
116
+ -> { @os.i_do_not_exist {} }.should raise_error(NoMethodError)
117
+ -> { @os.i_do_not_exist(1) }.should raise_error(NoMethodError)
118
+ -> { @os.i_do_not_exist(1) {} }.should raise_error(NoMethodError)
119
+ end
120
+ end
121
+ end
File without changes
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative "../../lib/rubyexts/random"
4
+
5
+ describe "Array#random" do
6
+ before(:each) do
7
+ @array = (1..1000).to_a
8
+ end
9
+
10
+ it "should returns random item *from array*" do
11
+ @array.should include(@array.random)
12
+ end
13
+
14
+ it "should returns *random* item from array" do
15
+ @array.random.should_not eql(@array.random)
16
+ end
17
+ end
18
+
19
+ describe "Range#random" do
20
+ before(:each) do
21
+ @range = (1..1000)
22
+ end
23
+
24
+ it "should returns random item *from range*" do
25
+ @range.should include(@range.random)
26
+ end
27
+
28
+ it "should returns *random* item from range" do
29
+ @range.random.should_not eql(@range.random)
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative "../../lib/rubyexts/string"
4
+
5
+ describe String do
6
+ describe "#titlecase" do
7
+ it "should returns titlecased string" do
8
+ "hello world!".titlecase.should eql("Hello World!")
9
+ end
10
+
11
+ it "should play well with unicode" do
12
+ pending do
13
+ "čus borče!".titlecase.should eql("Čus Borče!")
14
+ end
15
+ end
16
+ end
17
+
18
+ describe "#colorize" do
19
+ it "should returns colorized string" do
20
+ "whatever".colorize.should be_kind_of(ColoredString)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,88 @@
1
+ # encoding: utf-8
2
+
3
+ # Author: Martin Hrdlička
4
+
5
+ require_relative "../../lib/rubyexts/time_dsl"
6
+
7
+ SECOND = 1
8
+ MINUTE = 60 * SECOND
9
+ HOUR = 60 * MINUTE
10
+ DAY = 24 * HOUR
11
+ WEEK = 7 * DAY
12
+ MONTH = 30 * DAY
13
+ YEAR = 365 * DAY + 6 * HOUR
14
+
15
+ describe TimeDSL do
16
+ it "should return correct count of seconds for 1.second" do
17
+ 1.second.should eql SECOND
18
+ end
19
+
20
+ it "should return correct count of seconds for 2.seconds" do
21
+ 2.seconds.should eql 2 * SECOND
22
+ end
23
+
24
+ it "should return correct count of seconds for 1.minute" do
25
+ 1.minute.should eql MINUTE
26
+ end
27
+
28
+ it "should return correct count of seconds for 2.minutes" do
29
+ 2.minutes.should eql 2 * MINUTE
30
+ end
31
+
32
+ it "should return correct count of seconds for 1.hour" do
33
+ 1.hour.should eql HOUR
34
+ end
35
+
36
+ it "should return correct count of seconds for 2.hours" do
37
+ 2.hours.should eql 2 * HOUR
38
+ end
39
+
40
+ it "should return correct count of seconds for 1.day" do
41
+ 1.day.should eql DAY
42
+ end
43
+
44
+ it "should return correct count of seconds for 2.day" do
45
+ 2.day.should eql 2 * DAY
46
+ end
47
+
48
+ it "should return correct count of seconds for 1.week" do
49
+ 1.week.should eql WEEK
50
+ end
51
+
52
+ it "should return correct count of seconds for 2.weeks" do
53
+ 2.weeks.should eql 2 * WEEK
54
+ end
55
+
56
+ it "should return correct count of seconds for 1.month" do
57
+ 1.month.should eql MONTH
58
+ end
59
+
60
+ it "should return correct count of seconds for 2.months" do
61
+ 2.months.should eql 2 * MONTH
62
+ end
63
+
64
+ it "should return correct count of seconds for 1.year" do
65
+ pending "year can have 365 or 366 days"
66
+ 1.year.should eql YEAR
67
+ end
68
+
69
+ it "should return correct count of seconds for 2.years" do
70
+ pending "year can have 365 or 366 days"
71
+ 2.years.should eql 2 * YEAR
72
+ end
73
+
74
+ it "should return correct since time for any time" do
75
+ actual_time = ::Time.now
76
+ 1.day.since(actual_time).should eql actual_time + DAY
77
+ end
78
+
79
+ it "should return correct time from now for any time" do
80
+ actual_time = ::Time.now
81
+ 1.day.from_now(actual_time).should eql actual_time + DAY
82
+ end
83
+
84
+ it "should return correct time ago time for any time" do
85
+ actual_time = ::Time.now
86
+ 1.day.ago(actual_time).should eql actual_time - DAY
87
+ end
88
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative "../../lib/rubyexts/time"
4
+
5
+ describe Time do
6
+ describe ".timer" do
7
+ it "should returns how long it takes to run the block" do
8
+ Time.timer { sleep 0.001 }.round(3).should eql(0.001)
9
+ end
10
+ end
11
+ end
File without changes
@@ -0,0 +1,182 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative "../lib/rubyexts"
4
+
5
+ describe Kernel do
6
+ before(:each) do
7
+ dirname = File.dirname(__FILE__)
8
+ @path = File.join(dirname, "..", "stubs")
9
+ @path = File.expand_path(@path)
10
+ $LOADED_FEATURES.clear
11
+ end
12
+
13
+ def loaded
14
+ $LOADED_FEATURES.map do |loaded|
15
+ loaded.sub("#{@path}/acqs/", "")
16
+ end
17
+ end
18
+
19
+ describe "#acquire" do
20
+ before(:each) do
21
+ $:.push(@path) unless $:.include?(@path)
22
+ end
23
+
24
+ it "should raise LoadError if given directory doesn't exist at all" do
25
+ -> { acquire "a/b/c/d/*" }.should raise_error(LoadError)
26
+ end
27
+
28
+ it "should raise ArgumentError if given argument isn't a glob" do
29
+ -> { acquire "a/b/c/d" }.should raise_error(ArgumentError)
30
+ end
31
+
32
+ it "should returns empty array if no file match the glob, but the base directory exist" do
33
+ acquire "acqs/*.i"
34
+ loaded.should be_kind_of(Array)
35
+ loaded.should be_empty
36
+ end
37
+
38
+ it "should returns nonempty array if some files matched the glob" do
39
+ acquire "acqs/**/*"
40
+ loaded.should be_kind_of(Array)
41
+ loaded.should_not be_empty
42
+ end
43
+
44
+ it "should returns array with full paths" do
45
+ acquire "acqs/**/*"
46
+ $LOADED_FEATURES.each do |file|
47
+ File.file?(file).should be_true
48
+ File.expand_path(file).should eql(file)
49
+ end
50
+ end
51
+
52
+ it "should use .rb as default extension if any extension specified" do
53
+ acquire "acqs/**/*"
54
+ loaded.should include("lib.rb")
55
+ loaded.should include("dir/lib.rb")
56
+ loaded.should_not include("tasks.thor")
57
+ end
58
+
59
+ it "should load even files with another extension than just .rb" do
60
+ acquire "acqs/**/*.thor"
61
+ loaded.should include("tasks.thor")
62
+ loaded.should_not include("lib.rb")
63
+ end
64
+
65
+ it "should ignore dotfiles" do
66
+ acquire "acqs/**/*"
67
+ loaded.should_not include(".hidden.rb")
68
+ end
69
+
70
+ it "should ignore nonruby files" do
71
+ acquire "acqs/**/*"
72
+ loaded.should_not include("tasks.thor")
73
+ end
74
+
75
+ it "should permit to exclude a file" do
76
+ acquire "acqs/**/*", exclude: "dir/lib.rb"
77
+ loaded.should include("lib.rb")
78
+ loaded.should_not include("dir/lib.rb")
79
+ end
80
+
81
+ it "should permit to exclude array of files" do
82
+ acquire "acqs/**/*", exclude: ["lib.rb", "dir/lib.rb"]
83
+ loaded.should_not include("lib.rb")
84
+ loaded.should_not include("dir/lib.rb")
85
+ end
86
+
87
+ it "should permit to exclude a glob" do
88
+ acquire "acqs/**/*", exclude: "**/*_spec.rb"
89
+ loaded.should include("dir/lib.rb")
90
+ loaded.should_not include("dir/lib_spec.rb")
91
+ end
92
+
93
+ it "should permit to exclude list of globs" do
94
+ acquire "acqs/**/*", exclude: ["**/*_spec.rb", "**/lib.rb"]
95
+ loaded.should_not include("lib.rb")
96
+ loaded.should_not include("dir/lib.rb")
97
+ loaded.should_not include("dir/lib_spec.rb")
98
+ end
99
+ end
100
+
101
+ describe "#acquire_relative" do
102
+ before(:each) do
103
+ $:.delete(@path)
104
+ end
105
+
106
+ it "should raise LoadError if given directory doesn't exist at all" do
107
+ -> { acquire_relative "a/b/c/d/*" }.should raise_error(LoadError)
108
+ end
109
+
110
+ it "should raise ArgumentError if given argument isn't a glob" do
111
+ -> { acquire_relative "a/b/c/d" }.should raise_error(ArgumentError)
112
+ end
113
+
114
+ it "should returns empty array if no file match the glob, but the base directory exist" do
115
+ acquire_relative "../stubs/acqs/*.i"
116
+ loaded.should be_kind_of(Array)
117
+ loaded.should be_empty
118
+ end
119
+
120
+ it "should returns nonempty array if some files matched the glob" do
121
+ acquire_relative "../stubs/acqs/**/*"
122
+ loaded.should be_kind_of(Array)
123
+ loaded.should_not be_empty
124
+ end
125
+
126
+ it "should returns array with full paths" do
127
+ acquire_relative "../stubs/acqs/**/*"
128
+ $LOADED_FEATURES.each do |file|
129
+ File.file?(file).should be_true
130
+ File.expand_path(file).should eql(file)
131
+ end
132
+ end
133
+
134
+ it "should use .rb as default extension if any extension specified" do
135
+ acquire_relative "../stubs/acqs/**/*"
136
+ loaded.should include("lib.rb")
137
+ loaded.should include("dir/lib.rb")
138
+ loaded.should_not include("tasks.thor")
139
+ end
140
+
141
+ it "should load even files with another extension than just .rb" do
142
+ acquire_relative "../stubs/acqs/**/*.thor"
143
+ loaded.should include("tasks.thor")
144
+ loaded.should_not include("lib.rb")
145
+ end
146
+
147
+ it "should ignore dotfiles" do
148
+ acquire_relative "../stubs/acqs/**/*"
149
+ loaded.should_not include(".hidden.rb")
150
+ end
151
+
152
+ it "should ignore nonruby files" do
153
+ acquire_relative "../stubs/acqs/**/*"
154
+ loaded.should_not include("tasks.thor")
155
+ end
156
+
157
+ it "should permit to exclude a file" do
158
+ acquire_relative "../stubs/acqs/**/*", exclude: "dir/lib.rb"
159
+ loaded.should include("lib.rb")
160
+ loaded.should_not include("dir/lib.rb")
161
+ end
162
+
163
+ it "should permit to exclude array of files" do
164
+ acquire_relative "../stubs/acqs/**/*", exclude: ["lib.rb", "dir/lib.rb"]
165
+ loaded.should_not include("lib.rb")
166
+ loaded.should_not include("dir/lib.rb")
167
+ end
168
+
169
+ it "should permit to exclude a glob" do
170
+ acquire_relative "../stubs/acqs/**/*", exclude: "**/*_spec.rb"
171
+ loaded.should include("dir/lib.rb")
172
+ loaded.should_not include("dir/lib_spec.rb")
173
+ end
174
+
175
+ it "should permit to exclude list of globs" do
176
+ acquire_relative "../stubs/acqs/**/*", exclude: ["**/*_spec.rb", "**/lib.rb"]
177
+ loaded.should_not include("lib.rb")
178
+ loaded.should_not include("dir/lib.rb")
179
+ loaded.should_not include("dir/lib_spec.rb")
180
+ end
181
+ end
182
+ end