Skip to content

Commit

Permalink
replaced hardcoded localIds in tests with a helper function to grab a…
Browse files Browse the repository at this point in the history
… localId in the elm structure
  • Loading branch information
hossenlopp committed Jul 17, 2024
1 parent 8c5ac66 commit ec156ee
Showing 1 changed file with 107 additions and 24 deletions.
131 changes: 107 additions & 24 deletions test/elm/list/list-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,37 +671,51 @@ describe('First', () => {

it('should get first of a list of numbers and the localId should exist on the root context', async function () {
(await this.numbers.exec(this.ctx)).should.equal(1);
should(this.ctx.localId_context[213]).not.be.undefined();
const listLocalId = getLocalIdByPath('First', 'Numbers', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should get first of a list of letters and the localId should exist on the root context', async function () {
(await this.letters.exec(this.ctx)).should.equal('a');
should(this.ctx.localId_context[223]).not.be.undefined();
const listLocalId = getLocalIdByPath('First', 'Letters', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should get first of a list of lists and the localId should exist on the root context', async function () {
(await this.lists.exec(this.ctx)).should.eql(['a', 'b', 'c']);
should(this.ctx.localId_context[232]).not.be.undefined();
const listLocalId = getLocalIdByPath('First', 'Lists', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should get first of a list of tuples and the localId should exist on the root context', async function () {
(await this.tuples.exec(this.ctx)).should.eql({ a: 1, b: 2, c: 3 });
should(this.ctx.localId_context[246]).not.be.undefined();
const listLocalId = getLocalIdByPath('First', 'Tuples', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should get first of a list of unordered numbers and the localId should exist on the root context', async function () {
(await this.unordered.exec(this.ctx)).should.equal(3);
should(this.ctx.localId_context[260]).not.be.undefined();
const listLocalId = getLocalIdByPath('First', 'Unordered', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should return null for an empty list and the localId should exist on the root context', async function () {
should(await this.empty.exec(this.ctx)).be.null();
should(this.ctx.localId_context[271]).not.be.undefined();
const listLocalId = getLocalIdByPath('First', 'Empty', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should return null for a null list and the localId should exist on the root context', async function () {
should(await this.nullValue.exec(this.ctx)).be.null();
should(this.ctx.localId_context[278]).not.be.undefined();
const asLocalId = getLocalIdByPath('First', 'NullValue', 'source');
should(asLocalId).not.be.null();
should(this.ctx.localId_context[asLocalId]).not.be.undefined();
});
});

Expand All @@ -712,37 +726,51 @@ describe('Last', () => {

it('should get last of a list of numbers and the localId should exist on the root context', async function () {
(await this.numbers.exec(this.ctx)).should.equal(4);
should(this.ctx.localId_context[213]).not.be.undefined();
const listLocalId = getLocalIdByPath('Last', 'Numbers', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should get last of a list of letters and the localId should exist on the root context', async function () {
(await this.letters.exec(this.ctx)).should.equal('c');
should(this.ctx.localId_context[223]).not.be.undefined();
const listLocalId = getLocalIdByPath('Last', 'Letters', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should get last of a list of lists and the localId should exist on the root context', async function () {
(await this.lists.exec(this.ctx)).should.eql(['d', 'e', 'f']);
should(this.ctx.localId_context[232]).not.be.undefined();
const listLocalId = getLocalIdByPath('Last', 'Lists', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should get last of a list of tuples and the localId should exist on the root context', async function () {
(await this.tuples.exec(this.ctx)).should.eql({ a: 24, b: 25, c: 26 });
should(this.ctx.localId_context[246]).not.be.undefined();
const listLocalId = getLocalIdByPath('Last', 'Tuples', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should get last of a list of unordered numbers and the localId should exist on the root context', async function () {
(await this.unordered.exec(this.ctx)).should.equal(2);
should(this.ctx.localId_context[260]).not.be.undefined();
const listLocalId = getLocalIdByPath('Last', 'Unordered', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should return null for an empty list and the localId should exist on the root context', async function () {
should(await this.empty.exec(this.ctx)).be.null();
should(this.ctx.localId_context[271]).not.be.undefined();
const listLocalId = getLocalIdByPath('Last', 'Empty', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should return null for a null list and the localId should exist on the root context', async function () {
should(await this.nullValue.exec(this.ctx)).be.null();
should(this.ctx.localId_context[278]).not.be.undefined();
const asLocalId = getLocalIdByPath('Last', 'NullValue', 'source');
should(asLocalId).not.be.null();
should(this.ctx.localId_context[asLocalId]).not.be.undefined();
});
});

Expand Down Expand Up @@ -797,19 +825,29 @@ describe('Skip', () => {

it('should skip two elements and the localId of the source and the startIndex should exist on the root context', async function () {
(await this.skip2.exec(this.ctx)).should.eql([3, 4, 5]);
should(this.ctx.localId_context[213]).not.be.undefined();
should(this.ctx.localId_context[219]).not.be.undefined();
const listLocalId = getLocalIdByPath('Skip', 'Skip2', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
const literalLocalId = getLocalIdByPath('Skip', 'Skip2', 'startIndex');
should(literalLocalId).not.be.null();
should(this.ctx.localId_context[literalLocalId]).not.be.undefined();
});

it('should not skip when using null and the localId of the source should exist on the root context', async function () {
(await this.skipNull.exec(this.ctx)).should.eql([1, 3, 5]);
should(this.ctx.localId_context[226]).not.be.undefined();
const listLocalId = getLocalIdByPath('Skip', 'SkipNull', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should return empty list when using negative number and the localId of the source and the startIndex should exist on the root context', async function () {
(await this.skipEmpty.exec(this.ctx)).should.eql([]);
should(this.ctx.localId_context[239]).not.be.undefined();
should(this.ctx.localId_context[243]).not.be.undefined();
const listLocalId = getLocalIdByPath('Skip', 'SkipEmpty', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
const negateLocalId = getLocalIdByPath('Skip', 'SkipEmpty', 'startIndex');
should(negateLocalId).not.be.null();
should(this.ctx.localId_context[negateLocalId]).not.be.undefined();
});

it('should return null when given null', async function () {
Expand All @@ -824,12 +862,16 @@ describe('Tail', () => {

it('should get tail of list and the localId of the source should exist on the root context', async function () {
(await this.tail234.exec(this.ctx)).should.eql([2, 3, 4]);
should(this.ctx.localId_context[213]).not.be.undefined();
const listLocalId = getLocalIdByPath('Tail', 'Tail234', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should return empty list when given empty list and the localId of the source should exist on the root context', async function () {
(await this.tailEmpty.exec(this.ctx)).should.eql([]);
should(this.ctx.localId_context[225]).not.be.undefined();
const listLocalId = getLocalIdByPath('Tail', 'TailEmpty', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should return null when given null', async function () {
Expand All @@ -844,20 +886,61 @@ describe('Take', () => {

it('should take two elements and the localId of the source should exist on the root context', async function () {
(await this.take2.exec(this.ctx)).should.eql([1, 2]);
should(this.ctx.localId_context[213]).not.be.undefined();
const listLocalId = getLocalIdByPath('Take', 'Take2', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should return full list when asked for too many elements and the localId of the source should exist on the root context', async function () {
(await this.takeTooMany.exec(this.ctx)).should.eql([1, 2]);
should(this.ctx.localId_context[227]).not.be.undefined();
const listLocalId = getLocalIdByPath('Take', 'TakeTooMany', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should return empty list when using null and the localId of the source should exist on the root context', async function () {
(await this.takeEmpty.exec(this.ctx)).should.eql([]);
should(this.ctx.localId_context[239]).not.be.undefined();
const listLocalId = getLocalIdByPath('Take', 'TakeEmpty', 'source');
should(listLocalId).not.be.null();
should(this.ctx.localId_context[listLocalId]).not.be.undefined();
});

it('should return null when given null', async function () {
should(await this.takeIsNull.exec(this.ctx)).be.null();
});
});

/**
* Helper function to get the localId of an ELM clause by it's path under a given test name
* statement. Returns the localId if found or null otherwise.
*/
function getLocalIdByPath(
testName: string,
statementName: string,
...path: string[]
): string | null {
const statementExpression = data[testName].library.statements.def.find(
(d: any) => d.name === statementName
).expression;
const expression = findExpressionByPath(statementExpression, ...path);
if (expression?.localId) {
return expression.localId;
} else {
return null;
}
}

/**
* Helper function to navigate a structure by a given path.
*/
function findExpressionByPath(expression: any, ...path: string[]) {
let exp = expression;
for (const p of path) {
if (exp[p]) {
exp = exp[p];
} else {
return;
}
}
return exp;
}

0 comments on commit ec156ee

Please sign in to comment.