[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

test-[jscocoa] 003 derivation

Code

"actually excluded from tests" self cancelFileIn!

(NSObject subclass: #NSDerivedObjectTest) install.!
| o h |
o := NSDerivedObjectTest instance.
h := o handle.
self print: 'native instance hash=',h#hash.

"self print: 'About to release native instance'.
   h basicAt: #release. - will crash app if we force release the handle
"
o := h := nil.
Smalltalk gc.
self print: 'done'.!

self cancelFileIn! "actually excluded from tests"

self nativeCodeFollows!

	var o = NSDerivedObjectTest.alloc.init ;

	// Derived a new class and add an overloaded method
	// Then derived from that new class

	// Record original hash
	var originalHash = o.hash;
	
	// Overload hash method
	var wentThrough1 = false;
	function myHash() {
		var hash = this.Super(arguments);
		wentThrough1 = true;
		return	hash;
	}

	var added = JSCocoaController.overloadInstanceMethod_class_jsFunction('hash', NSDerivedObjectTest, myHash);
	if (!added)	throw "Couldn't overload method 1";

	// Check
	wentThrough1 = false;
	var hash = o.hash;
	if (hash != originalHash) throw 'invalided hash in overloaded method';
	if (!wentThrough1) throw 'invalided hash in overloaded method - did not go through';

	// Derivation of derivation
	var newClass = JSCocoaController.createClass_parentClass("NSDerivedObjectTest2", "NSDerivedObjectTest");
	
	// Allocate instance
	var o2 = NSDerivedObjectTest2.alloc.init;

	// Record original hash
	var originalHash2 = o2.hash;
	
	// Overload the same method
	var wentThrough2 = false;
	function myHash2() {
		var hash = this.Super(arguments);
		wentThrough2 = true;
		return	hash;
	}

	var added = JSCocoaController.overloadInstanceMethod_class_jsFunction('hash', NSDerivedObjectTest2, myHash2);
	if (!added)	throw "Couldn't overload method 2";

	// Check
	wentThrough1 = false;
	wentThrough2 = false;
	var hash2 = o2.hash;
	if (hash2 != originalHash2) throw 'invalided hash in overloaded method (2)';
	if (!wentThrough1) throw 'invalided hash in overloaded method - did not go through 1st derivation';
	if (!wentThrough2) throw 'invalided hash in overloaded method - did not go through 2nd derivation';

	o = null;
	o2 = null;
	newClass = null;

References