harmonyc 0.18.1 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{cli → dist/cli}/cli.js +0 -0
- package/{code_generator → dist/code_generator}/VitestGenerator.d.ts +9 -5
- package/{code_generator → dist/code_generator}/VitestGenerator.js +51 -32
- package/{code_generator → dist/code_generator}/test_phrases.d.ts +3 -3
- package/{code_generator → dist/code_generator}/test_phrases.js +3 -3
- package/dist/compiler/compile.d.ts +16 -0
- package/{compiler → dist/compiler}/compile.js +24 -6
- package/{compiler → dist/compiler}/compiler.d.ts +0 -2
- package/{compiler → dist/compiler}/compiler.js +3 -17
- package/{model → dist/model}/model.d.ts +8 -0
- package/dist/phrases_assistant/phrases_assistant.d.ts +13 -0
- package/dist/phrases_assistant/phrases_assistant.js +146 -0
- package/dist/vitest/index.d.ts +11 -0
- package/{vitest → dist/vitest}/index.js +30 -13
- package/package.json +29 -32
- package/LICENSE +0 -21
- package/README.md +0 -142
- package/compiler/compile.d.ts +0 -9
- package/vitest/index.d.ts +0 -9
- /package/{cli → dist/cli}/cli.d.ts +0 -0
- /package/{cli → dist/cli}/run.d.ts +0 -0
- /package/{cli → dist/cli}/run.js +0 -0
- /package/{cli → dist/cli}/watch.d.ts +0 -0
- /package/{cli → dist/cli}/watch.js +0 -0
- /package/{code_generator → dist/code_generator}/outFile.d.ts +0 -0
- /package/{code_generator → dist/code_generator}/outFile.js +0 -0
- /package/{filenames → dist/filenames}/filenames.d.ts +0 -0
- /package/{filenames → dist/filenames}/filenames.js +0 -0
- /package/{model → dist/model}/Router.d.ts +0 -0
- /package/{model → dist/model}/Router.js +0 -0
- /package/{model → dist/model}/model.js +0 -0
- /package/{optimizations → dist/optimizations}/autoLabel/autoLabel.d.ts +0 -0
- /package/{optimizations → dist/optimizations}/autoLabel/autoLabel.js +0 -0
- /package/{parser → dist/parser}/lexer.d.ts +0 -0
- /package/{parser → dist/parser}/lexer.js +0 -0
- /package/{parser → dist/parser}/lexer_rules.d.ts +0 -0
- /package/{parser → dist/parser}/lexer_rules.js +0 -0
- /package/{parser → dist/parser}/parser.d.ts +0 -0
- /package/{parser → dist/parser}/parser.js +0 -0
- /package/{util → dist/util}/indent.d.ts +0 -0
- /package/{util → dist/util}/indent.js +0 -0
- /package/{util → dist/util}/iterators.d.ts +0 -0
- /package/{util → dist/util}/iterators.js +0 -0
- /package/{util → dist/util}/xmur3.d.ts +0 -0
- /package/{util → dist/util}/xmur3.js +0 -0
package/{cli → dist/cli}/cli.js
RENAMED
|
File without changes
|
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CompilerOptions } from '../compiler/compile.ts';
|
|
2
|
+
import { Action, CodeGenerator, ErrorResponse, Feature, Phrase, PhraseMethod, Response, SaveToVariable, SetVariable, Test, TestGroup } from '../model/model.ts';
|
|
2
3
|
import { OutFile } from './outFile.ts';
|
|
3
4
|
export declare class VitestGenerator implements CodeGenerator {
|
|
4
5
|
private tf;
|
|
5
|
-
private
|
|
6
|
-
private
|
|
6
|
+
private sourceFileName;
|
|
7
|
+
private opts;
|
|
7
8
|
static error(message: string, stack: string): string;
|
|
8
9
|
framework: string;
|
|
9
10
|
phraseFns: Map<string, Phrase>;
|
|
10
11
|
currentFeatureName: string;
|
|
11
|
-
|
|
12
|
+
featureClassName: string;
|
|
13
|
+
phraseMethods: PhraseMethod[];
|
|
14
|
+
constructor(tf: OutFile, sourceFileName: string, opts: CompilerOptions);
|
|
12
15
|
feature(feature: Feature): void;
|
|
13
16
|
testGroup(g: TestGroup): void;
|
|
14
17
|
featureVars: Map<string, string>;
|
|
@@ -28,5 +31,6 @@ export declare class VitestGenerator implements CodeGenerator {
|
|
|
28
31
|
private paramName;
|
|
29
32
|
stringParamDeclaration(index: number): string;
|
|
30
33
|
variantParamDeclaration(index: number): string;
|
|
34
|
+
functionName(phrase: Phrase): string;
|
|
35
|
+
argPlaceholder(i: number): string;
|
|
31
36
|
}
|
|
32
|
-
export declare function functionName(phrase: Phrase): string;
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { basename } from 'path';
|
|
2
|
+
import { xyzab } from "../compiler/compile.js";
|
|
2
3
|
import { Arg, Response, Word, } from "../model/model.js";
|
|
4
|
+
const X = 'X'.codePointAt(0);
|
|
5
|
+
const A = 'A'.codePointAt(0);
|
|
3
6
|
export class VitestGenerator {
|
|
4
7
|
static error(message, stack) {
|
|
5
8
|
return `const e = new SyntaxError(${str(message)});
|
|
@@ -7,19 +10,22 @@ export class VitestGenerator {
|
|
|
7
10
|
throw e;
|
|
8
11
|
${stack ? `/* ${stack} */` : ''}`;
|
|
9
12
|
}
|
|
10
|
-
constructor(tf,
|
|
13
|
+
constructor(tf, sourceFileName, opts) {
|
|
11
14
|
this.tf = tf;
|
|
12
|
-
this.
|
|
13
|
-
this.
|
|
15
|
+
this.sourceFileName = sourceFileName;
|
|
16
|
+
this.opts = opts;
|
|
14
17
|
this.framework = 'vitest';
|
|
15
18
|
this.phraseFns = new Map();
|
|
16
19
|
this.currentFeatureName = '';
|
|
20
|
+
this.phraseMethods = [];
|
|
17
21
|
this.resultCount = 0;
|
|
18
22
|
this.extraArgs = [];
|
|
19
23
|
}
|
|
20
24
|
feature(feature) {
|
|
21
|
-
const phrasesModule = './' + basename(this.
|
|
22
|
-
const fn = (this.
|
|
25
|
+
const phrasesModule = './' + basename(this.sourceFileName.replace(/\.harmony$/, '.phrases.js'));
|
|
26
|
+
const fn = (this.featureClassName =
|
|
27
|
+
this.currentFeatureName =
|
|
28
|
+
pascalCase(feature.name) + 'Phrases');
|
|
23
29
|
this.phraseFns = new Map();
|
|
24
30
|
// test file
|
|
25
31
|
if (this.framework === 'vitest') {
|
|
@@ -30,26 +36,33 @@ export class VitestGenerator {
|
|
|
30
36
|
this.tf.print(`describe.todo(${str(feature.name)});`);
|
|
31
37
|
return;
|
|
32
38
|
}
|
|
33
|
-
this.tf.print(`import ${fn}
|
|
39
|
+
this.tf.print(`import ${fn} from ${str(phrasesModule)};`);
|
|
34
40
|
this.tf.print(``);
|
|
35
41
|
for (const item of feature.testGroups) {
|
|
36
42
|
item.toCode(this);
|
|
37
43
|
}
|
|
38
44
|
this.tf.print(``);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
for (const ph of this.phraseFns.keys()) {
|
|
46
|
+
const p = this.phraseFns.get(ph);
|
|
47
|
+
const parameters = p.args.map((a, i) => {
|
|
48
|
+
const declaration = a.toDeclaration(this, i);
|
|
49
|
+
const parts = declaration.split(': ');
|
|
50
|
+
return {
|
|
51
|
+
name: parts[0],
|
|
52
|
+
type: parts[1] || 'any',
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
if (p instanceof Response) {
|
|
56
|
+
parameters.push({
|
|
57
|
+
name: 'res',
|
|
58
|
+
type: 'any',
|
|
48
59
|
});
|
|
49
|
-
this.sf.print(`}`);
|
|
50
60
|
}
|
|
51
|
-
|
|
52
|
-
|
|
61
|
+
this.phraseMethods.push({
|
|
62
|
+
name: ph,
|
|
63
|
+
parameters,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
53
66
|
}
|
|
54
67
|
testGroup(g) {
|
|
55
68
|
this.tf.print(`describe(${str(g.label.text)}, () => {`, g.label.start, g.label.text);
|
|
@@ -124,7 +137,7 @@ export class VitestGenerator {
|
|
|
124
137
|
}
|
|
125
138
|
}
|
|
126
139
|
phrase(p) {
|
|
127
|
-
const phrasefn = functionName(p);
|
|
140
|
+
const phrasefn = this.functionName(p);
|
|
128
141
|
if (!this.phraseFns.has(phrasefn))
|
|
129
142
|
this.phraseFns.set(phrasefn, p);
|
|
130
143
|
const f = this.featureVars.get(p.feature.name);
|
|
@@ -139,7 +152,7 @@ export class VitestGenerator {
|
|
|
139
152
|
this.saveToVariable(p.saveToVariable, '');
|
|
140
153
|
}
|
|
141
154
|
this.tf.printn(`await ${f}.`);
|
|
142
|
-
this.tf.write(`${
|
|
155
|
+
this.tf.write(`${phrasefn}(${args.join(', ')})`, p.start, name);
|
|
143
156
|
this.tf.write(`);`);
|
|
144
157
|
this.tf.nl();
|
|
145
158
|
}
|
|
@@ -159,7 +172,7 @@ export class VitestGenerator {
|
|
|
159
172
|
return src.replace(/\$\{([^\s}]+)\}/g, (_, x) => `context.task.meta.variables?.[${str(x)}]`);
|
|
160
173
|
}
|
|
161
174
|
paramName(index) {
|
|
162
|
-
return
|
|
175
|
+
return xyzab(index);
|
|
163
176
|
}
|
|
164
177
|
stringParamDeclaration(index) {
|
|
165
178
|
return `${this.paramName(index)}: string`;
|
|
@@ -167,6 +180,23 @@ export class VitestGenerator {
|
|
|
167
180
|
variantParamDeclaration(index) {
|
|
168
181
|
return `${this.paramName(index)}: any`;
|
|
169
182
|
}
|
|
183
|
+
functionName(phrase) {
|
|
184
|
+
const { kind } = phrase;
|
|
185
|
+
let argIndex = -1;
|
|
186
|
+
return ((kind === 'response' ? 'Then_' : 'When_') +
|
|
187
|
+
(phrase.parts
|
|
188
|
+
.flatMap((c) => c instanceof Word
|
|
189
|
+
? words(c.text).filter((x) => x)
|
|
190
|
+
: c instanceof Arg
|
|
191
|
+
? [this.argPlaceholder(++argIndex)]
|
|
192
|
+
: [])
|
|
193
|
+
.join('_') || ''));
|
|
194
|
+
}
|
|
195
|
+
argPlaceholder(i) {
|
|
196
|
+
return typeof this.opts.argumentPlaceholder === 'function'
|
|
197
|
+
? this.opts.argumentPlaceholder(i)
|
|
198
|
+
: this.opts.argumentPlaceholder;
|
|
199
|
+
}
|
|
170
200
|
}
|
|
171
201
|
function str(s) {
|
|
172
202
|
if (s.includes('\n'))
|
|
@@ -208,14 +238,3 @@ function abbrev(s) {
|
|
|
208
238
|
.map((x) => x.charAt(0).toUpperCase())
|
|
209
239
|
.join('');
|
|
210
240
|
}
|
|
211
|
-
export function functionName(phrase) {
|
|
212
|
-
const { kind } = phrase;
|
|
213
|
-
return ((kind === 'response' ? 'Then_' : 'When_') +
|
|
214
|
-
(phrase.parts
|
|
215
|
-
.flatMap((c) => c instanceof Word
|
|
216
|
-
? words(c.text).filter((x) => x)
|
|
217
|
-
: c instanceof Arg
|
|
218
|
-
? ['']
|
|
219
|
-
: [])
|
|
220
|
-
.join('_') || ''));
|
|
221
|
-
}
|
|
@@ -3,9 +3,9 @@ export declare class TestPhrases {
|
|
|
3
3
|
constructor(context: any);
|
|
4
4
|
When_goodbye(): void;
|
|
5
5
|
When_hello(): string;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
When_greet_X(name: string): void;
|
|
7
|
+
Then_X_is_Y(x: string, y: string): Promise<void>;
|
|
8
8
|
Then_last_char(s: string): string;
|
|
9
9
|
Then_last_char_of_greeting(): any;
|
|
10
|
-
|
|
10
|
+
Then_X(s: string, r: string): void;
|
|
11
11
|
}
|
|
@@ -9,10 +9,10 @@ export class TestPhrases {
|
|
|
9
9
|
When_hello() {
|
|
10
10
|
return (this.context.task.meta.greeting = 'Hello!');
|
|
11
11
|
}
|
|
12
|
-
|
|
12
|
+
When_greet_X(name) {
|
|
13
13
|
this.context.task.meta.greeting = `Hello, ${name}!`;
|
|
14
14
|
}
|
|
15
|
-
async
|
|
15
|
+
async Then_X_is_Y(x, y) {
|
|
16
16
|
expect(x).toBe(y);
|
|
17
17
|
}
|
|
18
18
|
Then_last_char(s) {
|
|
@@ -21,7 +21,7 @@ export class TestPhrases {
|
|
|
21
21
|
Then_last_char_of_greeting() {
|
|
22
22
|
return this.context.task.meta.greeting.slice(-1);
|
|
23
23
|
}
|
|
24
|
-
|
|
24
|
+
Then_X(s, r) {
|
|
25
25
|
expect(s).toBe(r);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { OutFile } from '../code_generator/outFile.ts';
|
|
2
|
+
export interface CompilerOptions {
|
|
3
|
+
argumentPlaceholder: string | ((index: number) => string);
|
|
4
|
+
}
|
|
5
|
+
export interface CompiledFeature {
|
|
6
|
+
name: string;
|
|
7
|
+
code: Record<string, string>;
|
|
8
|
+
}
|
|
9
|
+
export declare function XYZAB(index: number): string;
|
|
10
|
+
export declare function xyzab(index: number): string;
|
|
11
|
+
export declare const DEFAULT_COMPILER_OPTIONS: CompilerOptions;
|
|
12
|
+
export declare function compileFeature(fileName: string, src: string, opts?: Partial<CompilerOptions>): {
|
|
13
|
+
outFile: OutFile;
|
|
14
|
+
phraseMethods: import("../model/model.ts").PhraseMethod[];
|
|
15
|
+
featureClassName: string;
|
|
16
|
+
};
|
|
@@ -1,11 +1,24 @@
|
|
|
1
1
|
import { basename } from 'node:path';
|
|
2
2
|
import { VitestGenerator } from "../code_generator/VitestGenerator.js";
|
|
3
3
|
import { OutFile } from "../code_generator/outFile.js";
|
|
4
|
-
import { base,
|
|
4
|
+
import { base, testFileName } from "../filenames/filenames.js";
|
|
5
5
|
import { Feature } from "../model/model.js";
|
|
6
6
|
import { autoLabel } from "../optimizations/autoLabel/autoLabel.js";
|
|
7
7
|
import { parse } from "../parser/parser.js";
|
|
8
|
-
|
|
8
|
+
const X = 'X'.codePointAt(0);
|
|
9
|
+
const A = 'A'.codePointAt(0);
|
|
10
|
+
const x = 'x'.codePointAt(0);
|
|
11
|
+
const a = 'a'.codePointAt(0);
|
|
12
|
+
export function XYZAB(index) {
|
|
13
|
+
return String.fromCodePoint(A + ((X - A + index) % 26));
|
|
14
|
+
}
|
|
15
|
+
export function xyzab(index) {
|
|
16
|
+
return String.fromCodePoint(a + ((x - a + index) % 26));
|
|
17
|
+
}
|
|
18
|
+
export const DEFAULT_COMPILER_OPTIONS = {
|
|
19
|
+
argumentPlaceholder: XYZAB,
|
|
20
|
+
};
|
|
21
|
+
export function compileFeature(fileName, src, opts = {}) {
|
|
9
22
|
const feature = new Feature(basename(base(fileName)));
|
|
10
23
|
try {
|
|
11
24
|
feature.root = parse(src);
|
|
@@ -24,9 +37,14 @@ export function compileFeature(fileName, src) {
|
|
|
24
37
|
autoLabel(feature.root);
|
|
25
38
|
const testFn = testFileName(fileName);
|
|
26
39
|
const testFile = new OutFile(testFn, fileName);
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
40
|
+
const cg = new VitestGenerator(testFile, fileName, {
|
|
41
|
+
...DEFAULT_COMPILER_OPTIONS,
|
|
42
|
+
...opts,
|
|
43
|
+
});
|
|
30
44
|
feature.toCode(cg);
|
|
31
|
-
return {
|
|
45
|
+
return {
|
|
46
|
+
outFile: testFile,
|
|
47
|
+
phraseMethods: cg.phraseMethods,
|
|
48
|
+
featureClassName: cg.featureClassName,
|
|
49
|
+
};
|
|
32
50
|
}
|
|
@@ -3,8 +3,6 @@ export declare function compileFiles(pattern: string | string[]): Promise<{
|
|
|
3
3
|
outFns: string[];
|
|
4
4
|
}>;
|
|
5
5
|
export declare function compileFile(fn: string): Promise<{
|
|
6
|
-
phrasesFileAction: string;
|
|
7
6
|
outFile: import("../code_generator/outFile.ts").OutFile;
|
|
8
|
-
phrasesFile: import("../code_generator/outFile.ts").OutFile;
|
|
9
7
|
} | undefined>;
|
|
10
8
|
export declare function preprocess(src: string): string;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import glob from 'fast-glob';
|
|
2
|
-
import {
|
|
2
|
+
import { readFileSync, writeFileSync } from 'fs';
|
|
3
3
|
import { resolve } from 'path';
|
|
4
|
-
import { VitestGenerator } from "../code_generator/VitestGenerator.js";
|
|
5
|
-
import { testFileName } from "../filenames/filenames.js";
|
|
6
4
|
import { compileFeature } from "./compile.js";
|
|
7
5
|
export async function compileFiles(pattern) {
|
|
8
6
|
var _a;
|
|
@@ -17,29 +15,17 @@ export async function compileFiles(pattern) {
|
|
|
17
15
|
}
|
|
18
16
|
console.log(`Compiled ${compiled.length} file${compiled.length === 1 ? '' : 's'}.`);
|
|
19
17
|
const features = compiled.filter((f) => f !== undefined);
|
|
20
|
-
const generated = features.filter((f) => f.phrasesFileAction === 'generated');
|
|
21
|
-
if (generated.length) {
|
|
22
|
-
console.log(`Generated ${generated.length} phrases file${generated.length === 1 ? '' : 's'}.`);
|
|
23
|
-
}
|
|
24
18
|
return { fns, outFns: features.map((f) => f.outFile.name) };
|
|
25
19
|
}
|
|
26
20
|
export async function compileFile(fn) {
|
|
27
|
-
var _a;
|
|
28
21
|
fn = resolve(fn);
|
|
29
22
|
const src = preprocess(readFileSync(fn, 'utf8').toString());
|
|
30
23
|
try {
|
|
31
|
-
const { outFile
|
|
24
|
+
const { outFile } = compileFeature(fn, src);
|
|
32
25
|
writeFileSync(outFile.name, outFile.value);
|
|
33
|
-
|
|
34
|
-
if (!existsSync(phrasesFile.name)) {
|
|
35
|
-
phrasesFileAction = 'generated';
|
|
36
|
-
writeFileSync(phrasesFile.name, phrasesFile.value);
|
|
37
|
-
}
|
|
38
|
-
return { phrasesFileAction, outFile, phrasesFile };
|
|
26
|
+
return { outFile };
|
|
39
27
|
}
|
|
40
28
|
catch (e) {
|
|
41
|
-
const outFileName = testFileName(fn);
|
|
42
|
-
writeFileSync(outFileName, VitestGenerator.error((_a = e.message) !== null && _a !== void 0 ? _a : `${e}`, e.stack));
|
|
43
29
|
return undefined;
|
|
44
30
|
}
|
|
45
31
|
}
|
|
@@ -14,6 +14,14 @@ export interface CodeGenerator {
|
|
|
14
14
|
codeLiteral(src: string): string;
|
|
15
15
|
stringParamDeclaration(index: number): string;
|
|
16
16
|
variantParamDeclaration(index: number): string;
|
|
17
|
+
phraseMethods: PhraseMethod[];
|
|
18
|
+
}
|
|
19
|
+
export interface PhraseMethod {
|
|
20
|
+
name: string;
|
|
21
|
+
parameters: {
|
|
22
|
+
name: string;
|
|
23
|
+
type: string;
|
|
24
|
+
}[];
|
|
17
25
|
}
|
|
18
26
|
export interface Location {
|
|
19
27
|
line: number;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as t from 'ts-morph';
|
|
2
|
+
import { PhraseMethod } from '../model/model';
|
|
3
|
+
export declare class PhrasesAssistant {
|
|
4
|
+
project: t.Project;
|
|
5
|
+
file: t.SourceFile;
|
|
6
|
+
clazz: t.ClassDeclaration;
|
|
7
|
+
constructor(content: string, className: string);
|
|
8
|
+
ensureMethods(methods: PhraseMethod[]): void;
|
|
9
|
+
ensureMethod(method: PhraseMethod): void;
|
|
10
|
+
addMethod(method: PhraseMethod): void;
|
|
11
|
+
sortMethods(): void;
|
|
12
|
+
toCode(): string;
|
|
13
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import * as t from 'ts-morph';
|
|
2
|
+
export class PhrasesAssistant {
|
|
3
|
+
constructor(content, className) {
|
|
4
|
+
var _a;
|
|
5
|
+
this.project = new t.Project({
|
|
6
|
+
useInMemoryFileSystem: true,
|
|
7
|
+
});
|
|
8
|
+
this.file = this.project.createSourceFile('filename.ts', content, {
|
|
9
|
+
overwrite: true,
|
|
10
|
+
});
|
|
11
|
+
const clazz = this.file.getClass(className);
|
|
12
|
+
if (!clazz) {
|
|
13
|
+
const defaultExport = this.file.getDefaultExportSymbol();
|
|
14
|
+
if (defaultExport) {
|
|
15
|
+
const decl = defaultExport.getDeclarations()[0];
|
|
16
|
+
if (t.Node.isClassDeclaration(decl)) {
|
|
17
|
+
this.clazz = decl;
|
|
18
|
+
this.clazz.rename(className);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
this.clazz = clazz;
|
|
24
|
+
if (!this.clazz.isDefaultExport()) {
|
|
25
|
+
this.clazz.setIsDefaultExport(true);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
(_a = this.clazz) !== null && _a !== void 0 ? _a : (this.clazz = this.file.addClass({
|
|
29
|
+
name: className,
|
|
30
|
+
isDefaultExport: true,
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
ensureMethods(methods) {
|
|
34
|
+
for (const method of methods) {
|
|
35
|
+
this.ensureMethod(method);
|
|
36
|
+
}
|
|
37
|
+
this.clazz
|
|
38
|
+
.getMethods()
|
|
39
|
+
.filter((m) => !methods.find((md) => md.name === m.getName()))
|
|
40
|
+
.forEach((m) => {
|
|
41
|
+
if (m.getStatements().length === 1 &&
|
|
42
|
+
m
|
|
43
|
+
.getStatements()[0]
|
|
44
|
+
.getText()
|
|
45
|
+
.match(/throw new Error\(["']TODO /)) {
|
|
46
|
+
m.remove();
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
this.sortMethods();
|
|
50
|
+
}
|
|
51
|
+
ensureMethod(method) {
|
|
52
|
+
let existing = this.clazz.getMethod(method.name);
|
|
53
|
+
if (!existing) {
|
|
54
|
+
this.addMethod(method);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
addMethod(method) {
|
|
58
|
+
const m = this.clazz.addMethod({
|
|
59
|
+
isAsync: true,
|
|
60
|
+
name: method.name,
|
|
61
|
+
parameters: method.parameters,
|
|
62
|
+
statements: [`throw new Error("TODO ${method.name}");`],
|
|
63
|
+
});
|
|
64
|
+
m.formatText({ indentSize: 2 });
|
|
65
|
+
}
|
|
66
|
+
sortMethods() {
|
|
67
|
+
const groups = ['When_', 'Then_'];
|
|
68
|
+
const members = this.clazz.getMembersWithComments();
|
|
69
|
+
const sorted = members.slice().sort((a, b) => {
|
|
70
|
+
const kindA = t.Node.isMethodDeclaration(a)
|
|
71
|
+
? groups.findIndex((g) => a.getName().startsWith(g))
|
|
72
|
+
: -1;
|
|
73
|
+
const kindB = t.Node.isMethodDeclaration(b)
|
|
74
|
+
? groups.findIndex((g) => b.getName().startsWith(g))
|
|
75
|
+
: -1;
|
|
76
|
+
if (kindA !== kindB) {
|
|
77
|
+
return kindA - kindB;
|
|
78
|
+
}
|
|
79
|
+
if (kindA === -1 && kindB === -1) {
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
82
|
+
if (!t.Node.isMethodDeclaration(a) || !t.Node.isMethodDeclaration(b)) {
|
|
83
|
+
// never happens
|
|
84
|
+
return 0;
|
|
85
|
+
}
|
|
86
|
+
return a.getName() < b.getName() ? -1 : 1;
|
|
87
|
+
});
|
|
88
|
+
const moves = calculateMoves(members, sorted);
|
|
89
|
+
for (const move of moves) {
|
|
90
|
+
const method = members[move.fromIndex];
|
|
91
|
+
if (t.Node.isCommentClassElement(method)) {
|
|
92
|
+
// something went wrong
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
method.setOrder(move.toIndex);
|
|
96
|
+
const [moved] = members.splice(move.fromIndex, 1);
|
|
97
|
+
members.splice(move.toIndex, 0, moved);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
toCode() {
|
|
101
|
+
let s = this.file.getFullText();
|
|
102
|
+
// fix extra space
|
|
103
|
+
const closing = this.clazz.getEnd() - 1;
|
|
104
|
+
if (s.slice(closing - 2, closing + 1) === '\n }') {
|
|
105
|
+
s = s.slice(0, closing - 1) + s.slice(closing);
|
|
106
|
+
}
|
|
107
|
+
return s;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Calculates a set of move operations to transform one array into another.
|
|
112
|
+
* Both arrays must contain the same elements, just in different order.
|
|
113
|
+
*
|
|
114
|
+
* @param actual - The current array that needs to be reordered
|
|
115
|
+
* @param desired - The target array with the desired order
|
|
116
|
+
* @returns Array of move operations {fromIndex, toIndex} that transform actual into desired
|
|
117
|
+
*/
|
|
118
|
+
function calculateMoves(actual, desired) {
|
|
119
|
+
if (actual.length !== desired.length) {
|
|
120
|
+
throw new Error('Arrays must have the same length');
|
|
121
|
+
}
|
|
122
|
+
// Create a working copy to track changes
|
|
123
|
+
const working = [...actual];
|
|
124
|
+
const moves = [];
|
|
125
|
+
// For each position in the desired array
|
|
126
|
+
for (let targetIndex = 0; targetIndex < desired.length; targetIndex++) {
|
|
127
|
+
const targetElement = desired[targetIndex];
|
|
128
|
+
// Find where this element currently is in our working array
|
|
129
|
+
const currentIndex = working.indexOf(targetElement);
|
|
130
|
+
if (currentIndex === -1) {
|
|
131
|
+
throw new Error('Arrays must contain the same elements');
|
|
132
|
+
}
|
|
133
|
+
// If it's not in the right position, move it
|
|
134
|
+
if (currentIndex !== targetIndex) {
|
|
135
|
+
// Record the move operation
|
|
136
|
+
moves.push({
|
|
137
|
+
fromIndex: currentIndex,
|
|
138
|
+
toIndex: targetIndex,
|
|
139
|
+
});
|
|
140
|
+
// Apply the move to our working array
|
|
141
|
+
const [movedElement] = working.splice(currentIndex, 1);
|
|
142
|
+
working.splice(targetIndex, 0, movedElement);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return moves;
|
|
146
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
import { CompilerOptions } from '../compiler/compile.ts';
|
|
3
|
+
export interface HarmonyPluginOptions extends Partial<CompilerOptions> {
|
|
4
|
+
autoEditPhrases?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export default function harmonyPlugin(opts?: HarmonyPluginOptions): Plugin;
|
|
7
|
+
declare module 'vitest' {
|
|
8
|
+
interface TaskMeta {
|
|
9
|
+
phrases?: string[];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
import { readFile, writeFile } from 'fs/promises';
|
|
1
2
|
import c from 'tinyrainbow';
|
|
2
3
|
import { compileFeature } from "../compiler/compile.js";
|
|
3
4
|
import { preprocess } from "../compiler/compiler.js";
|
|
4
|
-
|
|
5
|
+
import { PhrasesAssistant } from "../phrases_assistant/phrases_assistant.js";
|
|
6
|
+
const DEFAULT_OPTIONS = {
|
|
7
|
+
autoEditPhrases: true,
|
|
8
|
+
};
|
|
9
|
+
export default function harmonyPlugin(opts = {}) {
|
|
10
|
+
const options = { ...DEFAULT_OPTIONS, ...opts };
|
|
5
11
|
return {
|
|
6
12
|
name: 'harmony',
|
|
7
13
|
resolveId(id) {
|
|
@@ -9,11 +15,14 @@ export default function harmonyPlugin({} = {}) {
|
|
|
9
15
|
return id;
|
|
10
16
|
}
|
|
11
17
|
},
|
|
12
|
-
transform(code, id
|
|
18
|
+
transform(code, id) {
|
|
13
19
|
if (!id.endsWith('.harmony'))
|
|
14
20
|
return null;
|
|
15
21
|
code = preprocess(code);
|
|
16
|
-
const { outFile } = compileFeature(id, code);
|
|
22
|
+
const { outFile, phraseMethods, featureClassName } = compileFeature(id, code, opts);
|
|
23
|
+
if (options.autoEditPhrases) {
|
|
24
|
+
void updatePhrasesFile(id, phraseMethods, featureClassName);
|
|
25
|
+
}
|
|
17
26
|
return {
|
|
18
27
|
code: outFile.valueWithoutSourceMap,
|
|
19
28
|
map: outFile.sourceMap,
|
|
@@ -29,16 +38,6 @@ export default function harmonyPlugin({} = {}) {
|
|
|
29
38
|
}
|
|
30
39
|
config.test.reporters.splice(0, 0, new HarmonyReporter());
|
|
31
40
|
},
|
|
32
|
-
// This has been removed in favor of using transform, so no need to generate an actual file
|
|
33
|
-
// async configureServer(server) {
|
|
34
|
-
// const isWatchMode = server.config.server.watch !== null
|
|
35
|
-
// const patterns = [`${watchDir}/**/*.harmony`]
|
|
36
|
-
// if (isWatchMode) {
|
|
37
|
-
// await watchFiles(patterns)
|
|
38
|
-
// } else {
|
|
39
|
-
// await compileFiles(patterns)
|
|
40
|
-
// }
|
|
41
|
-
// },
|
|
42
41
|
};
|
|
43
42
|
}
|
|
44
43
|
class HarmonyReporter {
|
|
@@ -82,3 +81,21 @@ function addPhrases(task, depth = 2) {
|
|
|
82
81
|
delete task.meta.phrases; // to make sure not to add them again
|
|
83
82
|
}
|
|
84
83
|
}
|
|
84
|
+
async function updatePhrasesFile(id, phraseMethods, featureClassName) {
|
|
85
|
+
try {
|
|
86
|
+
const phrasesFile = id.replace(/\.harmony$/, '.phrases.ts');
|
|
87
|
+
let phrasesFileContent = '';
|
|
88
|
+
try {
|
|
89
|
+
phrasesFileContent = await readFile(phrasesFile, 'utf-8');
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// File doesn't exist
|
|
93
|
+
}
|
|
94
|
+
const pa = new PhrasesAssistant(phrasesFileContent, featureClassName);
|
|
95
|
+
pa.ensureMethods(phraseMethods);
|
|
96
|
+
await writeFile(phrasesFile, pa.toCode());
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
console.error('Error updating phrases file:', e);
|
|
100
|
+
}
|
|
101
|
+
}
|
package/package.json
CHANGED
|
@@ -1,49 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "harmonyc",
|
|
3
|
-
"
|
|
4
|
-
"version": "0.18.1",
|
|
5
|
-
"author": "Bernát Kalló",
|
|
3
|
+
"version": "0.20.0",
|
|
6
4
|
"type": "module",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "tsc",
|
|
7
|
+
"build:watch": "tsc --watch",
|
|
8
|
+
"harmonyc": "tsx ./src/cli/cli",
|
|
9
|
+
"test": "vitest --run",
|
|
10
|
+
"test:compile": "tsx ./src/cli/cli 'src/**/*.harmony'",
|
|
11
|
+
"dev": "vitest --watch",
|
|
12
|
+
"publish": "npm publish",
|
|
13
|
+
"release": "npm run build && release publish"
|
|
9
14
|
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
10
18
|
"exports": {
|
|
11
19
|
"./vitest": {
|
|
12
|
-
"types": "./vitest/index.d.ts",
|
|
13
|
-
"default": "./vitest/index.js"
|
|
20
|
+
"types": "./dist/vitest/index.d.ts",
|
|
21
|
+
"default": "./dist/vitest/index.js"
|
|
14
22
|
}
|
|
15
23
|
},
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
"type": "git",
|
|
19
|
-
"url": "git+https://github.com/harmony-ac/code.git"
|
|
20
|
-
},
|
|
21
|
-
"bugs": {
|
|
22
|
-
"url": "https://github.com/harmony-ac/code/issues"
|
|
24
|
+
"bin": {
|
|
25
|
+
"harmonyc": "./dist/cli/cli.js"
|
|
23
26
|
},
|
|
24
27
|
"dependencies": {
|
|
25
28
|
"fast-glob": "^3.3.2",
|
|
26
29
|
"tinyrainbow": "1",
|
|
30
|
+
"ts-morph": "^27.0.2",
|
|
27
31
|
"typescript-parsec": "0.3.4"
|
|
28
32
|
},
|
|
29
33
|
"optionalDependencies": {
|
|
30
34
|
"watcher": "^2.3.1"
|
|
31
35
|
},
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
"model-based testing",
|
|
44
|
-
"test model",
|
|
45
|
-
"test modeling",
|
|
46
|
-
"test modelling",
|
|
47
|
-
"vitest"
|
|
48
|
-
]
|
|
49
|
-
}
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@ossjs/release": "^0.10.0",
|
|
38
|
+
"@types/debug": "^4.1.12",
|
|
39
|
+
"@types/node": "^22.10.6",
|
|
40
|
+
"expect": "^29.7.0",
|
|
41
|
+
"tsx": "^4.7.1",
|
|
42
|
+
"typescript": "^5.3.3",
|
|
43
|
+
"vite": "^7.1.10",
|
|
44
|
+
"vitest": "^3.2.4"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024-2025 Bernát Kalló
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/README.md
DELETED
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
# Harmony Code
|
|
2
|
-
|
|
3
|
-
A test design & BDD tool that helps you separate _what_ you test and _how_ you automate it. You write test cases in a simple easy-to-read format, and then automate them with Vitest.
|
|
4
|
-
|
|
5
|
-
## Setup
|
|
6
|
-
|
|
7
|
-
You need to have Node.js installed. Then you can install Harmony Code in your project folder by:
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
npm install harmonyc
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
Then add it as a plugin to your `vitest.config.js` or `vite.config.js` file, and make sure to include your `.harmony` files:
|
|
14
|
-
|
|
15
|
-
```js
|
|
16
|
-
import harmony from 'harmonyc/vitest'
|
|
17
|
-
|
|
18
|
-
export default {
|
|
19
|
-
plugins: [harmony()],
|
|
20
|
-
include: ['src/**/*.harmony'],
|
|
21
|
-
}
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
This will run .harmony files in vitest.
|
|
25
|
-
|
|
26
|
-
## VSCode plugin
|
|
27
|
-
|
|
28
|
-
Harmony Code has a [VSCode plugin](https://marketplace.visualstudio.com/items?itemName=harmony-ac.harmony-code) that supports syntax highlighting.
|
|
29
|
-
|
|
30
|
-
Harmony Code is compatible with Vitest's VSCode plugin, so you can run and debug tests from the editor.
|
|
31
|
-
|
|
32
|
-
## Syntax
|
|
33
|
-
|
|
34
|
-
A `.harmony` file is a text file with a syntax that looks like this:
|
|
35
|
-
|
|
36
|
-
```
|
|
37
|
-
+ Products API:
|
|
38
|
-
+ Create:
|
|
39
|
-
+ Anonymous:
|
|
40
|
-
- create product => !! "unauthorized"
|
|
41
|
-
+ Admin:
|
|
42
|
-
- authenticate with "admin" => product count `0`
|
|
43
|
-
- create product
|
|
44
|
-
=> product created
|
|
45
|
-
=> product count `1`
|
|
46
|
-
- Delete:
|
|
47
|
-
- delete product => product deleted => product count `0`
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### Indentation
|
|
51
|
-
|
|
52
|
-
The lines of a file are nodes of a tree. The tree is specified with the indentation of the lines, which is n times 2 spaces and a `+` or `-` with one more space. The `+` or `-` sign is considered to be part of the indentation.
|
|
53
|
-
|
|
54
|
-
### Sequences and forks
|
|
55
|
-
|
|
56
|
-
`-` means a sequence: the node follows the previous sibling node and its descendants.
|
|
57
|
-
|
|
58
|
-
`+` means a fork: the node directly follows its parent node. All siblings with `+` are separate branches, they will generate separate scenarios.
|
|
59
|
-
|
|
60
|
-
### Phrases (actions and responses)
|
|
61
|
-
|
|
62
|
-
After the mark, every node can contain an **action** and zero or more **responses**, together called **phrases**. The action is the text before the `=>`, and the responses are the text after the `=>`.
|
|
63
|
-
|
|
64
|
-
Both actions and responses get compiled to simple function calls - in JavaScript, awaited function calls. Actions will become `When_*` functions, and responses will become `Then_*` functions. The return value of the action is passed to the responses of the same step as the last argument.
|
|
65
|
-
|
|
66
|
-
### Arguments
|
|
67
|
-
|
|
68
|
-
Phrases (actions and responses) can have arguments which are passed to the implementation function. There are two types of arguments: strings and code fragments:
|
|
69
|
-
|
|
70
|
-
```harmony
|
|
71
|
-
+ strings:
|
|
72
|
-
+ hello "John"
|
|
73
|
-
+ code fragment:
|
|
74
|
-
+ greet `3` times
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
becomes
|
|
78
|
-
|
|
79
|
-
```javascript
|
|
80
|
-
test('T1 - strings', async () => {
|
|
81
|
-
const P = new Phrases()
|
|
82
|
-
await P.When_hello_('John')
|
|
83
|
-
})
|
|
84
|
-
test('T2 - code fragment', async () => {
|
|
85
|
-
const P = new Phrases()
|
|
86
|
-
await P.When_greet__times(3)
|
|
87
|
-
})
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
### Labels
|
|
91
|
-
|
|
92
|
-
Labels are lines that start with `-` or `+` and end with `:`. You can use them to structure your test design.
|
|
93
|
-
They are not included in the test case, but the test case name is generated from the labels.
|
|
94
|
-
|
|
95
|
-
### Comments
|
|
96
|
-
|
|
97
|
-
Lines starting with `#` or `//` are comments and are ignored.
|
|
98
|
-
|
|
99
|
-
### Switches
|
|
100
|
-
|
|
101
|
-
You can generate multiple test cases by adding a `{ A / B / C }` syntax into action(s) and possibly response(s).
|
|
102
|
-
|
|
103
|
-
```harmony
|
|
104
|
-
+ password is { "A" / "asdf" / "password123" } => !! "password is too weak"
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
### Error matching
|
|
108
|
-
|
|
109
|
-
You can use `!!` to denote an error response. This will verify that the action throws an error. You can specify the error message after the `!!`.
|
|
110
|
-
|
|
111
|
-
### Variables
|
|
112
|
-
|
|
113
|
-
You can set variables in the tests and use them in strings and code fragments:
|
|
114
|
-
|
|
115
|
-
```
|
|
116
|
-
+ set variable:
|
|
117
|
-
+ ${name} "John"
|
|
118
|
-
+ greet "${name}" => "hello John"
|
|
119
|
-
+ store result into variable:
|
|
120
|
-
+ run process => ${result}
|
|
121
|
-
+ "${result}" is "success"
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
becomes
|
|
125
|
-
|
|
126
|
-
```javascript
|
|
127
|
-
test('T1 - set variable', (context) => {
|
|
128
|
-
const P = new Phrases();
|
|
129
|
-
(context.task.meta.variables ??= {})['name'] = "John";
|
|
130
|
-
await P.When_greet_(context.task.meta.variables?.['name']);
|
|
131
|
-
})
|
|
132
|
-
test('T2 - store result in variable', (context) => {
|
|
133
|
-
const P = new Phrases();
|
|
134
|
-
const r = await P.When_run_process();
|
|
135
|
-
(context.task.meta.variables ??= {})['result'] = r;
|
|
136
|
-
await P.Then__is_(`${context.task.meta.variables?.['result']});
|
|
137
|
-
})
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
## License
|
|
141
|
-
|
|
142
|
-
MIT
|
package/compiler/compile.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { OutFile } from '../code_generator/outFile.ts';
|
|
2
|
-
export interface CompiledFeature {
|
|
3
|
-
name: string;
|
|
4
|
-
code: Record<string, string>;
|
|
5
|
-
}
|
|
6
|
-
export declare function compileFeature(fileName: string, src: string): {
|
|
7
|
-
outFile: OutFile;
|
|
8
|
-
phrasesFile: OutFile;
|
|
9
|
-
};
|
package/vitest/index.d.ts
DELETED
|
File without changes
|
|
File without changes
|
/package/{cli → dist/cli}/run.js
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|