-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenAssert.js
More file actions
48 lines (44 loc) · 1.33 KB
/
genAssert.js
File metadata and controls
48 lines (44 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
let highlightBlocks = document.querySelectorAll("div.highlight>pre");
let inRegex = /In \[\d+\]: /gm;
let outRegex = /Out\[\d+\]: /gm;
highlightBlocks.forEach(block => {
let button = document.createElement("div");
button.classList.add("md-clipboard", "md-icon")
button.style.right = "2em";
button.style.color = "red";
button.addEventListener("click", () => {
let assertList = genAsserts(groupStatements(button.parentElement.innerText));
let formattedText = formatClipboardText(assertList);
navigator.clipboard.writeText(formattedText);
});
block.appendChild(button);
})
const groupStatements = (text) => {
let inList = [];
let outList = [];
text.split("\n").forEach(line => {
if (inRegex.test(line)){
inList.push(line.split(inRegex)[1]);
} else if (outRegex.test(line)){
outList.push(line.split(outRegex)[1]);
}
})
return {
inList,
outList
}
}
const genAsserts = (lines) => {
assertStatements = [];
lines.inList.forEach((line, index) => {
assertStatements.push(`assert ${line} == ${lines.outList[index]}`);
})
return assertStatements;
}
const formatClipboardText = (assertList) => {
let text = "";
assertList.forEach(line => {
text += `${line} \n`;
})
return text;
}