Skip to content

Commit

Permalink
Reorganized unit tests to match the file structure of the source.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobie committed Dec 11, 2008
1 parent 54bf343 commit 2525b21
Show file tree
Hide file tree
Showing 13 changed files with 656 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
@@ -1 +1,3 @@
.DS_Store
.DS_Store
pkg
test/unit/tmp/*
3 changes: 2 additions & 1 deletion dist/.gitignore
@@ -1 +1,2 @@
prototype.js
prototype.js
prototype_update_helper.js
130 changes: 130 additions & 0 deletions test/unit/class_test.js
@@ -0,0 +1,130 @@
new Test.Unit.Runner({
testClassCreate: function() {
this.assert(Object.isFunction(Animal), 'Animal is not a constructor');
this.assertEnumEqual([Cat, Mouse, Dog, Ox], Animal.subclasses);
Animal.subclasses.each(function(subclass) {
this.assertEqual(Animal, subclass.superclass);
}, this);

var Bird = Class.create(Animal);
this.assertEqual(Bird, Animal.subclasses.last());
// for..in loop (for some reason) doesn't iterate over the constructor property in top-level classes
this.assertEnumEqual(Object.keys(new Animal).sort(), Object.keys(new Bird).without('constructor').sort());
},

testClassInstantiation: function() {
var pet = new Animal("Nibbles");
this.assertEqual("Nibbles", pet.name, "property not initialized");
this.assertEqual('Nibbles: Hi!', pet.say('Hi!'));
this.assertEqual(Animal, pet.constructor, "bad constructor reference");
this.assertUndefined(pet.superclass);

var Empty = Class.create();
this.assert('object', typeof new Empty);
},

testInheritance: function() {
var tom = new Cat('Tom');
this.assertEqual(Cat, tom.constructor, "bad constructor reference");
this.assertEqual(Animal, tom.constructor.superclass, 'bad superclass reference');
this.assertEqual('Tom', tom.name);
this.assertEqual('Tom: meow', tom.say('meow'));
this.assertEqual('Tom: Yuk! I only eat mice.', tom.eat(new Animal));
},

testSuperclassMethodCall: function() {
var tom = new Cat('Tom');
this.assertEqual('Tom: Yum!', tom.eat(new Mouse));

// augment the constructor and test
var Dodo = Class.create(Animal, {
initialize: function($super, name) {
$super(name);
this.extinct = true;
},

say: function($super, message) {
return $super(message) + " honk honk";
}
});

var gonzo = new Dodo('Gonzo');
this.assertEqual('Gonzo', gonzo.name);
this.assert(gonzo.extinct, 'Dodo birds should be extinct');
this.assertEqual("Gonzo: hello honk honk", gonzo.say("hello"));
},

testClassAddMethods: function() {
var tom = new Cat('Tom');
var jerry = new Mouse('Jerry');

Animal.addMethods({
sleep: function() {
return this.say('ZZZ');
}
});

Mouse.addMethods({
sleep: function($super) {
return $super() + " ... no, can't sleep! Gotta steal cheese!";
},
escape: function(cat) {
return this.say('(from a mousehole) Take that, ' + cat.name + '!');
}
});

this.assertEqual('Tom: ZZZ', tom.sleep(), "added instance method not available to subclass");
this.assertEqual("Jerry: ZZZ ... no, can't sleep! Gotta steal cheese!", jerry.sleep());
this.assertEqual("Jerry: (from a mousehole) Take that, Tom!", jerry.escape(tom));
// insure that a method has not propagated *up* the prototype chain:
this.assertUndefined(tom.escape);
this.assertUndefined(new Animal().escape);

Animal.addMethods({
sleep: function() {
return this.say('zZzZ');
}
});

this.assertEqual("Jerry: zZzZ ... no, can't sleep! Gotta steal cheese!", jerry.sleep());
},

testBaseClassWithMixin: function() {
var grass = new Plant('grass', 3);
this.assertRespondsTo('getValue', grass);
this.assertEqual('#<Plant: grass>', grass.inspect());
},

testSubclassWithMixin: function() {
var snoopy = new Dog('Snoopy', 12, 'male');
this.assertRespondsTo('reproduce', snoopy);
},

testSubclassWithMixins: function() {
var cow = new Ox('cow', 400, 'female');
this.assertEqual('#<Ox: cow>', cow.inspect());
this.assertRespondsTo('reproduce', cow);
this.assertRespondsTo('getValue', cow);
},

testClassWithToStringAndValueOfMethods: function() {
var Foo = Class.create({
toString: function() { return "toString" },
valueOf: function() { return "valueOf" }
});

var Parent = Class.create({
m1: function(){ return 'm1' },
m2: function(){ return 'm2' }
});
var Child = Class.create(Parent, {
m1: function($super) { return 'm1 child' },
m2: function($super) { return 'm2 child' }
});

this.assert(new Child().m1.toString().indexOf('m1 child') > -1);

this.assertEqual("toString", new Foo().toString());
this.assertEqual("valueOf", new Foo().valueOf());
}
});
5 changes: 5 additions & 0 deletions test/unit/date_test.js
@@ -0,0 +1,5 @@
new Test.Unit.Runner({
testDateToJSON: function() {
this.assertEqual('\"1970-01-01T00:00:00Z\"', new Date(Date.UTC(1970, 0, 1)).toJSON());
}
});
83 changes: 83 additions & 0 deletions test/unit/fixtures/class.js
@@ -0,0 +1,83 @@
// base class
var Animal = Class.create({
initialize: function(name) {
this.name = name;
},
name: "",
eat: function() {
return this.say("Yum!");
},
say: function(message) {
return this.name + ": " + message;
}
});

// subclass that augments a method
var Cat = Class.create(Animal, {
eat: function($super, food) {
if (food instanceof Mouse) return $super();
else return this.say("Yuk! I only eat mice.");
}
});

// empty subclass
var Mouse = Class.create(Animal, {});

//mixins
var Sellable = {
getValue: function(pricePerKilo) {
return this.weight * pricePerKilo;
},

inspect: function() {
return '#<Sellable: #{weight}kg>'.interpolate(this);
}
};

var Reproduceable = {
reproduce: function(partner) {
if (partner.constructor != this.constructor || partner.sex == this.sex)
return null;
var weight = this.weight / 10, sex = Math.random(1).round() ? 'male' : 'female';
return new this.constructor('baby', weight, sex);
}
};

// base class with mixin
var Plant = Class.create(Sellable, {
initialize: function(name, weight) {
this.name = name;
this.weight = weight;
},

inspect: function() {
return '#<Plant: #{name}>'.interpolate(this);
}
});

// subclass with mixin
var Dog = Class.create(Animal, Reproduceable, {
initialize: function($super, name, weight, sex) {
this.weight = weight;
this.sex = sex;
$super(name);
}
});

// subclass with mixins
var Ox = Class.create(Animal, Sellable, Reproduceable, {
initialize: function($super, name, weight, sex) {
this.weight = weight;
this.sex = sex;
$super(name);
},

eat: function(food) {
if (food instanceof Plant)
this.weight += food.weight;
},

inspect: function() {
return '#<Ox: #{name}>'.interpolate(this);
}
});
13 changes: 13 additions & 0 deletions test/unit/fixtures/function.js
@@ -0,0 +1,13 @@
var arg1 = 1;
var arg2 = 2;
var arg3 = 3;
function TestObj() { };
TestObj.prototype.assertingEventHandler =
function(event, assertEvent, assert1, assert2, assert3, a1, a2, a3) {
assertEvent(event);
assert1(a1);
assert2(a2);
assert3(a3);
};

var globalBindTest = null;
6 changes: 6 additions & 0 deletions test/unit/fixtures/object.html
@@ -0,0 +1,6 @@
<div id="test"></div>
<ul id="list">
<li></li>
<li></li>
<li></li>
</ul>
7 changes: 7 additions & 0 deletions test/unit/fixtures/object.js
@@ -0,0 +1,7 @@
var Person = function(name){
this.name = name;
};

Person.prototype.toJSON = function() {
return '-' + this.name;
};

1 comment on commit 2525b21

@Mehreenkiran
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please tell me how to add text file in GITHUB account

Please sign in to comment.