diff --git a/package.json b/package.json index 35fd60e4..b03fec9f 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "start": "webpack-dev-server -d source-map --config webpack.config.js", "build": "webpack --mode=production", "graphql:codegen": "npx graphql-codegen --config codegen.yml", - "test": "echo Tests command not implemented.", + "test": "jest", "format:app": "prettier --write 'src'", "format:check:app": "prettier --check 'src/**/*.{js,jsx,ts,tsx,css}' ", "lint": "eslint --ext .js,.jsx,.ts,.tsx src", diff --git a/src/util/parser.test.ts b/src/util/parser.test.ts new file mode 100644 index 00000000..eb4665bc --- /dev/null +++ b/src/util/parser.test.ts @@ -0,0 +1,27 @@ +import { extractSigners } from "./parser"; + +const oneSigner = ` +transaction { + prepare(acct: auth(LoadValue, SaveValue) &Account) {} +}` + + +const twoSigners = ` +transaction { + prepare(authorizer1: auth(Capabilities,SomethingElse) &Account, authorizer2: auth(Storage,Vaults) &Account) { } +}` + +describe('parser tests', () => { + it('should extract one code signer', () => { + + const signers = extractSigners(oneSigner) + + expect(signers).toHaveLength(1) + }) + it('should extract two code signer', () => { + + const signers = extractSigners(twoSigners) + + expect(signers).toHaveLength(2) + }) +}) diff --git a/src/util/parser.ts b/src/util/parser.ts index cb72e40c..f649f260 100644 --- a/src/util/parser.ts +++ b/src/util/parser.ts @@ -4,8 +4,12 @@ export const stripNewLines = (input: string) => export const generateSchema = (argsDefinition: string) => argsDefinition + .replace(/\(([^()]+)\)/g, (match) => { + // Replace commas with placeholder inside the parentheses content + return match.replace(/,/g, ''); + }) .split(',') - .map((item) => item.replace(/\s*/g, '')) + .map((item) => item.replace(/\s*/g, '').replace(//g,',')) .filter((item) => item !== ''); export const stripComments = (code: string) => { @@ -32,5 +36,5 @@ export const extract = (code: string, keyWord: string) => { }; export const extractSigners = (code: string) => { - return extract(code, `(?:prepare\\s*\\(\\s*)([^\\)]*)(?:\\))`); + return extract(code, `(?:prepare\\s*\\(\\s*)([^{}]*)(?:\\))`); };