Skip to content

Commit 5986a31

Browse files
author
jared
committed
Code cleanup
1 parent 7ceac2c commit 5986a31

File tree

6 files changed

+49
-29
lines changed

6 files changed

+49
-29
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
# Ignore the autogenerated files from flake-lang.nix
12
.extra-dependencies
23
data
4+
node_modules

testsuites/lbt-plutus/lbt-plutus-typescript/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"author": "Jared Pon",
1515
"license": "ISC",
1616
"files": [
17-
"./dist/**/*"
17+
"./dist/**/*",
18+
"./.extra-dependencies/**/*"
1819
],
1920
"devDependencies": {
2021
"@types/node": "^20.11.7",

testsuites/lbt-plutus/lbt-plutus-typescript/src/Utils.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import * as Fs from "node:fs/promises";
44
import * as Path from "node:path";
55

66
// WARNING(jaredponn): {@link findGoldens} and {@link fromToGoldenTest} are
7-
// essentially duplicated code (well minor generalizations) of the same testing functions in
7+
// essentially duplicated code (well minor generalizations) of the same testing
8+
// functions in the lbt-prelude testsuite.
89

910
/**
1011
/* @param goldenDir: directory for the golden files
1112
/* @param regexFileFilter: see {@link RegExpFileFilter}.
13+
*
14+
/* @returns Tuple of [path to golden file, the resulting "regexFileFilter"ed file]
1215
*/
1316
export async function findGoldens(
1417
goldenDir: string,
@@ -61,6 +64,13 @@ export class RegExpFileFilter {
6164
}
6265
}
6366

67+
/**
68+
* Runs golden tests in the provided `goldenDir` which satisfy the
69+
* `regexFileFilter` where the test passes if `assertGolden` does not throw an
70+
* exception. Note that `goldens` is essentially unused and is only used to
71+
* warn if the number of the TS representation of equivalent HS generated tests
72+
* match.
73+
*/
6474
export async function fromToGoldenTest<A>(
6575
goldenDir: string,
6676
regexFileFilter: RegExpFileFilter,
@@ -78,26 +88,31 @@ export async function fromToGoldenTest<A>(
7888

7989
if (foundGoldens.length !== goldens.length) {
8090
const errMsg =
81-
`lbt-plutus-typescript: warning: expected to find ${goldens.length} golden files for ${regexFileFilter} in ${goldenDir}, but got ${foundGoldens.length}. Forgot to (re)generate the goldens? Or there is a mismatch between the TS goldens and generated Haskell goldens`;
91+
`lbt-plutus-typescript: warning: expected to find ${goldens.length} golden files for ${regexFileFilter} in ${goldenDir}, but got ${foundGoldens.length}. Forgot to (re)generate the goldens? Or there is a mismatch in the number of TS goldens and generated Haskell goldens`;
8292
console.warn(errMsg); // TODO(jaredponn): apparently there is a mismatch between the TS goldens
83-
// and the HS goldens.. The HS script apparently only likes outputting 10
84-
// golden tests when there are clearly more for things like e.g.
85-
// PlutusData.
93+
// and the HS goldens.. The HS script apparently only likes outputting at most 10
94+
// golden tests when there are clearly more tests (e.g.
95+
// PlutusData has many more tests than just 10)
8696
// One day, add this back in the future:
8797
// ```
8898
// assert.fail(errMsg)
8999
// ```
100+
// Note this doesn't actually effect the correctness of the tests.
90101
}
91102

92103
for (const [filepath, index] of foundGoldens) {
93-
const contents = await Fs.readFile(filepath, { encoding: "utf8" });
94-
95-
it(`${index}: at ${filepath}`, () => {
104+
await it(`${index} at ${filepath}`, async () => {
105+
const contents = await Fs.readFile(filepath, { encoding: "utf8" });
96106
assertGolden(index, contents);
97107
});
98108
}
99109
}
100110

111+
/**
112+
* @returns a function which throws an exception if
113+
* `serialise(to(from(deserialise(contents)))) != contents` (or any of
114+
* `serialise`, `to`, `from`, `deserialise` throws an exception)
115+
*/
101116
export function mkFromToAssertGolden<A, B>(
102117
deserialise: (contents: string) => A,
103118
from: (a: A) => B,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
# Ignore the autogenerated files from flake-lang.nix
12
.extra-dependencies
23
data
4+
node_modules

testsuites/lbt-prelude/lbt-prelude-typescript/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"author": "Jared Pon",
1515
"license": "ISC",
1616
"files": [
17-
"./dist/**/*"
17+
"./dist/**/*",
18+
"./.extra-dependencies/**/*"
1819
],
1920
"devDependencies": {
2021
"@types/node": "^20.11.7",

testsuites/lbt-prelude/lbt-prelude-typescript/src/Json-test.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,33 @@ export async function fromToGoldenTest<A>(
4949
}
5050

5151
for (const [filepath, index] of foundGoldens) {
52-
const contents = await Fs.readFile(filepath, { encoding: "utf8" });
52+
await it(`${index} at ${filepath}`, async () => {
53+
const contents = await Fs.readFile(filepath, { encoding: "utf8" });
5354

54-
try {
55-
const fromToJson = PreludeJson.stringify(
56-
jsonDict.toJson(
57-
jsonDict.fromJson(
58-
PreludeJson.parseJson(contents),
55+
try {
56+
const fromToJson = PreludeJson.stringify(
57+
jsonDict.toJson(
58+
jsonDict.fromJson(
59+
PreludeJson.parseJson(contents),
60+
),
5961
),
60-
),
61-
);
62+
);
6263

63-
// TODO(jaredponn). This fails on the _first_ test instead of
64-
// accumulating all of them.. Perhaps we want to accumulate all of
65-
// them by e.g. wrapping this with `it`.
66-
if (contents !== fromToJson) {
64+
if (contents !== fromToJson) {
65+
assert.fail(
66+
`Golden test failed for ${index}. Expected:\n\`${contents}\`\nbut got\n\`${fromToJson}\``,
67+
);
68+
}
69+
} catch (err) {
6770
assert.fail(
68-
`Golden test failed for ${index}. Expected:\n\`${contents}\`\nbut got\n\`${fromToJson}\``,
71+
`Golden test failed for ${index} since an error was thrown: \`${err}\`.`,
6972
);
7073
}
71-
} catch (err) {
72-
assert.fail(
73-
`Golden test failed for ${index} since an error was thrown: \`${err}\`.`,
74-
);
75-
}
74+
});
7675
}
7776
}
7877

79-
describe("JSON tests", () => {
78+
describe("JSON tests (toJson . fromJson)", () => {
8079
const goldenDir = `data/lbt-prelude-golden-data`;
8180
it(`Foo.A from to golden tests`, async () => {
8281
await fromToGoldenTest(

0 commit comments

Comments
 (0)