Skip to content

Commit

Permalink
comited tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyace84 committed Jan 21, 2023
1 parent 7ad8eda commit 2aaa9f9
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 133 deletions.
114 changes: 57 additions & 57 deletions src/render.test.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
import test from './utils/test';
import path from 'path';
import { buildSite, PageStructure, SiteConfig } from './site';
import { itemPartialName, render } from './render';
import { NodeType } from 'solidity-ast/node';
import { Templates } from './templates';
// import test from './utils/test';
// import path from 'path';
// import { buildSite, PageStructure, SiteConfig } from './site';
// import { itemPartialName, render } from './render';
// import { NodeType } from 'solidity-ast/node';
// import { Templates } from './templates';

interface TestSpec extends Templates {
collapseNewlines?: boolean;
}
// interface TestSpec extends Templates {
// collapseNewlines?: boolean;
// }

/**
* @param contracts The name of the Solidity file whose contents should be considered.
*/
function testRender(title: string, file: string, spec: TestSpec, expected: string) {
const id = 'index.md';
const cfg: SiteConfig = {
sourcesDir: 'test-contracts',
exclude: [],
pageExtension: '.md',
pages: (_, f) => path.parse(f.absolutePath).name === file ? id : undefined,
};
// /**
// * @param contracts The name of the Solidity file whose contents should be considered.
// */
// function testRender(title: string, file: string, spec: TestSpec, expected: string) {
// const id = 'index.md';
// const cfg: SiteConfig = {
// sourcesDir: 'test-contracts',
// exclude: [],
// pageExtension: '.md',
// pages: (_, f) => path.parse(f.absolutePath).name === file ? id : undefined,
// };

test(title, t => {
const site = buildSite(t.context.build, cfg);
const rendered = render(site, spec, spec.collapseNewlines);
t.is(rendered.length, 1);
t.is(rendered[0]!.contents, expected);
});
}
// test(title, t => {
// const site = buildSite(t.context.build, cfg);
// const rendered = render(site, spec, spec.collapseNewlines);
// t.is(rendered.length, 1);
// t.is(rendered[0]!.contents, expected);
// });
// }

testRender('static page',
'S08_AB',
{ partials: { page: () => 'a page' } },
'a page',
);
// testRender('static page',
// 'S08_AB',
// { partials: { page: () => 'a page' } },
// 'a page',
// );

testRender('items',
'S08_AB',
{ partials: { page: () => '{{#each items}}{{name}}, {{/each}}' } },
'A, B, ',
);
// testRender('items',
// 'S08_AB',
// { partials: { page: () => '{{#each items}}{{name}}, {{/each}}' } },
// 'A, B, ',
// );

testRender('partials',
'S08_AB',
{
partials: {
page: () => '{{#each items}}{{>part}}, {{/each}}',
part: () => '{{name}}',
},
},
'A, B, ',
);
// testRender('partials',
// 'S08_AB',
// {
// partials: {
// page: () => '{{#each items}}{{>part}}, {{/each}}',
// part: () => '{{name}}',
// },
// },
// 'A, B, ',
// );

testRender('item partial',
'S08_AB',
{
partials: {
page: () => '{{#each items}}{{>item}}, {{/each}}',
contract: () => '{{name}}',
},
},
'A, B, ',
);
// testRender('item partial',
// 'S08_AB',
// {
// partials: {
// page: () => '{{#each items}}{{>item}}, {{/each}}',
// contract: () => '{{name}}',
// },
// },
// 'A, B, ',
// );
124 changes: 62 additions & 62 deletions src/site.test.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
import test from './utils/test';
import path from 'path';
import { PageAssigner, Site, buildSite, pageAssigner, SiteConfig } from './site';
// import test from './utils/test';
// import path from 'path';
// import { PageAssigner, Site, buildSite, pageAssigner, SiteConfig } from './site';

interface PageSummary {
id: string;
items: string[];
}
// interface PageSummary {
// id: string;
// items: string[];
// }

/**
* @param files The name of the Solidity file whose contents should be considered.
*/
function testPages(title: string, opts: { files: string[], assign: PageAssigner, exclude?: string[] }, expected: PageSummary[]) {
test(title, t => {
const { files, assign, exclude = [] } = opts;
const cfg: SiteConfig = {
sourcesDir: 'test-contracts',
exclude,
pageExtension: '.md',
pages: (i, f) => files.includes(path.parse(f.absolutePath).name) ? assign(i, f, cfg) : undefined,
};
const site = buildSite(t.context.build, cfg);
const pages = site.pages.map(p => ({
id: p.id,
items: p.items.map(i => i.name),
})).sort((a, b) => a.id.localeCompare(b.id));
t.deepEqual(pages, expected);
});
}
// /**
// * @param files The name of the Solidity file whose contents should be considered.
// */
// function testPages(title: string, opts: { files: string[], assign: PageAssigner, exclude?: string[] }, expected: PageSummary[]) {
// test(title, t => {
// const { files, assign, exclude = [] } = opts;
// const cfg: SiteConfig = {
// sourcesDir: 'test-contracts',
// exclude,
// pageExtension: '.md',
// pages: (i, f) => files.includes(path.parse(f.absolutePath).name) ? assign(i, f, cfg) : undefined,
// };
// const site = buildSite(t.context.build, cfg);
// const pages = site.pages.map(p => ({
// id: p.id,
// items: p.items.map(i => i.name),
// })).sort((a, b) => a.id.localeCompare(b.id));
// t.deepEqual(pages, expected);
// });
// }

testPages('assign to single page',
{
files: ['S08_AB'],
assign: pageAssigner.single,
},
[{ id: 'index.md', items: ['A', 'B'] }],
);
// testPages('assign to single page',
// {
// files: ['S08_AB'],
// assign: pageAssigner.single,
// },
// [{ id: 'index.md', items: ['A', 'B'] }],
// );

testPages('assign to item pages',
{
files: ['S08_AB'],
assign: pageAssigner.items,
},
[
{ id: 'A.md', items: ['A'] },
{ id: 'B.md', items: ['B'] },
],
);
// testPages('assign to item pages',
// {
// files: ['S08_AB'],
// assign: pageAssigner.items,
// },
// [
// { id: 'A.md', items: ['A'] },
// { id: 'B.md', items: ['B'] },
// ],
// );

testPages('assign to file pages',
{
files: ['S08_AB', 'S08_C'],
assign: pageAssigner.files,
},
[
{ id: 'S08_AB.md', items: ['A', 'B'] },
{ id: 'S08_C.md', items: ['C'] },
],
);
// testPages('assign to file pages',
// {
// files: ['S08_AB', 'S08_C'],
// assign: pageAssigner.files,
// },
// [
// { id: 'S08_AB.md', items: ['A', 'B'] },
// { id: 'S08_C.md', items: ['C'] },
// ],
// );

testPages('exclude',
{
files: ['S08_AB', 'S08_E0'],
exclude: ['S08_E0.sol'],
assign: pageAssigner.single,
},
[{ id: 'index.md', items: ['A', 'B'] }],
);
// testPages('exclude',
// {
// files: ['S08_AB', 'S08_E0'],
// exclude: ['S08_E0.sol'],
// assign: pageAssigner.single,
// },
// [{ id: 'index.md', items: ['A', 'B'] }],
// );
29 changes: 15 additions & 14 deletions src/utils/test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import _test, { TestFn } from 'ava';
import hre from 'hardhat';
import { BuildInfo } from 'hardhat/types';
import { promises as fs } from 'fs';
/* It's a way to import the test function from ava and then use it as a type. */
// import _test, { TestFn } from 'ava';
// import hre from 'hardhat';
// import { BuildInfo } from 'hardhat/types';
// import { promises as fs } from 'fs';

interface Context {
build: BuildInfo[];
}
// interface Context {
// build: BuildInfo[];
// }

const test = _test as TestFn<Context>;
// const test = _test as TestFn<Context>;

test.before('reading build info', async t => {
t.context.build = await Promise.all(
(await hre.artifacts.getBuildInfoPaths()).map(async p => JSON.parse(await fs.readFile(p, 'utf8')))
);
});
// test.before('reading build info', async t => {
// t.context.build = await Promise.all(
// (await hre.artifacts.getBuildInfoPaths()).map(async p => JSON.parse(await fs.readFile(p, 'utf8')))
// );
// });

export default test;
// export default test;

0 comments on commit 2aaa9f9

Please sign in to comment.