Skip to content

Commit 5142305

Browse files
author
Alberto Iannaccone
committed
test monitor utils
1 parent 7063f92 commit 5142305

File tree

5 files changed

+190
-13
lines changed

5 files changed

+190
-13
lines changed

arduino-ide-extension/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
"download": "^7.1.0",
103103
"grpc_tools_node_protoc_ts": "^4.1.0",
104104
"mocha": "^7.0.0",
105+
"mockdate": "^3.0.5",
105106
"moment": "^2.24.0",
106107
"protoc": "^1.0.4",
107108
"shelljs": "^0.8.3",

arduino-ide-extension/src/browser/monitor/monitor-utils.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { Line, SerialMonitorOutput } from './serial-monitor-send-output';
22

3-
export function messageToLines(
3+
export function messagesToLines(
44
messages: string[],
5-
prevLines: Line[],
5+
prevLines: Line[] = [],
6+
charCount = 0,
67
separator = '\n'
78
): [Line[], number] {
89
const linesToAdd: Line[] = prevLines.length
910
? [prevLines[prevLines.length - 1]]
1011
: [{ message: '', lineLen: 0 }];
11-
let charCount = 0;
1212

1313
for (const message of messages) {
1414
const messageLen = message.length;
@@ -38,11 +38,12 @@ export function messageToLines(
3838

3939
export function truncateLines(
4040
lines: Line[],
41-
charCount: number
41+
charCount: number,
42+
maxCharacters: number = SerialMonitorOutput.MAX_CHARACTERS
4243
): [Line[], number] {
43-
let charsToDelete = charCount - SerialMonitorOutput.MAX_CHARACTERS;
44+
let charsToDelete = charCount - maxCharacters;
4445
let lineIndex = 0;
45-
while (charsToDelete > 0) {
46+
while (charsToDelete > 0 || lineIndex > 0) {
4647
const firstLineLength = lines[lineIndex]?.lineLen;
4748

4849
if (charsToDelete >= firstLineLength) {
@@ -55,6 +56,7 @@ export function truncateLines(
5556

5657
// delete all previous lines
5758
lines.splice(0, lineIndex);
59+
lineIndex = 0;
5860

5961
const newFirstLine = lines[0]?.message?.substring(charsToDelete);
6062
const deletedCharsCount = firstLineLength - newFirstLine.length;

arduino-ide-extension/src/browser/monitor/serial-monitor-send-output.tsx

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import AutoSizer from 'react-virtualized-auto-sizer';
66
import { MonitorModel } from './monitor-model';
77
import { MonitorConnection } from './monitor-connection';
88
import dateFormat = require('dateformat');
9-
import { messageToLines, truncateLines } from './monitor-utils';
9+
import { messagesToLines, truncateLines } from './monitor-utils';
1010

1111
export type Line = { message: string; timestamp?: Date; lineLen: number };
1212

@@ -66,14 +66,12 @@ export class SerialMonitorOutput extends React.Component<
6666
this.scrollToBottom();
6767
this.toDisposeBeforeUnmount.pushAll([
6868
this.props.monitorConnection.onRead(({ messages }) => {
69-
const [newLines, charsToAddCount] = messageToLines(
69+
const [newLines, totalCharCount] = messagesToLines(
7070
messages,
71-
this.state.lines
72-
);
73-
const [lines, charCount] = truncateLines(
74-
newLines,
75-
this.state.charCount + charsToAddCount
71+
this.state.lines,
72+
this.state.charCount
7673
);
74+
const [lines, charCount] = truncateLines(newLines, totalCharCount);
7775

7876
this.setState({
7977
lines,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import { expect } from 'chai';
2+
import {
3+
messagesToLines,
4+
truncateLines,
5+
} from '../../browser/monitor/monitor-utils';
6+
import { Line } from '../../browser/monitor/serial-monitor-send-output';
7+
import { set, reset } from 'mockdate';
8+
9+
type TestLine = {
10+
messages: string[];
11+
prevLines?: { lines: Line[]; charCount: number };
12+
expected: { lines: Line[]; charCount: number };
13+
expectedTruncated?: {
14+
lines: Line[];
15+
charCount: number;
16+
maxCharacters?: number;
17+
};
18+
};
19+
20+
const date = new Date();
21+
const testLines: TestLine[] = [
22+
{
23+
messages: ['Hello'],
24+
expected: { lines: [{ message: 'Hello', lineLen: 5 }], charCount: 5 },
25+
},
26+
{
27+
messages: ['Hello', 'Dog!'],
28+
expected: { lines: [{ message: 'HelloDog!', lineLen: 9 }], charCount: 9 },
29+
},
30+
{
31+
messages: ['Hello\n', 'Dog!'],
32+
expected: {
33+
lines: [
34+
{ message: 'Hello\n', lineLen: 6 },
35+
{ message: 'Dog!', lineLen: 4 },
36+
],
37+
charCount: 10,
38+
},
39+
},
40+
{
41+
messages: ['Dog!'],
42+
prevLines: { lines: [{ message: 'Hello\n', lineLen: 6 }], charCount: 6 },
43+
expected: {
44+
lines: [
45+
{ message: 'Hello\n', lineLen: 6 },
46+
{ message: 'Dog!', lineLen: 4 },
47+
],
48+
charCount: 10,
49+
},
50+
},
51+
{
52+
messages: [' Dog!\n', " Who's a good ", 'boy?\n', "You're a good boy!"],
53+
prevLines: { lines: [{ message: 'Hello', lineLen: 5 }], charCount: 5 },
54+
expected: {
55+
lines: [
56+
{ message: 'Hello Dog!\n', lineLen: 11 },
57+
{ message: " Who's a good boy?\n", lineLen: 19 },
58+
{ message: "You're a good boy!", lineLen: 8 },
59+
],
60+
charCount: 48,
61+
},
62+
expectedTruncated: {
63+
maxCharacters: 20,
64+
charCount: 20,
65+
lines: [
66+
{ message: '?\n', lineLen: 2 },
67+
{ message: "You're a good boy!", lineLen: 8 },
68+
],
69+
},
70+
},
71+
{
72+
messages: ['boy?\n', "You're a good boy!"],
73+
prevLines: {
74+
lines: [
75+
{ message: 'Hello Dog!\n', lineLen: 11 },
76+
{ message: " Who's a good ", lineLen: 14 },
77+
],
78+
charCount: 25,
79+
},
80+
expected: {
81+
lines: [
82+
{ message: 'Hello Dog!\n', lineLen: 11 },
83+
{ message: " Who's a good boy?\n", lineLen: 19 },
84+
{ message: "You're a good boy!", lineLen: 8 },
85+
],
86+
charCount: 48,
87+
},
88+
expectedTruncated: {
89+
maxCharacters: 20,
90+
charCount: 20,
91+
lines: [
92+
{ message: '?\n', lineLen: 2 },
93+
{ message: "You're a good boy!", lineLen: 8 },
94+
],
95+
},
96+
},
97+
{
98+
messages: ["Who's a good boy?\n", 'Yo'],
99+
prevLines: {
100+
lines: [{ message: 'Hello Dog!\n', lineLen: 11 }],
101+
charCount: 11,
102+
},
103+
expected: {
104+
lines: [
105+
{ message: 'Hello Dog!\n', lineLen: 11 },
106+
{ message: "Who's a good boy?\n", lineLen: 18 },
107+
{ message: 'Yo', lineLen: 2 },
108+
],
109+
charCount: 31,
110+
},
111+
expectedTruncated: {
112+
maxCharacters: 20,
113+
charCount: 20,
114+
lines: [
115+
{ message: "Who's a good boy?\n", lineLen: 18 },
116+
{ message: 'Yo', lineLen: 2 },
117+
],
118+
},
119+
},
120+
];
121+
122+
testLines.forEach((t) =>
123+
[...t.expected.lines, ...(t.prevLines?.lines || [])].forEach(
124+
(l) => (l.timestamp = date)
125+
)
126+
);
127+
128+
describe.only('Monitor Utils', () => {
129+
beforeEach(() => {
130+
set(date);
131+
});
132+
133+
afterEach(() => {
134+
reset();
135+
});
136+
137+
testLines.forEach((testLine) => {
138+
context('when converting messages', () => {
139+
it('should give the right result', () => {
140+
const [newLines, addedCharCount] = messagesToLines(
141+
testLine.messages,
142+
testLine.prevLines?.lines,
143+
testLine.prevLines?.charCount
144+
);
145+
newLines.forEach((line, index) => {
146+
expect(line.message).to.equal(testLine.expected.lines[index].message);
147+
expect(line.timestamp).to.deep.equal(
148+
testLine.expected.lines[index].timestamp
149+
);
150+
});
151+
expect(addedCharCount).to.equal(testLine.expected.charCount);
152+
153+
const [truncatedLines, totalCharCount] = truncateLines(
154+
newLines,
155+
addedCharCount,
156+
testLine.expectedTruncated?.maxCharacters
157+
);
158+
let charCount = 0;
159+
if (testLine.expectedTruncated) {
160+
truncatedLines.forEach((line, index) => {
161+
expect(line.message).to.equal(
162+
testLine.expectedTruncated?.lines[index].message
163+
);
164+
charCount += line.message.length;
165+
});
166+
expect(totalCharCount).to.equal(charCount);
167+
}
168+
});
169+
});
170+
});
171+
});

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -11743,6 +11743,11 @@ mocha@^7.0.0:
1174311743
yargs-parser "13.1.2"
1174411744
yargs-unparser "1.6.0"
1174511745

11746+
mockdate@^3.0.5:
11747+
version "3.0.5"
11748+
resolved "https://registry.yarnpkg.com/mockdate/-/mockdate-3.0.5.tgz#789be686deb3149e7df2b663d2bc4392bc3284fb"
11749+
integrity sha512-iniQP4rj1FhBdBYS/+eQv7j1tadJ9lJtdzgOpvsOHng/GbcDh2Fhdeq+ZRldrPYdXvCyfFUmFeEwEGXZB5I/AQ==
11750+
1174611751
modify-values@^1.0.0:
1174711752
version "1.0.1"
1174811753
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"

0 commit comments

Comments
 (0)