PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/test/extensions_test.rb

https://github.com/dudeman/aws-s3
Ruby | 340 lines | 279 code | 59 blank | 2 comment | 2 complexity | 6119e2d7504a786bd5c0c84bfc3680f2 MD5 | raw file
Possible License(s): JSON
  1. require File.dirname(__FILE__) + '/test_helper'
  2. class HashExtensionsTest < Test::Unit::TestCase
  3. def test_to_query_string
  4. # Because hashes aren't ordered, I'm mostly testing against hashes with just one key
  5. symbol_keys = {:one => 1}
  6. string_keys = {'one' => 1}
  7. expected = '?one=1'
  8. [symbol_keys, string_keys].each do |hash|
  9. assert_equal expected, hash.to_query_string
  10. end
  11. end
  12. def test_empty_hash_returns_no_query_string
  13. assert_equal '', {}.to_query_string
  14. end
  15. def test_include_question_mark
  16. hash = {:one => 1}
  17. assert_equal '?one=1', hash.to_query_string
  18. assert_equal 'one=1', hash.to_query_string(false)
  19. end
  20. def test_elements_joined_by_ampersand
  21. hash = {:one => 1, :two => 2}
  22. qs = hash.to_query_string
  23. assert qs['one=1&two=2'] || qs['two=2&one=1']
  24. end
  25. def test_normalized_options
  26. expectations = [
  27. [{:foo_bar => 1}, {'foo-bar' => '1'}],
  28. [{'foo_bar' => 1}, {'foo-bar' => '1'}],
  29. [{'foo-bar' => 1}, {'foo-bar' => '1'}],
  30. [{}, {}]
  31. ]
  32. expectations.each do |(before, after)|
  33. assert_equal after, before.to_normalized_options
  34. end
  35. end
  36. end
  37. class StringExtensionsTest < Test::Unit::TestCase
  38. def test_previous
  39. expectations = {'abc' => 'abb', '123' => '122', '1' => '0'}
  40. expectations.each do |before, after|
  41. assert_equal after, before.previous
  42. end
  43. end
  44. def test_to_header
  45. transformations = {
  46. 'foo' => 'foo',
  47. :foo => 'foo',
  48. 'foo-bar' => 'foo-bar',
  49. 'foo_bar' => 'foo-bar',
  50. :foo_bar => 'foo-bar',
  51. 'Foo-Bar' => 'foo-bar',
  52. 'Foo_Bar' => 'foo-bar'
  53. }
  54. transformations.each do |before, after|
  55. assert_equal after, before.to_header
  56. end
  57. end
  58. def test_valid_utf8?
  59. assert !"318597/620065/GTL_75\24300_A600_A610.zip".valid_utf8?
  60. assert "318597/620065/GTL_75£00_A600_A610.zip".valid_utf8?
  61. end
  62. def test_remove_extended
  63. assert "318597/620065/GTL_75\24300_A600_A610.zip".remove_extended.valid_utf8?
  64. assert "318597/620065/GTL_75£00_A600_A610.zip".remove_extended.valid_utf8?
  65. end
  66. end
  67. class CoercibleStringTest < Test::Unit::TestCase
  68. def test_coerce
  69. coercions = [
  70. ['1', 1],
  71. ['false', false],
  72. ['true', true],
  73. ['2006-10-29T23:14:47.000Z', Time.parse('2006-10-29T23:14:47.000Z')],
  74. ['Hello!', 'Hello!'],
  75. ['false23', 'false23'],
  76. ['03 1-2-3-Apple-Tree.mp3', '03 1-2-3-Apple-Tree.mp3'],
  77. ['0815', '0815'] # This number isn't coerced because the leading zero would be lost
  78. ]
  79. coercions.each do |before, after|
  80. assert_nothing_raised do
  81. assert_equal after, CoercibleString.coerce(before)
  82. end
  83. end
  84. end
  85. end
  86. class KerneltExtensionsTest < Test::Unit::TestCase
  87. class Foo
  88. def foo
  89. __method__
  90. end
  91. def bar
  92. foo
  93. end
  94. def baz
  95. bar
  96. end
  97. end
  98. class Bar
  99. def foo
  100. calling_method
  101. end
  102. def bar
  103. calling_method
  104. end
  105. def calling_method
  106. __method__(1)
  107. end
  108. end
  109. def test___method___works_regardless_of_nesting
  110. f = Foo.new
  111. [:foo, :bar, :baz].each do |method|
  112. assert_equal 'foo', f.send(method)
  113. end
  114. end
  115. def test___method___depth
  116. b = Bar.new
  117. assert_equal 'foo', b.foo
  118. assert_equal 'bar', b.bar
  119. end
  120. end if RUBY_VERSION <= '1.8.7'
  121. class ModuleExtensionsTest < Test::Unit::TestCase
  122. class Foo
  123. def foo(reload = false)
  124. expirable_memoize(reload) do
  125. Time.now
  126. end
  127. end
  128. def bar(reload = false)
  129. expirable_memoize(reload, :baz) do
  130. Time.now
  131. end
  132. end
  133. def quux
  134. Time.now
  135. end
  136. memoized :quux
  137. end
  138. def setup
  139. @instance = Foo.new
  140. end
  141. def test_memoize
  142. assert !instance_variables_of(@instance).include?('@foo')
  143. cached_result = @instance.foo
  144. assert_equal cached_result, @instance.foo
  145. assert instance_variables_of(@instance).include?('@foo')
  146. assert_equal cached_result, @instance.send(:instance_variable_get, :@foo)
  147. assert_not_equal cached_result, new_cache = @instance.foo(:reload)
  148. assert_equal new_cache, @instance.foo
  149. assert_equal new_cache, @instance.send(:instance_variable_get, :@foo)
  150. end
  151. def test_customizing_memoize_storage
  152. assert !instance_variables_of(@instance).include?('@bar')
  153. assert !instance_variables_of(@instance).include?('@baz')
  154. cached_result = @instance.bar
  155. assert !instance_variables_of(@instance).include?('@bar')
  156. assert instance_variables_of(@instance).include?('@baz')
  157. assert_equal cached_result, @instance.bar
  158. assert_equal cached_result, @instance.send(:instance_variable_get, :@baz)
  159. assert_nil @instance.send(:instance_variable_get, :@bar)
  160. end
  161. def test_memoized
  162. assert !instance_variables_of(@instance).include?('@quux')
  163. cached_result = @instance.quux
  164. assert_equal cached_result, @instance.quux
  165. assert instance_variables_of(@instance).include?('@quux')
  166. assert_equal cached_result, @instance.send(:instance_variable_get, :@quux)
  167. assert_not_equal cached_result, new_cache = @instance.quux(:reload)
  168. assert_equal new_cache, @instance.quux
  169. assert_equal new_cache, @instance.send(:instance_variable_get, :@quux)
  170. end
  171. def test_constant_setting
  172. some_module = Module.new
  173. assert !some_module.const_defined?(:FOO)
  174. assert_nothing_raised do
  175. some_module.constant :FOO, 'bar'
  176. end
  177. assert some_module.const_defined?(:FOO)
  178. assert_nothing_raised do
  179. some_module::FOO
  180. some_module.foo
  181. end
  182. assert_equal 'bar', some_module::FOO
  183. assert_equal 'bar', some_module.foo
  184. assert_nothing_raised do
  185. some_module.constant :FOO, 'baz'
  186. end
  187. assert_equal 'bar', some_module::FOO
  188. assert_equal 'bar', some_module.foo
  189. end
  190. private
  191. # For 1.9 compatibility
  192. def instance_variables_of(object)
  193. object.instance_variables.map do |instance_variable|
  194. instance_variable.to_s
  195. end
  196. end
  197. end
  198. class AttributeProxyTest < Test::Unit::TestCase
  199. class BlindProxyUsingDefaultAttributesHash
  200. include SelectiveAttributeProxy
  201. proxy_to :exlusively => false
  202. end
  203. class BlindProxyUsingCustomAttributeHash
  204. include SelectiveAttributeProxy
  205. proxy_to :settings
  206. end
  207. class ProxyUsingPassedInAttributeHash
  208. include SelectiveAttributeProxy
  209. def initialize(attributes = {})
  210. @attributes = attributes
  211. end
  212. end
  213. class RestrictedProxy
  214. include SelectiveAttributeProxy
  215. private
  216. def proxiable_attribute?(name)
  217. %w(foo bar baz).include?(name)
  218. end
  219. end
  220. class NonExclusiveProxy
  221. include SelectiveAttributeProxy
  222. proxy_to :settings, :exclusively => false
  223. end
  224. def test_using_all_defaults
  225. b = BlindProxyUsingDefaultAttributesHash.new
  226. assert_nothing_raised do
  227. b.foo = 'bar'
  228. end
  229. assert_nothing_raised do
  230. b.foo
  231. end
  232. assert_equal 'bar', b.foo
  233. end
  234. def test_storage_is_autovivified
  235. b = BlindProxyUsingDefaultAttributesHash.new
  236. assert_nothing_raised do
  237. b.send(:attributes)['foo'] = 'bar'
  238. end
  239. assert_nothing_raised do
  240. b.foo
  241. end
  242. assert_equal 'bar', b.foo
  243. end
  244. def test_limiting_which_attributes_are_proxiable
  245. r = RestrictedProxy.new
  246. assert_nothing_raised do
  247. r.foo = 'bar'
  248. end
  249. assert_nothing_raised do
  250. r.foo
  251. end
  252. assert_equal 'bar', r.foo
  253. assert_raises(NoMethodError) do
  254. r.quux = 'foo'
  255. end
  256. assert_raises(NoMethodError) do
  257. r.quux
  258. end
  259. end
  260. def test_proxying_is_exclusive_by_default
  261. p = ProxyUsingPassedInAttributeHash.new('foo' => 'bar')
  262. assert_nothing_raised do
  263. p.foo
  264. p.foo = 'baz'
  265. end
  266. assert_equal 'baz', p.foo
  267. assert_raises(NoMethodError) do
  268. p.quux
  269. end
  270. end
  271. def test_setting_the_proxy_as_non_exclusive
  272. n = NonExclusiveProxy.new
  273. assert_nothing_raised do
  274. n.foo = 'baz'
  275. end
  276. assert_nothing_raised do
  277. n.foo
  278. end
  279. assert_equal 'baz', n.foo
  280. end
  281. end