-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Worker } from 'near-workspaces'; | ||
import test from 'ava'; | ||
|
||
test.beforeEach(async t => { | ||
const worker = await Worker.init(); | ||
const root = worker.rootAccount; | ||
|
||
const reqireInitFalse = await root.devDeploy('build/require_init_false.wasm'); | ||
const reqireInitTrue = await root.devDeploy('build/require_init_true.wasm'); | ||
|
||
const ali = await root.createSubAccount('ali'); | ||
|
||
t.context.worker = worker; | ||
t.context.accounts = { | ||
root, | ||
reqireInitFalse, | ||
reqireInitTrue, | ||
ali, | ||
}; | ||
}); | ||
|
||
test.afterEach(async t => { | ||
await t.context.worker.tearDown().catch(error => { | ||
console.log('Failed to tear down the worker:', error); | ||
}); | ||
}); | ||
|
||
test('Uninitialized contract throw error if requireInit = true', async t => { | ||
const { ali, reqireInitTrue } = t.context.accounts; | ||
|
||
const callResult = await ali.callRaw(reqireInitTrue, 'setStatus', { status: 'hello' }); | ||
t.assert(callResult.result.status.Failure.ActionError.kind.FunctionCallError.ExecutionError.includes('Contract must be initialized')); | ||
|
||
const err = await t.throwsAsync(() => reqireInitTrue.view('getStatus', {})); | ||
t.assert(err.message.includes('Contract must be initialized')); | ||
}) | ||
|
||
test('Uninitialized contract does not throw error if requireInit = false', async t => { | ||
const { ali, reqireInitFalse } = t.context.accounts; | ||
|
||
await ali.callRaw(reqireInitFalse, 'setStatus', { status: 'hello' }); | ||
|
||
t.is(await reqireInitFalse.view('getStatus', {}), 'hello') | ||
|
||
}) | ||
|
||
test('Init function panics if called more then once', async t => { | ||
const { ali, reqireInitTrue, reqireInitFalse } = t.context.accounts; | ||
|
||
await ali.call(reqireInitTrue, 'init', { status: 'hello' }); | ||
const res1 = await ali.callRaw(reqireInitTrue, 'init', { status: 'hello' }); | ||
t.assert(res1.result.status.Failure.ActionError.kind.FunctionCallError.ExecutionError.includes('Contract already initialized')); | ||
|
||
|
||
await ali.call(reqireInitFalse, 'init', { status: 'hello' }); | ||
const res2 = await ali.callRaw(reqireInitFalse, 'init', { status: 'hello' }); | ||
t.assert(res2.result.status.Failure.ActionError.kind.FunctionCallError.ExecutionError.includes('Contract already initialized')); | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
"type": "module", | ||
"scripts": { | ||
"postinstall": "cd .. && yarn link && cd tests && yarn link near-sdk-js", | ||
"build": "yarn build:context-api && yarn build:math-api && yarn build:storage-api && yarn build:log-panic-api && yarn build:promise-api && yarn build:promise-batch-api && yarn build:function-params && yarn build:lookup-map && yarn build:lookup-set && yarn build:unordered-map && yarn build:unordered-set && yarn build:vector && yarn build:bytes && yarn build:typescript && yarn build:public-key", | ||
"build": "yarn build:context-api && yarn build:math-api && yarn build:storage-api && yarn build:log-panic-api && yarn build:promise-api && yarn build:promise-batch-api && yarn build:function-params && yarn build:lookup-map && yarn build:lookup-set && yarn build:unordered-map && yarn build:unordered-set && yarn build:vector && yarn build:bytes && yarn build:typescript && yarn build:public-key && yarn build:near-bindgen", | ||
"build:context-api": "near-sdk-js build src/context_api.js build/context_api.wasm", | ||
"build:math-api": "near-sdk-js build src/math_api.js build/math_api.wasm", | ||
"build:storage-api": "near-sdk-js build src/storage_api.js build/storage_api.wasm", | ||
|
@@ -22,6 +22,7 @@ | |
"build:bytes": "near-sdk-js build src/bytes.js build/bytes.wasm", | ||
"build:typescript": "near-sdk-js build src/typescript.ts build/typescript.wasm", | ||
"build:public-key": "near-sdk-js build src/public-key.js build/public-key.wasm", | ||
"build:near-bindgen": "near-sdk-js build src/near_bindgen/require_init_true.ts build/require_init_true.wasm && near-sdk-js build src/near_bindgen/require_init_false.ts build/require_init_false.wasm", | ||
"test": "ava", | ||
"test:context-api": "ava __tests__/test_context_api.ava.js", | ||
"test:math-api": "ava __tests__/test_math_api.ava.js", | ||
|
@@ -36,7 +37,8 @@ | |
"test:vector": "ava __tests__/vector.ava.js", | ||
"test:bytes": "ava __tests__/bytes.ava.js", | ||
"test:typescript": "ava __tests__/typescript.ava.js", | ||
"test:public-key": "ava __tests__/test-public-key.ava.js" | ||
"test:public-key": "ava __tests__/test-public-key.ava.js", | ||
"test:near-bindgen": "ava __tests__/near_bindgen.ava.js" | ||
}, | ||
"author": "Near Inc <[email protected]>", | ||
"license": "Apache-2.0", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { | ||
near, | ||
NearBindgen, | ||
call, | ||
view, | ||
initialize, | ||
} from 'near-sdk-js' | ||
|
||
@NearBindgen({ requireInit: false }) | ||
class NBTest { | ||
status: string; | ||
|
||
constructor() { | ||
this.status = ''; | ||
} | ||
|
||
@initialize | ||
init({ status }: { status: string }): void { | ||
near.log(`init: ${status}`) | ||
this.status = status; | ||
} | ||
|
||
@view | ||
getStatus(): string { | ||
near.log(`getStatus: ${this.status}`) | ||
return this.status; | ||
} | ||
|
||
@call | ||
setStatus({ status }: { status: string }): void { | ||
near.log(`setStatus: ${status}`) | ||
this.status = status; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { | ||
near, | ||
NearBindgen, | ||
call, | ||
view, | ||
initialize, | ||
} from 'near-sdk-js' | ||
|
||
@NearBindgen({ requireInit: true }) | ||
class NBTest { | ||
status: string; | ||
|
||
constructor() { | ||
this.status = ''; | ||
} | ||
|
||
@initialize | ||
init({ status }: { status: string }): void { | ||
near.log(`init: ${status}`) | ||
this.status = status; | ||
} | ||
|
||
@view | ||
getStatus(): string { | ||
near.log(`getStatus: ${this.status}`) | ||
return this.status; | ||
} | ||
|
||
@call | ||
setStatus({ status }: { status: string }): void { | ||
near.log(`setStatus: ${status}`) | ||
this.status = status; | ||
} | ||
} | ||
|
||
|