Skip to content

Commit 92b51e7

Browse files
committed
Start building tests of types
1 parent b741c61 commit 92b51e7

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

lib/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ Function.prototype.inspect = function () {
145145
}
146146
Function.prototype.equals = function (other) {
147147
// If they're not both functions then they're definitely not equal
148-
if (this.prototype !== other.prototype) {
148+
if (this.constructor !== other.constructor) {
149149
return false
150150
}
151151
// Args must be the same length

test/test-types.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Pull in the helper to make sure we're tracking coverage
2+
var helper = require('./helper'),
3+
expect = require('expect.js')
4+
5+
var types = require('../lib/types')
6+
7+
describe('Type-system', function () {
8+
var rootObject = new types.Object('fake')
9+
rootObject.supertype = null
10+
rootObject.isRoot = true
11+
12+
describe('given an Object', function () {
13+
var object = null
14+
it('should be constructed correctly', function () {
15+
object = new types.Object(rootObject)
16+
expect(object.supertype).to.eql(rootObject)
17+
expect(object.properties).to.eql({})
18+
expect(object.initializers).to.eql([])
19+
})
20+
it('should throw an error on missing property', function () {
21+
expect(function () {
22+
var type = object.getTypeOfProperty('missing')
23+
}).to.throwException()
24+
})
25+
var Number = new types.Number(rootObject)
26+
it('should create a property', function () {
27+
object.setTypeOfProperty('present', Number)
28+
})
29+
it('should get that property', function () {
30+
var type = object.getTypeOfProperty('present')
31+
expect(type).to.eql(Number)
32+
})
33+
})
34+
35+
describe('given a Function', function () {
36+
var fn = null,
37+
Number = new types.Number(rootObject),
38+
String = new types.String(rootObject)
39+
it('should be constructed correctly', function () {
40+
fn = new types.Function(rootObject, [Number], String)
41+
expect(fn.args.length).to.eql(1)
42+
expect(fn.args[0]).to.eql(Number)
43+
expect(fn.ret).to.eql(String)
44+
})
45+
it('should not equal non-Function type', function () {
46+
expect(fn.equals(Number)).to.be(false)
47+
})
48+
it('should not equal Function with different number of arguments', function () {
49+
var otherFn = new types.Function(rootObject, [], String)
50+
expect(fn.equals(otherFn)).to.be(false)
51+
})
52+
it('should not equal Function with different types of arguments', function () {
53+
var otherFn = new types.Function(rootObject, [String], String)
54+
expect(fn.equals(otherFn)).to.be(false)
55+
})
56+
it('should not equal Function with different return type', function () {
57+
var otherFn = new types.Function(rootObject, [Number], Number)
58+
expect(fn.equals(otherFn)).to.be(false)
59+
})
60+
})
61+
})
62+

0 commit comments

Comments
 (0)