Adding Group

This commit is contained in:
metacryst
2024-09-12 15:08:51 -05:00
parent 9432c65fa4
commit cfdf67998d
10 changed files with 54 additions and 26 deletions

315
Test/Skeleton/init.test.js Normal file
View File

@@ -0,0 +1,315 @@
window.testSuites.push( class testInit {
ObjectAsStateField() {
class File extends Shadow {
$form
constructor(...params) {
super(...params)
}
}
window.register(File, "file-el")
let form = {data: "asdf"}
const el = window.File(form)
if(!(el.form === form)) {
return `State field does not match object passed in!`
}
}
MultiParams() {
class File2 extends Shadow {
$form
$tag
constructor(...params) {
super(...params)
}
}
window.register(File2, "file2-el")
let form = {data: "asdf"}
const el = window.File2(form, "tag")
if(!(el.form === form)) {
return `Form field does not match object passed in!`
}
if(!(el.tag === "tag")) {
return `Tag field does not match object passed in!`
}
}
onlyGetFieldsNotUsing$() {
class File5 extends Shadow {
$form
$tag
constructor(...params) {
super(...params)
}
}
window.register(File5, "file5-el")
let form = {data: "asdf"}
const el = window.File5(form, "tag")
if(el.$tag !== undefined) {
return "Got field the wrong way!"
}
}
ChangeAttrChangesField() {
class File3 extends Shadow {
$form
$tag
constructor(...params) {
super(...params)
}
}
window.register(File3, "file3-el")
let form = {data: "asdf"}
const el = window.File3(form, "tag")
el.setAttribute("tag", "asdf")
if(el.tag !== "asdf") {
return "Field did not change!"
}
}
ChangeFieldChangesAttr() {
class File4 extends Shadow {
$form
$tag
constructor(...params) {
super(...params)
}
}
window.register(File4, "file4-el")
let form = {data: "asdf"}
const el = window.File4(form, "tag")
el.tag = "asdf"
if(el.getAttribute("tag") !== "asdf") {
return "Attribute did not change!"
}
}
ConstructorCanUseState() {
class File7 extends Shadow {
$form = {data: "asdf"}
extra
constructor() {
super()
this.extra = this.form
}
}
window.register(File7, "file7-el")
const el = window.File7()
if(el.extra === undefined) {
return `Constructor could not access state`
}
}
DefaultStateFieldWorks() {
class File6 extends Shadow {
$form = {data: "asdf"}
}
window.register(File6, "file6-el")
const el = window.File6()
if(el.form === undefined) {
return `Default value did not work`
}
}
// this needs to be fixed
// CannotAddUndefinedProperties() {
// register(class File extends Shadow {
// render = () => {
// p("boi")
// }
// constructor() {
// super()
// this.hey = "unallowed"
// }
// }, randomName("file"))
// try {
// const file = File()
// return "Did not throw error!"
// } catch(e) {
// if(!e.message.includes("Extensible")) {
// throw e
// }
// }
// }
CannotAddUndefinedPropertiesAfterConstructor() {
register(class File extends Shadow {
render = () => {
p("boi")
}
constructor() {
super()
}
}, randomName("file"))
try {
const file = File()
file.hey = "unallowed"
return "Did not throw error!"
} catch(e) {
if(!e.message.includes("extensible")) {
throw e
}
}
}
CannotAddUndefinedPropertiesAfterDefaultConstructor() {
register(class File extends Shadow {
render = () => {
p("boi")
}
}, randomName("file"))
try {
const file = File()
file.hey = "unallowed"
return "Did not throw error!"
} catch(e) {
if(!e.message.includes("extensible")) {
throw e
}
}
}
NonStateFieldsGetSet() {
register(class File extends Shadow {
nonStateField
constructor() {
super()
}
}, randomName("file"))
const file = File("asd")
if(!(file.nonStateField === "asd")) {
return "Did not set field!"
}
}
AllFieldsMustBeSet() {
register(class File extends Shadow {
$field1
$field2
constructor() {
super()
}
}, randomName("file"))
try {
const file = File("asd")
console.log(file.field1, file.field2)
return "No error thrown"
} catch(e) {
if(!e.message.includes("field2\" must be initialized")) {
throw e
}
}
}
ErrorIfNotObservedObject() {
window.register(class ChildSpace extends Shadow {
$$form
$name
render = () => {
}
constructor() {
super()
this.name = this.form.path.split("/").pop()
}
}, randomName("space-"))
try {
let space = ChildSpace({path: "/asd"})
return "no error thrown!"
} catch(e) {
if(e.message.includes("Observed Object")) {
} else {
throw e
}
}
}
FieldsInCorrectOrder() {
window.register(class ChildSpace extends Shadow {
$$form
$name
render = () => {
}
constructor() {
super()
this.name = this.form.path.split("/").pop()
}
}, randomName("space-"))
class Form extends ObservedObject {
$path
}
try {
let space = ChildSpace(Form.create({path: "/asd"}))
} catch(e) {
if(e.message.includes("Cannot read properties of undefined (reading 'path')")) {
return "Form did not get initialized!"
} else {
throw e
}
}
}
UnneededArgumentThrowsError() {
register(class SidebarFile extends Shadow {
}, randomName("sb-file"))
try {
SidebarFile({path: "/asd"})
return "Did not throw error!"
} catch(e) {
if(!e.message.includes("passed in where")) {
throw e
}
}
}
ConflictingPropertyNameThrowsError() {
register(class SidebarFile extends Shadow {
$width = 0
}, randomName("sb-file"))
try {
SidebarFile()
return "Did not throw error!"
} catch(e) {
if(!e.message.includes(`Property name "width" is not valid`)) {
throw e
}
}
}
})

View File

@@ -0,0 +1,162 @@
window.testSuites.push( class testObservedObject {
FromJSONFailsWithoutAllFields() {
class Form extends ObservedObject {
id
path
$canvasPosition
}
try {
let obj = Form.create({id: "123"})
return "Not implemented"
} catch {}
}
FromJSONInitsAllFields() {
class Form extends ObservedObject {
id
path
$canvasPosition
}
let obj = Form.create({id: "123", path: "/", canvasPosition: "25|25"})
if(!(obj && obj["id"] === "123" && obj["path"] === "/" && obj["canvasPosition"] === "25|25")) {
return "Not all fields initialized!"
}
}
DefaultValueWorks() {
class WindowState extends ObservedObject {
$sidebarOut = false
}
let obj = WindowState.create()
console.log(obj)
if(obj.sidebarOut !== false) {
return "Default field not set"
}
}
// throw some sort of warning if a global OO is accessed without "this"
DefaultObservedObject() {
window.Form = class Form extends ObservedObject {
id
path
$canvasPosition
}
class File extends Shadow {
$$form = Form.create({id: "123", path: "/", canvasPosition: "25|25"})
render = () => {
p(this.form.path)
}
}
window.register(File, "file-1")
let file = window.File()
if(file.firstChild?.innerText !== "/") {
return "Path is not inside of paragraph tag"
}
}
ObservedObject() {
let Form = class Form extends ObservedObject {
id
$path
$canvasPosition
}
let object = Form.create({id: "123", path: "/", canvasPosition: "25|25"});
register(class File extends Shadow {
$$form
render = () => {
p(this.form.path)
}
}, randomName("file"))
let file = File(object)
if(file.firstChild?.innerText !== "/") {
return "Path is not inside of paragraph tag"
}
object.path = "/asd"
if(file.form.path !== "/asd") {
return "Path did not change when changing original object"
}
if(file.firstChild?.innerText !== "/asd") {
return "Observed Object did not cause a reactive change"
}
}
ObservedObjectWithArray() {
let Form = class Form extends ObservedObject {
id
$children
}
let object = Form.create({id: "123", children: [{path: "berry"}, {path: "blue"}]});
register(class File extends Shadow {
$$form
render = () => {
ForEach(this.form.children, (child) => {
p(child.path)
})
}
}, randomName("file"))
let file = File(object)
if(file.firstChild?.innerText !== "berry" || file.children[1].innerText !== "blue") {
return "Paths did not render correctly in children"
}
file.form.children.push({path: "hello"})
if(file.children.length !== 3) {
return "No reactivity for adding children"
}
}
NotExtensible() {
return "not done"
}
// MustInitAllFields() {
// class Form extends ObservedObject {
// id
// path
// $canvasPosition
// }
// let obj = Form.create({id: "123", path: "/", canvasPosition: "25|25"})
// if(!(obj && obj["id"] === "123" && obj["path"] === "/" && obj["canvasPosition"] === "25|25")) {
// return "Not all fields initialized!"
// }
// }
// ChangingObjChangesInstance() {
// class Form extends ObservedObject {
// id
// path
// $canvasPosition
// }
// let json = {id: "123", path: "/", canvasPosition: "25|25"}
// let obj = Form.create({id: "123", path: "/", canvasPosition: "25|25"})
// json.id = "456"
// if(!(obj["id"] === "456")) {
// return "Change to original object was not reflected!"
// }
// }
})

View File

@@ -0,0 +1,39 @@
window.testSuites.push( class testParams {
Args() {
}
Constructor() {
}
Default() {
}
// 2/3
DefaultArgs() {
}
ConstructorArgs() {
}
ConstructorDefault() {
}
// 3/3
ConstructorDefaultArgs() {
}
ConstructorDefault() {
}
})

151
Test/Skeleton/parse.test.js Normal file
View File

@@ -0,0 +1,151 @@
window.testSuites.push( class testParse {
testParseConstructor() {
class Space extends Shadow {
form
contents = []
constructor(...params) {
super(...params)
}
}
let newClass = window.Registry.parseConstructor(Space)
if(!newClass.prototype.constructor.toString().includes("window.Registry.construct(")) {
return "'window.Registry.construct(' not detected!"
}
}
testParseConstructorIfNoneProvided() {
class Space extends Shadow {
$form
}
let newClass = window.Registry.parseConstructor(Space)
if(!newClass.prototype.constructor.toString().includes("window.Registry.construct(")) {
return "'window.Registry.construct(' not detected!"
}
}
testParseConstructorFailsIfNoSuper() {
class Space extends Shadow {
form
contents = []
constructor() {
}
}
try {
let newClass = window.Registry.parseConstructor(Space)
return "No error thrown!"
} catch(e) {
}
}
testParseClassFieldsWithNoDefault() {
class Space extends Shadow {
form
contents = []
constructor(...params) {
super(...params)
}
}
const fields = window.Registry.parseClassFields(Space);
if(!(JSON.stringify(fields) === JSON.stringify(["form", "contents"]))) {
return `Fields don't match`
}
}
testParseClassFieldsWithEqualityCheck() {
class Space extends Shadow {
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(...params) {
super(...params)
}
}
const fields = window.Registry.parseClassFields(Space);
if(!(JSON.stringify(fields) === JSON.stringify(["form", "contents"]))) {
return `Fields don't match`
}
}
testParseClassFieldsWithInnerFunctionVariable() {
class Space extends Shadow {
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(...params) {
super(...params)
}
}
const fields = window.Registry.parseClassFields(Space);
if(!(JSON.stringify(fields) === JSON.stringify(["form", "contents"]))) {
return `Fields don't match`
}
}
ParseConstructorWithFunctionsBelow() {
class Space extends Shadow {
$$form = Forms.observe(window.location.pathname, this)
render = () => {
ForEach(this.form.children, (form) => {
switch(form.type) {
case "file":
File(form)
break
case "space":
ChildSpace(form)
break
case "link":
Link()
break
}
})
}
constructor() {
super()
}
connectedCallback() {
}
}
window.Registry.parseConstructor(Space)
}
})

View File

@@ -0,0 +1,60 @@
/*
"(" is preceding character: el, el.attr, if, switch
el: the el is window.rendering
el.attr:
find the function and attr in the string
if there are multiple instances of being used with this el, add to a list (and if no list then make it and we are first)
if: the el is window.rendering. rerender el
switch: the el is window.rendering. rerender el
*/
window.testSuites.push(
class ParseRender {
ParseRender() {
class Sidebar extends Shadow {
$$windowState = windowState
$$form = Forms.observe(window.location.pathname)
$sidebarPos = -200
render = () => {
VStack(() => {
ForEach(this.form.children, (form) => {
switch(form.constructor.name) {
case "File":
SidebarFile(form)
break
case "Space":
SidebarSpace(form)
break
}
})
})
.background("black")
.positionType("absolute")
.x(this.windowState.sidebarOut ? 0 : -200)
.y(0)
.width(200, "px")
.height(100, "vh")
}
}
let result = new Registry.parseRender(Sidebar).parse()
console.log(result)
let expectedOutput = "[[VStack.ForEach, form.children], [VStack.x, windowState.sidebarOut]]"
if(JSON.stringify(result) !== JSON.stringify(expectedOutput)) {
return "Result does not match expected array!"
}
}
// only functions which return a view may be called
// no addEventListener usage
}
)

126
Test/Skeleton/state.test.js Normal file
View File

@@ -0,0 +1,126 @@
window.testSuites.push( class testState {
SimpleParagraphWithState() {
class File extends Shadow {
$form
render = () => {
p(this.form.data)
}
constructor() {
super()
}
}
window.register(File, randomName("file"))
let form = {data: "asdf"}
const el = window.File(form)
if(!(el.firstChild?.matches("p"))) {
return `Child paragraph not rendered`
}
if(!(el.firstChild.innerText === "asdf")) {
return "Child para does not have inner text"
}
}
ParagraphConstructorChangeState() {
register(class File extends Shadow {
$name
render = () => {
p(this.name)
}
constructor() {
super()
}
}, randomName("file"))
let name = "asdf"
const file = File(name)
file.name = "hey123"
if(file.firstChild.innerText !== "hey123") {
return "Paragraph did not react to change!"
}
}
LiteralDoesNotCreateFalseReactivity() {
register(class File extends Shadow {
$name = "asd"
render = () => {
p(this.name)
p("asd")
}
constructor() {
super()
}
}, randomName("file"))
const file = File()
file.name = "hey123"
if(file.children[1].innerText === "hey123") {
return "Paragraph innertext falsely changed"
}
}
/*
State itself should check if the reactivity is based on an element or a standalone expression
If standalone, handle it
If element, push the info for initReactivity to handle it
*/
TernaryInState() {
register(class File extends Shadow {
$name
render = () => {
p(this.name)
.fontSize(this.name === "asdf" ? 16 : 32)
}
constructor() {
super()
}
}, randomName("file"))
let name = "asdf"
const file = File(name)
if(file.style.fontSize !== "16px") {
return "fail"
}
}
StateWorksWithCustomStyleFunctions() {
// reactive setting needs to use the actual style functions
register(class File extends Shadow {
$paraWidth = 16
render = () => {
p("guppy")
.width(this.paraWidth)
}
constructor() {
super()
}
}, randomName("file"))
const file = File()
file.paraWidth = 18
if(file.firstChild.style.width !== "18px") {
return "Width did not reactively change!"
}
}
})