diff options
author | davidot <david.tuin@gmail.com> | 2021-08-28 17:11:05 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-09-01 13:39:14 +0100 |
commit | def8b44c4083f2d79bdb7b44701d6a353cb52394 (patch) | |
tree | aad665b8885c15f53acd6aa35048d17b867bfcb9 /Userland/Libraries/LibJS/Tests | |
parent | 3b6a8d1d53e4d530ff827e2ab3e61388ff62cb8b (diff) | |
download | serenity-def8b44c4083f2d79bdb7b44701d6a353cb52394.zip |
LibJS: Add support for public fields in classes
Diffstat (limited to 'Userland/Libraries/LibJS/Tests')
-rw-r--r-- | Userland/Libraries/LibJS/Tests/classes/class-public-fields.js | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/classes/class-public-fields.js b/Userland/Libraries/LibJS/Tests/classes/class-public-fields.js new file mode 100644 index 0000000000..86c1e86de7 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/classes/class-public-fields.js @@ -0,0 +1,87 @@ +test("basic functionality", () => { + class A { + number = 3; + + string = "foo"; + + uninitialized; + } + + const a = new A(); + expect(a.number).toBe(3); + expect(a.string).toBe("foo"); + expect(a.uninitialized).toBeUndefined(); +}); + +test("extended name syntax", () => { + class A { + "field with space" = 1; + + 12 = "twelve"; + + [`he${"llo"}`] = 3; + } + + const a = new A(); + expect(a["field with space"]).toBe(1); + expect(a[12]).toBe("twelve"); + expect(a.hello).toBe(3); +}); + +test("initializer has correct this value", () => { + class A { + this_val = this; + + this_name = this.this_val; + } + + const a = new A(); + expect(a.this_val).toBe(a); + expect(a.this_name).toBe(a); +}); + +test("static fields", () => { + class A { + static simple = 1; + simple = 2; + + static "with space" = 3; + + static 24 = "two dozen"; + + static [`he${"llo"}`] = "friends"; + + static this_val = this; + static this_name = this.name; + static this_val2 = this.this_val; + } + + expect(A.simple).toBe(1); + expect(A["with space"]).toBe(3); + expect(A[24]).toBe("two dozen"); + expect(A.hello).toBe("friends"); + + expect(A.this_val).toBe(A); + expect(A.this_name).toBe("A"); + expect(A.this_val2).toBe(A); + + const a = new A(); + expect(a.simple).toBe(2); +}); + +test("with super class", () => { + class A { + super_field = 3; + } + + class B extends A { + references_super_field = super.super_field; + arrow_ref_super = () => (super.super_field = 4); + } + + const b = new B(); + expect(b.super_field).toBe(3); + expect(b.references_super_field).toBeUndefined(); + b.arrow_ref_super(); + expect(b.super_field).toBe(4); +}); |