From 24124414cc870d5d2e200f895903ce9c8297badc Mon Sep 17 00:00:00 2001 From: Stuart Colville Date: Wed, 30 Sep 2015 17:31:49 +0100 Subject: [PATCH] Add message object --- src/const.js | 4 ++++ src/constants.js | 2 -- src/message.js | 56 +++++++++++++++++++++++++++++++++++++++++++ tests/test.message.js | 48 +++++++++++++++++++++++++++++++++++++ tests/test.xpi.js | 2 +- 5 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 src/const.js delete mode 100644 src/constants.js create mode 100644 src/message.js create mode 100644 tests/test.message.js diff --git a/src/const.js b/src/const.js new file mode 100644 index 00000000..c49d9ee4 --- /dev/null +++ b/src/const.js @@ -0,0 +1,4 @@ +export const DEFLATE_COMPRESSION = 8; +export const NO_COMPRESSION = 0; +export const MESSAGE_TYPES = ['error', 'notice', 'warning']; +export const SIGNING_SEVERITIES = ['trivial', 'low', 'medium', 'high']; diff --git a/src/constants.js b/src/constants.js deleted file mode 100644 index 51c511f4..00000000 --- a/src/constants.js +++ /dev/null @@ -1,2 +0,0 @@ -export const DEFLATE_COMPRESSION = 8; -export const NO_COMPRESSION = 0; diff --git a/src/message.js b/src/message.js new file mode 100644 index 00000000..606a952d --- /dev/null +++ b/src/message.js @@ -0,0 +1,56 @@ +import { MESSAGE_TYPES, SIGNING_SEVERITIES } from 'const'; + + +// These are the optional fields we expect to pull out of +// the opts object passed to the Message constructor. +export var fields = [ + 'id', + 'message', + 'description', + 'file', + 'line', + 'column', + 'for_appversions', + 'compatibility_type', + 'signing_help', + 'signing_severity', +]; + + +export default class Message { + + constructor(type, opts={}) { + this.type = type; + for (let field of fields) { + this[field] = opts[field]; + } + this.editorsOnly = opts.editorsOnly || false; + } + + get type() { + return this._type; + } + + set type(type) { + if (MESSAGE_TYPES.indexOf(type) === -1) { + throw new Error( + `Message type "${type}" is not one of ${MESSAGE_TYPES.join(', ')}`); + } + this._type = type; + } + + get signing_severity() { + return this._signing_severity; + } + + set signing_severity(severity) { + if (typeof severity !== 'undefined') { + if (SIGNING_SEVERITIES.indexOf(severity) === -1) { + throw new Error( + `Severity "${severity}" is not one of ` + + `${SIGNING_SEVERITIES.join(', ')}`); + } + } + this._signing_severity = severity; + } +} diff --git a/tests/test.message.js b/tests/test.message.js new file mode 100644 index 00000000..98b10dcd --- /dev/null +++ b/tests/test.message.js @@ -0,0 +1,48 @@ +import { default as Message, fields } from 'message'; + +/*eslint no-unused-vars:0*/ + +describe('Message', function() { + + it('should throw on missing type', () => { + assert.throws(() => { + var MyMessage = new Message(); + }, Error, /Message type "undefined" is not/); + }); + + it('should throw on invalid type', () => { + assert.throws(() => { + var MyMessage = new Message('awooga'); + }, Error, /Message type "awooga" is not/); + }); + + it('should throw on incorrect signing_severity', () => { + assert.throws(() => { + var MyMessage = new Message('error', + {signing_severity: 'whatever'}); + }, Error, /Severity "whatever" is not/); + }); + + it('should define all expected fields', () => { + var fakeOpts = {}; + for (let field of fields) { + fakeOpts[field] = field; + } + fakeOpts.signing_severity = 'medium'; + var MyMessage = new Message('error', fakeOpts); + for (let field of fields) { + if (field === 'signing_severity') { + assert.equal(MyMessage.signing_severity, 'medium'); + } else { + assert.equal(MyMessage[field], field); + } + } + }); + + it ("shouldn't define random opts", () => { + var MyMessage = new Message('error', {random: 'foo'}); + assert.notEqual(MyMessage.random, 'foo'); + }); + +}); + diff --git a/tests/test.xpi.js b/tests/test.xpi.js index e2e57235..47a42b6f 100644 --- a/tests/test.xpi.js +++ b/tests/test.xpi.js @@ -1,7 +1,7 @@ import { Readable } from 'stream'; import Xpi from 'xpi'; -import { DEFLATE_COMPRESSION, NO_COMPRESSION } from 'constants'; +import { DEFLATE_COMPRESSION, NO_COMPRESSION } from 'const'; const defaultData = { compressionMethod: DEFLATE_COMPRESSION,