Skip to content

Commit

Permalink
Now passing the serialization tests!
Browse files Browse the repository at this point in the history
  • Loading branch information
NQNStudios committed Jun 8, 2018
1 parent 2d78069 commit 72e8d9a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.*.json
dist/
*.swp
# Logs
Expand Down
8 changes: 6 additions & 2 deletions src/Idea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ export class Idea

@JsonProperty("id", Number)
id: number;

@JsonProperty("name", String)
name: string;

@JsonProperty("description", String)
description: string = '';

Expand Down Expand Up @@ -157,9 +155,15 @@ export class Idea
return result;
}

// TODO it would be nice not to need this function!
become(otherIdea: Idea) {
this.id = otherIdea.id;
this.name = otherIdea.name;
this.description = otherIdea.description;
this.tags = otherIdea.tags;
this.children = otherIdea.children;
this._progress = otherIdea._progress;
this._duration = otherIdea._duration;
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/processes/Serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,34 @@ export class LoadProcess extends BotProcess
let jsonInput = input.substr(jsonStart);

let jsonObject: IdeaJson = JSON.parse(jsonInput) as IdeaJson;
console.log(jsonObject);
let newRootIdea = LoadProcess.converter.deserializeObject(jsonObject, Idea);

// Recursively load all the children as Idea objects
for (let i = 0; i < jsonObject.children.length; ++i) {
let childLoadProcess = new LoadProcess(this.bot, new Idea());
this.bot.startProcess(childLoadProcess);
console.log(JSON.stringify(jsonObject.children[i]));
this.bot.handleInput(countInput + JSON.stringify(jsonObject.children[i]));
newRootIdea.children[i] = childLoadProcess.rootIdea;
}

// FIXME if deserialization tests are failing it is probably because of
// this line, which is necessary because ES5 doesn't define
// Object.assign()
this.rootIdea.become(newRootIdea);

// Make sure TotalCount is properly set
Idea.TotalCount = parseInt(countInput);

this.status = BotStatus.Idle;
}

finish(): void {
// TODO: Don't always do this
console.log(this.rootIdea.children);
}
}

export class SaveProcess extends BotProcess
{
start(): BotStatus {
return BotStatus.HasOutput;
start() {
this.status = BotStatus.HasOutput;
}

getOutput(): string {
Expand Down
66 changes: 58 additions & 8 deletions test/serialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,50 @@ var davincibot = require('../dist/DaVinciBot.js');
var addIdeaProcess = require('../dist/processes/AddIdeaProcess.js');
var serialization = require('../dist/processes/Serialization.js');

describe('Serialization test', () => {
function createTestIdea() {
idea.Idea.TotalCount = 0;
var rootIdea = new idea.Idea('root has a name!');
rootIdea.addChild('test1');
rootIdea.addChild('test2');
rootIdea.addChild('test3');
rootIdea.children[2].description = 'test3 has a description';
return rootIdea;
}

describe('JSON serialization test', () => {
var bot = new davincibot.DaVinciBot();
var rootIdea = new idea.Idea();
bot.startProcess(new addIdeaProcess.AddIdeaProcess(bot, rootIdea));
bot.getOutput();
bot.handleInput('test1');
bot.handleInput('test2');
bot.handleInput('test3');
bot.handleInput('quit');
var rootIdea = createTestIdea();

bot.startProcess(new serialization.SaveProcess(bot, rootIdea));
var json = bot.getOutput();


it('Should serialize the idea count', () => {
// TODO parse and assert totalCount is 3
// var totalCount =
});

var jsonObject = JSON.parse(json.substring(1));

it('should serialize the root name', () => {
expect(jsonObject.name).to.equal('root has a name!');
});

it('Should serialize all children', () => {
expect(jsonObject.children.length).to.equal(3);
});
it("Should serialize all children's names", () => {
expect(jsonObject.children[0].name).to.equal("test1");
expect(jsonObject.children[1].name).to.equal("test2");
expect(jsonObject.children[2].name).to.equal("test3");
});
// TODO should serialize descriptions
// TODO should serialize all tags
});

describe('File serialization test', () => {
var bot = new davincibot.DaVinciBot();
var rootIdea = createTestIdea();

bot.startProcess(new serialization.SaveFileProcess(bot, rootIdea));
bot.handleInput('.serial-test-1.json');
Expand All @@ -24,10 +58,26 @@ describe('Serialization test', () => {
bot.startProcess(new serialization.LoadFileProcess(bot, newRootIdea));
bot.handleInput('.serial-test-1.json');

it('Should deserialize the global idea count', () => {
expect(idea.Idea.TotalCount).to.equal(4);
});
it('Should deserialize all children', () => {
expect(newRootIdea.children.length).to.equal(3);
});
it('Should preserve the Idea names after deserialization', () => {
expect(newRootIdea.children[0].name).to.equal('test1');
expect(newRootIdea.children[1].name).to.equal('test2');
expect(newRootIdea.children[2].name).to.equal('test3');
});
it('Should deserialize children as fully-featured Idea objects', () => {
expect(() => {newRootIdea.children[0].addChild('Test4');}).to.not.throw();
});

it('Should deserialize children with descriptions intact', () => {
expect(newRootIdea.children[2].description).to.equal("test3 has a description");
});
// TODO should preserve progress values
// TODO should preserve all tags
// TODO should preserve all moment.Duration objects -- this definitely
// shouldn't work yet
});

0 comments on commit 72e8d9a

Please sign in to comment.