CS2613/assignments/A4/spec/moarlife.spec.js
2022-11-04 17:53:24 -03:00

103 lines
3.7 KiB
JavaScript

let life=require ("../moarlife.js");
let plan= ["############################",
"##### ######",
"## *** **##",
"# *##** ** O *##",
"# *** O ##** *#",
"# O ##*** #",
"# ##** #",
"# O #* #",
"#* #** O #",
"#*** ##** O **#",
"##**** ###*** *###",
"############################"];
describe("World",
function () {
let valley = new life.LifelikeWorld(plan,
{"#": life.Wall,
"O": life.PlantEater,
"*": life.Plant});
it("roundtrip",
function() {
let rows = valley.toString().split("\n");
// drop blank row
rows.pop();
expect(rows).toEqual(plan);
});
});
describe("actionTypes",
function () {
it("grow",
function () {
let critter = new life.Plant();
let energy = critter.energy;
life.actionTypes.grow(critter);
expect(critter.energy).toBeGreaterThan(energy);
});
})
describe("PlantEater",
function () {
it("constructor",
function () {
let pe = new life.PlantEater();
expect('energy' in pe).toBe(true);
expect(pe.energy).toBe(20);
});
it("act, reproduce",
function () {
let pe = new life.PlantEater();
pe.energy = 65
expect(pe.act({find: function (ch) { if (ch === " ") return "n"; } })).toEqual({ type: "reproduce", direction: "n" });
});
it("act, eat",
function () {
let pe = new life.PlantEater();
pe.energy = 20
expect(pe.act({find: function (ch ) { if (ch === "*") return "n"; } })).toEqual({ type: "eat", direction: "n" });
});
it("act, move",
function () {
let pe = new life.PlantEater();
expect(pe.act({find: function (ch) { if (ch === " ") return "n"; } })).toEqual({ type: "move", direction: "n" });
});
});
describe("ExplodingBunnyRabbit",
function () {
it("constructor",
function () {
let pe = new life.ExplodingBunnyRabbit();
expect('energy' in pe).toBe(true);
expect(pe.energy).toBe(20);
});
it("act, reproduce",
function () {
let pe = new life.ExplodingBunnyRabbit();
pe.energy = 65
expect(pe.act({find: function (ch) { if (ch === " ") return "n"; } })).toEqual({ type: "reproduce", direction: "n" });
});
it("act, eat",
function () {
let pe = new life.ExplodingBunnyRabbit();
pe.energy = 20
expect(pe.act({find: function (ch ) { if (ch === "*") return "n"; } })).toEqual({ type: "eat", direction: "n" });
});
it("act, move",
function () {
let pe = new life.ExplodingBunnyRabbit();
expect(pe.act({find: function (ch) { if (ch === " ") return "n"; } })).toEqual({ type: "move", direction: "n" });
});
it("act, explode",
function () {
let pe = new life.ExplodingBunnyRabbit();
expect(pe.act({find: function (ch) { if (ch === "O") return "n"; } })).toEqual({ type: "explode", direction: "n" });
});
});