Skip to content

Commit

Permalink
normalize statement
Browse files Browse the repository at this point in the history
  • Loading branch information
albertolerda committed Oct 4, 2023
1 parent a2a8f53 commit 5b8d08c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
20 changes: 11 additions & 9 deletions pkg/core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,16 @@ export class ExecParams {
}
}

export const buildNormalizedPharse = (phrase: string) =>
phrase.toLowerCase().replace(' ', '_');

export class Plugin {
#params: string[];
constructor(params: string[]) {
#phrase: string;

constructor(phrase: string, params: string[]) {
this.#params = params
this.#phrase = buildNormalizedPharse(phrase)
}

protected buildParams(bindings: Map<string, string>, execParams: ExecParams): Jsonable[] {
Expand All @@ -61,25 +67,21 @@ export class Plugin {
})
return args
}
getPhrase() {
return this.#phrase
}
}

export class ReadPlugin extends Plugin {
#phrase: string;
#func: (...args: Jsonable[]) => Jsonable;

constructor(phrase: string, params: string[], func: (...args: Jsonable[]) => Jsonable) {
super(params);
this.#phrase = phrase;
super(phrase, params);
this.#func = func;
}

execute(bindings: Map<string, string>, execParams: ExecParams) {
const args = this.buildParams(bindings, execParams)
return this.#func(...args)
}


getPhrase() {
return this.#phrase
}
}
5 changes: 3 additions & 2 deletions pkg/core/src/slangroom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Plugin,
ExecParams,
ReadPlugin,
buildNormalizedPharse,
} from '@slangroom/core/plugin';
import { lex } from '@slangroom/core/lexer';
import { parse } from '@slangroom/core/parser';
Expand All @@ -29,7 +30,7 @@ export class Slangroom {

async executePlugin(p: Statement, params: ExecParams) {
if(p.action.kind == ActionType.Read) {
const normalizedBuzzwords = p.action.buzzwords.toLowerCase()
const normalizedBuzzwords = buildNormalizedPharse(p.action.buzzwords)
const plugin = this.#readPlugins.get(normalizedBuzzwords)
if(plugin) {
const result = plugin.execute(p.bindings, params)
Expand All @@ -42,7 +43,7 @@ export class Slangroom {
}
params.set(p.action.into[0] || "", val)
} else {
params.set(normalizedBuzzwords.replace(' ', '_'), result)
params.set(normalizedBuzzwords, result)
}
} else {
throw new Error("Unknown phrase")
Expand Down
9 changes: 6 additions & 3 deletions pkg/core/test/slangroom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,26 @@ test("Runs all unknown statements", async (t) => {
t.is(output, "foo")
return "bar"
})
const r3 = new ReadPlugin("c", ["a"], (...[output]) => {
const r3 = new ReadPlugin("c d", ["a"], (...[output]) => {
useR3 = true
t.is(output, "bar")
return "foobar"
})
const script = `
Rule caller restroom-mw
Given I read A
Given I have a 'string' named 'a'
Then print 'a'
Then I pass 'a' and read B
Then I pass a 'b' and read c into 'mimmo'
Then I pass a 'b' and read c D
Then I pass a 'b' and read C d into 'mimmo'
`
const slangroom = new Slangroom(r1, r2, r3);
const res = await slangroom.execute(script, {})
t.truthy(useR1, "r1 is not used")
t.truthy(useR2, "r2 is not used")
t.truthy(useR3, "r3 is not used")
t.deepEqual(res.result, { a: 'foo', b: 'bar', mimmo: 'foobar' }, res.logs)
t.deepEqual(res.result, { a: 'foo', b: 'bar', c_d: 'foobar', mimmo: 'foobar' }, res.logs)
})

0 comments on commit 5b8d08c

Please sign in to comment.