Skip to content

Commit

Permalink
fix(test): contract suite
Browse files Browse the repository at this point in the history
  • Loading branch information
mradkov committed Jun 10, 2021
1 parent 64993ba commit 334f4dd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
9 changes: 4 additions & 5 deletions bin/commands/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import * as R from 'ramda'
import path from 'path'

import { prepareCallParams, readFile, writeFile } from '../utils/helpers'
import * as R from 'ramda'
import { exit, initClientByWalletFile, initCompiler } from '../utils/cli'
import { handleApiError } from '../utils/errors'
import { printError, print, logContractDescriptor, printTransaction, printUnderscored } from '../utils/print'
import { prepareCallParams, readFile, writeFile } from '../utils/helpers'
import { logContractDescriptor, print, printError, printTransaction, printUnderscored } from '../utils/print'

// ## Function which compile your `source` code
export async function compile (file, options) {
Expand Down Expand Up @@ -188,7 +187,7 @@ async function call (walletPath, fn, args, options) {
// Call static or call
const contract = await client.getContractInstance(params.source, { contractAddress: params.address })
const callResult = await contract.call(fn, args, { ...params.options, callStatic, top })
// The execution result, if successful, will be an AEVM-encoded result
// The execution result, if successful, will be an FATE-encoded result
// value. Once type decoding will be implemented in the SDK, this value will
// not be a hexadecimal string, anymore.
json && print(callResult)
Expand Down
46 changes: 24 additions & 22 deletions test/cli/contract.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,26 @@
*/

import fs from 'fs'
import { before, after, describe, it } from 'mocha'

import { configure, plan, ready, WALLET_NAME, execute as exec, KEY_PAIR } from './index'
import { after, before, describe, it } from 'mocha'
import { configure, execute as exec, KEY_PAIR, plan, ready, WALLET_NAME } from './index'

// CONTRACT SOURCE
const testContract = `contract Identity =
entrypoint main(x : int, y: int) = x + y
const testContract = `
@compiler >= 6
contract Identity =
entrypoint test(x : int, y: int) = x + y
`

const CALL_DATA = 'cb_KxG4F37sKwIEFmEjaA=='
const DECODED_CALL_DATA = { arguments: [{ type: 'int', value: 1 }, { type: 'int', value: 2 }], function: 'main' }
const CALL_DATA = 'cb_KxGSiyA2KwIEFfUrtQ=='
const DECODED_CALL_DATA = { arguments: [{ type: 'int', value: 1 }, { type: 'int', value: 2 }], function: 'test' }

plan(1000000000)

describe('CLI Contract Module', function () {
configure(this)
const contractFile = 'testContract'
let deployDescriptor
let wallet
let bytecode
let cAddress
let deployDescriptor, wallet, bytecode, cAddress

before(async function () {
// Spend tokens for wallet
Expand All @@ -57,16 +56,16 @@ describe('CLI Contract Module', function () {
it('Compile Contract', async () => {
// Create contract file
// Compile contract
const compiled = await wallet.contractCompile(testContract)
const compiled = await wallet.contractCompile(testContract).catch(console.error)
const compiledCLI = (await exec(['contract', 'compile', contractFile]))
const bytecodeCLI = compiledCLI.split(':')[1].trim()
bytecode = compiled.bytecode

bytecodeCLI.should.equal(compiled.bytecode)
bytecodeCLI.should.be.equal(compiled.bytecode)
})

it('Encode callData', async () => {
const { callData } = JSON.parse(await exec(['contract', 'encodeData', contractFile, 'main', '1', '2', '--json']))
const { callData } = JSON.parse(await exec(['contract', 'encodeData', contractFile, 'test', '1', '2', '--json']))
callData.should.be.equal(CALL_DATA)
})

Expand All @@ -89,31 +88,34 @@ describe('CLI Contract Module', function () {
cAddress = contractId
contractId.should.be.a('string')
transaction.should.be.a('string')
name.should.equal(contractFile)
pref.should.equal('deploy')
add.should.equal(KEY_PAIR.publicKey.split('_')[1])
name.should.be.equal(contractFile)
pref.should.be.equal('deploy')
add.should.be.equal(KEY_PAIR.publicKey.split('_')[1])
})

it('Call Contract by descriptor', async () => {
// Call contract
const res = await exec(['contract', 'call', WALLET_NAME, '--password', 'test', '--json', '--descrPath', deployDescriptor, 'main', '1', '2'])
const res = await exec(['contract', 'call', WALLET_NAME, '--password', 'test', '--json', '--descrPath', deployDescriptor, 'test', '1', '2'])
const callResponse = JSON.parse(res)
callResponse.result.returnValue.should.contain('cb_')
callResponse.decodedResult.should.equal(3)
callResponse.decodedResult.should.be.equal(3)
})

it('Call Contract static by descriptor', async () => {
// Call contract
const callResponse = (JSON.parse(await exec(['contract', 'call', WALLET_NAME, '--password', 'test', '--json', '--descrPath', deployDescriptor, 'main', '1', '2', '--callStatic'])))
const callResponse = (JSON.parse(await exec(['contract', 'call', WALLET_NAME, '--password', 'test', '--json', '--descrPath', deployDescriptor, 'test', '1', '2', '--callStatic'])))
callResponse.result.returnValue.should.contain('cb_')
callResponse.decodedResult.should.equal(3)
})

it('Call Contract by contract address', async () => {
const callResponse = (JSON.parse(await exec(['contract', 'call', WALLET_NAME, '--password', 'test', '--json', '--contractAddress', cAddress, '--contractSource', contractFile, 'main', '1', '2'])))
const callResponse = (JSON.parse(await exec(['contract', 'call', WALLET_NAME, '--password', 'test', '--json', '--contractAddress', cAddress, '--contractSource', contractFile, 'test', '1', '2'])))
callResponse.result.returnValue.should.contain('cb_')
callResponse.decodedResult.should.equal(3)
})

it('Call Contract static by contract address', async () => {
const callResponse = (JSON.parse(await exec(['contract', 'call', WALLET_NAME, '--password', 'test', '--json', '--contractAddress', cAddress, '--contractSource', contractFile, 'main', '1', '2', '--callStatic'])))
const callResponse = (JSON.parse(await exec(['contract', 'call', WALLET_NAME, '--password', 'test', '--json', '--contractAddress', cAddress, '--contractSource', contractFile, 'test', '1', '2', '--callStatic'])))
callResponse.result.returnValue.should.contain('cb_')
callResponse.decodedResult.should.equal(3)
})
Expand Down

0 comments on commit 334f4dd

Please sign in to comment.