Attributation works in literal class but not in anonymous class?




# Creates a method that necessitates overridding.
# If it not overridden and called upon a TypeError
# will be raised.
#
# class C
# abstract :a
# end
#
# c = C.new
# c.a #=> Error: undefined abstraction #a
#
class Module
def abstract( *sym )
sym.each { |s|
define_method( s ) { raise TypeError, "undefined abstraction
##{s}" }
}
end
end


require 'test/unit'
#require 'nano/module/abstract'

# fixture

class Aq
abstract :q
end

class TC_Module < Test::Unit::TestCase

# abstract literal

def test01
ac = Aq.new
assert_raises( TypeError ) { ac.q }
end

# abstract anonymous

def test02
ac = Class.new { abstract :q }
assert_raises( TypeError ) { ac.q }
end

end



Loaded suite tc_abstract
Started
..F
Finished in 0.065452 seconds.

1) Failure:
test02(TC_Module) [tc_abstract.rb:22]:
<TypeError> exception expected but was
Class: <NoMethodError>
Message: <"undefined method `q' for #<Class:0xb7cfedf0>">
---Backtrace---
tc_abstract.rb:22:in `test02'
tc_abstract.rb:22:in `assert_raise'
tc_abstract.rb:22:in `test02'
---------------

2 tests, 2 assertions, 1 failures, 0 errors



T.

.



Relevant Pages

  • Re: Checking function calls
    ... I'm trying to catch a TypeError when calling the function ... py> def func: ... somewhere in your client class ... raise ServerError, 'Too few args!' ...
    (comp.lang.python)
  • Re: Python 3.0 - is this true?
    ... How often do you care about equality ignoring order for lists containing ... except TypeError: ... def unordered_equals: ... solve the problem assuming only that the items support equality: ...
    (comp.lang.python)
  • Re: is_iterable function.
    ... The call to iter will fail for objects that don't support the ... def deeply_mapped: ... TypeError: ... yield func ...
    (comp.lang.python)
  • Re: invert dictionary with list &c
    ... > return reduce(process_element, iter, d) ... > def count: ... Traceback: ... TypeError: takes exactly 1 argument ...
    (comp.lang.python)