From 4da25f8d09aa0ef36ce0a1a15a445431e632412b Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Wed, 7 Oct 2020 12:46:43 +0300 Subject: test(gpio): extract constants --- src/peripherals/gpio.spec.ts | 56 ++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/src/peripherals/gpio.spec.ts b/src/peripherals/gpio.spec.ts index 820aa52..d4b3c7d 100644 --- a/src/peripherals/gpio.spec.ts +++ b/src/peripherals/gpio.spec.ts @@ -1,14 +1,18 @@ import { CPU } from '../cpu/cpu'; import { AVRIOPort, portBConfig, PinState } from './gpio'; +const PINB = 0x23; +const DDRB = 0x24; +const PORTB = 0x25; + describe('GPIO', () => { it('should invoke the listeners when the port is written to', () => { const cpu = new CPU(new Uint16Array(1024)); const port = new AVRIOPort(cpu, portBConfig); const listener = jest.fn(); - cpu.writeData(0x24, 0x0f); // DDRB <- 0x0f + cpu.writeData(DDRB, 0x0f); port.addListener(listener); - cpu.writeData(0x25, 0x55); // PORTB <- 0x55 + cpu.writeData(PORTB, 0x55); expect(listener).toHaveBeenCalledWith(0x55, 0); expect(cpu.data[0x23]).toEqual(0x5); // PINB should return port value }); @@ -17,9 +21,9 @@ describe('GPIO', () => { const cpu = new CPU(new Uint16Array(1024)); const port = new AVRIOPort(cpu, portBConfig); const listener = jest.fn(); - cpu.writeData(0x25, 0x55); // PORTB <- 0x55 + cpu.writeData(PORTB, 0x55); port.addListener(listener); - cpu.writeData(0x24, 0xf0); // DDRB <- 0xf0 + cpu.writeData(DDRB, 0xf0); expect(listener).toHaveBeenCalledWith(0x55, 0x55); }); @@ -28,7 +32,7 @@ describe('GPIO', () => { const port = new AVRIOPort(cpu, portBConfig); const listener = jest.fn(); port.addListener(listener); - cpu.writeData(0x25, 0x55); // PORTB <- 0x55 + cpu.writeData(PORTB, 0x55); expect(listener).toHaveBeenCalledWith(0x55, 0); }); @@ -37,11 +41,11 @@ describe('GPIO', () => { const port = new AVRIOPort(cpu, portBConfig); const listener = jest.fn(); port.addListener(listener); - cpu.writeData(0x24, 0x0f); // DDRB <- 0x0f - cpu.writeData(0x25, 0x55); // PORTB <- 0x55 - cpu.writeData(0x23, 0x01); // PINB <- 0x0f + cpu.writeData(DDRB, 0x0f); + cpu.writeData(PORTB, 0x55); + cpu.writeData(PINB, 0x01); expect(listener).toHaveBeenCalledWith(0x54, 0x55); - expect(cpu.data[0x23]).toEqual(0x4); // PINB should return port value + expect(cpu.data[PINB]).toEqual(0x4); // PINB should return port value }); describe('removeListener', () => { @@ -50,9 +54,9 @@ describe('GPIO', () => { const port = new AVRIOPort(cpu, portBConfig); const listener = jest.fn(); port.addListener(listener); - cpu.writeData(0x24, 0x0f); // DDRB <- 0x0f + cpu.writeData(DDRB, 0x0f); port.removeListener(listener); - cpu.writeData(0x25, 0x99); // PORTB <- 0x99 + cpu.writeData(PORTB, 0x99); expect(listener).toBeCalledTimes(1); }); }); @@ -61,16 +65,16 @@ describe('GPIO', () => { it('should return PinState.High when the pin set to output and HIGH', () => { const cpu = new CPU(new Uint16Array(1024)); const port = new AVRIOPort(cpu, portBConfig); - cpu.writeData(0x24, 0x1); // DDRB <- 0x1 - cpu.writeData(0x25, 0x1); // PORTB <- 0x1 + cpu.writeData(DDRB, 0x1); + cpu.writeData(PORTB, 0x1); expect(port.pinState(0)).toEqual(PinState.High); }); it('should return PinState.Low when the pin set to output and LOW', () => { const cpu = new CPU(new Uint16Array(1024)); const port = new AVRIOPort(cpu, portBConfig); - cpu.writeData(0x24, 0x8); // DDRB <- 0x8 - cpu.writeData(0x25, 0xf7); // PORTB <- 0xF7 (~8) + cpu.writeData(DDRB, 0x8); + cpu.writeData(PORTB, 0xf7); expect(port.pinState(3)).toEqual(PinState.Low); }); @@ -83,8 +87,8 @@ describe('GPIO', () => { it('should return PinState.InputPullUp when the pin is set to input with pullup', () => { const cpu = new CPU(new Uint16Array(1024)); const port = new AVRIOPort(cpu, portBConfig); - cpu.writeData(0x24, 0); // DDRB <- 0 - cpu.writeData(0x25, 0x2); // PORTB <- 0x2 + cpu.writeData(DDRB, 0); + cpu.writeData(PORTB, 0x2); expect(port.pinState(1)).toEqual(PinState.InputPullUp); }); @@ -96,9 +100,9 @@ describe('GPIO', () => { expect(port.pinState(0)).toBe(PinState.High); }); expect(port.pinState(0)).toBe(PinState.Input); - cpu.writeData(0x24, 0x01); // DDRB <- 0x01 + cpu.writeData(DDRB, 0x01); port.addListener(listener); - cpu.writeData(0x25, 0x01); // PORTB <- 0x01 + cpu.writeData(PORTB, 0x01); expect(listener).toHaveBeenCalled(); }); @@ -111,7 +115,7 @@ describe('GPIO', () => { }); expect(port.pinState(0)).toBe(PinState.Input); port.addListener(listener); - cpu.writeData(0x24, 0x01); // DDRB <- 0x01 + cpu.writeData(DDRB, 0x01); expect(listener).toHaveBeenCalled(); }); }); @@ -120,7 +124,7 @@ describe('GPIO', () => { it('should set the value of the given pin', () => { const cpu = new CPU(new Uint16Array(1024)); const port = new AVRIOPort(cpu, portBConfig); - cpu.writeData(0x24, 0); // DDRB <- 0 + cpu.writeData(DDRB, 0); port.setPin(4, true); expect(cpu.data[0x23]).toEqual(0x10); port.setPin(4, false); @@ -130,12 +134,12 @@ describe('GPIO', () => { it('should only update PIN register when pin in Input mode', () => { const cpu = new CPU(new Uint16Array(1024)); const port = new AVRIOPort(cpu, portBConfig); - cpu.writeData(0x24, 0x10); // DDRB <- 0x10 - cpu.writeData(0x25, 0x0); // PORTB <- 0x0 + cpu.writeData(DDRB, 0x10); + cpu.writeData(PORTB, 0x0); port.setPin(4, true); - expect(cpu.data[0x23]).toEqual(0x0); - cpu.writeData(0x24, 0x0); // DDRB <- 0x0 - expect(cpu.data[0x23]).toEqual(0x10); + expect(cpu.data[PINB]).toEqual(0x0); + cpu.writeData(DDRB, 0x0); + expect(cpu.data[PINB]).toEqual(0x10); }); }); }); -- cgit v1.2.3