JackDanger / object_proxy
- Source
- Commits
- Network (0)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
cad9db5
object_proxy / test / test_object_proxy.rb
| fec8667a » | JackDanger | 2008-03-05 | 1 | require 'test/unit' | |
| 2 | require File.dirname(__FILE__) + '/../lib/object_proxy' | ||||
| 3 | |||||
| 4 | class OneHundredProxy < ObjectProxy | ||||
| 5 | def initialize | ||||
| 6 | @target = 100 | ||||
| 7 | end | ||||
| 8 | end | ||||
| 9 | |||||
| 10 | class ObjectProxyTest < Test::Unit::TestCase | ||||
| 11 | def test_does_not_respond_to_target | ||||
| 12 | assert_raises(NoMethodError) { ObjectProxy.new('some_value').responds_to?(:target) } | ||||
| 13 | end | ||||
| 14 | |||||
| 15 | def test_has_target_method_that_returns_the_provided_value | ||||
| 16 | assert_equal 'target', ObjectProxy.new('target') | ||||
| 17 | end | ||||
| 18 | |||||
| 19 | def test_target_object_does_not_have_identity_with_initialized_object | ||||
| 20 | object = Object.new | ||||
| 21 | assert !object.equal?(ObjectProxy.new(object)) | ||||
| 22 | end | ||||
| 23 | |||||
| 24 | def test_class_method_is_passed_to_target | ||||
| 25 | assert_equal String, ObjectProxy.new('some value').class | ||||
| 26 | end | ||||
| 27 | |||||
| 28 | def test_send_is_passed_to_target | ||||
| 29 | object = 'ABCDEF' | ||||
| 30 | assert_equal object.send(:length), ObjectProxy.new(object).send(:length) | ||||
| 31 | end | ||||
| 32 | |||||
| 33 | def test_proxy_class_accesses_the_class_of_the_object_proxy | ||||
| 34 | assert_equal ObjectProxy, ObjectProxy.new('value').proxy_class | ||||
| 35 | end | ||||
| 36 | |||||
| 37 | def test_proxy_class_allows_methods_added_to_the_proxy_class | ||||
| 38 | object = 'ABCDEF' | ||||
| 39 | proxy = ObjectProxy.new(object) | ||||
| 40 | proxy.proxy_class.class_eval do | ||||
| 41 | define_method :return_target do | ||||
| 42 | @target | ||||
| 43 | end | ||||
| 44 | end | ||||
| 45 | assert_equal object, proxy.return_target | ||||
| 46 | end | ||||
| 47 | |||||
| 48 | def test_method_missing_errors_are_raised_from_the_target_object | ||||
| 49 | begin | ||||
| 50 | ObjectProxy.new(12345).howzitgoin | ||||
| 51 | rescue => e | ||||
| 52 | assert_equal %Q(undefined method `howzitgoin' for 12345:Fixnum), e.message | ||||
| 53 | end | ||||
| 54 | end | ||||
| 55 | |||||
| 56 | def test_subclass_of_object_proxy_can_set_target_however_it_wants | ||||
| 57 | assert_equal 100, OneHundredProxy.new.target | ||||
| 58 | end | ||||
| 59 | end | ||||
