diff options
author | Linus Groh <mail@linusgroh.de> | 2020-12-02 09:54:55 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-12-02 12:52:31 +0100 |
commit | bb6bc70c5bda33b3c226793255b67cb15462419c (patch) | |
tree | acd36c235e06de30442309842907fc7965701258 /Libraries/LibJS/Tests/builtins | |
parent | 3d05836a3e2f622687475fab97e93e9ef40a56fe (diff) | |
download | serenity-bb6bc70c5bda33b3c226793255b67cb15462419c.zip |
LibJS: Add more tests for TypedArray
Diffstat (limited to 'Libraries/LibJS/Tests/builtins')
-rw-r--r-- | Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js b/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js new file mode 100644 index 0000000000..c78fba3a85 --- /dev/null +++ b/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js @@ -0,0 +1,60 @@ +// Update when more typed arrays get added +const TYPED_ARRAYS = [Uint8Array, Uint16Array, Uint32Array, Int8Array, Int16Array, Int32Array]; + +const getTypedArrayConstructor = () => Object.getPrototypeOf(TYPED_ARRAYS[0]); + +test("basic functionality", () => { + const TypedArray = getTypedArrayConstructor(); + expect(TypedArray).toHaveLength(0); + expect(TypedArray.name).toBe("TypedArray"); + expect(TypedArray.prototype.constructor).toBe(TypedArray); + TYPED_ARRAYS.forEach(T => { + expect(T.prototype.constructor).toBe(T); + }); + const FunctionPrototype = Object.getPrototypeOf(() => {}); + expect(Object.getPrototypeOf(TypedArray)).toBe(FunctionPrototype); +}); + +test("typed array constructors must be invoked with 'new'", () => { + TYPED_ARRAYS.forEach(T => { + expect(() => { + T(); + }).toThrowWithMessage(TypeError, `${T.name} constructor must be called with 'new'`); + }); +}); + +test("typed array constructors have TypedArray as prototype", () => { + const TypedArray = getTypedArrayConstructor(); + TYPED_ARRAYS.forEach(T => { + expect(Object.getPrototypeOf(T)).toBe(TypedArray); + }); +}); + +test("typed array prototypes have TypedArray.prototype as prototype", () => { + const TypedArray = getTypedArrayConstructor(); + TYPED_ARRAYS.forEach(T => { + const TPrototype = Object.getPrototypeOf(new T()); + expect(Object.getPrototypeOf(TPrototype)).toBe(TypedArray.prototype); + }); +}); + +test("typed arrays inherit from TypedArray", () => { + const TypedArray = getTypedArrayConstructor(); + TYPED_ARRAYS.forEach(T => { + expect(new T()).toBeInstanceOf(TypedArray); + }); +}); + +test("TypedArray is not exposed on the global object", () => { + expect(globalThis.TypedArray).toBeUndefined(); +}); + +test("TypedArray is abstract", () => { + const TypedArray = getTypedArrayConstructor(); + expect(() => { + TypedArray(); + }).toThrowWithMessage(TypeError, "Abstract class TypedArray cannot be constructed directly"); + expect(() => { + new TypedArray(); + }).toThrowWithMessage(TypeError, "Abstract class TypedArray cannot be constructed directly"); +}); |