Skip to content

Commit

Permalink
Multiline Arguments and Optionals (#17)
Browse files Browse the repository at this point in the history
* Update code

* Update flow.json

* Start api documentation

* Add example tests

* Remove spaces in getArrayType result

* Add method to completely remove all spaces in a string

* Export additional methods and arguments

* Add more tests and API documentation

* Replace default dependency

* Update templates

* Add fix for non existent folders

* Add test Cadence templates for generator

* Add generator tests

* Regenerate templates

* Add documentation for Prettier and generator related methods

* Reformat headers. refactor method description

* Update environment logic. add setEnvironment method

* Add env tests

* Export additional methods

* Add docs for environment methods

* Ad aliases

* Add transaction documentation

* Fix dev tests

* Refactor for seal status

* Update handlebars templates to default interaction props to empty object

* update package lock

* Refactor parser to work with multiline arguments

* Add Optionals mapping

* Move generator tests into new folder

* Create tests for Optionals

* fix test name

* Refactor code for mapped arguments

* Update generate-dev test

* Fix emulator logging. Add Dictionary test

* Refactor Dictionary resolver

* Fix failing tests

* Add ignore pattern for dev tests

* Fix code for failed tests. Update tests

* Update import

* Add tests for multiline arguments. Update parser to remove trailing comma

* Update changelog

* Update CHANGELOG

* Add crypto libraries for signing transactions

* Add tx related utilities

* Add transactions tests

* Update private key

* Bump version. Move prettier into dependencies

* Refactor
  • Loading branch information
Maksim Daunarovich authored Sep 20, 2021
1 parent 340e73d commit 9c050e8
Show file tree
Hide file tree
Showing 44 changed files with 22,362 additions and 485 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 0.1.7 - 2021-09-20
- Added support for Optionals
- Added support for multi-line arguments

### 0.1.6 - 2021-07-26
- Fixed support for nested arrays
- Improved support for complex types
Expand Down
12 changes: 12 additions & 0 deletions dev-test/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}
5 changes: 5 additions & 0 deletions dev-test/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
testEnvironment: 'node',
verbose: true,
coveragePathIgnorePatterns: ["/node_modules/"],
};
21,437 changes: 21,000 additions & 437 deletions dev-test/package-lock.json

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions dev-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "node index.js"
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@onflow/config": "^0.0.2",
"@onflow/fcl": "^0.0.73",
"@babel/core": "^7.15.5",
"@babel/preset-env": "^7.15.6",
"babel-jest": "^27.2.0",
"elliptic": "^6.5.4",
"esm": "^3.2.25",
"rlp": "^2.2.6",
"sha3": "^2.1.4"
"flow-js-testing": "^0.1.13",
"jest": "^27.2.0",
"jest-environment-node": "^27.2.0"
},
"dependencies": {
"@onflow/fcl-config": "^0.0.1"
"sha3": "^2.1.4"
}
}
230 changes: 230 additions & 0 deletions dev-test/test/args.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
import path from "path";
import { emulator, init } from "flow-js-testing";
import { query } from "@onflow/fcl";
import { mapValuesToCode } from "../../src";
import { padAddress, toFixedValue } from "../../src/fixer";

// Increase timeout if your tests failing due to timeout
jest.setTimeout(10000);

describe("optional arguments", () => {
beforeEach(async () => {
const basePath = path.resolve(__dirname, "../cadence");
// You can specify different port to parallelize execution of describe blocks
const port = 8080;
// Setting logging flag to true will pipe emulator output to console
const logging = false;

await init(basePath, { port });
return emulator.start(port, logging);
});

// Stop emulator, so it could be restarted
afterEach(async () => {
return emulator.stop();
});

it("optionals - String? - no value", async () => {
const input = null;
const cadence = `
pub fun main(message: String?): String?{
return message
}
`;

const args = () => mapValuesToCode(cadence, [input]);
const output = await query({ cadence, args });

expect(output).toBe(input);
});
it("optionals - String? - with value", async () => {
const input = "Cadence";
const cadence = `
pub fun main(message: String?): String?{
return message
}
`;

const args = () => mapValuesToCode(cadence, [input]);
const output = await query({ cadence, args });

expect(output).toBe(input);
});
it("optionals - Address? - no value", async () => {
const input = null;
const expected = false;
const cadence = `
pub fun main(address: Address?): Bool{
if(address != nil){
return true
}
return false
}
`;

const args = () => mapValuesToCode(cadence, [input]);
const output = await query({ cadence, args });

expect(output).toBe(expected);
});
it("optionals - Address? - with value", async () => {
const input = "0x01";
const expected = true;
const cadence = `
pub fun main(address: Address?): Bool{
if(address != nil){
return true
}
return false
}
`;

const args = () => mapValuesToCode(cadence, [input]);
const output = await query({ cadence, args });

expect(output).toBe(expected);
});
it("optionals - [String?] - no value", async () => {
const input = [null];
const expected = null;
const cadence = `
pub fun main(names: [String?]): String?{
return names[0]
}
`;

const args = () => mapValuesToCode(cadence, [input]);
const output = await query({ cadence, args });

expect(output).toBe(expected);
});
it("optionals - [String?] - with value", async () => {
const input = ["Cadence"];
const expected = "Cadence";
const cadence = `
pub fun main(names: [String?]): String?{
return names[0]
}
`;

const args = () => mapValuesToCode(cadence, [input]);
const output = await query({ cadence, args });

expect(output).toBe(expected);
});
it("optionals - {String: String?} - no value", async () => {
const input = { name: "Cadence" };
const expected = null;
const cadence = `
pub fun main(metadata: {String: String?}): String?{
log(metadata)
let meta = metadata["empty"]
if ( meta == nil){
return nil
}
return meta!
}
`;

const args = () => mapValuesToCode(cadence, [input]);
const output = await query({ cadence, args });

expect(output).toBe(expected);
});
it("optionals - {String: String?} - with value", async () => {
const input = { name: "Cadence" };
const expected = "Cadence";
const cadence = `
pub fun main(metadata: {String: String?}): String?{
return metadata["name"]!
}
`;

const args = () => mapValuesToCode(cadence, [input]);
const output = await query({ cadence, args });

expect(output).toBe(expected);
});
it("case suit", async () => {
const makeTemplate = (type) => `
pub fun main(message: ${type}?): ${type}?{
return message
}
`;

const StringMap = `
pub fun main(metadata: {String: String?}, key: String): String?{
return metadata[key]!
}
`;

const StringArray = `
pub fun main(names: [String?]): String?{
return names[0]
}
`;

const templates = {
UFix64: makeTemplate("UFix64"),
String: makeTemplate("String"),
Address: makeTemplate("Address"),
};

const tests = [
{
input: "Cadence",
cadence: templates.String,
},
{
input: null,
cadence: templates.String,
},
{
input: toFixedValue(1.337),
cadence: templates.UFix64,
},
{
input: null,
cadence: templates.UFix64,
},
{
input: padAddress("0x01"),
cadence: templates.Address,
},
{
input: null,
cadence: templates.Address,
},
{
input: ["Cadence"],
cadence: StringArray,
expected: "Cadence",
},
{
input: [null],
cadence: StringArray,
expected: null,
},
{
rawArgs: [
{
name: "James",
},
"name",
],
cadence: StringMap,
expected: "James",
},
];

for (const i in tests) {
const { input, expected, cadence, rawArgs } = tests[i];

const mapped = mapValuesToCode(cadence, rawArgs || [input]);
const args = () => mapped;
const output = await query({ cadence, args });
await expect(output).toBe(expected === undefined ? input : expected);
}
await expect.assertions(tests.length);
});
});
Loading

0 comments on commit 9c050e8

Please sign in to comment.