Skip to content
Snippets Groups Projects
Commit af104079 authored by Travis Savo's avatar Travis Savo
Browse files

Fix those unit tests!

parent fac29f7c
No related branches found
No related tags found
No related merge requests found
Pipeline #
require "base"
require "bodyPart"
require "proportionate"
 
global.$game = {} if not global.$game
global.$game.constants = {} if not global.$game.constants
Loading
Loading
@@ -34,10 +33,12 @@ human.stats.agility = ["useless", "sloth-like", "slow", "delayed", "adequate", "
human.stats.luck = ["non-existant", "doomed", "terrible", "unfortunate", "not the best", "not an issue", "better than some", "better than most", "uncanny", "great", "charmed", "on a streak", "unstoppable", "favored by deities", "so good you can't possibly go wrong"]
 
global.$game.constants.body.human.formatHeight = (height)->
require("proportionate").arrays()
global.$game.constants.body.human.height.proportionate(height, global.$game.constants.body.human.maxHeight)
 
global.$game.constants.body.human.formatWeight = (weight, height = @getHeight())->
return global.$game.constants.body.human.weight.proportionate(weight, 100 * height)
require("proportionate").arrays()
global.$game.constants.body.human.weight.proportionate(weight, 100 * height)
 
if not global.$game.classes.HumanBody
global.$game.classes.HumanBody = class HumanBody
Loading
Loading
chai = require "chai"
expect = chai.expect
chai.should()
require("../src/proportionate")
describe "Proportionate", ->
it "should index directly on a one-to-one mapping", ->
[1, 2, 3].proportionate(0, 3).should.equal 1
[1, 2, 3].proportionate(1, 3).should.equal 2
[1, 2, 3].proportionate(2, 3).should.equal 2
[1, 2, 3].proportionate(3, 3).should.equal 3
it "should only index extremes when extremes are met", ->
[1, 2, 3].proportionate(0, 6).should.equal 1
[1, 2, 3].proportionate(1, 6).should.equal 2
[1, 2, 3].proportionate(2, 6).should.equal 2
[1, 2, 3].proportionate(3, 6).should.equal 2
[1, 2, 3].proportionate(4, 6).should.equal 2
[1, 2, 3].proportionate(5, 6).should.equal 2
[1, 2, 3].proportionate(6, 6).should.equal 3
it "should skip items when the max is small", ->
[1,2,3,4,5,6,7,8,9,10].proportionate(0,3).should.equal 1
[1,2,3,4,5,6,7,8,9,10].proportionate(1,3).should.equal 4
[1,2,3,4,5,6,7,8,9,10].proportionate(2,3).should.equal 7
[1,2,3,4,5,6,7,8,9,10].proportionate(3,3).should.equal 10
it "should clamp to the extremes when out of bounds", ->
[1, 2, 3].proportionate(-1, 3).should.equal 1
[1, 2, 3].proportionate(4, 3).should.equal 3
\ No newline at end of file
chai = require "chai"
expect = chai.expect
chai.should()
serialize = require("../src/serialize")
describe "Serialization", ->
it "should serialize a basic graph", ->
base =
int:1
float:2.99e81
str:"test"
obj:
int:1
str:"test"
nullValue:null
arrayValue:[{}]
infinityValue:Infinity
negInfinityValue:-Infinity
nanValue:NaN
dateValue:new Date()
func: (a, b)->
a+b
base.dupe1 = {}
base.dupe2 = base.dupe1
base.dupe1.dupe2 = base.dupe2
base.dupe3 = {dupe2:base.dupe2}
base.dupe3.more = {base:base, dupe2:base.dupe2}
base.self = base
deserialized = serialize.unserialize serialize.serialize base, true
base.int.should.equal deserialized.int
base.str.should.equal deserialized.str
base.float.should.equal deserialized.float
base.obj.int.should.equal deserialized.obj.int
base.obj.str.should.equal deserialized.obj.str
expect(base.nullValue).to.equal deserialized.nullValue
base.infinityValue.should.equal deserialized.infinityValue
base.negInfinityValue.should.equal deserialized.negInfinityValue
expect(deserialized.nanValue).to.be.NaN
base.func.toString().should.equal deserialized.func.toString()
deserialized.self.self.self.self.self.should.equal deserialized
deserialized.func(2, 2).should.equal 4
it "should throw an error when encountering a native function", ->
expect ->
serialize.serialize {console:console}
.to.throw()
it "should not trown an error when it's allowed to serialize native functions", ->
expect ->
serialize.serialize {console:console}, true
.not.to.throw()
it "should serialize a complex graph", ->
x = {}
y = {}
z = {}
a = {}
x.y = y
y.z = z
z.a = a
a.x = x
x.n = {}
x.n.n = {}
x.n.n.n = {}
x.n.n.n.n = y
z.y = y
x.func = x.func2 = x.func3 = ->
deserialized = serialize.unserialize serialize.serialize x
deserialized.y.z.a.x.should.equal deserialized
deserialized.n.n.n.n.should.equal deserialized.y
deserialized.y.z.y.should.equal deserialized.y
deserialized.func.should.equal deserialized.func2
deserialized.func.should.equal deserialized.func3
it "should serialize a function which knows about 'this'", ->
x =
value:"hello"
func: ->
@value
serialize.unserialize(serialize.serialize(x)).func().should.equal x.value
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment