From f2fcf204fdd5421a435ec3431a15b47905fbcc57 Mon Sep 17 00:00:00 2001 From: truongnguyen Date: Thu, 5 Nov 2020 17:25:55 +0700 Subject: [PATCH] feat(bridge): :sparkles: should add same for ICONContractAdater and EOSContractAdapter (#71) --- packages/bridge/src/eos/EOSContractAdapter.ts | 4 ++-- .../bridge/src/icon/ICONContractAdapter.ts | 4 ++-- .../test/eos/EOSContractAdapter.test.ts | 22 +++++++++++-------- .../test/icon/ICONContractAdapter.test.ts | 18 ++++++++++----- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/packages/bridge/src/eos/EOSContractAdapter.ts b/packages/bridge/src/eos/EOSContractAdapter.ts index 82ef955..41c064a 100644 --- a/packages/bridge/src/eos/EOSContractAdapter.ts +++ b/packages/bridge/src/eos/EOSContractAdapter.ts @@ -30,11 +30,11 @@ export class EOSContractAdapter extends AbsContractAdapter { } this.typeInterpreter = new EOSTypeInterpreter(); + this.templatePath = path.resolve(this.templatePath, this.blockchainType); } generateFromTemplate() { - this.templatePath = path.resolve(this.templatePath, this.blockchainType); - + if (!this.templatePath) throw new Error('Template path not found'); this.actionCreators = [new EOSCreAction(), new EOSUpdAction(), new EOSDelAction()]; this.actionCreators.forEach(ac => { ac.templatePath = this.templatePath; diff --git a/packages/bridge/src/icon/ICONContractAdapter.ts b/packages/bridge/src/icon/ICONContractAdapter.ts index 1bb9d58..9578dcd 100644 --- a/packages/bridge/src/icon/ICONContractAdapter.ts +++ b/packages/bridge/src/icon/ICONContractAdapter.ts @@ -24,16 +24,16 @@ export class ICONContractAdapter extends AbsContractAdapter { this.blockchainType = 'icon'; } this.typeInterpreter = new ICONTypeInterpreter(); + this.templatePath = path.resolve(this.templatePath, this.blockchainType); } generateFromTemplate() { + if (!this.templatePath) throw new Error('Template path not found'); this.pyFilePrinter = new FilePrinter(this.outputPath, new PythonPrettier(), this.logger); this.jsFilePrinter = new FilePrinter(this.outputPath, new JsPrettier(), this.logger); // this.coder = new FileCoder(this.outputPath, new CplusplusPrettier(), this.logger); - this.templatePath = path.resolve(this.templatePath, this.blockchainType); - this.generateInit(); this.generatePackage(); this.generateMainPy(); diff --git a/packages/bridge/test/eos/EOSContractAdapter.test.ts b/packages/bridge/test/eos/EOSContractAdapter.test.ts index b586c0f..33cc313 100644 --- a/packages/bridge/test/eos/EOSContractAdapter.test.ts +++ b/packages/bridge/test/eos/EOSContractAdapter.test.ts @@ -62,7 +62,7 @@ describe('test EOS contract addapter', () => { expect(adapter.templatePath).toEqual( path.resolve( path.dirname(require.resolve('@aloxide/bridge')), - '../smart-contract-templates', + '../smart-contract-templates/' + adapter.blockchainType, ), ); @@ -72,15 +72,24 @@ describe('test EOS contract addapter', () => { }); }); describe('test generateFromTemplate', () => { - it('should update templatePath to specific blockchain', () => { + it('should throw exception when template is null', () => { + const adapter = new EOSContractAdapter(); + adapter.entityConfigs = entityConfigs; + adapter.logger = { + info: jest.fn(), + debug: jest.fn(), + }; + adapter.templatePath = null; + expect(() => adapter.generateFromTemplate()).toThrowError('Template path not found'); + }); + + it('should generateFromTemplate', () => { const adapter = new EOSContractAdapter(); adapter.entityConfigs = entityConfigs; adapter.logger = { info: jest.fn(), debug: jest.fn(), }; - const templatePath = path.resolve(__dirname, '../../smart-contract-templates'); - adapter.templatePath = templatePath; const createTables = jest.spyOn(adapter, 'createTables'); createTables.mockImplementation(jest.fn()); @@ -95,7 +104,6 @@ describe('test EOS contract addapter', () => { spyGenerateHpp.mockImplementation(jest.fn()); adapter.generateFromTemplate(); - expect(adapter.templatePath).toEqual(path.resolve(templatePath, blockchain)); expect(createActions).toBeCalledTimes(1); expect(createActions).toBeCalledTimes(1); @@ -111,9 +119,6 @@ describe('test EOS contract addapter', () => { debug: jest.fn(), }; - const templatePath = 'test-path'; - adapter.templatePath = templatePath; - const createTables = jest.spyOn(adapter, 'createTables'); createTables.mockImplementation(jest.fn()); @@ -131,7 +136,6 @@ describe('test EOS contract addapter', () => { spyCompile.mockReturnValue(template); adapter.generateFromTemplate(); - expect(adapter.templatePath).toEqual(path.resolve(templatePath, blockchain)); expect(createActions).toBeCalledTimes(1); expect(createActions).toBeCalledTimes(1); diff --git a/packages/bridge/test/icon/ICONContractAdapter.test.ts b/packages/bridge/test/icon/ICONContractAdapter.test.ts index db4250a..81e5e8d 100644 --- a/packages/bridge/test/icon/ICONContractAdapter.test.ts +++ b/packages/bridge/test/icon/ICONContractAdapter.test.ts @@ -75,7 +75,7 @@ describe('test ICON contract addapter', () => { expect(adapter.templatePath).toEqual( path.resolve( path.dirname(require.resolve('@aloxide/bridge')), - '../smart-contract-templates', + '../smart-contract-templates/' + adapter.blockchainType, ), ); @@ -85,15 +85,24 @@ describe('test ICON contract addapter', () => { }); }); describe('test generateFromTemplate', () => { - it('should update templatePath to specific blockchain', () => { + it('should throw error when template path does not exist', () => { + const adapter = new ICONContractAdapter(); + adapter.entityConfigs = entityConfigs; + adapter.logger = { + info: jest.fn(), + debug: jest.fn(), + }; + adapter.templatePath = null; + expect(() => adapter.generateFromTemplate()).toThrowError('Template path not found'); + }); + + it('generateFromTemplate', () => { const adapter = new ICONContractAdapter(); adapter.entityConfigs = entityConfigs; adapter.logger = { info: jest.fn(), debug: jest.fn(), }; - const templatePath = 'test-path'; - adapter.templatePath = templatePath; const spyGenerateInit = jest.spyOn(adapter, 'generateInit'); spyGenerateInit.mockImplementation(jest.fn()); @@ -111,7 +120,6 @@ describe('test ICON contract addapter', () => { spyGenerateTXAPI.mockImplementation(jest.fn()); adapter.generateFromTemplate(); - expect(adapter.templatePath).toEqual(path.resolve(templatePath, blockchain)); expect(spyGenerateInit).toBeCalledTimes(1); expect(spyGeneratePackage).toBeCalledTimes(1);