67 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { expect } from "chai";
 | |
| 
 | |
| import { DPT1 } from "../src/DPT1"
 | |
| import { BufferLengthError } from "../src/errors/BufferLengthError";
 | |
| import { InvalidValueError } from "../src/errors/InvalidValueError";
 | |
| 
 | |
| import { compareBuffers } from "./util"
 | |
| 
 | |
| let bufferTrue = Buffer.from([1])
 | |
| let bufferFalse = Buffer.from([0])
 | |
| let bufferInvalid = Buffer.from([5])
 | |
| let bufferEmpty = Buffer.from([])
 | |
| let bufferBiggerSize = Buffer.from([1, 0])
 | |
| 
 | |
| 
 | |
| describe("Test DPT001", (): void => {
 | |
| 
 | |
|   let dpt = new DPT1();
 | |
| 
 | |
|   it("Decode true", async function () {
 | |
|     const value = dpt.decoder(bufferTrue)
 | |
|     expect(value).is.equal(1);
 | |
|   });
 | |
| 
 | |
|   it("Decode false", async function () {
 | |
|     const value = dpt.decoder(bufferFalse)
 | |
|     expect(value).is.equal(0);
 | |
|   });
 | |
| 
 | |
|   it("Decode invalid value buffer", async function () {
 | |
|     var testFunction = function () {
 | |
|       const value = dpt.decoder(bufferInvalid)
 | |
|     }
 | |
|     expect(testFunction).to.throw(InvalidValueError);
 | |
|   });
 | |
| 
 | |
|   it("Decode empty buffer", async function () {
 | |
|     var testFunction = function () {
 | |
|       const value = dpt.decoder(bufferEmpty)
 | |
|     }
 | |
|     expect(testFunction).to.throw(BufferLengthError);
 | |
|   });
 | |
| 
 | |
|   it("Decode oversized buffer", async function () {
 | |
|     var testFunction = function () {
 | |
|       const value = dpt.decoder(bufferBiggerSize)
 | |
|     }
 | |
|     expect(testFunction).to.throw(BufferLengthError);
 | |
|   });
 | |
| 
 | |
|   it("encode true", async function () {
 | |
|     const value = dpt.encoder(1)
 | |
|     expect(compareBuffers(value, bufferTrue)).is.true;
 | |
|   });
 | |
| 
 | |
|   it("encode false", async function () {
 | |
|     const value = dpt.encoder(0)
 | |
|     expect(compareBuffers(value, bufferFalse)).is.true;
 | |
|   });
 | |
| 
 | |
|   it("encode invalid", async function () {
 | |
|     var testFunction = function () {
 | |
|       const value = dpt.encoder(5)
 | |
|     }
 | |
|     expect(testFunction).to.throw(InvalidValueError);
 | |
|   });
 | |
| }); |