dolzenko 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,189 @@
1
+ module TryBlock
2
+ def self.install!
3
+ unless Object.include?(self)
4
+ Object.send(:include, self)
5
+ Object.send(:public, :try_block)
6
+ end
7
+ end
8
+
9
+ def try_block(&block)
10
+ raise ArgumentError, "should be given block" unless block
11
+
12
+ return if self.nil?
13
+
14
+ caller_size = caller.size
15
+
16
+ # JRuby reports this weird ":1" line in caller
17
+ if RUBY_PLATFORM =~ /\bjava\b/ && caller[-1] == ":1"
18
+ caller_size -= 1
19
+ end
20
+
21
+ begin
22
+ self.instance_eval(&block)
23
+ rescue NoMethodError => e
24
+ if e.backtrace.size - caller_size == 3 &&
25
+ e.message =~ /^undefined method.+for nil:NilClass$/
26
+
27
+ return nil
28
+ end
29
+ raise
30
+ end
31
+ end
32
+ end
33
+
34
+ def TryBlock(&block)
35
+ raise ArgumentError, "should be given block" unless block
36
+
37
+ return if self.nil?
38
+
39
+ caller_size = caller.size
40
+
41
+ # JRuby reports this weird ":1" line in caller
42
+ if RUBY_PLATFORM =~ /\bjava\b/ && caller[-1] == ":1"
43
+ caller_size -= 1
44
+ end
45
+
46
+ begin
47
+ yield
48
+ rescue NoMethodError => e
49
+ if e.backtrace.size - caller_size == 2 &&
50
+ e.message =~ /^undefined method.+for nil:NilClass$/
51
+ return nil
52
+ end
53
+ raise
54
+ end
55
+ end
56
+
57
+ if $PROGRAM_NAME == __FILE__
58
+ require "test/unit"
59
+
60
+ class TestBlockTry < Test::Unit::TestCase
61
+ def setup
62
+ TryBlock.install!
63
+ end
64
+
65
+ def test_properly_eats_exception_on_nil_object_from_call_site
66
+ some_nil = nil
67
+
68
+ assert_nil TryBlock { some_nil.something_else }
69
+
70
+ assert_nil "qwerty".try_block { some_nil.something_else }
71
+
72
+ assert_nil some_nil.try_block { something_else }
73
+
74
+ assert_nil [false].try_block { detect { |e| e }.something }
75
+ end
76
+
77
+ def test_name_error_in_block
78
+ assert_raises NameError do
79
+ TryBlock { no_such_name }
80
+ end
81
+
82
+ assert_raises NameError do
83
+ "qwerty".try_block { no_such_name }
84
+ end
85
+ end
86
+
87
+ def test_doesnt_eat_general_exception
88
+ assert_raises RuntimeError do
89
+ TryBlock { raise "FAIL" }
90
+ end
91
+
92
+ assert_raises RuntimeError do
93
+ "qwerty".try_block { raise "FAIL" }
94
+ end
95
+ end
96
+
97
+ def test_doesnt_eat_general_exception_originated_from_somewhere_else
98
+ proc_raiser = proc { raise "FAIL" }
99
+ obj_raiser = "qwerty".tap { |obj| def obj.raiser; raise "FAIL" end }
100
+
101
+ assert_raises RuntimeError do
102
+ TryBlock { proc_raiser[] }
103
+ end
104
+
105
+ assert_raises RuntimeError do
106
+ obj_raiser.try_block { raiser }
107
+ end
108
+
109
+ assert_raises RuntimeError do
110
+ [1].try_block { detect { |e| raise "FAIL" } }
111
+ end
112
+ end
113
+
114
+ def test_doesnt_eat_nil_object_exception_originated_from_somewhere_else
115
+ some_nil = nil
116
+ no_method_raiser = proc { some_nil.doesnt_exist_on_nil }
117
+ obj_no_method_raiser = "qwerty".tap { |obj| def obj.raiser; doesnt_exist_on_string() end }
118
+
119
+ if RUBY_VERSION < '1.9'
120
+ # ["try_block.rb:97",
121
+ # "try_block.rb:118:in `[]'",
122
+ # "try_block.rb:118:in `test_doesnt_eat_nil_object_exception_originated_from_somewhere_else'",
123
+ # "try_block.rb:37:in `BlockTry'",
124
+ # "try_block.rb:118:in `test_doesnt_eat_nil_object_exception_originated_from_somewhere_else'", <= call site
125
+ assert_raises(NoMethodError) do
126
+ TryBlock do
127
+ no_method_raiser[]
128
+ end
129
+ end
130
+ else
131
+ # ["try_block.rb:97:in `block in test_doesnt_eat_nil_object_exception_originated_from_somewhere_else'",
132
+ # "try_block.rb:112:in `[]'",
133
+ # "try_block.rb:112:in `block (2 levels) in test_doesnt_eat_nil_object_exception_originated_from_somewhere_else'",
134
+ # "try_block.rb:37:in `BlockTry'",
135
+ # "try_block.rb:112:in `block in test_doesnt_eat_nil_object_exception_originated_from_somewhere_else'", <= call site
136
+ # ...
137
+ assert_raises(NoMethodError) { TryBlock { no_method_raiser[] } } # this will only work in 1.9
138
+ end
139
+
140
+
141
+ assert_raises NoMethodError do
142
+ obj_no_method_raiser.try_block { raiser }
143
+ end
144
+ end
145
+
146
+ def test_hash_digging
147
+ h = { :asd => 123, :rty => 456}
148
+ assert_nil TryBlock { h[:qwe][:asd][:zxc] }
149
+ assert_equal 123, TryBlock { h[:asd] }
150
+
151
+ assert_nil h.try_block { self[:qwe][:asd][:zxc] }
152
+ assert_nil h.try_block { |ha| ha[:qwe][:asd][:zxc] }
153
+ assert_equal 456, h.try_block { |ha| ha[:rty] }
154
+ end
155
+
156
+ def test_asserts_passed_block
157
+ assert_raises ArgumentError do
158
+ TryBlock.try_block
159
+ end
160
+
161
+ assert_raises ArgumentError do
162
+ "qwerty".try_block
163
+ end
164
+ end
165
+
166
+ def test_core_ext
167
+ assert_nil nil.try_block { 42 }
168
+ assert_equal 6, "qwerty".try_block { length }
169
+ end
170
+
171
+ def test_meta_method
172
+ obj_raiser = Class.new.tap { |c| c.module_eval("def raiser; raise 'FAIL' end ") }
173
+
174
+ assert_raises(RuntimeError) { TryBlock { obj_raiser.new.raiser } }
175
+ assert_raises(RuntimeError) { obj_raiser.new.try_block { raiser } }
176
+ end
177
+
178
+ def test_eval
179
+ assert_raises(NoMethodError) { eval("TryBlock { 'qwerty'.doesnt_exist_on_string }") }
180
+ assert_nil(eval("TryBlock { nil.doesnt_exist_on_nil }")) unless RUBY_PLATFORM =~ /\bjava\b/
181
+
182
+ assert_raises(NoMethodError) { eval("'qwerty'.try_block { doesnt_exist_on_string() }") }
183
+ assert_nil(eval("'qwerty'.try_block { nil.doesnt_exist_on_nil }")) unless RUBY_PLATFORM =~ /\bjava\b/
184
+
185
+ assert_equal 6, eval("'qwerty'.try_block { length }")
186
+ assert_nil(eval("[false].try_block { detect { |e| e }.doesnt_exist_on_nil.also_doesnt_exist_on_nil }")) unless RUBY_PLATFORM =~ /\bjava\b/
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,11 @@
1
+ require "English"
2
+ require "yaml"
3
+ require "open-uri"
4
+
5
+ # These can be autoloaded consistently (i.e. define single constant)
6
+ autoload :FileUtils, "fileutils"
7
+ autoload :OptionParser, "optparse"
8
+ autoload :OpenStruct, "ostruct"
9
+ module Net
10
+ autoload :HTTP, "net/http"
11
+ end
data/lib/dolzenko.rb CHANGED
@@ -1,14 +1,4 @@
1
- require "English"
2
- require "yaml"
3
- require "open-uri"
4
-
5
- # These can be autoloaded consistently (i.e. define single constant)
6
- autoload :FileUtils, "fileutils"
7
- autoload :OptionParser, "optparse"
8
- autoload :OpenStruct, "ostruct"
9
- module Net
10
- autoload :HTTP, "net/http"
11
- end
1
+ require "dolzenko-light"
12
2
 
13
3
  require "active_support/all"
14
4
  require "facets"
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 10
9
- version: 0.0.10
8
+ - 11
9
+ version: 0.0.11
10
10
  platform: ruby
11
11
  authors:
12
12
  - Evgeniy Dolzhenko
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-29 00:00:00 +04:00
17
+ date: 2010-05-28 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -56,6 +56,16 @@ extensions: []
56
56
  extra_rdoc_files: []
57
57
 
58
58
  files:
59
+ - lib/dolzenko/django_f_object.rb
60
+ - lib/dolzenko/django_q_object.rb
61
+ - lib/dolzenko/error_print.rb
62
+ - lib/dolzenko/gist_readme.rb
63
+ - lib/dolzenko/includable_with_options.rb
64
+ - lib/dolzenko/io_interceptor.rb
65
+ - lib/dolzenko/remote_download.rb
66
+ - lib/dolzenko/shell_out.rb
67
+ - lib/dolzenko/try_block.rb
68
+ - lib/dolzenko-light.rb
59
69
  - lib/dolzenko.rb
60
70
  - dolzenko.gemspec
61
71
  has_rdoc: true