summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests
diff options
context:
space:
mode:
authordavidot <david.tuin@gmail.com>2021-06-13 18:09:34 +0200
committerLinus Groh <mail@linusgroh.de>2021-06-14 09:57:06 +0100
commit6c13cc67c6391806b5e8345a02e247a796b5bd51 (patch)
tree1597534eb4c0dac279c999078c16df72394a9896 /Userland/Libraries/LibJS/Tests
parent417f7523066be0c836c324e704dbaa0666f7fc6c (diff)
downloadserenity-6c13cc67c6391806b5e8345a02e247a796b5bd51.zip
LibJS: Implement Array.prototype.copyWithin generically
Diffstat (limited to 'Userland/Libraries/LibJS/Tests')
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js29
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.copyWithin.js45
2 files changed, 74 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js
index af1b189834..c509ea3056 100644
--- a/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js
+++ b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype-generic-functions.js
@@ -130,6 +130,35 @@ describe("ability to work with generic non-array objects", () => {
}
});
+ test("copyWithin", () => {
+ const initial_o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
+ {
+ const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
+ // returns value and modifies
+ expect(Array.prototype.copyWithin.call(o, 0, 0)).toEqual(o);
+ expect(o).toEqual(initial_o);
+ }
+
+ {
+ const o = {};
+ expect(Array.prototype.copyWithin.call(o, 1, 16, 32)).toEqual(o);
+ expect(o).toEqual({});
+ }
+
+ {
+ const o = { length: 100 };
+ expect(Array.prototype.copyWithin.call(o, 1, 16, 32)).toEqual(o);
+ expect(o).toEqual({ length: 100 });
+ }
+
+ {
+ const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
+ // returns value and modifies
+ expect(Array.prototype.copyWithin.call(o, 2, 0)).toEqual(o);
+ expect(o).toEqual({ length: 5, 0: "foo", 1: "bar", 2: "foo", 3: "bar" });
+ }
+ });
+
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
test("every", () => {
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.copyWithin.js b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.copyWithin.js
new file mode 100644
index 0000000000..831269574a
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.copyWithin.js
@@ -0,0 +1,45 @@
+test("length is 2", () => {
+ expect(Array.prototype.copyWithin).toHaveLength(2);
+});
+
+describe("normal behavior", () => {
+ test("Noop", () => {
+ var array = [1, 2];
+ array.copyWithin(0, 0);
+ expect(array).toEqual([1, 2]);
+ });
+
+ test("basic behavior", () => {
+ var array = [1, 2, 3];
+
+ var b = array.copyWithin(1, 2);
+ expect(b).toEqual(array);
+ expect(array).toEqual([1, 3, 3]);
+
+ b = array.copyWithin(2, 0);
+ expect(b).toEqual(array);
+ expect(array).toEqual([1, 3, 1]);
+ });
+
+ test("start > target", () => {
+ var array = [1, 2, 3];
+ var b = array.copyWithin(0, 1);
+ expect(b).toEqual(array);
+ expect(array).toEqual([2, 3, 3]);
+ });
+
+ test("overwriting behavior", () => {
+ var array = [1, 2, 3];
+ var b = array.copyWithin(1, 0);
+ expect(b).toEqual(array);
+ expect(array).toEqual([1, 1, 2]);
+ });
+
+ test("specify end", () => {
+ var array = [1, 2, 3];
+
+ b = array.copyWithin(2, 0, 1);
+ expect(b).toEqual(array);
+ expect(array).toEqual([1, 2, 1]);
+ });
+});