added unit tests, parsing class fields

This commit is contained in:
metacryst
2024-03-13 16:07:26 -05:00
parent dbf4d7cb19
commit fc4434b5de
4 changed files with 260 additions and 89 deletions

61
Test/parse.test.js Normal file
View File

@@ -0,0 +1,61 @@
window.testSuites.push( class testParse {
testParseClassFieldsWithEqualityCheck() {
class Space extends HTMLElement {
form = Forms.observe(window.location.pathname, this)
contents = [
...this.form.children.map(form => {
switch(form.type) {
case "file" === "file":
return File(form)
case "space":
return ChildSpace(form)
case "link":
return Link()
}
})
]
constructor() {
super()
}
}
const fields = window.Registry.parseClassFields(Space.toString());
if(!(JSON.stringify(fields) === JSON.stringify(["form", "contents"]))) {
return `Fields don't match`
}
}
testParseClassFieldsWithInnerFunctionVariable() {
class Space extends HTMLElement {
form = Forms.observe(window.location.pathname, this)
contents = [
...this.form.children.map(form => {
let file;
file = "hey"
switch(form.type) {
case "file" === "file":
return File(form)
case "space":
return ChildSpace(form)
case "link":
return Link()
}
})
]
constructor() {
super()
}
}
const fields = window.Registry.parseClassFields(Space.toString());
if(!(JSON.stringify(fields) === JSON.stringify(["form", "contents"]))) {
return `Fields don't match`
}
}
})

14
Test/test.html Normal file
View File

@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Quill</title>
<link rel="icon" href="">
<link rel="stylesheet" href="">
<script src=""></script>
<script src="../index.js"></script>
<script src="test.js"></script>
</head>
<body>
</body>
</html>

56
Test/test.js Normal file
View File

@@ -0,0 +1,56 @@
console.log("Tests initializing.")
window.testSuites = [];
import ("./parse.test.js")
window.test = async function() {
// window.testSuites.sort();
window.alert = () => true;
window.confirm = () => true;
let failed = 0;
let success = 0;
var start = new Date();
for(let j=0; j<window.testSuites.length; j++) {
let testSuite = window.testSuites[j];
console.log(`%c ➽ ${j+1} ${testSuite.name.replace("test", "")}`, 'color: #ffffff; font-size: 17px; padding-left: -20px; padding-top: 10px; padding-bottom: 10px; text-align: right;')
let suite = new testSuite();
let testNum = 0;
let suiteContents = Object.getOwnPropertyNames(testSuite.prototype)
for(let i=0; i<suiteContents.length; i++) {
let test = suiteContents[i];
if(typeof suite[test] === 'function' && test !== "constructor") {
testNum++;
let fail = await suite[test]();
if(fail) {
failed++;
console.log(`%c ${testNum}. ${test}: ${fail}`, 'background: #222; color: rgb(254, 62, 43)');
} else {
success++;
console.log(`%c ${testNum}. ${test}`, 'background: #222; color: #00FF00');
}
}
// Need to flush ws buffer since saving is disabled, and that is what usually does the flushing
// Frame.ws.sendMessage('Flush Buffer');
}
// await wait(100);
}
console.log("")
console.log("")
let elapsed = new Date() - start;
if(failed === 0) {
console.log(`%cRan ${failed+success} tests in ${elapsed}ms`, 'background: #222; color: #00FF00');
console.log(`%c ${success} passed`, 'background: #222; color: #00FF00');
console.log(`%c ${failed} failed`, 'background: #222;');
} else {
console.log(`%cRan ${failed+success} tests in ${elapsed}ms`, 'background: #222; color: rgb(254, 62, 43)');
console.log(`%c ${success} `, 'background: #222; color: #00FF00', "passed");
console.log(`%c ${failed} failed`, 'background: #222; color: rgb(254, 62, 43)');
}
}
window.wait = ms => new Promise(res => setTimeout(res, ms));
window.__defineGetter__("test", test);

View File

@@ -16,7 +16,40 @@ window.$ = function(selector, el = document) {
/* REGISTER */
window.registerElement = (el, tagname) => {
window.Registry = class Registry {
static parseClassFields(classStr) {
const lines = classStr.split('\n');
const fields = [];
let braceDepth = 0; // Tracks the depth of curly braces to identify when we're inside a function/method
for (let line of lines) {
const trimmedLine = line.trim();
// Update braceDepth based on the current line
braceDepth += (trimmedLine.match(/{/g) || []).length;
braceDepth -= (trimmedLine.match(/}/g) || []).length;
// Check if the line is outside any function/method (top-level within the class)
if (braceDepth === 1 && trimmedLine.includes('=') && !trimmedLine.startsWith('...')) {
// Extract the field name, which is before the '=' symbol
const fieldName = trimmedLine.split('=')[0].trim();
// Make sure the field name doesn't include invalid characters or spaces
if (!fieldName.includes(' ') && !fieldName.startsWith('this.')) {
fields.push(fieldName);
}
}
// If we encounter the constructor, stop the parsing as we're only interested in fields above it
if (trimmedLine.startsWith('constructor')) {
break;
}
}
return fields;
}
static registerElement = (el, tagname) => {
const randomClassName = 'Custom' + Math.random().toString(36).substring(2, 7);
const DynamicClass = {[randomClassName]: class extends el {}}[randomClassName];
const randomTagName = 'custom-' + Math.random().toString(36).substring(2, 7);
@@ -112,7 +145,9 @@ window.registerElement = (el, tagname) => {
return elem
}
}
}
window.registerElement = Registry.registerElement
/* PROTOTYPE FUNCTIONS */
@@ -358,3 +393,8 @@ function getSafariVersion() {
console.red = function(message) {
this.log(`%c${message}`, "color: rgb(254, 79, 42);");
};
console.green = function(message) {
this.log(`%c${message}`, "color: rgb(79, 254, 42);");
}