-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathsub-routine.ts
110 lines (94 loc) · 3.42 KB
/
sub-routine.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { types } from "@algo-builder/web";
import { assert } from "chai";
import { RUNTIME_ERRORS } from "../../src/errors/errors-list";
import { AccountStore, Runtime } from "../../src/index";
import { useFixture } from "../helpers/integration";
import { expectRuntimeError } from "../helpers/runtime-errors";
describe("TEALv4: Sub routine", function () {
useFixture("sub-routine");
const john = new AccountStore(10e6);
let runtime: Runtime;
let approvalProgramPassFileName: string;
let approvalProgramFailFileName: string;
let approvalProgramFail1FileName: string;
let clearProgramFilename: string;
let appDefinition: types.AppDefinitionFromFile;
this.beforeAll(async function () {
runtime = new Runtime([john]); // setup test
approvalProgramPassFileName = "approval-pass.teal";
approvalProgramFailFileName = "approval-fail.teal";
approvalProgramFail1FileName = "approval-fail-1.teal";
clearProgramFilename = "clear.teal";
appDefinition = {
appName: "app",
metaType: types.MetaType.FILE,
approvalProgramFilename: approvalProgramPassFileName,
clearProgramFilename,
globalBytes: 1,
globalInts: 1,
localBytes: 1,
localInts: 1,
appArgs: ["int:5"],
};
});
it("should pass during create application", function () {
// this code will pass, because sub-routine is working
assert.doesNotThrow(() => runtime.deployApp(john.account, appDefinition, {}));
});
it("should fail during create application", function () {
// this fails because in last condition we check if over subroutine section was executed
appDefinition.approvalProgramFilename = approvalProgramFailFileName;
expectRuntimeError(
() => runtime.deployApp(john.account, appDefinition, {}),
RUNTIME_ERRORS.TEAL.REJECTED_BY_LOGIC
);
});
it("should fail during create application", function () {
// this fails because there is no callsub before retsub(therefore callstack is empty)
appDefinition.approvalProgramFilename = approvalProgramFail1FileName;
expectRuntimeError(
() => runtime.deployApp(john.account, appDefinition, {}),
RUNTIME_ERRORS.TEAL.CALL_STACK_EMPTY
);
});
it("should calculate correct fibonacci number", function () {
appDefinition.approvalProgramFilename = "fibonacci.teal";
appDefinition.appName = "Fibo5";
let appID = runtime.deployApp(
john.account,
{
...appDefinition,
},
{}
).appID;
// 5th fibonacci
let result = runtime.getGlobalState(appID, "result");
assert.equal(result, 5n);
// 6th fibonacci
appDefinition.appArgs = ["int:6"];
appDefinition.appName = "Fibo6";
appID = runtime.deployApp(john.account, appDefinition, {}).appID;
result = runtime.getGlobalState(appID, "result");
assert.equal(result, 8n);
// 8th fibonacci
appDefinition.appArgs = ["int:8"];
appDefinition.appName = "Fibo8";
appID = runtime.deployApp(john.account, appDefinition, {}).appID;
result = runtime.getGlobalState(appID, "result");
assert.equal(result, 21n);
// 1st fibonacci
appDefinition.appArgs = ["int:1"];
appDefinition.appName = "Fibo1";
appID = runtime.deployApp(john.account, appDefinition, {}).appID;
result = runtime.getGlobalState(appID, "result");
assert.equal(result, 1n);
});
it("should throw cost exceed error", function () {
appDefinition.appArgs = ["int:9"];
appDefinition.approvalProgramFilename = "fibonacci.teal";
expectRuntimeError(
() => runtime.deployApp(john.account, appDefinition, {}),
RUNTIME_ERRORS.TEAL.MAX_COST_EXCEEDED
);
});
});