Skip to content

Commit

Permalink
Add more Dry.clone() unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Jan 23, 2018
1 parent 00bf709 commit a2e10b3
Showing 1 changed file with 101 additions and 9 deletions.
110 changes: 101 additions & 9 deletions test/dry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ var assert = require('assert'),
Dry.Classes = Blast.Classes;
Dry.Classes.__Protoblast = Blast;

function MyPerson(firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}

MyPerson.prototype.fullname = function fullname() {
return this.firstname + ' ' + this.lastname;
};

describe('Dry', function TestDry() {

var dryCirc2,
Expand Down Expand Up @@ -115,15 +124,6 @@ describe('Dry', function TestDry() {
dried,
res;

function MyPerson(firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}

MyPerson.prototype.fullname = function fullname() {
return this.firstname + ' ' + this.lastname;
};

Dry.registerDrier('MyPerson', function dryMyPerson(holder, key, value) {
return {
firstname : value.firstname,
Expand Down Expand Up @@ -594,5 +594,97 @@ describe('Dry', function TestDry() {

assert.equal(clone, 1);
});

it('should use a custom method if given', function() {

var dried,
temp,
flo;

function Pet(species, name) {
this.species = species;
this.name = name;
}

// Create the new class now
flo = new Pet('cat', 'Flo');

temp = Dry.clone(flo);

// No dry methods are added yet, so it should be a simple object
assert.equal(temp.constructor == Object, true);
assert.equal(temp.species, 'cat');
assert.equal(temp.name, 'Flo');

// First add a toJSON method, the final fallback
Pet.prototype.toJSON = function toJSON() {
return {
species : this.species,
name : this.name
};
};

temp = Dry.clone(flo);
assert.equal(temp.constructor == Object, true);
assert.equal(temp.species, 'cat');
assert.equal(temp.name, 'Flo');

// Add the toDry method
Pet.prototype.toDry = function toDry() {
return {
value: {
species : this.species,
name : this.name
}
};
};

temp = Dry.clone(flo);

// No undry method is added yet, so it should be a simple object
assert.equal(temp.constructor == Object, true);
assert.equal(temp.species, 'cat');
assert.equal(temp.name, 'Flo');

// Add the undry methods
Pet.unDry = function unDry(value) {
return new Pet(value.species, value.name);
}

Pet.prototype.getDescription = function getDescription() {
return this.name + ' is a ' + this.species;
};

Dry.registerClass(Pet);

dried = Dry.stringify(flo);
temp = Dry.parse(dried);

assert.equal(temp.constructor === Pet, true);
assert.equal(temp.getDescription(), 'Flo is a cat');

dried = Dry.clone(flo, 'getDescription');

// It should have used "getDescription" to clone it
assert.equal(dried, 'Flo is a cat');

// It should use the dry methods if no special clone method is available
temp = Dry.clone(flo);

assert.equal(temp.constructor === Pet, true);
assert.equal(temp.getDescription(), 'Flo is a cat');
});

it('should use driers if nothing else is available', function() {

var griet = new MyPerson('Griet', 'De Leener'),
temp;

temp = Dry.clone(griet);

assert.equal(temp.constructor === MyPerson, true);
assert.equal(temp.firstname, 'Griet');
assert.equal(temp.lastname, 'De Leener');
});
});
});

0 comments on commit a2e10b3

Please sign in to comment.