From 49a6a46052f8262dbb47df2bf3f06388caa1880b Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Thu, 10 Aug 2017 16:47:50 +0200 Subject: [PATCH] tests added --- src/meterbus.test.ts | 89 ++++++++++++++++++++++---------------------- src/meterbus.ts | 2 + src/utils.test.ts | 23 ++++++++++++ 3 files changed, 70 insertions(+), 44 deletions(-) diff --git a/src/meterbus.test.ts b/src/meterbus.test.ts index 596c293..1153c5d 100644 --- a/src/meterbus.test.ts +++ b/src/meterbus.test.ts @@ -4,7 +4,7 @@ import {MeterbusLibExceptions} from './exceptions' import {MeterbusLibFrames} from './simpleframes' import {MeterbusLibCodeTables} from './codetables' - + import * as mocha from 'mocha' import * as chai from 'chai' @@ -43,7 +43,7 @@ let inputOKControlframe = "68 03 03 68 01 02 03 06 16" let inputNOKControlframe_wrong_stopcode = "68 03 03 68 01 02 03 06 15" let inputNOKControlframe_wrong_secondlength = "68 03 04 68 01 02 03 06 16" let inputNOKLongframe_too_short = "68 02 02 68 01 02 03 16" - + let inputNOk_wrong_startcode = "15 01 02 03 16" @@ -138,47 +138,48 @@ describe('The Meterbus Library', () => { telegram.parse() expect((telegram.frame as MeterbusLibFrames.ControlFrame).ciField).to.equal(3) }) + + +}) - describe('The Meterbus Longframe Library', () => { - let telegram : MeterbusLib.Telegram - - beforeEach(() => { - telegram = new MeterbusLib.Telegram() - telegram.fromHexString(inputOkLongFrame_1phase_electric) - telegram.parse() - }) - - it('should find the identNo', () => { - expect((telegram.frame as MeterbusLibLongFrame.LongFrame).fixedDataHeader.identNo) - .to.equal(17001300) - }) - it('should find the manufacturer', () => { - expect((telegram.frame as MeterbusLibLongFrame.LongFrame).fixedDataHeader.manufacturer) - .to.equal("FIN") - }) - it('should find the version', () => { - expect((telegram.frame as MeterbusLibLongFrame.LongFrame).fixedDataHeader.version) - .to.equal(0x24) - }) - it('should find the medium', () => { - expect((telegram.frame as MeterbusLibLongFrame.LongFrame).fixedDataHeader.medium) - .to.equal("Electrity") - }) - it('should find the accessNo', () => { - expect((telegram.frame as MeterbusLibLongFrame.LongFrame).fixedDataHeader.accessNo) - .to.equal(0xd6) - }) - it('should find the status', () => { - expect((telegram.frame as MeterbusLibLongFrame.LongFrame).fixedDataHeader.status) - .to.equal(0) - }) - it('should find the signature', () => { - expect((telegram.frame as MeterbusLibLongFrame.LongFrame).fixedDataHeader.signature) - .to.deep.equal([0,0]) - }) - it('should prepare itself as JSON', () => { - expect((telegram.frame as MeterbusLibLongFrame.LongFrame).getJSON()).to.equal(json_1phase_electric) - }) +describe('The Meterbus Longframe Library', () => { + let telegram : MeterbusLib.Telegram + + beforeEach(() => { + telegram = new MeterbusLib.Telegram() + telegram.fromHexString(inputOkLongFrame_1phase_electric) + telegram.parse() }) - -}) \ No newline at end of file + + it('should find the identNo', () => { + expect((telegram.frame as MeterbusLibLongFrame.LongFrame).fixedDataHeader.identNo) + .to.equal(17001300) + }) + it('should find the manufacturer', () => { + expect((telegram.frame as MeterbusLibLongFrame.LongFrame).fixedDataHeader.manufacturer) + .to.equal("FIN") + }) + it('should find the version', () => { + expect((telegram.frame as MeterbusLibLongFrame.LongFrame).fixedDataHeader.version) + .to.equal(0x24) + }) + it('should find the medium', () => { + expect((telegram.frame as MeterbusLibLongFrame.LongFrame).fixedDataHeader.medium) + .to.equal("Electrity") + }) + it('should find the accessNo', () => { + expect((telegram.frame as MeterbusLibLongFrame.LongFrame).fixedDataHeader.accessNo) + .to.equal(0xd6) + }) + it('should find the status', () => { + expect((telegram.frame as MeterbusLibLongFrame.LongFrame).fixedDataHeader.status) + .to.equal(0) + }) + it('should find the signature', () => { + expect((telegram.frame as MeterbusLibLongFrame.LongFrame).fixedDataHeader.signature) + .to.deep.equal([0,0]) + }) + it('should prepare itself as JSON', () => { + expect((telegram.frame as MeterbusLibLongFrame.LongFrame).getJSON()).to.equal(json_1phase_electric) + }) +}) diff --git a/src/meterbus.ts b/src/meterbus.ts index 825e62a..63c3e0e 100644 --- a/src/meterbus.ts +++ b/src/meterbus.ts @@ -1,6 +1,8 @@ import {MeterbusLibExceptions} from './exceptions' import {MeterbusLibFrames} from './simpleframes' import {MeterbusLibLongFrame} from './longframe' +import {MeterbusLibUtils} from './utils' + export namespace MeterbusLib { diff --git a/src/utils.test.ts b/src/utils.test.ts index a00d03d..ec90883 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -26,3 +26,26 @@ describe('The Meterbus Library Utils', () => { expect(MeterbusLibUtils.manufCode([0x2e, 0x19])).to.equal("FIN") }) }) + +describe('The jsonPrepare function in the Meterbus Library Utils', () => { + it('should forward regular properties of an object', () => { + let objIn = {'a': 1, 'b':2} + let objOut = objIn + expect(MeterbusLibUtils.jsonPrepaper(objIn, [])).to.deep.equal(objOut) + }) + it('should hide properties in the hide list', () => { + let objIn = {'a': 1, 'b':2} + let objOut = {'a': 1} + expect(MeterbusLibUtils.jsonPrepaper(objIn, ['b'])).to.deep.equal(objOut) + }) + it('should hide properties beginning with two underscores', () => { + let objIn = {'a': 1, '__b':2} + let objOut = {'a': 1} + expect(MeterbusLibUtils.jsonPrepaper(objIn, [])).to.deep.equal(objOut) + }) + it('should remove one leading underscore from property names', () => { + let objIn = {'_a': 1, 'b':2} + let objOut = {'a': 1, 'b':2} + expect(MeterbusLibUtils.jsonPrepaper(objIn, [])).to.deep.equal(objOut) + }) +}) \ No newline at end of file