Skip to content

Commit cf0498d

Browse files
add tests
Signed-off-by: salaheldinsoliman <[email protected]>
1 parent 92ab9df commit cf0498d

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed

integration/soroban/.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
*.js
21
*.so
32
*.key
43
*.json
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import * as StellarSdk from '@stellar/stellar-sdk';
2+
import { readFileSync } from 'fs';
3+
import { expect } from 'chai';
4+
import path from 'path';
5+
import { fileURLToPath } from 'url';
6+
import { call_contract_function } from './test_helpers.js';
7+
8+
const __filename = fileURLToPath(import.meta.url);
9+
const dirname = path.dirname(__filename);
10+
11+
describe('Runtime Error', () => {
12+
let keypair;
13+
const server = new StellarSdk.SorobanRpc.Server(
14+
"https://soroban-testnet.stellar.org:443",
15+
);
16+
17+
let contractAddr;
18+
let contract;
19+
before(async () => {
20+
21+
console.log('Setting up runtime_error.sol contract tests...');
22+
23+
// read secret from file
24+
const secret = readFileSync('alice.txt', 'utf8').trim();
25+
keypair = StellarSdk.Keypair.fromSecret(secret);
26+
27+
let contractIdFile = path.join(dirname, '.soroban', 'contract-ids', 'Error.txt');
28+
// read contract address from file
29+
contractAddr = readFileSync(contractIdFile, 'utf8').trim().toString();
30+
31+
// load contract
32+
contract = new StellarSdk.Contract(contractAddr);
33+
34+
// initialize the contract
35+
await call_contract_function("init", server, keypair, contract);
36+
37+
// call decrement once. The second call however will result in a runtime error
38+
await call_contract_function("decrement", server, keypair, contract);
39+
});
40+
41+
it('get correct initial counter', async () => {
42+
43+
// decrement the counter again, resulting in a runtime error
44+
let res = await call_contract_function("decrement", server, keypair, contract);
45+
46+
expect(res).to.contain('runtime_error: math overflow in runtime_error.sol:6:9-19');
47+
});
48+
49+
});
50+
51+
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import * as StellarSdk from '@stellar/stellar-sdk';
2+
import { readFileSync } from 'fs';
3+
import { expect } from 'chai';
4+
import path from 'path';
5+
import { fileURLToPath } from 'url';
6+
import { call_contract_function } from './test_helpers.js'; // Helper to interact with the contract
7+
8+
const __filename = fileURLToPath(import.meta.url);
9+
const dirname = path.dirname(__filename);
10+
11+
describe('StorageTypes', () => {
12+
let keypair;
13+
const server = new StellarSdk.SorobanRpc.Server(
14+
"https://soroban-testnet.stellar.org:443",
15+
);
16+
17+
let contractAddr;
18+
let contract;
19+
before(async () => {
20+
console.log('Setting up storage_types contract tests...');
21+
22+
// Read secret from file
23+
const secret = readFileSync('alice.txt', 'utf8').trim();
24+
keypair = StellarSdk.Keypair.fromSecret(secret);
25+
26+
let contractIdFile = path.join(dirname, '.soroban', 'contract-ids', 'storage_types.txt');
27+
// Read contract address from file
28+
contractAddr = readFileSync(contractIdFile, 'utf8').trim().toString();
29+
30+
// Load contract
31+
contract = new StellarSdk.Contract(contractAddr);
32+
33+
// Initialize the contract if necessary (adapt according to your contract logic)
34+
await call_contract_function("init", server, keypair, contract);
35+
});
36+
37+
it('check initial values', async () => {
38+
// Check initial values of all storage variables
39+
let sesa = await call_contract_function("sesa", server, keypair, contract);
40+
expect(sesa.toString()).eq("1");
41+
42+
let sesa1 = await call_contract_function("sesa1", server, keypair, contract);
43+
expect(sesa1.toString()).eq("1");
44+
45+
let sesa2 = await call_contract_function("sesa2", server, keypair, contract);
46+
expect(sesa2.toString()).eq("2");
47+
48+
let sesa3 = await call_contract_function("sesa3", server, keypair, contract);
49+
expect(sesa3.toString()).eq("2");
50+
});
51+
52+
it('increment values', async () => {
53+
// Increment all values by calling the inc function
54+
await call_contract_function("inc", server, keypair, contract);
55+
56+
// Check the incremented values
57+
let sesa = await call_contract_function("sesa", server, keypair, contract);
58+
expect(sesa.toString()).eq("2");
59+
60+
let sesa1 = await call_contract_function("sesa1", server, keypair, contract);
61+
expect(sesa1.toString()).eq("2");
62+
63+
let sesa2 = await call_contract_function("sesa2", server, keypair, contract);
64+
expect(sesa2.toString()).eq("3");
65+
66+
let sesa3 = await call_contract_function("sesa3", server, keypair, contract);
67+
expect(sesa3.toString()).eq("3");
68+
});
69+
70+
it('decrement values', async () => {
71+
// Decrement all values by calling the dec function
72+
await call_contract_function("dec", server, keypair, contract);
73+
74+
// Check the decremented values
75+
let sesa = await call_contract_function("sesa", server, keypair, contract);
76+
expect(sesa.toString()).eq("1");
77+
78+
let sesa1 = await call_contract_function("sesa1", server, keypair, contract);
79+
expect(sesa1.toString()).eq("1");
80+
81+
let sesa2 = await call_contract_function("sesa2", server, keypair, contract);
82+
expect(sesa2.toString()).eq("2");
83+
84+
let sesa3 = await call_contract_function("sesa3", server, keypair, contract);
85+
expect(sesa3.toString()).eq("2");
86+
});
87+
});

0 commit comments

Comments
 (0)