Skip to content

Commit 4da25f8

Browse files
committed
test(gpio): extract constants
1 parent cb71d87 commit 4da25f8

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

src/peripherals/gpio.spec.ts

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import { CPU } from '../cpu/cpu';
22
import { AVRIOPort, portBConfig, PinState } from './gpio';
33

4+
const PINB = 0x23;
5+
const DDRB = 0x24;
6+
const PORTB = 0x25;
7+
48
describe('GPIO', () => {
59
it('should invoke the listeners when the port is written to', () => {
610
const cpu = new CPU(new Uint16Array(1024));
711
const port = new AVRIOPort(cpu, portBConfig);
812
const listener = jest.fn();
9-
cpu.writeData(0x24, 0x0f); // DDRB <- 0x0f
13+
cpu.writeData(DDRB, 0x0f);
1014
port.addListener(listener);
11-
cpu.writeData(0x25, 0x55); // PORTB <- 0x55
15+
cpu.writeData(PORTB, 0x55);
1216
expect(listener).toHaveBeenCalledWith(0x55, 0);
1317
expect(cpu.data[0x23]).toEqual(0x5); // PINB should return port value
1418
});
@@ -17,9 +21,9 @@ describe('GPIO', () => {
1721
const cpu = new CPU(new Uint16Array(1024));
1822
const port = new AVRIOPort(cpu, portBConfig);
1923
const listener = jest.fn();
20-
cpu.writeData(0x25, 0x55); // PORTB <- 0x55
24+
cpu.writeData(PORTB, 0x55);
2125
port.addListener(listener);
22-
cpu.writeData(0x24, 0xf0); // DDRB <- 0xf0
26+
cpu.writeData(DDRB, 0xf0);
2327
expect(listener).toHaveBeenCalledWith(0x55, 0x55);
2428
});
2529

@@ -28,7 +32,7 @@ describe('GPIO', () => {
2832
const port = new AVRIOPort(cpu, portBConfig);
2933
const listener = jest.fn();
3034
port.addListener(listener);
31-
cpu.writeData(0x25, 0x55); // PORTB <- 0x55
35+
cpu.writeData(PORTB, 0x55);
3236
expect(listener).toHaveBeenCalledWith(0x55, 0);
3337
});
3438

@@ -37,11 +41,11 @@ describe('GPIO', () => {
3741
const port = new AVRIOPort(cpu, portBConfig);
3842
const listener = jest.fn();
3943
port.addListener(listener);
40-
cpu.writeData(0x24, 0x0f); // DDRB <- 0x0f
41-
cpu.writeData(0x25, 0x55); // PORTB <- 0x55
42-
cpu.writeData(0x23, 0x01); // PINB <- 0x0f
44+
cpu.writeData(DDRB, 0x0f);
45+
cpu.writeData(PORTB, 0x55);
46+
cpu.writeData(PINB, 0x01);
4347
expect(listener).toHaveBeenCalledWith(0x54, 0x55);
44-
expect(cpu.data[0x23]).toEqual(0x4); // PINB should return port value
48+
expect(cpu.data[PINB]).toEqual(0x4); // PINB should return port value
4549
});
4650

4751
describe('removeListener', () => {
@@ -50,9 +54,9 @@ describe('GPIO', () => {
5054
const port = new AVRIOPort(cpu, portBConfig);
5155
const listener = jest.fn();
5256
port.addListener(listener);
53-
cpu.writeData(0x24, 0x0f); // DDRB <- 0x0f
57+
cpu.writeData(DDRB, 0x0f);
5458
port.removeListener(listener);
55-
cpu.writeData(0x25, 0x99); // PORTB <- 0x99
59+
cpu.writeData(PORTB, 0x99);
5660
expect(listener).toBeCalledTimes(1);
5761
});
5862
});
@@ -61,16 +65,16 @@ describe('GPIO', () => {
6165
it('should return PinState.High when the pin set to output and HIGH', () => {
6266
const cpu = new CPU(new Uint16Array(1024));
6367
const port = new AVRIOPort(cpu, portBConfig);
64-
cpu.writeData(0x24, 0x1); // DDRB <- 0x1
65-
cpu.writeData(0x25, 0x1); // PORTB <- 0x1
68+
cpu.writeData(DDRB, 0x1);
69+
cpu.writeData(PORTB, 0x1);
6670
expect(port.pinState(0)).toEqual(PinState.High);
6771
});
6872

6973
it('should return PinState.Low when the pin set to output and LOW', () => {
7074
const cpu = new CPU(new Uint16Array(1024));
7175
const port = new AVRIOPort(cpu, portBConfig);
72-
cpu.writeData(0x24, 0x8); // DDRB <- 0x8
73-
cpu.writeData(0x25, 0xf7); // PORTB <- 0xF7 (~8)
76+
cpu.writeData(DDRB, 0x8);
77+
cpu.writeData(PORTB, 0xf7);
7478
expect(port.pinState(3)).toEqual(PinState.Low);
7579
});
7680

@@ -83,8 +87,8 @@ describe('GPIO', () => {
8387
it('should return PinState.InputPullUp when the pin is set to input with pullup', () => {
8488
const cpu = new CPU(new Uint16Array(1024));
8589
const port = new AVRIOPort(cpu, portBConfig);
86-
cpu.writeData(0x24, 0); // DDRB <- 0
87-
cpu.writeData(0x25, 0x2); // PORTB <- 0x2
90+
cpu.writeData(DDRB, 0);
91+
cpu.writeData(PORTB, 0x2);
8892
expect(port.pinState(1)).toEqual(PinState.InputPullUp);
8993
});
9094

@@ -96,9 +100,9 @@ describe('GPIO', () => {
96100
expect(port.pinState(0)).toBe(PinState.High);
97101
});
98102
expect(port.pinState(0)).toBe(PinState.Input);
99-
cpu.writeData(0x24, 0x01); // DDRB <- 0x01
103+
cpu.writeData(DDRB, 0x01);
100104
port.addListener(listener);
101-
cpu.writeData(0x25, 0x01); // PORTB <- 0x01
105+
cpu.writeData(PORTB, 0x01);
102106
expect(listener).toHaveBeenCalled();
103107
});
104108

@@ -111,7 +115,7 @@ describe('GPIO', () => {
111115
});
112116
expect(port.pinState(0)).toBe(PinState.Input);
113117
port.addListener(listener);
114-
cpu.writeData(0x24, 0x01); // DDRB <- 0x01
118+
cpu.writeData(DDRB, 0x01);
115119
expect(listener).toHaveBeenCalled();
116120
});
117121
});
@@ -120,7 +124,7 @@ describe('GPIO', () => {
120124
it('should set the value of the given pin', () => {
121125
const cpu = new CPU(new Uint16Array(1024));
122126
const port = new AVRIOPort(cpu, portBConfig);
123-
cpu.writeData(0x24, 0); // DDRB <- 0
127+
cpu.writeData(DDRB, 0);
124128
port.setPin(4, true);
125129
expect(cpu.data[0x23]).toEqual(0x10);
126130
port.setPin(4, false);
@@ -130,12 +134,12 @@ describe('GPIO', () => {
130134
it('should only update PIN register when pin in Input mode', () => {
131135
const cpu = new CPU(new Uint16Array(1024));
132136
const port = new AVRIOPort(cpu, portBConfig);
133-
cpu.writeData(0x24, 0x10); // DDRB <- 0x10
134-
cpu.writeData(0x25, 0x0); // PORTB <- 0x0
137+
cpu.writeData(DDRB, 0x10);
138+
cpu.writeData(PORTB, 0x0);
135139
port.setPin(4, true);
136-
expect(cpu.data[0x23]).toEqual(0x0);
137-
cpu.writeData(0x24, 0x0); // DDRB <- 0x0
138-
expect(cpu.data[0x23]).toEqual(0x10);
140+
expect(cpu.data[PINB]).toEqual(0x0);
141+
cpu.writeData(DDRB, 0x0);
142+
expect(cpu.data[PINB]).toEqual(0x10);
139143
});
140144
});
141145
});

0 commit comments

Comments
 (0)