diff --git a/README.md b/README.md index eda7793..bd7e059 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,13 @@ Navigate to http://localhost:8080/ Matrix driver & Unicorn Hat HD driver inspired by https://github.com/vesteraas/node-unicornhathd +### Unit tests +This application has some unit tests, making use of [Jest](http://facebook.github.io/jest/). Jest is configured as a dev dependency. + +execute: + + npm test + ### Drawing ![Device drawing](https://raw.githubusercontent.com/sexybiggetje/pixdisp/screenshots/device.jpg "Drawing on the device") diff --git a/__tests__/drawingoperations.js b/__tests__/drawingoperations.js new file mode 100644 index 0000000..94f24d8 --- /dev/null +++ b/__tests__/drawingoperations.js @@ -0,0 +1,91 @@ +'use strict'; + +let driver, config; + +let { DriverFactory } = require( '../drivers/driverfactory' ); +let { Dummy } = require( '../drivers/dummy' ); + +let driverFactory = new DriverFactory(); + +beforeEach(() => { + config = { + "driver": "dummy", + "matrix": { + "width": 4, + "height": 4, + "brightness": 1, + "flipHorizontal": false, + "flipVertical": false + } + }; +}); + +test( 'Matrix creation', () => { + driver = driverFactory.createFromConfig( config ); + + expect( driver.getSize() ).toEqual( { width: 4, height: 4 } ); + expect( driver.getBuffer().length ).toEqual( 48 ); +} ); + +test( 'Set a pixel', () => { + driver = driverFactory.createFromConfig( config ); + + driver.setPixel( 0, 0, 255, 255, 255, 1 ); + driver.setPixel( 3, 0, 255, 255, 255, 1 ); + driver.setPixel( 0, 3, 255, 255, 255, 1 ); + driver.setPixel( 3, 3, 255, 255, 255, 1 ); + + let matrix = driver.getMatrix(); + + expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} ); + expect( matrix[ 3 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} ); + expect( matrix[ 0 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} ); + expect( matrix[ 3 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} ); + + expect( matrix[ 0 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} ); + expect( matrix[ 3 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} ); + expect( matrix[ 1 ][ 3 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} ); + expect( matrix[ 2 ][ 3 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} ); + +} ); + +test( 'Draw a line', () => { + driver = driverFactory.createFromConfig( config ); + + driver.drawLine( 0, 0, 3, 3, 255, 255, 255, 1 ); + + let matrix = driver.getMatrix(); + + expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} ); + expect( matrix[ 1 ][ 1 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} ); + expect( matrix[ 2 ][ 2 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} ); + expect( matrix[ 3 ][ 3 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} ); + + expect( matrix[ 0 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} ); + expect( matrix[ 1 ][ 2 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} ); + expect( matrix[ 2 ][ 3 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} ); + expect( matrix[ 3 ][ 0 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} ); + +} ); + +test( 'Draw a circle', () => { + config.matrix.width = 8; + config.matrix.height = 8; + + driver = driverFactory.createFromConfig( config ); + + driver.drawCircle( 3, 3, 4, 255, 255, 255, 1 ); + + let matrix = driver.getMatrix(); + + expect( matrix[ 1 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} ); + expect( matrix[ 2 ][ 0 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} ); + expect( matrix[ 2 ][ 6 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} ); + expect( matrix[ 3 ][ 6 ] ).toEqual( { r: 255, g: 255, b: 255, a: 1} ); + + expect( matrix[ 0 ][ 0 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} ); + expect( matrix[ 0 ][ 7 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} ); + expect( matrix[ 2 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} ); + expect( matrix[ 3 ][ 1 ] ).toEqual( { r: 0, g: 0, b: 0, a: 1} ); + +} ); \ No newline at end of file diff --git a/drivers/driver.js b/drivers/driver.js index cad3b70..aa8b7f1 100644 --- a/drivers/driver.js +++ b/drivers/driver.js @@ -64,8 +64,8 @@ class Driver { * Set pixel color */ setPixel( x, y, r = 0, g = 0, b = 0, a = 1 ) { - if ( x >= 0 && x <= this.width && - y >= 0 && y <= this.height ) { + if ( x >= 0 && x < this.width && + y >= 0 && y < this.height ) { this.matrix[x][y].r = r; this.matrix[x][y].g = g; this.matrix[x][y].b = b; @@ -169,6 +169,13 @@ class Driver { } } + /** + * Returns the current matrix at any given state + */ + getMatrix() { + return this.matrix; + } + /** * Get the buffer for writing * @return {Buffer} diff --git a/package.json b/package.json index c5f6416..9b479a7 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ }, "jest": { "name": "pixdisp", - "testRegex": "(/__tests__/.[^_]*|(\\.|/)(test|spec))\\.jsx?$" + "testRegex": "(/__tests__/.[^_]*|(\\.|/)(test|spec))\\.jsx?$", + "verbose": true } }