Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/core/Model.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -54,6 +54,20 @@ class Model {

}

/**
* Create a "type" property on an object that is non-writable
* @param {Object} object The object to define the property on
* @param {String} type The value to use for the type
*/
static defineTypeProp(object, type) {
Object.defineProperty(object, `type`, {
configurable: true,
enumerable: true,
value: type,
writable: false,
});
}

/**
* A utility function that defines a property on a class that validates its value when set.
* @param {Object} object [description]
Expand All@@ -79,20 +93,6 @@ class Model {

}

/**
* Create a "type" property on an object that is non-writable
* @param {Object} object The object to define the property on
* @param {String} type The value to use for the type
*/
static defineTypeProp(object, type) {
Object.defineProperty(object, `type`, {
configurable: true,
enumerable: true,
value: type,
writable: false,
});
}

}

export default Model;
2 changes: 2 additions & 0 deletions src/models/Language.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -54,6 +54,7 @@ function validateISOCode(input) {
* @prop {String} glottolog - The Glottocode for this language
* @prop {String} iso - The ISO 639-3 code for this language
* @prop {models.MultiLangString} name - The name of this language
* @prop {String} type - "Language"
*/
class Language extends Model {

Expand All@@ -72,6 +73,7 @@ class Language extends Model {
// Property Definitions

Model.defineModelProp(this, `name`, MultiLangString);
Model.defineTypeProp(this, `Language`);
Model.defineValidatedProp(this, `abbreviation`, validateAbbreviation);
Model.defineValidatedProp(this, `glottolog`, validateGlottoCode);
Model.defineValidatedProp(this, `iso`, validateISOCode);
Expand Down
58 changes: 13 additions & 45 deletions src/models/Language.test.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,81 +13,49 @@ describe(`Language`, () => {
Language.name.should.equal(`Language`);
});

it(`Abbreviation`, () => {
it(`abbreviation`, () => {
const lang = new Language;
(() => { lang.abbreviation = undefined; }).should.not.throw();
(() => { lang.abbreviation = `ctm`; }).should.not.throw();
(() => { lang.abbreviation = `en!`; }).should.throw().with.property(`name`, `AbbreviationError`);
(typeof lang.abbreviation).should.not.equal(`object`);
});

it(`Custom Property`, () => {
it(`custom property`, () => {
const lang = new Language;
(() => { lang.deleted = true; }).should.not.throw();
lang.deleted.should.equal(true);
});

it(`Glottocode`, () => {
it(`glottolog`, () => {
const lang = new Language;
(() => { lang.glottolog = undefined; }).should.not.throw();
(() => { lang.glottolog = `stan1293`; }).should.not.throw();
(() => { lang.glottolog = `stan129`; }).should.throw().with.property(`name`, `GlottoCodeError`);
(typeof lang.glottolog).should.not.equal(`object`);
});

it(`ISO 639-3 code`, () => {
it(`iso`, () => {
const lang = new Language;
(() => { lang.iso = undefined; }).should.not.throw();
(() => { lang.iso = `ctm`; }).should.not.throw();
(() => { lang.iso = `en`; }).should.throw().with.property(`name`, `ISOCodeError`);
(typeof lang.iso).should.not.equal(`object`);
});

describe(`name`, () => {
it(`name`, () => {

it(`class: MultiLangString`, () => {
const lang = new Language();
lang.name.should.be.instanceOf(MultiLangString);
});
const lang = new Language({ name: { eng: `Chitimacha` } });

it(`enumerable`, () => {
const lang = new Language({ name: 'Chitimacha' });
Object.keys(lang).should.contain(`name`);
});
lang.name.should.be.instanceOf(MultiLangString);
lang.name.get(`eng`).should.equal(`Chitimacha`);

it(`Success: String`, () => {

const name = `Chitimacha`;
const lang = new Language;

lang.name = name;

lang.name.get(`eng`).should.equal(name);

});

it(`Success: Object`, () => {

const name = {
eng: `Chitimacha`,
fra: `chitimacha`,
};

const lang = new Language;

lang.name = name;

lang.name.get(`eng`).should.equal(name.eng);
lang.name.get(`fra`).should.equal(name.fra);

});

it(`Error: bad data`, () => {
const lang = new Language;
const setBadLang = () => { lang.name = false; };
setBadLang.should.throw().with.property(`name`, `MultiLangStringDataError`);
});
});

it(`type`, () => {
const lang = new Language;
lang.type.should.equal(`Language`);
(() => { lang.type = `language`; }).should.throw();
});

});