Skip to content

Commit

Permalink
Add QUnit test suite.
Browse files Browse the repository at this point in the history
  • Loading branch information
codysoyland authored and mitsuhiko committed Apr 27, 2010
1 parent feb35d1 commit ad9b9b4
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 0 deletions.
139 changes: 139 additions & 0 deletions tests/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
test('Classy exists', function() {
ok(typeof(Class) == 'function',
'Class is a function defined globally');
});

test('$noConflict unsets Class from window', function() {
var original = Class;

var Classy = Class.$noConflict();

equals(typeof(Class), 'undefined',
'$noConflict unsets Class from window');

same(Classy, original,
'the returned Class is the same as the original');

// cleanup
window.Class = original;
});

test('$super calls parent method', function() {
var Greeter = Class.$extend({
greeting: function() { return 'Armin!'; },
});
var Spaniard = Greeter.$extend({
greeting: function() { return 'Hola, ' + this.$super(); },
});
var Englishman = Greeter.$extend({
greeting: function() { return 'Hello, ' + this.$super(); },
});
same((Spaniard().greeting()), 'Hola, Armin!',
'Spanish greeting generated.');
same((Englishman().greeting()), 'Hello, Armin!',
'English greeting generated.');
});

test('$extend exists and works', function() {
same(typeof(Class.$extend), 'function',
'basic class has $extends function');

var SubClass = Class.$extend({});

same(typeof(SubClass.$extend), 'function',
'subclasses also receive $extends');

same(SubClass.$extend, Class.$extend,
'base class and subclass have same $extend function');
});

test('classes can be used with or without `new`.', function() {
var pig1 = new Animal('pig');
var pig2 = Animal('pig');

same(pig1, pig2,
'Animal instances are the same when made with or without `new`');
});

test('__init__ is called with correct arguments', function() {
var Instrument = Class.$extend({
__init__: function(volume) {
this.volume = volume;
}
});
flute = Instrument(20);
cymbal = Instrument(100);
equal(flute.volume, 20);
equal(cymbal.volume, 100);
});

test('basic classical inheritence works', function() {
garfield = HouseCat({funny: true});
heathcliff = HouseCat({funny: false});

equal(garfield.funny, true,
'attribute was set on instance');
equal(heathcliff.funny, false,
'attribute was set on instance');
equal(typeof(HouseCat().funny), 'undefined',
'base HouseCat is not affected by subclass mutations');
});

test('instanceof works', function() {
var lion = Lion();
ok(lion instanceof Lion,
'instanceof works on class instances');
ok(lion instanceof Cat,
'instanceof works on subclass instances');
ok(lion instanceof Animal,
'instanceof works on deep subclass instances');
ok(!(lion instanceof HouseCat),
'isinsinstance is false when expected');
});

test('mixins work', function() {
FatMixin = {
'is_fat': true,
'describe': function() {
return this.name + ' is fat!';
},
};
FatCat = Cat.$extend({
'__include__': [FatMixin],
});
garfield = FatCat({name:'Garfield'});

ok(garfield.is_fat,
'mixin attribute is defined');
equal(garfield.describe(), 'Garfield is fat!',
'mixin has access to corrent `this.');
});

test('exercise test methods', function() {
var tick = Parasite();
var leo = Lion()
var garfield = HouseCat();

ok(!(garfield.scary),
'Cat instances are not scary.')

ok(leo.scary,
'Lion instances are scary.')

equal(garfield.health, 100,
'default health is 100');

tick.eat(garfield);

equal(garfield.health, 95,
'tick removes 5 health');

ok(!(garfield.dead()),
'still not dead');

leo.eat(garfield);

ok(garfield.dead(),
'garfield loses a life');
});

47 changes: 47 additions & 0 deletions tests/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var Animal = Class.$extend({
__init__: function(options) {
options = (typeof(options) == 'object' ? options : {name: 'Animal', health: 100})
for (key in options) {
this[key] = options[key];
};
// required defaults
this.name = (typeof(options.name) == 'string' ? options.name : 'Animal');
this.health = (typeof(options.health) == 'integer' ? options.health : 100);
},

die: function() {
this.health = 0;
},

eat: function(what) {
this.health += 5;
},

dead: function() {
return (this.health <= 0);
}
});

var Parasite = Animal.$extend({
eat: function(animal) {
this.$super(animal);
animal.health -= 5;
}
});

var Cat = Animal.$extend({
eat: function(animal) {
this.$super(animal);
animal.die();
}
});

var HouseCat = Cat.$extend({
'cute': true,
'scary': false
});

var Lion = Cat.$extend({
'cute': false,
'scary': true
});
18 changes: 18 additions & 0 deletions tests/run_tests.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>Classy Test Suite</title>
<link rel="stylesheet" href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css" type="text/css" media="screen">
<script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js"></script>
<script type="text/javascript" src="../classy.js"></script>
<script type="text/javascript" src="data.js"></script>
<script type="text/javascript" src="core.js"></script>
</head>
<body>
<h1 id="qunit-header">Classy Test Suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2
<ol id="qunit-tests"></ol>
</body>
</html>

0 comments on commit ad9b9b4

Please sign in to comment.