Skip to content

Commit

Permalink
feat: agrega testes a todas ls utils
Browse files Browse the repository at this point in the history
  • Loading branch information
drusco committed Aug 8, 2023
1 parent a1b9119 commit 874984c
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/Emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ export default class Emulator extends EventTarget implements Exotic.Emulator {
return lib.methods.isRevoked(this, value);
}

entries(): Iterable<Exotic.Proxy> {
entries(): IterableIterator<Exotic.Proxy> {
return lib.methods.entries(this);
}

entriesBefore(value: Exotic.traceable): Iterable<Exotic.Proxy> {
entriesBefore(value: Exotic.traceable): IterableIterator<Exotic.Proxy> {
return lib.methods.entriesBefore(this, value);
}

entriesAfter(value: Exotic.traceable): Iterable<Exotic.Proxy> {
entriesAfter(value: Exotic.traceable): IterableIterator<Exotic.Proxy> {
return lib.methods.entriesAfter(this, value);
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/methods/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { proxyGenerator } from "../../utils";

export default function* entries(
scope: Exotic.Emulator,
): Iterable<Exotic.Proxy> {
): IterableIterator<Exotic.Proxy> {
for (const proxy of proxyGenerator(scope)) {
yield proxy;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/methods/entriesAfter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { findProxy, proxyGenerator } from "../../utils";
export default function* entriesAfter(
scope: Exotic.Emulator,
value: Exotic.traceable,
): Iterable<Exotic.Proxy> {
): IterableIterator<Exotic.Proxy> {
const currentProxy = findProxy(value);
for (const proxy of proxyGenerator(scope, value, false)) {
if (proxy !== currentProxy) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/methods/entriesBefore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { findProxy, proxyGenerator } from "../../utils";
export default function* entriesBefore(
scope: Exotic.Emulator,
value: Exotic.traceable,
): Iterable<Exotic.Proxy> {
): IterableIterator<Exotic.Proxy> {
const currentProxy = findProxy(value);
for (const proxy of proxyGenerator(scope, value, true)) {
if (proxy !== currentProxy) {
Expand Down
10 changes: 5 additions & 5 deletions src/types/Exotic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ declare namespace Exotic {
ownKeys(value?: traceable): key[];
revoke(value: traceable): boolean;
isRevoked(value: traceable): boolean;
entries(): Iterable<Proxy>;
entriesBefore(value: traceable): Iterable<Proxy>;
entriesAfter(value: traceable): Iterable<Proxy>;
entries(): IterableIterator<Exotic.Proxy>;
entriesBefore(value: traceable): IterableIterator<Exotic.Proxy>;
entriesAfter(value: traceable): IterableIterator<Exotic.Proxy>;
encode(value: any): Exotic.payload;
refs: key[];
active: number;
Expand All @@ -47,11 +47,11 @@ declare namespace Exotic {
interface Mock {
(...args: any[]): void;
[x: key]: any;
[Symbol.iterator](): Iterator<any>;
[Symbol.iterator](): IterableIterator<any>;
}

interface Proxy extends Mock {
[Symbol.iterator](): Iterator<Proxy>;
[Symbol.iterator](): IterableIterator<Proxy>;
}

// eslint-disable-next-line @typescript-eslint/no-namespace
Expand Down
34 changes: 30 additions & 4 deletions src/utils/createProxy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
import Emulator from "../Emulator";
import createProxy from "./createProxy";

const scope = new Emulator();
const $ = new Emulator();

describe("(function) createProxy", () => {
it("Requires a scope parameter that represents the emulator instance", () => {
const proxy = createProxy(scope);
expect(typeof proxy).toBe("function");
it("Returns an existing proxy", () => {
const proxy = $.use();
const sameProxy = createProxy($, proxy);
expect(proxy).toBe(sameProxy);
});

it("Returns an existing proxy by reference key", () => {
const refKey = "test";
const proxy = $.useRef(refKey);
const sameProxy = createProxy($, proxy, undefined, refKey);

expect(proxy).toBe(sameProxy);
});

it("Adds a new reference key", () => {
const refKey = "new";
const refKeyExisted = $.refs.includes(refKey);

createProxy($, undefined, undefined, refKey);

expect(refKeyExisted).toBe(false);
expect($.refs.includes(refKey)).toBe(true);
});

it("Adds any target value to a proxy", () => {
const target = "target";
const proxy = createProxy($, target);

expect($.target(proxy)).toBe(target);
});
});
10 changes: 5 additions & 5 deletions src/utils/createProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ const createProxy = (
// create new proxy

const id = ++data.totalProxies;
const mock = function mock() {} as Exotic.Mock;
const mock = Object.setPrototypeOf(
function mock() {},
mockPrototype,
) as Exotic.Mock;
const sandbox = Object.create(null);
const traceable = isTraceable(target);

const { proxy, revoke } = Proxy.revocable<Exotic.Proxy>(
Object.setPrototypeOf(mock, mockPrototype),
traps,
);
const { proxy, revoke } = Proxy.revocable<Exotic.Proxy>(mock, traps);

if (validRefKey) {
// create unique reference
Expand Down
29 changes: 27 additions & 2 deletions src/utils/findProxy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import Emulator from "../Emulator";
import findProxy from "./findProxy";

const $ = new Emulator();

describe("(function) findProxy", () => {
it("Runs an exotic proxy emulator", () => {
expect(true).toBeTruthy();
it("Returns undefined when a proxy is not found", () => {
const param = "notProxy_notTarget";
const proxy = findProxy(param);

expect(proxy).toBeUndefined();
});

it("Returns the same proxy if the search parameter is a proxy", () => {
const param = $.use();
const proxy = findProxy(param);

expect(proxy).not.toBeUndefined();
expect(typeof proxy).toBe("function");
expect(proxy).toBe(param);
});

it("Finds a proxy by target reference", () => {
const target = [];
const proxy = $.use(target);
const search = findProxy(target);

expect(search).toBe(proxy);
});
});
19 changes: 17 additions & 2 deletions src/utils/getPayload.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import Emulator from "../Emulator";
import getPayload from "./getPayload";

const $ = new Emulator();

describe("(function) getPayload", () => {
it("Runs an exotic proxy emulator", () => {
expect(true).toBeTruthy();
it("Returns a payload object from a proxy", () => {
const proxy = $.use();
const payload = getPayload($, proxy);

expect(payload).toEqual({ encoded: true, value: 1 });
});

it("Returns a payload object from a non proxy value", () => {
const value = null;
const payload = getPayload($, value);

expect(payload).toEqual({ encoded: false, value });
});
});
28 changes: 26 additions & 2 deletions src/utils/isiTraceable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import Emulator from "../Emulator";
import isTraceable from "./isTraceable";

const $ = new Emulator();

describe("(function) isTraceable", () => {
it("Runs an exotic proxy emulator", () => {
expect(true).toBeTruthy();
it("Returns true when the parameter is a non-null object or a function that is not a proxy", () => {
// define prossible targets
const trueTargets = [[], {}, function () {}, () => {}, function* () {}];
const falseTargets = [
null,
undefined,
"string",
$.use(),
10,
NaN,
Infinity,
true,
false,
Symbol(),
];

const traceable = trueTargets.every(isTraceable);
const untraceable = falseTargets.some(isTraceable);

expect(traceable).toBe(true);
expect(untraceable).toBe(false);
});
});
43 changes: 41 additions & 2 deletions src/utils/proxyGenerator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
import Emulator from "../Emulator";
import proxyGenerator from "./proxyGenerator";

const $ = new Emulator();

const proxyA = $.use();
const proxyB = $.use();
const proxyC = $.use();

describe("(function*) proxyGenerator", () => {
it("Runs an exotic proxy emulator", () => {
expect(true).toBeTruthy();
it("Returns an IterableIterator of all non-revoked proxies in the emulator instance", () => {
const iterator = proxyGenerator($);

expect(iterator.next().value).toBe(proxyA);
expect(iterator.next().value).toBe(proxyB);
expect(iterator.next().value).toBe(proxyC);
expect(iterator.next().done).toBe(true);
});

it("Returns an IterableIterator of all non-revoked proxies in the emulator instance in reverse order", () => {
const iterator = proxyGenerator($, undefined, true);

expect(iterator.next().value).toBe(proxyC);
expect(iterator.next().value).toBe(proxyB);
expect(iterator.next().value).toBe(proxyA);
expect(iterator.next().done).toBe(true);
});

it("Returns an IterableIterator of all non-revoked proxies in the emulator instance starting from an specific proxy in time", () => {
const iterator = proxyGenerator($, proxyB);

expect(iterator.next().value).toBe(proxyB);
expect(iterator.next().value).toBe(proxyC);
expect(iterator.next().done).toBe(true);
});

it("Returns an IterableIterator of all non-revoked proxies in the emulator instance starting from an specific proxy in time backwards", () => {
const iterator = proxyGenerator($, proxyB, true);

expect(iterator.next().value).toBe(proxyB);
expect(iterator.next().value).toBe(proxyA);
expect(iterator.next().done).toBe(true);
});
});
14 changes: 9 additions & 5 deletions src/utils/proxyGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ const proxyGenerator = function* (
scope: Exotic.Emulator,
value?: Exotic.traceable,
reverse: boolean = false,
): Iterable<Exotic.Proxy> {
): IterableIterator<Exotic.Proxy> {
if (value === undefined) {
const { firstProxy } = map.emulators.get(scope);
const { firstProxy, lastProxy } = map.emulators.get(scope);
const item = reverse ? lastProxy : firstProxy;

if (!firstProxy) return;
if (!item) return;

for (const proxy of proxyGenerator(scope, firstProxy, false)) {
for (const proxy of proxyGenerator(scope, item, reverse)) {
if (!scope.isRevoked(proxy)) {
yield proxy;
}
Expand All @@ -21,7 +22,10 @@ const proxyGenerator = function* (
}

const proxy = findProxy(value);
if (!proxy) return;

if (!proxy) {
return;
}

if (!scope.isRevoked(proxy)) {
yield proxy;
Expand Down
29 changes: 27 additions & 2 deletions src/utils/revokeProxy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import Emulator from "../Emulator";
import revokeProxy from "./revokeProxy";

const $ = new Emulator();

describe("(function) revokeProxy", () => {
it("Runs an exotic proxy emulator", () => {
expect(true).toBeTruthy();
it("Returns false when the parameter is not a proxy", () => {
const param = [100];
const isrevoked = revokeProxy($, param);

expect(isrevoked).toBe(false);
});

it("Returns true when the parameter is a proxy", () => {
const proxy = $.use();
const isrevoked = revokeProxy($, proxy);

expect(isrevoked).toBe(true);
expect(proxy).toThrow();
});

it("Returns true when the parameter is a proxy's target", () => {
const target = {};
const proxy = $.use(target);
const isrevoked = revokeProxy($, target);

expect(isrevoked).toBe(true);
expect(proxy).toThrow();
});
});

0 comments on commit 874984c

Please sign in to comment.