Skip to content

Commit

Permalink
Merge pull request #2 from Pinta365/tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
Pinta365 authored Mar 13, 2024
2 parents ce64a77 + 9ecb055 commit 8b31d25
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 2 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/bun.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Bun CI

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: antongolub/[email protected]
with:
bun-version: v1.x # Uses latest bun 1
- run: bun x jsr add @cross/test @std/assert # Installs dependencies
- run: bun test # Runs the tests
28 changes: 28 additions & 0 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Deno CI

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Setup repo
uses: actions/checkout@v4

- name: Setup Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.x

- name: Verify formatting
run: deno fmt --check

- name: Run linter
run: deno lint

- name: Check types
run: deno check mod.ts

- name: Run tests
run: deno test
18 changes: 18 additions & 0 deletions .github/workflows/node.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Node.js CI

on: [push, pull_request]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 21.x]

steps:
- uses: actions/checkout@v4
- run: npx jsr add @cross/test @std/assert
- run: "echo '{ \"type\": \"module\" }' > package.json" # Needed for tsx to work
- run: npx --yes tsx --test tests/*.test.ts
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
.env
.vscode
test.ts
wip
dev
package.json
package-lock.json
deno.lock
node_modules
bun.lockb
bunfig.toml
4 changes: 3 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"exports": "./mod.ts",

"tasks": {
"test": "cd tests && deno test",
"publish-dry": "deno publish --dry-run"
},
"lock": false,
"fmt": {
"lineWidth": 120,
"indentWidth": 4
}
},
"imports": { "@cross/test": "jsr:@cross/test@^0.0.8", "@std/assert": "jsr:@std/assert@^0.219.1" }
}
85 changes: 85 additions & 0 deletions tests/deepmerge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { assertEquals } from "@std/assert";
import { test } from "@cross/test";
import { deepMerge, DeepMergeOptions } from "../mod.ts";

test("Basic deepMerge scenario", () => {
const options: DeepMergeOptions = {
arrayMergeStrategy: "unique",
setMergeStrategy: "combine",
mapMergeStrategy: "combine",
};

const obj1 = {
arr: [1, 2, 3],
mySet: new Set(["a", "b"]),
myMap: new Map([["x", 1], ["y", 2]]),
};

const obj2 = {
arr: [3, 4, 5],
mySet: new Set(["c"]),
myMap: new Map([["y", 3]]),
};

const merged = deepMerge.withOptions(options, obj1, obj2);

assertEquals(Array.from(merged.arr), [1, 2, 3, 4, 5]);
assertEquals(Array.from(merged.mySet), ["a", "b", "c"]);
assertEquals(Array.from(merged.myMap), [["x", 1], ["y", 3]]);
});

test("Array Combine", () => {
const obj1 = { arr: [1, 2, 3] };
const obj2 = { arr: [3, 4, 5] };
const merged = deepMerge.withOptions({ arrayMergeStrategy: "combine" }, obj1, obj2);

assertEquals(Array.from(merged.arr), [1, 2, 3, 3, 4, 5]);
});

test("Array Unique", () => {
const obj1 = { arr: [1, 2, 3] };
const obj2 = { arr: [3, 4, 5] };
const merged = deepMerge.withOptions({ arrayMergeStrategy: "unique" }, obj1, obj2);

assertEquals(Array.from(merged.arr), [1, 2, 3, 4, 5]);
});

test("Array Replace", () => {
const obj1 = { arr: [1, 2, 3] };
const obj2 = { arr: [3, 4, 5] };
const merged = deepMerge.withOptions({ arrayMergeStrategy: "replace" }, obj1, obj2);

assertEquals(Array.from(merged.arr), [3, 4, 5]);
});

test("Set Combine", () => {
const obj1 = { mySet: new Set(["a", "b"]) };
const obj2 = { mySet: new Set(["c"]) };
const merged = deepMerge.withOptions({ setMergeStrategy: "combine" }, obj1, obj2);

assertEquals(Array.from(merged.mySet), ["a", "b", "c"]);
});

test("Set Replace", () => {
const obj1 = { mySet: new Set(["a", "b"]) };
const obj2 = { mySet: new Set(["c"]) };
const merged = deepMerge.withOptions({ setMergeStrategy: "replace" }, obj1, obj2);

assertEquals(Array.from(merged.mySet), ["c"]);
});

test("Map Combine", () => {
const obj1 = { myMap: new Map([["x", 1], ["y", 2]]) };
const obj2 = { myMap: new Map([["y", 3]]) };
const merged = deepMerge.withOptions({ mapMergeStrategy: "combine" }, obj1, obj2);

assertEquals(Array.from(merged.myMap), [["x", 1], ["y", 3]]);
});

test("Map Replace", () => {
const obj1 = { myMap: new Map([["x", 1], ["y", 2]]) };
const obj2 = { myMap: new Map([["y", 3]]) };
const merged = deepMerge.withOptions({ mapMergeStrategy: "replace" }, obj1, obj2);

assertEquals(Array.from(merged.myMap), [["y", 3]]);
});
35 changes: 35 additions & 0 deletions tests/simplemerge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { assertEquals } from "@std/assert";
import { test } from "@cross/test";
import { simpleMerge } from "../mod.ts";

test("Basic simpleMerge scenario", () => {
interface MergeableObj {
a?: number;
b?: {
c?: number;
d?: number;
e?: number;
};
}

const object1: MergeableObj = {
a: 1,
b: {
c: 2,
d: 1,
e: 3,
},
};
const object2: MergeableObj = {
b: {
d: 3,
},
};

const merged = simpleMerge(object1, object2);
assertEquals(merged.a, 1);
assertEquals(typeof merged.b, "object");
assertEquals(merged.b!.c, 2);
assertEquals(merged.b!.d, 3);
assertEquals(merged.b!.e, 3);
});

0 comments on commit 8b31d25

Please sign in to comment.