Skip to content

Commit

Permalink
chore: add tests for DecodeableMap
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel committed Aug 16, 2023
1 parent 9d24d82 commit acec124
Showing 1 changed file with 168 additions and 0 deletions.
168 changes: 168 additions & 0 deletions test/collections/decodeableMap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { expect } from 'chai';
import * as sinon from 'sinon';
import { DecodeableMap } from '../../src/collections/decodeableMap';

describe('DecodeableMap', () => {
let dMap: DecodeableMap<string, string>;
const ENCODED_KEY = 'encodedKey';
const DECODED_KEY = 'decodedKey';

const sandbox = sinon.createSandbox();
let hasDecodedSpy: sinon.SinonSpy;
let getDecodedSpy: sinon.SinonSpy;
let hasMapSpy: sinon.SinonSpy;
let getMapSpy: sinon.SinonSpy;
let setMapSpy: sinon.SinonSpy;
let deleteMapSpy: sinon.SinonSpy;

beforeEach(() => {
dMap = new DecodeableMap([
['Layout-v1%2E1 Layout', ENCODED_KEY],
['Layout-v9.2 Layout', DECODED_KEY],
]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
hasDecodedSpy = sandbox.spy(dMap, 'hasDecoded' as any);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getDecodedSpy = sandbox.spy(dMap, 'getDecoded' as any);
hasMapSpy = sandbox.spy(Map.prototype, 'has');
getMapSpy = sandbox.spy(Map.prototype, 'get');
setMapSpy = sandbox.spy(Map.prototype, 'set');
deleteMapSpy = sandbox.spy(Map.prototype, 'delete');
});

afterEach(() => {
sandbox.restore();
});

describe('has()', () => {
it('should match on exact key without decoding', () => {
expect(dMap.has('Layout-v1%2E1 Layout')).to.be.true;
expect(hasMapSpy.called).to.be.true;
expect(hasDecodedSpy.called).to.be.false;
});

it('should match encoded key with decoded value', () => {
expect(dMap.has('Layout-v1.1 Layout')).to.be.true;
expect(hasMapSpy.called).to.be.true;
expect(hasDecodedSpy.called).to.be.true;
});

it('should match decoded key with encoded value', () => {
expect(dMap.has('Layout-v9%2E2 Layout')).to.be.true;
expect(hasMapSpy.called).to.be.true;
expect(hasDecodedSpy.called).to.be.true;
});

it('should not match on no existing key', () => {
expect(dMap.has('Layout-MISSING Layout')).to.be.false;
expect(hasMapSpy.called).to.be.true;
expect(hasDecodedSpy.called).to.be.true;
});
});

describe('get()', () => {
it('should get value with exact key without decoding', () => {
expect(dMap.get('Layout-v1%2E1 Layout')).to.equal(ENCODED_KEY);
expect(getMapSpy.calledOnce).to.be.true;
expect(getDecodedSpy.called).to.be.false;
});

it('should get value of encoded key using decoded key', () => {
expect(dMap.get('Layout-v1.1 Layout')).to.equal(ENCODED_KEY);
expect(getMapSpy.calledTwice).to.be.true;
expect(getDecodedSpy.calledOnce).to.be.true;
});

it('should get value of decoded key using encoded key', () => {
expect(dMap.get('Layout-v9%2E2 Layout')).to.equal(DECODED_KEY);
expect(getMapSpy.calledTwice).to.be.true;
expect(getDecodedSpy.calledOnce).to.be.true;
});

it('should return undefined on no existing key', () => {
expect(dMap.get('Layout-MISSING Layout')).to.be.undefined;
expect(getMapSpy.calledOnce).to.be.true;
expect(getDecodedSpy.called).to.be.true;
});
});

describe('set()', () => {
const NEW_VALUE = 'new value from set';

it('should set value with exact key', () => {
expect(dMap.set('Layout-v1%2E1 Layout', NEW_VALUE)).to.equal(dMap);
expect(setMapSpy.called).to.be.true;
expect(setMapSpy.lastCall.args[0]).to.equal('Layout-v1%2E1 Layout');
expect(setMapSpy.lastCall.args[1]).to.equal(NEW_VALUE);
expect(dMap.size).to.equal(2);
expect(dMap.get('Layout-v1%2E1 Layout')).to.equal(NEW_VALUE);
});

it('should set value of encoded key using decoded key', () => {
expect(dMap.set('Layout-v1.1 Layout', NEW_VALUE)).to.equal(dMap);
expect(setMapSpy.called).to.be.true;
expect(setMapSpy.lastCall.args[0]).to.equal('Layout-v1%2E1 Layout');
expect(setMapSpy.lastCall.args[1]).to.equal(NEW_VALUE);
expect(dMap.size).to.equal(2);
expect(dMap.get('Layout-v1%2E1 Layout')).to.equal(NEW_VALUE);
});

it('should set value of decoded key using encoded key', () => {
expect(dMap.set('Layout-v9%2E2 Layout', NEW_VALUE)).to.equal(dMap);
expect(setMapSpy.called).to.be.true;
expect(setMapSpy.lastCall.args[0]).to.equal('Layout-v9.2 Layout');
expect(setMapSpy.lastCall.args[1]).to.equal(NEW_VALUE);
expect(dMap.size).to.equal(2);
expect(dMap.get('Layout-v9.2 Layout')).to.equal(NEW_VALUE);
});

it('should set new entry on no existing key', () => {
expect(dMap.set('Layout-MISSING Layout', NEW_VALUE)).to.equal(dMap);
expect(setMapSpy.called).to.be.true;
expect(setMapSpy.lastCall.args[0]).to.equal('Layout-MISSING Layout');
expect(setMapSpy.lastCall.args[1]).to.equal(NEW_VALUE);
expect(dMap.size).to.equal(3);
expect(dMap.get('Layout-MISSING Layout')).to.equal(NEW_VALUE);
});
});

describe('delete()', () => {
it('should delete using exact key', () => {
expect(dMap.delete('Layout-v1%2E1 Layout')).to.be.true;
expect(deleteMapSpy.calledOnce).to.be.true;
expect(deleteMapSpy.firstCall.args[0]).to.equal('Layout-v1%2E1 Layout');
expect(dMap.size).to.equal(1);
expect(dMap.has('Layout-v1%2E1 Layout')).to.be.false;
});

it('should delete the encoded key using decoded value', () => {
expect(dMap.delete('Layout-v1.1 Layout')).to.be.true;
expect(deleteMapSpy.calledOnce).to.be.true;
expect(deleteMapSpy.firstCall.args[0]).to.equal('Layout-v1%2E1 Layout');
expect(dMap.size).to.equal(1);
expect(dMap.has('Layout-v1.1 Layout')).to.be.false;
});

it('should delete the decoded key using encoded value', () => {
expect(dMap.delete('Layout-v9%2E2 Layout')).to.be.true;
expect(deleteMapSpy.calledOnce).to.be.true;
expect(deleteMapSpy.firstCall.args[0]).to.equal('Layout-v9.2 Layout');
expect(dMap.size).to.equal(1);
expect(dMap.has('Layout-v9%2E2 Layout')).to.be.false;
});

it('should not delete on no existing key', () => {
expect(dMap.delete('Layout-MISSING Layout')).to.be.false;
expect(deleteMapSpy.calledOnce).to.be.true;
expect(deleteMapSpy.firstCall.args[0]).to.equal('Layout-MISSING Layout');
expect(dMap.size).to.equal(2);
expect(dMap.has('Layout-MISSING Layout')).to.be.false;
});
});
});

1 comment on commit acec124

@svc-cli-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: acec124 Previous: 9d24d82 Ratio
eda-componentSetCreate-linux 2801 ms 2307 ms 1.21
eda-sourceToMdapi-linux 10353 ms 8275 ms 1.25
eda-sourceToZip-linux 8900 ms 7539 ms 1.18
eda-mdapiToSource-linux 6675 ms 5123 ms 1.30
lotsOfClasses-componentSetCreate-linux 22544 ms 17776 ms 1.27
lotsOfClasses-sourceToMdapi-linux 31746 ms 25207 ms 1.26
lotsOfClasses-sourceToZip-linux 28996 ms 23512 ms 1.23
lotsOfClasses-mdapiToSource-linux 25074 ms 20931 ms 1.20
lotsOfClassesOneDir-componentSetCreate-linux 75735 ms 65913 ms 1.15
lotsOfClassesOneDir-sourceToMdapi-linux 85504 ms 74781 ms 1.14
lotsOfClassesOneDir-sourceToZip-linux 82965 ms 74046 ms 1.12
lotsOfClassesOneDir-mdapiToSource-linux 79450 ms 70466 ms 1.13

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.