Skip to content

Commit e641cef

Browse files
Willem Wyndhamamgando
Willem Wyndham
andauthored
Use new near-mock-vm (#24)
* Remove Compiler warnings for null checks and cleaned up a little * switched to use near-mock-vm and added tests to nearcore tests * Update config to use strings for u128 and update outcome as class * Transition to decorator support and simplify * implement PersistentSet with tests * update nearcore-tests - removed config arg from cli - will return to this after updated near-vm-runtime-runner is released Co-authored-by: Sherif Abushadi <[email protected]>
1 parent c185872 commit e641cef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+545
-5545
lines changed

.gitpod.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ github:
1818
# add a "Review in Gitpod" button to the pull request's description (defaults to false)
1919
addBadge: false
2020
# add a label once the prebuild is ready to pull requests (defaults to false)
21-
addLabel: false
21+
addLabel: true
2222

2323
# List the start up tasks. You can start them in parallel in multiple terminals. See https://www.gitpod.io/docs/config-start-tasks/
2424
tasks:

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ tsconfig.json
33
nearcore-tests/
44
assembly/__tests__
55
**/*/out/
6+
bindgen/src
7+
bindgen/out
8+
bindgen/asconfig.js
9+
bindgen/test.js

assembly/__tests__/bindgen/main.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//@nearfile
21
// These imports need to be copied properly
32
import { storage, context, ContractPromise, logging } from "../..";
43
import { FooBar, ContainerClass, AnotherContainerClass, PromiseArgs, MyCallbackResult, MyContractPromiseResult } from "./model";

assembly/__tests__/bindgen/model.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

2-
// @nearfile
32

43
import { u128 } from "../..";
54

5+
@nearBindgen
66
export class FooBar {
77
foo: i32 = 0;
88
bar: u32 = 1;
@@ -21,20 +21,24 @@ export class FooBar {
2121
u64Arr: u64[];
2222
}
2323

24+
@nearBindgen
2425
export class Nullables {
2526
str: string;
2627
u128: u128;
2728
uint8Array: Uint8Array;
2829
}
2930

31+
@nearBindgen
3032
export class ContainerClass {
3133
foobar: FooBar
3234
}
3335

36+
@nearBindgen
3437
export class AnotherContainerClass {
3538
foobars: Array<Array<FooBar>>
3639
}
3740

41+
@nearBindgen
3842
export class PromiseArgs {
3943
receiver: string;
4044
methodName: string;
@@ -45,16 +49,19 @@ export class PromiseArgs {
4549
callbackBalance: i32;
4650
}
4751

52+
@nearBindgen
4853
export class MyContractPromiseResult {
4954
ok: bool;
5055
r: MyCallbackResult;
5156
}
5257

58+
@nearBindgen
5359
export class MyCallbackResult {
5460
rs: MyContractPromiseResult[];
5561
n: string;
5662
}
5763

64+
@nearBindgen
5865
export class Generic<T> {
5966
value: T;
6067
}

assembly/__tests__/contract.spec.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,6 @@ import { Contract } from "./contract";
33
import { VM } from "../vm";
44

55
describe("Contract", () => {
6-
beforeAll(() => {
7-
VM.saveState();
8-
});
9-
10-
afterAll(() => {
11-
VM.restoreState();
12-
});
13-
14-
it("should be able to be stored and retrieved", () => {
15-
const contract = new Contract("unique name!");
16-
storage.set("contract", contract);
17-
const otherContract = storage.get<Contract>("contract")!;
18-
expect(contract.name).toBe(otherContract.name, "contracts should have the same name");
19-
expect(contract.name).not.toBe("", "contract's name should not be empty");
20-
});
21-
22-
it("should still be in the storage", () => {
23-
const otherContract = storage.get<Contract>("contract")!;
24-
expect(otherContract.name).toBe("unique name!", "contracts should have the same name");
25-
expect(otherContract.name).not.toBe("", "contract's name should not be empty");
26-
});
27-
});
28-
29-
describe("Contract", () => {
30-
beforeEach( () => {
31-
VM.saveState();
32-
});
336

347
afterEach(() => {
358
VM.restoreState();

assembly/__tests__/contract.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//@nearfile
21

2+
@nearBindgen
33
export class Contract {
44
constructor(public name: string) {}
55
}

assembly/__tests__/model.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
//@nearfile
1+
22
// Exporting a new class TextMessage so it can be used outside of this file.
3+
@nearBindgen
34
export class TextMessage {
45
sender: string;
56
text: string;

assembly/__tests__/runtime/generic.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@nearfile
22

3+
@nearBindgen
34
export class Box<T> {
45
t: T;
56
}

assembly/__tests__/runtime/main.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//@nearfile
21
import { u128, context, storage, logging, base58, base64, PersistentMap, PersistentVector, PersistentDeque, ContractPromise, math } from "../..";
32
import { TextMessage } from "./model";
43
import { _testTextMessage, _testTextMessageTwo, _testBytes, _testBytesTwo } from "./util";

assembly/__tests__/runtime/model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//@nearfile
21
// Exporting a new class TextMessage so it can be used outside of this file.
2+
@nearBindgen
33
export class TextMessage {
44
sender: string;
55
text: string;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { PersistentSet } from "../../runtime";
2+
3+
let set: PersistentSet<string>
4+
const item = "hello"
5+
6+
function addItem(_item: string = item): void {
7+
set.add(_item)
8+
}
9+
10+
describe("Sets should handle", () => {
11+
12+
beforeEach(() => {
13+
set = new PersistentSet<string>("set1");
14+
})
15+
16+
it("supports adding items", () => {
17+
addItem()
18+
expect(set.has(item)).toBeTruthy("The set should have the item")
19+
})
20+
21+
it("checks for items", () => {
22+
expect(set.has(item)).toBeFalsy("The set should not have the item")
23+
})
24+
25+
throws("if attempting to delete a non-existent item", () => {
26+
set.delete(item)
27+
})
28+
29+
it("can be cleared", () => {
30+
addItem()
31+
expect(set.size).toBe(1)
32+
set.clear()
33+
expect(set.size).toBe(0)
34+
})
35+
36+
it("only allows items to be added once", () => {
37+
addItem()
38+
addItem()
39+
addItem()
40+
expect(set.size).toBe(1)
41+
})
42+
43+
it("allows multiple items to be added", () => {
44+
addItem("hello")
45+
addItem("world")
46+
expect(set.size).toBe(2)
47+
})
48+
49+
it("deleting items", () => {
50+
addItem()
51+
expect(set.has(item)).toBeTruthy("The item should be found first")
52+
set.delete(item)
53+
expect(set.has(item)).toBeFalsy("The item should be deleted")
54+
});
55+
56+
it("returns a list of values as an array", () => {
57+
addItem("one")
58+
addItem("two")
59+
addItem("three")
60+
61+
expect(set.values()).toStrictEqual(["one", "two", "three"])
62+
})
63+
64+
it("returns a list of values out of order after delete", () => {
65+
addItem("one")
66+
addItem("two")
67+
addItem("three")
68+
addItem("four")
69+
70+
set.delete("two")
71+
72+
let values = set.values()
73+
expect(values).not.toInclude("two")
74+
expect(values).toStrictEqual(["one", "four", "three"])
75+
})
76+
77+
78+
})

0 commit comments

Comments
 (0)