Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 1ccf76e

Browse files
author
Maksim Daunarovich
authored
Add code transformers (#51)
* Implement transformers * Add transformer for Playground * Create TestUtilities contract * Add scripts and transactions for TestUtilities * Add tests for TestUtilities * Temporarily enable dev-tests * Update path resolvers to use enable complex configs * Add future code for removing hacky solution with import * Update config for base path * Drop type * Update tests * Uncomment line * Update NOTICE * Move testing utilities into FlowManager * Remove TestUtilities * Add interactions for block offset * Remove dangerous rm -rf call * Update package.lock * Add utilities tests * Add transformers for interactions * Add transformers code * Regenerate code * Use AuthAccount * Update utilities tests * Update blockHeight related code and tests * Brush up dev tests * Refactor playground transformer * Refactor emulator * More tests around transformers * Update year. Remove unused import * Provide public method to resolve alias to address * Regenerate code * Update transformers and FlowManager to allow passing arguments instead of hardcoded values * Refactor and clean the code * Add new line at eof * Remove dev tests * Add new line at eof * Fix package.json * Update exports * Remove unused import * Fix test * Regenerate code * Export blockOffset related methods from generated code * Update header with new year range * Update tests to use generated method for controlling blockOffset * Add documentation for blockOffset methods. Update executeScript and sendTransaction signatures. Add CadenceTransformer type * Fix templates * Bump flow-cadut version. Bump package version * Update docs with new links. Update code examples in API section * Create runnable code * Add example runner * Update examples in API documentation * Create runnable examples * Update deploy code logic * Prettify * Update cadut package * Update runner to include list of examples and allow to run by number * Add more examples * Update typo in documentation * Get title from filename * Update cadut package * Refactor * Update code for getBlockOffset and setBlockOffset methods * Add more functionality to emulator * Refactor block offset example * Update emulator management example * Add manager tests for block offset * Fix exports for block offset methods * Add test for emulator processing * Refactor methods for data parsing * Add array of dictionaries example * Default logging to false * Enable dev-test temporarly * Add example to pass array of dictionaries * Update cadut dependency * Fix complex arg example * Update example regexp * Provide more examples * Update example code in API reference * Update flow-cadut package * Prettify * Use new version of flow-cadut * Update array value * Fix flow-cadut imports
1 parent 7d70b70 commit 1ccf76e

Some content is hidden

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

81 files changed

+15725
-28163
lines changed

NOTICE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Flow
2-
Copyright 2019-2020 Dapper Labs, Inc.
2+
Copyright 2019-2021 Dapper Labs, Inc.
33

44
This product includes software developed at Dapper Labs, Inc. (https://www.dapperlabs.com/).

README.md

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
<br />
2-
<p align="center">
1+
<div style="text-align: center; margin: 1em 0 2em">
32
<a href="#">
43
<img src="./js-testing-banner.svg" alt="Logo" width="680" height="auto">
54
</a>
6-
5+
76
<p align="center">
87
<i>Test your Flow applications written in Cadence with ease</i>
98
<br />
@@ -14,11 +13,9 @@
1413
·
1514
<a href="https://github.com/onflow/flow-js-testing/blob/master/CONTRIBUTING.md">Contribute</a>
1615
·
17-
<a href="/docs/installation.md">Installation</a>
16+
<a href="/docs/install.md">Installation</a>
1817
</p>
19-
</p>
20-
<br />
21-
<br />
18+
</div>
2219

2320
# JavaScript Testing Framework for Flow Network
2421

@@ -41,11 +38,11 @@ If you have it already installed, run the `flow init` in your terminal to create
4138
Then start the emulator with `flow emulator -v`.
4239

4340
## Documentation
44-
- [Installation](/docs/installation.md)
41+
42+
- [Installation](/docs/install.md)
4543
- [API](/docs/api.md)
46-
- Examples
47-
- [Basic Usage](/docs/examples/basic.md)
48-
- [Pass Metadata](/docs/examples/metadata.md)
44+
- Extra Examples
45+
- [Metadata](/docs/examples/metadata.md)
4946

5047
## Playground Integration
5148

cadence/contracts/FlowManager.cdc

+65
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub contract FlowManager {
22

3+
/// Account Manager
34
pub event AccountAdded(address: Address)
45

56
pub struct Mapper {
@@ -19,15 +20,79 @@ pub contract FlowManager {
1920
}
2021
}
2122

23+
pub fun getAccountAddress(_ name: String): Address?{
24+
let accountManager = self.account
25+
.getCapability(self.accountManagerPath)
26+
.borrow<&FlowManager.Mapper>()!
27+
28+
return accountManager.getAddress(name)
29+
}
30+
31+
pub let defaultAccounts: {Address : String}
32+
33+
pub fun resolveDefaultAccounts(_ address: Address): Address{
34+
let alias = self.defaultAccounts[address]!
35+
return self.getAccountAddress(alias)!
36+
}
37+
2238
pub let accountManagerStorage: StoragePath
2339
pub let contractManagerStorage: StoragePath
2440
pub let accountManagerPath: PublicPath
2541
pub let contractManagerPath: PublicPath
2642

43+
/// Environment Manager
44+
pub event BlockOffsetChanged(offset: UInt64)
45+
46+
pub struct MockBlock {
47+
pub let id: [UInt8; 32]
48+
pub let height: UInt64
49+
pub let view: UInt64
50+
pub let timestamp: UFix64
51+
52+
init(_ id: [UInt8; 32], _ height: UInt64, _ view: UInt64, _ timestamp: UFix64){
53+
self.id = id
54+
self.height = height
55+
self.view = view
56+
self.timestamp = timestamp
57+
}
58+
}
59+
60+
pub fun setBlockOffset(_ offset: UInt64){
61+
self.blockOffset = offset
62+
emit FlowManager.BlockOffsetChanged(offset: offset)
63+
}
64+
65+
pub fun getBlockHeight(): UInt64 {
66+
var block = getCurrentBlock()
67+
return block.height + self.blockOffset
68+
}
69+
70+
pub fun getBlock(): MockBlock {
71+
var block = getCurrentBlock()
72+
let mockBlock = MockBlock(block.id, block.height, block.view, block.timestamp);
73+
return mockBlock
74+
}
75+
76+
pub var blockOffset: UInt64;
77+
78+
79+
// Initialize contract
2780
init(){
81+
// Environment defaults
82+
self.blockOffset = 0;
83+
84+
// Account Manager initialization
2885
let accountManager = Mapper()
2986
let contractManager = Mapper()
3087

88+
self.defaultAccounts = {
89+
0x01: "Alice",
90+
0x02: "Bob",
91+
0x03: "Charlie",
92+
0x04: "Dave",
93+
0x05: "Eve"
94+
}
95+
3196
self.accountManagerStorage = /storage/testSuitAccountManager
3297
self.contractManagerStorage = /storage/testSuitContractManager
3398

cadence/scripts/get-block-offset.cdc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import FlowManager from 0x01
2+
3+
pub fun main():UInt64 {
4+
return FlowManager.blockOffset
5+
}

cadence/transactions/create-account.cdc

-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,3 @@ transaction (_ name: String, pubKey: String, manager: Address) {
1515
accountManager.setAddress(name, address: address)
1616
}
1717
}
18-

cadence/transactions/deploy-contract.cdc

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ transaction(name:String, code: String, manager: Address ##ARGS-WITH-TYPES##) {
1717
let address = acct.address
1818
contractManager.setAddress(name, address: address)
1919
}
20-
}
20+
}

cadence/transactions/register-contract.cdc

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ transaction(name: String, address: Address) {
88
.borrow<&FlowManager.Mapper>()!
99
contractManager.setAddress(name, address: address)
1010
}
11-
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import FlowManager from 0x01
2+
3+
transaction(offset: UInt64){
4+
prepare(signer:AuthAccount){
5+
FlowManager.setBlockOffset(offset)
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import FlowManager from 0x01
2+
3+
pub fun main(): UInt64 {
4+
let mockedHeight = FlowManager.getBlockHeight();
5+
let realHeight = getCurrentBlock().height;
6+
log("Mocked Height: ".concat(mockedHeight.toString()))
7+
log("Real Height: ".concat(realHeight.toString()))
8+
9+
return mockedHeight - realHeight;
10+
}

dev-test/imports.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe("import resolver", () => {
1515
beforeEach(async () => {
1616
const basePath = path.resolve(__dirname, "./cadence");
1717
const port = 8080;
18-
await init(basePath, port);
18+
await init(basePath, { port });
1919
return emulator.start(port, false);
2020
});
2121

dev-test/metadata.test.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import path from "path";
22
import { init, emulator, executeScript, shallResolve } from "../src";
3-
import { mapValuesToCode } from "flow-cadut";
43

54
jest.setTimeout(10000);
65

dev-test/usage.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ describe("Basic Usage test", () => {
2222
beforeEach(async () => {
2323
const basePath = path.resolve(__dirname, "./cadence");
2424
const port = 8080;
25-
await init(basePath, port);
26-
return emulator.start(port, false);
25+
await init(basePath, { port });
26+
return emulator.start(port);
2727
});
2828

2929
// Stop emulator, so it could be restarted

0 commit comments

Comments
 (0)