Skip to content

Commit

Permalink
feat: module support @Describe
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Jun 6, 2021
1 parent 9592bb4 commit 8eaf70d
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/Cases.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as _ from "lodash";
import Clients, { UnitClient } from "./Clients";
import { Module } from "./Loader";
import { JsonaAnnotation, JsonaArray, JsonaObject, JsonaProperty, JsonaValue, Position } from "./types";
import { getType, toPosString } from "./utils";

Expand Down Expand Up @@ -37,12 +38,13 @@ export default class Cases {
private clients: Clients;
private mixin: JsonaObject;

public constructor(clients: Clients, mixin: JsonaObject, modules: [string, JsonaProperty[]][]) {
public constructor(clients: Clients, mixin: JsonaObject, modules: Module[]) {
this.clients = clients;
this.mixin = mixin;
for (const [moduleName, props] of modules) {
this.describes[moduleName] = moduleName;
for (const prop of props) {
for (const mod of modules) {
const { moduleName, properties, describe} = mod;
this.describes[moduleName] = describe || moduleName;
for (const prop of properties) {
this.addProp([moduleName], prop);
}
}
Expand Down
27 changes: 24 additions & 3 deletions src/Loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ import Clients from "./Clients";
import { loadJsonaFile, getType, toPosString } from "./utils";
import { JsonaAnnotation, JsonaObject, JsonaProperty, JsonaValue } from "./types";

export interface Module {
moduleName: string;
properties: JsonaProperty[];
describe: string;
}

export default class Loader {
private workDir: string;
private cases: Cases;
private jslibs: string[] = [];
private clients: Clients = new Clients();
private modules: [string, JsonaProperty[]][] = [];
private modules: Module[] = [];
private mixin: JsonaObject;

public async load(target: string, env: string) {
Expand All @@ -28,7 +34,11 @@ export default class Loader {
if (jsa.type !== "Object") {
throw new Error("main: should have object value");
}
this.modules.push(["main", jsa.properties]);
this.modules.push({
moduleName: "main",
properties: jsa.properties,
describe: this.retriveModuleDescribe("main", jsa),
});
for (const anno of jsa.annotations) {
if (anno.name === "client") {
await this.loadClient(anno);
Expand Down Expand Up @@ -150,9 +160,20 @@ export default class Loader {
if (jsa.type !== "Object") {
throw new Error(`main@module(${moduleName}): should have object value`);
}
this.modules.push([moduleName, jsa.properties]);
const describe = this.retriveModuleDescribe(moduleName, jsa);
this.modules.push({moduleName, properties: jsa.properties, describe});
} else {
throw new Error(`main@module: should have string value${toPosString(anno.position)}`);
}
}

private retriveModuleDescribe(moduleName, value: JsonaValue) {
const describeAnno = value.annotations.find(v => v.name === "describe");
if (!describeAnno) return;
if (typeof describeAnno.value === "string") {
return describeAnno.value;
} else {
throw new Error(`${moduleName}@describe: should have string value${toPosString(describeAnno.position)}`);
}
}
}
18 changes: 18 additions & 0 deletions tests/__snapshots__/cases.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`cases describe 1`] = `
"This is a module
This is a group
A unit in group ✔
This is a nested group
A unit in nested group ✔
"
`;

exports[`cases describe-default 1`] = `
"main
group1
test1 ✔
group2
test1 ✔
"
`;

exports[`cases invalid group value type 1`] = `
"main.group1: should have object value at line 2 col 3
"
Expand Down
10 changes: 10 additions & 0 deletions tests/cases.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
const { spwanTest } = require("./utils");

describe("cases", () => {
test("describe-default", async () => {
const { code, stdout } = await spwanTest("cases/describe-default.jsona", ["--ci"]);
expect(code).toEqual(0);
expect(stdout).toMatchSnapshot();
});
test("describe", async () => {
const { code, stdout } = await spwanTest("cases/describe.jsona", ["--ci"]);
expect(code).toEqual(0);
expect(stdout).toMatchSnapshot();
});
test("group", async () => {
const { code } = await spwanTest("cases/group.jsona", ["--ci"]);
expect(code).toEqual(0);
Expand Down
15 changes: 15 additions & 0 deletions tests/fixtures/cases/describe-default.jsona
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
@client({name:"default",kind:"echo"})
group1: { @group
test1: {
req: {
}
},
group2: { @group
test1: {
req: {
}
}
}
}
}
16 changes: 16 additions & 0 deletions tests/fixtures/cases/describe.jsona
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
@client({name:"default",kind:"echo"})
@describe("This is a module")
group1: { @group @describe("This is a group")
test1: { @describe("A unit in group")
req: {
}
},
group2: { @group @describe("This is a nested group")
test1: { @describe("A unit in nested group")
req: {
}
}
}
}
}

0 comments on commit 8eaf70d

Please sign in to comment.