Better DPT237.
Add tests for 21, 237.
This commit is contained in:
		
							parent
							
								
									123bd0c0ba
								
							
						
					
					
						commit
						fe6751a771
					
				| @ -61,6 +61,10 @@ export class DPT237 implements DPT { | ||||
|     if (value === undefined || value === null) | ||||
|       throw new InvalidValueError('Cannot write null value'); | ||||
| 
 | ||||
|     if (value.daliAddress < 0 || value.daliAddress > 63) | ||||
|       throw new InvalidValueError(`daliAddress must be between [0, 63]. git ${value.daliAddress}`); | ||||
| 
 | ||||
| 
 | ||||
|     // LSB
 | ||||
|     let LSB = (value.readResponse === false ? "0" : "1") + | ||||
|       (value.addressIndicator === false ? "0" : "1") + | ||||
|  | ||||
| @ -14,7 +14,10 @@ describe("Test DPT021", (): void => { | ||||
|     let value = [false, false, false, true, false, true, false, false] | ||||
|     let buffer = Buffer.from([0x14]) | ||||
|     const decoded = dpt.decoder(buffer) | ||||
|     expect(decoded).is.equal(value); | ||||
| 
 | ||||
|     expect(decoded.length).is.equal(value.length) | ||||
|     for (let i = 0; i < decoded.length; i++) | ||||
|       expect(decoded[i]).is.equal(value[i]); | ||||
|   }); | ||||
| 
 | ||||
|   it("Decode empty buffer", async function () { | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| import { expect } from "chai"; | ||||
| 
 | ||||
| import { DPT237 } from "../src/DPT237" | ||||
| import { DPT237, DPT237Result } from "../src/DPT237" | ||||
| import { BufferLengthError } from "../src/errors/BufferLengthError"; | ||||
| import { InvalidValueError } from "../src/errors/InvalidValueError"; | ||||
| 
 | ||||
| @ -10,4 +10,76 @@ describe("Test DPT237", (): void => { | ||||
| 
 | ||||
|   let dpt = new DPT237(); | ||||
| 
 | ||||
|   it("Decode buffer acceptable", async function () { | ||||
|     let value: DPT237Result = { | ||||
|       readResponse: false, | ||||
|       addressIndicator: false, | ||||
|       daliAddress: 0, | ||||
|       lampFailure: false, | ||||
|       ballastFailure: false, | ||||
|       convertorError: false | ||||
|     } | ||||
|     let buffer = Buffer.from([0x00, 0x00]) | ||||
|     const decoded = dpt.decoder(buffer) | ||||
|     expect(decoded.readResponse).is.equal(value.readResponse); | ||||
|     expect(decoded.addressIndicator).is.equal(value.addressIndicator); | ||||
|     expect(decoded.daliAddress).is.equal(value.daliAddress); | ||||
|     expect(decoded.lampFailure).is.equal(value.lampFailure); | ||||
|     expect(decoded.ballastFailure).is.equal(value.ballastFailure); | ||||
|     expect(decoded.convertorError).is.equal(value.convertorError); | ||||
|   }); | ||||
| 
 | ||||
|   it("Decode empty buffer", async function () { | ||||
|     let bufferEmpty = Buffer.from([]) | ||||
|     var testFunction = function () { | ||||
|       const value = dpt.decoder(bufferEmpty) | ||||
|     } | ||||
|     expect(testFunction).to.throw(BufferLengthError); | ||||
|   }); | ||||
| 
 | ||||
|   it("Decode oversized buffer", async function () { | ||||
|     let bufferBiggerSize = Buffer.from([0x20, 0x15, 0x00]) | ||||
| 
 | ||||
|     var testFunction = function () { | ||||
|       const value = dpt.decoder(bufferBiggerSize) | ||||
|     } | ||||
|     expect(testFunction).to.throw(BufferLengthError); | ||||
|   }); | ||||
| 
 | ||||
|   /* Encoder tests */ | ||||
|   it("encode valid", async function () { | ||||
|     let value: DPT237Result = { | ||||
|       readResponse: true, | ||||
|       addressIndicator: false, | ||||
|       daliAddress: 31, | ||||
|       lampFailure: false, | ||||
|       ballastFailure: false, | ||||
|       convertorError: true | ||||
|     } | ||||
|     let buffer = Buffer.from([0x04, 0x9f]) | ||||
| 
 | ||||
|     expect(compareBuffers(dpt.encoder(value), buffer)).is.true; | ||||
|   }); | ||||
| 
 | ||||
|   it("encode undefined", async function () { | ||||
|     var testFunction = function () { | ||||
|       const value = dpt.encoder(undefined) | ||||
|     } | ||||
|     expect(testFunction).to.throw(InvalidValueError); | ||||
|   }); | ||||
| 
 | ||||
|   it("encode invalid value", async function () { | ||||
|     let value: DPT237Result = { | ||||
|       readResponse: true, | ||||
|       addressIndicator: false, | ||||
|       daliAddress: 64, | ||||
|       lampFailure: false, | ||||
|       ballastFailure: false, | ||||
|       convertorError: false | ||||
|     } | ||||
|     var testFunction = function () { | ||||
|       const result = dpt.encoder(value) | ||||
|     } | ||||
|     expect(testFunction).to.throw(InvalidValueError); | ||||
|   }); | ||||
| }); | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user