From: Eric Mahurin Date: 2005-09-28T00:58:57+09:00 Subject: Re: random testing with Test::Unit --0-930656867-1127836718=:60009 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Content-Id: Content-Disposition: inline --- Kev Jackson wrote: > >- AOP. Make a wrapper class or module that does pre and > post > >checks of the methods. Use that wrapper class/module when > >testing. When the pre-check fails, the current test would > be > >aborted/skipped (random generation of inputs yielded > something > >invalid). When the post-check fails, the test would fail. > > > > > Hi I'd love to see how you implemented this as I wanted to > write my own > AOP-like code for ruby but I got stuck with my lack of ruby > knowledge! I've attached an example (executable) of one way to do AOP and random testing for testing Array#push and Array#pop. I'm no expert in AOP and exactly what its definition is (did it before knowing what it was), but here are the ways I can think of for doing AOP: - make a derived class for the class under test (what this example does). Put the pre/post tests around a call to "super" for each method. If you need to use other orginal (super) methods in your pre/post tests (that you've also put pre/post tests in), you have to jump through hoops to do it. - make a wrapper class for testing the methods of an object. new/initialize should take this object and store it in an instance variable. For each method, put pre/post tests around a call to the object's method. - directly modify the class under test. Alias each original method and redefine each method to do pre/post test around calls to the original (aliased) method. I was also thinking you might be able to do this stuff in a module and include it, but I couldn't figure it out. Again, AOP is only one way of getting expected results for random testing. I mentioned 2 others. You can see from this example how several features (mentioned in my previous message) would be useful for random testing. __________________________________ Yahoo! Mail - PC Magazine Editors' Choice 2005 http://mail.yahoo.com --0-930656867-1127836718=:60009 Content-Type: application/x-ruby; name="test_array.rb" Content-Transfer-Encoding: base64 Content-Description: 970930991-test_array.rb Content-Disposition: attachment; filename="test_array.rb" cmVxdWlyZSAndGVzdC91bml0JwoKY2xhc3MgQXJyYXlBT1AgPCBBcnJheQog ICAgaW5jbHVkZSBUZXN0OjpVbml0OjpBc3NlcnRpb25zCiAgICBkZWYgcHVz aCgqYXJncykKICAgICAgICBwcmludCgiI3tzZWxmLmluc3BlY3R9LnB1c2go KiN7YXJncy5pbnNwZWN0fSkgLT4gIikKICAgICAgICBuID0gc2l6ZQogICAg ICAgIG5hID0gYXJncy5zaXplCiAgICAgICAgcmV0ID0gc3VwZXIKICAgICAg ICBhc3NlcnRfZXF1YWwobituYSxzaXplKQogICAgICAgIGFzc2VydF9lcXVh bChzbGljZSgtbmEsbmEpLGFyZ3MpCiAgICAgICAgYXNzZXJ0X3NhbWUoc2Vs ZixyZXQpCiAgICAgICAgcChyZXQpCiAgICAgICAgcmV0CiAgICBlbmQKICAg IGRlZiBwb3AKICAgICAgICBwcmludCgiI3tzZWxmLmluc3BlY3R9LnBvcCAt PiAiKQogICAgICAgIG4gPSBzaXplCiAgICAgICAgcmV0MCA9IG4ubm9uemVy bz8gPyBzbGljZSgtMSkgOiBuaWwKICAgICAgICByZXQgPSBzdXBlcgogICAg ICAgIGFzc2VydF9lcXVhbChyZXQwLHJldCkKICAgICAgICBhc3NlcnRfZXF1 YWwobi5ub256ZXJvPyA/IG4tMSA6IDAsc2l6ZSkKICAgICAgICBwKHJldCkK ICAgICAgICByZXQKICAgIGVuZAplbmQKCmNsYXNzIEFycmF5VGVzdCA8IFRl c3Q6OlVuaXQ6OlRlc3RDYXNlCiAgICBkZWYgc2VsZi5zdWl0ZQogICAgICAg IEBAb2JqZWN0ID0gQXJyYXlBT1BbXQogICAgICAgIHN1aXRlID0gc3VwZXIK ICAgICAgICB0ZXN0cyA9IHN1aXRlLnRlc3RzCiAgICAgICAgKGNsYXNzIDw8 IHRlc3RzO3NlbGY7ZW5kKS5jbGFzc19ldmFsIHsKICAgICAgICAgICAgZGVm IGVhY2gKICAgICAgICAgICAgICAgIChAaXRlcmF0aW9ucypzaXplKS50b19p LnRpbWVzIHsKICAgICAgICAgICAgICAgICAgICB5aWVsZChzbGljZShyYW5k KHNpemUpKSkKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgZW5kCiAg ICAgICAgfQogICAgICAgIHRlc3RzLmluc3RhbmNlX2V2YWwgeyBAaXRlcmF0 aW9ucyA9IDggfQogICAgICAgIHN1aXRlCiAgICBlbmQKICAgIGRlZiB0ZXN0 X3B1c2gKICAgICAgICBhcmdzID0gW10KICAgICAgICByYW5kKDQpLnRpbWVz IHsKICAgICAgICAgICAgYXJncyA8PCBbMCwiIixbXSxuaWwsZmFsc2VdW3Jh bmQoNSldCiAgICAgICAgfQogICAgICAgIEBAb2JqZWN0LnB1c2goKmFyZ3Mp CiAgICBlbmQKICAgIGRlZiB0ZXN0X3BvcAogICAgICAgIHJhbmQoNCkudGlt ZXMgewogICAgICAgICAgICBAQG9iamVjdC5wb3AKICAgICAgICB9CiAgICBl bmQKZW5kCgoK --0-930656867-1127836718=:60009--