This commit is contained in:
folex
2019-08-16 17:24:18 +03:00
parent 6b061dde92
commit fc3b9bad73
18 changed files with 79 additions and 992 deletions

View File

@ -12,7 +12,6 @@
"description": "", "description": "",
"devDependencies": { "devDependencies": {
"assemblyscript": "github:assemblyscript/assemblyscript", "assemblyscript": "github:assemblyscript/assemblyscript",
"assemblyscript-json": "github:fluencelabs/assemblyscript-json#update-as1",
"assemblyscript-sdk": "github:fluencelabs/assemblyscript-sdk", "assemblyscript-sdk": "github:fluencelabs/assemblyscript-sdk",
"@as-pect/assembly": "^2.3.1", "@as-pect/assembly": "^2.3.1",
"@as-pect/cli": "^2.3.1", "@as-pect/cli": "^2.3.1",

View File

@ -1,69 +0,0 @@
module.exports = {
/**
* A set of globs passed to the glob package that qualify typescript files for testing.
*/
include: ["assembly/__tests__/**/*.spec.ts"],
/**
* A set of globs passed to the glob package that quality files to be added to each test.
*/
add: ["assembly/__tests__/**/*.include.ts"],
/**
* All the compiler flags needed for this test suite. Make sure that a binary file is output.
*/
flags: {
/** To output a wat file, uncomment the following line. */
// "--textFile": ["output.wat"],
/** A runtime must be provided here. */
"--runtime": ["full"] // Acceptable values are: full, half, stub (arena), and none
},
/**
* A set of regexp that will disclude source files from testing.
*/
disclude: [/node_modules/],
/**
* Add your required AssemblyScript imports here.
*/
imports: {},
/**
* All performance statistics reporting can be configured here.
*/
performance: {
/** Enable performance statistics gathering for every test. */
enabled: false,
/** Set the maximum number of samples to run for every test. */
maxSamples: 10000,
/** Set the maximum test run time in milliseconds for every test. */
maxTestRunTime: 5000,
/** Report the median time in the default reporter for every test. */
reportMedian: true,
/** Report the average time in milliseconds for every test. */
reportAverage: true,
/** Report the standard deviation for every test. */
reportStandardDeviation: false,
/** Report the maximum run time in milliseconds for every test. */
reportMax: false,
/** Report the minimum run time in milliseconds for every test. */
reportMin: false,
},
/**
* Add a custom reporter here if you want one. The following example is in typescript.
*
* @example
* import { TestReporter, TestGroup, TestResult, TestContext } from "as-pect";
*
* export class CustomReporter extends TestReporter {
* // implement each abstract method here
* public abstract onStart(suite: TestContext): void;
* public abstract onGroupStart(group: TestGroup): void;
* public abstract onGroupFinish(group: TestGroup): void;
* public abstract onTestStart(group: TestGroup, result: TestResult): void;
* public abstract onTestFinish(group: TestGroup, result: TestResult): void;
* public abstract onFinish(suite: TestContext): void;
* }
*/
// reporter: new CustomReporter(),
/**
* Specify if the binary wasm file should be written to the file system.
*/
outputBinary: false,
};

View File

@ -1,865 +0,0 @@
/**
* This function creates a test group in the test loader.
*
* @param {string} description - This is the name of the test group.
* @param {() => void} callback - A function that contains all of the closures for this test group.
*
* @example
*
* ```ts
* describe("my test suite", (): void => {
* // put your tests here
* });
* ```
*/
declare function describe(description: string, callback: () => void): void;
/**
* This function creates a test inside the given test group. It must be placed inside a describe
* block.
*
* @param {string} description - This is the name of the test, and should describe a behavior.
* @param {() => void} callback - A function that contains a set of expectations for this test.
*
* @example
*
* ```ts
* describe("the meaning of life", (): void => {
* it("should be 42", (): void => {
* // put your expectations here
* expect<i32>(29 + 13).toBe(42);
* });
* });
* ```
*/
declare function it(description: string, callback: () => void): void;
/**
* A test that does not run, and is longhand equivalent to using todo function without a
* callback. This test does not get run and is reported like a todo.
*
* @param {string} description - This is the name of the test, and should describe a behavior.
* @param {() => void} callback - A function that contains a set of expectations for this test.
*/
declare function xit(description: string, callback: () => void): void;
/**
* A test that does not run, and is longhand equivalent to using todo function without a
* callback. This test does not get run and is reported like a todo.
*
* @param {string} description - This is the name of the test, and should describe a behavior.
* @param {() => void} callback - A function that contains a set of expectations for this test.
*/
declare function xtest(description: string, callback: () => void): void;
/**
* This function creates a test inside the given test group. It must be placed inside a describe
* block.
*
* @param {string} description - This is the name of the test, and should describe a behavior.
* @param {() => void} callback - A function that contains a set of expectations for this test.
*
* @example
* ```ts
* describe("the meaning of life", (): void => {
* test("the value should be 42", (): void => {
* // put your expectations here
* expect<i32>(29 + 13).toBe(42);
* });
* });
* ```
*/
declare function test(description: string, callback: () => void): void;
/**
* This function creates a test that is expected to fail. This is useful to verify if a given
* behavior is expected to throw.
*
* @param {string} description - This is the name of the test, and should describe a behavior.
* @param {() => void} callback - A function that contains a set of expectations for this test.
* @param {string?} message - A message that describes why the test should fail.
*
* @example
*
* ```ts
* describe("the meaning of life", (): void => {
* throws("the value should be 42", (): void => {
* // put your expectations here
* expect<i32>(29 + 13).not.toBe(42);
* });
* });
* ```
*/
declare function throws(description: string, callback: () => void, message?: string): void;
/**
* This function creates a test that is expected to fail. This is useful to verify if a given
* behavior is expected to throw.
*
* @param {string} description - This is the name of the test, and should describe a behavior.
* @param {() => void} callback - A function that contains a set of expectations for this test.
* @param {string?} message - A message that describes why the test should fail.
*
* @example
*
* ```ts
* describe("the meaning of life", (): void => {
* itThrows("when the value should be 42", (): void => {
* // put your expectations here
* expect<i32>(29 + 13).not.toBe(42);
* }, "The value is actually 42.");
* });
* ```
*/
declare function itThrows(description: string, callback: () => void, message?: string): void;
/**
* This function creates a callback that is called before each individual test is run in this test
* group.
*
* @param {function} callback - The function to be run before each test in the current test group.
*
* @example
*
* ```ts
* // create a global
* var cat: Cat = new Cat();
*
* describe("cats", (): void => {
* beforeEach((): void => {
* cat.meow(1); // meow once per test
* });
* });
* ```
*/
declare function beforeEach(callback: () => void): void;
/**
* This function creates a callback that is called before the whole test group is run, and only
* once.
*
* @param {function} callback - The function to be run before each test in the current test group.
*
* @example
*
* ```ts
* // create a global
* var dog: Dog = null;
* describe("dogs", (): void => {
* beforeAll((): void => {
* dog = new Dog(); // create a single dog once before the tests start
* });
* });
* ```
*/
declare function beforeAll(callback: () => void): void;
/**
* This function creates a callback that is called after each individual test is run in this test
* group.
*
* @param {function} callback - The function to be run after each test in the current test group.
*
* @example
*
* ```ts
* // create a global
* var cat: Cat = new Cat();
*
* describe("cats", (): void => {
* afterEach((): void => {
* cat.sleep(12); // cats sleep a lot
* });
* });
* ```
*/
declare function afterEach(callback: () => void): void;
/**
* This function creates a callback that is called after the whole test group is run, and only
* once.
*
* @param {function} callback - The function to be run after each test in the current test group.
*
* @example
*
* ```ts
* // create a global
* var dog: Dog = null;
* describe("dogs", (): void => {
* afterAll((): void => {
* memory.free(changetype<usize>(dog)); // free some memory
* });
* });
* ```
*/
declare function afterAll(callback: () => void): void;
/**
* Describes a value and returns an expectation to test the value.
*
* @type {T} - The expectation's type.
* @param {T} actual - The value being tested.
*
* @example
*
* ```ts
* expect<i32>(42).not.toBe(-1, "42 should not be -1");
* expect<i32>(19 + 23).toBe(42, "19 + 23 should equal 42");
* ```
*/
declare function expect<T>(actual: T | null): Expectation<T>;
/**
* Describes a void function and returns an expectation to test the function.
*
* @param {() => void} callback - The callback being tested.
*
* @example
*
* ```ts
* expectFn((): void => unreachable()).toThrow("unreachables do not throw");
* expectFn((): void => {
* cat.meow();
* }).not.toThrow("Uhoh, cats can't meow!");;
* ```
*/
declare function expectFn(cb: () => void): Expectation<() => void>;
/**
* Describes a test that needs to be written.
*
* @param {string} description - The description of the test that needs to be written.
*/
declare function todo(description: string): void;
/**
* Logs a single value to the logger, and is stringified. It works for references, values, and
* strings.
*
* @type {T} - The type to be logged.
* @param {T | null} value - The value to be logged.
*
* @example
*
* ```ts
* log<string>("This is a logged value.");
* log<i32>(42);
* log<Vec3>(new Vec(1, 2, 3));
* log<Vec3>(null);
* ```
*/
declare function log<T>(value: T | null): void;
/**
* An expectation for a value.
*/
// @ts-ignore
declare class Expectation<T> {
/**
* Create a new expectation.
*
* @param {T | null} actual - The actual value of the expectation.
*/
constructor(actual: T | null);
/**
* This expectation performs a strict equality on value types and reference types.
*
* @param {T | null} expected - The value to be compared.
* @param {string} message - The optional message that describes the expectation.
*
* @example
*
* ```ts
* expect<i32>(42).not.toBe(-1, "42 should not be -1");
* expect<i32>(19 + 23).toBe(42, "19 + 23 should equal 42");
* ```
*/
toBe(expected: T | null, message?: string): void;
/**
* This expectation performs a strict equality on value types and performs a memcompare on
* reference types. If the reference type `T` has reference types as properties, the comparison does
* not perform property traversal. It will only compare the pointer values in the memory block, and
* only compare `offsetof<T>()` bytes, regardless of the allocated block size.
*
* @param {T | null} expected - The value to be compared.
* @param {string} message - The optional message that describes the expectation.
*
* @example
*
* ```ts
* expect<Vec3>(new Vec3(1, 2, 3)).toStrictEqual(new Vec(1, 2, 3), "Vectors of the same shape should be equal");
* ```
*/
toStrictEqual(expected: T | null, message?: string): void;
/**
* This expectation performs a strict memory block equality based on the allocated block sizes.
*
* @param {T | null} expected - The value to be compared.
* @param {string} message - The optional message that describes the expectation.
*
* @example
*
* ```ts
* expect<Vec3>(new Vec3(1, 2, 3)).toBlockEqual(new Vec(1, 2, 3), "Vectors of the same shape should be equal");
* ```
*/
toBlockEqual(expected: T | null, message?: string): void;
/**
* If the value is callable, it calls the function, and fails the expectation if it throws, or hits
* an unreachable().
*
* @param {string} message - The optional message that describes the expectation.
*
* @example
*
* ```ts
* expectFn((): void => unreachable()).toThrow("unreachable() should throw.");
* expectFn((): void => {
* cat.sleep(100); // cats can sleep quite a lot
* }).not.toThrow("cats should sleep, not throw");
* ```
*/
toThrow(message?: string): void;
/**
* This expecation asserts that the value is truthy, like in javascript. If the value is a string,
* then strings of length 0 are not truthy.
*
* @param {string} message - The optional message that describes the expectation.
*
* @example
*
* ```ts
* expect<bool>(true).toBeTruthy("true is truthy.");
* expect<i32>(1).toBeTruthy("numeric values that are not 0 are truthy.");
* expect<Vec3>(new Vec3(1, 2, 3)).toBeTruthy("reference types that aren't null are truthy.");
* expect<bool>(false).not.toBeTruthy("false is not truthy.");
* expect<i32>(0).not.toBeTruthy("0 is not truthy.");
* expect<Vec3>(null).not.toBeTruthy("null is not truthy.");
* ```
*/
toBeTruthy(message?: string): void;
/**
* This expectation tests the value to see if it is null. If the value is a value type, it is
* never null. If the value is a reference type, it performs a strict null comparison.
*
* @param {string} message - The optional message that describes the expectation.
*
* @example
*
* ```ts
* expect<i32>(0).not.toBeNull("numbers are never null");
* expect<Vec3>(null).toBeNull("null reference types are null.");
* ```
*/
toBeNull(message?: string): void;
/**
* This expecation assert that the value is falsy, like in javascript. If the value is a string,
* then strings of length 0 are falsy.
*
* @param {string} message - The optional message that describes the expectation.
*
* @example
*
* ```ts
* expect<bool>(false).toBeFalsy("false is falsy.");
* expect<i32>(0).toBeFalsy("0 is falsy.");
* expect<Vec3>(null).toBeFalsy("null is falsy.");
* expect<bool>(true).not.toBeFalsy("true is not falsy.");
* expect<i32>(1).not.toBeFalsy("numeric values that are not 0 are not falsy.");
* expect<Vec3>(new Vec3(1, 2, 3)).not.toBeFalsy("reference types that aren't null are not falsy.");
* ```
*/
toBeFalsy(message?: string): void;
/**
* This expectation asserts that the value is greater than the expected value. Since operators can
* be overloaded in assemblyscript, it's possible for this to work on reference types.
*
* @param {T | null} expected - The expected value that the actual value should be greater than.
* @param {string} message - The optional message that describes this expectation.
*
* @example
*
* ```ts
* expect<i32>(10).toBeGreaterThan(4);
* expect<i32>(12).not.toBeGreaterThan(42);
* ```
*/
toBeGreaterThan(expected: T | null, message?: string): void;
/**
* This expectation asserts that the value is less than the expected value. Since operators can
* be overloaded in assemblyscript, it's possible for this to work on reference types.
*
* @param {T | null} value - The expected value that the actual value should be less than.
* @param {string} message - The optional message that describes this expectation.
*
* @example
*
* ```ts
* expect<i32>(10).not.toBeLessThan(4);
* expect<i32>(12).toBeLessThan(42);
* ```
*/
toBeLessThan(expected: T | null, message?: string): void;
/**
* This expectation asserts that the value is greater than or equal to the expected value. Since
* operators can be overloaded in assemblyscript, it's possible for this to work on reference
* types.
*
* @param {T | null} value - The expected value that the actual value should be greater than or
* equal to.
* @param {string} message - The optional message that describes this expectation.
*
* @example
*
* ```ts
* expect<i32>(42).toBeGreaterThanOrEqual(42);
* expect<i32>(10).toBeGreaterThanOrEqual(4);
* expect<i32>(12).not.toBeGreaterThanOrEqual(42);
* ```
*/
toBeGreaterThanOrEqual(expected: T | null, message?: string): void;
/**
* This expectation asserts that the value is less than or equal to the expected value. Since
* operators can be overloaded in assemblyscript, it's possible for this to work on reference
* types.
*
* @param {T | null} value - The expected value that the actual value should be less than or equal
* to.
* @param {string} message - The optional message that describes this expectation.
*
* @example
*
* ```ts
* expect<i32>(42).toBeLessThanOrEqual(42);
* expect<i32>(10).not.toBeLessThanOrEqual(4);
* expect<i32>(12).toBeLessThanOrEqual(42);
* ```
*/
toBeLessThanOrEqual(expected: T | null, message?: string): void;
/**
* This expectation asserts that the value is close to another value. Both numbers must be finite,
* and T must extend f64 or f32.
*
* @param {T extends f64 | f32} value - The expected value to be close to.
* @param {i32} decimalPlaces - The number of decimal places used to calculate epsilon. Default is
* 2.
* @param {string} message - The optional message that describes this expectation.
*
* @example
*
* ```ts
* expect<f64>(0.1 + 0.2).toBeCloseTo(0.3);
* ```
*/
toBeCloseTo(expected: T, decimalPlaces?: number, message?: string): void;
/**
* This function asserts the float type value is NaN.
*
* @param {string} message - The optional message the describes this expectation.
*
* @example
*
* ```ts
* expect<f64>(NaN).toBeNaN();
* expect<f32>(42).not.toBeNaN();
* ```
*/
toBeNaN(message?: string): void;
/**
* This function asserts a float is finite.
*
* @param {string} message - The optional message the describes this expectation.
* @example
*
* ```ts
* expect<f32>(42).toBeFinite();
* expect<f64>(Infinity).not.toBeFinite();
* ```
*/
toBeFinite(message?: string): void;
/**
* This method asserts the item has the expected length.
*
* @param {i32} expected - The expected length.
* @param {string} message - The optional message the describes this expectation.
*
* ```ts
* expect<i32[]>([1, 2, 3]).toHaveLength(3);
* ```
*/
toHaveLength(expected: i32, message?: string): void;
/**
* This method asserts that a given T that extends `Array<U>` has a value/reference included.
*
* @param {valueof<T>} expected - The expected item to be included in the Array.
* @param {string} message - The optional message the describes this expectation.
*
* @example
*
* ```ts
* expect<i32[]>([1, 2, 3]).toInclude(3);
* ```
*/
// @ts-ignore: expected value should be known at compile time
toInclude(expected: valueof<T>, message?: string): void;
/**
* This method asserts that a given T that extends `Array<U>` has a value/reference included.
*
* @param {valueof<T>} expected - The expected item to be included in the Array.
* @param {string} message - The optional message the describes this expectation.
*
* @example
*
* ```ts
* expect<i32[]>([1, 2, 3]).toContain(3);
* ```
*/
// @ts-ignore: expected value should be known at compile time
toContain(expected: valueof<T>, message?: string): void;
/**
* This method asserts that a given T that extends `Array<U>` has a value/reference included and
* compared via memory.compare().
*
* @param {i32} expected - The expected item to be included in the Array.
* @param {string} message - The optional message the describes this expectation.
*
* @example
* ```ts
* expect<Vec3[]>([new Vec3(1, 2, 3)]).toInclude(new Vec3(1, 2, 3));
* ```
*/
// @ts-ignore: expected value should be known at compile time
toIncludeEqual(expected: valueof<T>, message?: string): void;
/**
* This method asserts that a given T that extends `Array<U>` has a value/reference included and
* compared via memory.compare().
*
* @param {i32} expected - The expected item to be included in the Array.
* @param {string} message - The optional message the describes this expectation.
*
* @example
* ```ts
* expect<Vec3[]>([new Vec3(1, 2, 3)]).toInclude(new Vec3(1, 2, 3));
* ```
*/
// @ts-ignore: expected value should be known at compile time
toContainEqual(expected: valueof<T>, message?: string): void;
/**
* This computed property is chainable, and negates the existing expectation. It returns itself.
*
* @example
* ```ts
* expect<i32>(42).not.toBe(0, "42 is not 0");
*/
not: Expectation<T>;
/**
* The actual value of the expectation.
*/
actual: T | null;
}
/**
* This is called to stop the debugger. e.g. `node --inspect-brk asp`.
*/
declare function debug(): void;
/**
* This class contains a set of methods related to performance configuration.
*/
// @ts-ignore
declare class Performance {
/**
* This function call enables performance statistics gathering for the following test.
*
* @param {bool} enabled - The bool to indicate if performance statistics should be gathered.
*/
public static enabled(enabled: bool): void;
/**
* This function call sets the maximum number of samples to complete the following test.
*
* @param {f64} count - The maximum number of samples required.
*/
public static maxSamples(count: f64): void;
/**
* This function call sets the number of decimal places to round to for the following test.
*
* @param {i32} deicmalPlaces - The number of decimal places to round to
*/
public static roundDecimalPlaces(count: i32): void;
/**
* This function call will set the maximum amount of time that should pass before it can stop
* gathering samples for the following test.
*
* @param {f64} time - The ammount of time in milliseconds.
*/
public static maxTestRunTime(time: f64): void;
/**
* This function call enables gathering the average/mean run time of each sample for the following
* test.
*
* @param {bool} enabled - The bool to indicate if the average/mean should be gathered.
*/
public static reportAverage(enabled: bool): void;
/**
* This function call enables gathering the median run time of each sample for the following test.
*
* @param {bool} enabled - The bool to indicate if the median should be gathered.
*/
public static reportMedian(value: bool): void;
/**
* This function call enables gathering the standard deviation of the run times of the samples
* collected for the following test.
*
* @param {bool} enabled - The bool to indicate if the standard deviation should be gathered.
*/
public static reportStdDev(value: bool): void;
/**
* This function call enables gathering the largest run time of the samples collected for the
* following test.
*
* @param {bool} enabled - The bool to indicate if the max should be gathered.
*/
public static reportMax(value: bool): void;
/**
* This function call enables gathering the smallest run time of the samples collected for the
* following test.
*
* @param {bool} enabled - The bool to indicate if the min should be gathered.
*/
public static reportMin(value: bool): void;
/**
* This function call enables gathering the varaince of the samples collected for the following test.
*
* @param {bool} enabled - The bool to indicate if the variance should be calculated.
*/
public static reportVariance(value: bool): void;
}
/**
* Assemblyscript uses reference counting to perform garbage collection. This means when you
* allocate a managed object and return it, it's reference count is one. If another variable aliases
* it then the reference count goes up. This static class contains a few convenience methods for
* developers to test the current number of blocks allocated on the heap to make sure you aren't leaking
* references, e.i. keeping references to objects you expect to be collected.
*/
declare class RTrace {
/**
* This bool indicates if `RTrace` should call into JavaScript to obtain reference counts.
*/
public static enabled: bool;
/**
* This method returns the current number of active references on the heap.
*/
public static count(): i32;
/**
* This method starts a new refcounting group, and causes the next call to `RTrace.end(label)` to
* return a delta in reference counts on the heap.
*
* @param {i32} label - The numeric label for this refcounting group.
*/
public static start(label: i32): void;
/**
* This method returns a delta of how many new (positive) or collected (negative) are on the heap.
*
* @param {i32} label - The numeric label for this refcounting group.
*/
public static end(label: i32): i32;
/**
* This method returns the number of increments that have occurred over the course of a test
* file.
*/
public static increments(): i32;
/**
* This method returns the number of decrements that have occurred over the course of a test
* file.
*/
public static decrements(): i32;
/**
* This method returns the number of increments that have occurred over the course of a test
* group.
*/
public static groupIncrements(): i32;
/**
* This method returns the number of decrements that have occurred over the course of a test
* group.
*/
public static groupDecrements(): i32;
/**
* This method returns the number of increments that have occurred over the course of a test
* group.
*/
public static testIncrements(): i32;
/**
* This method returns the number of decrements that have occurred over the course of a test
* group.
*/
public static testDecrements(): i32;
/**
* This method returns the number of allocations that have occurred over the course of a test
* file.
*/
public static allocations(): i32;
/**
* This method returns the number of frees that have occurred over the course of a test
* file.
*/
public static frees(): i32;
/**
* This method returns the number of allocations that have occurred over the course of a test
* group.
*/
public static groupAllocations(): i32;
/**
* This method returns the number of frees that have occurred over the course of a test
* group.
*/
public static groupFrees(): i32;
/**
* This method returns the number of allocations that have occurred over the course of a test
* group.
*/
public static testAllocations(): i32;
/**
* This method returns the number of frees that have occurred over the course of a test
* group.
*/
public static testFrees(): i32;
/**
* This method triggers a garbage collection.
*/
public static collect(): void;
/**
* Get the class id of the pointer.
*
* @param {usize} pointer - The pointer.
* @returns {u32} - The class id of the allocated block.
*/
public static classIdOf(pointer: usize): u32;
/**
* Get the size of a block or buffer.
*
* @param {T} reference - The reference.
* @returns {u32} - The size of the allocated block.
*/
public static sizeOf<T>(reference: T): u32;
/**
* Get the currently allocated blocks.
*/
public static activeBlocks(): usize[];
/**
* Get the current groups allocated blocks.
*/
public static activeGroupBlocks(): usize[];
/**
* Get the current tests allocated blocks.
*/
public static activeTestBlocks(): usize[];
}
/**
* This class is static and contains private global values that contain metadata about the Actual
* value.
*
* @example
* ```ts
* Actual.report<string>("This is an expected string.");
* Actual.report<i32[]>([1, 2, 3]);
* Actual.report<u8>(42);
* ```
*/
declare class Actual {
/**
* This function performs reporting to javascript what the actual value of this expectation is.
*
* @param {T} actual - The actual value to be reported.
*/
public static report<T>(value: T): void;
/**
* Clear the actual value and release any private memory stored as a global.
*/
public static clear(): void;
}
/**
* This class is static and contains private global values that contain metadata about the Expected
* value.
*
* @example
* ```ts
* Expected.report<string>("This is an expected string.");
* Expected.report<i32[]>([1, 2, 3]);
* Expected.report<u8>(42, i32(true)); // not 42
* ```
*/
declare class Expected {
/**
* This function performs reporting to javascript what the expected value of this expectation is.
* It notifies javascript if the expectation is negated.
*
* @param {T} value - The actual value to be reported.
* @param {i32} negated - An indicator if the expectation is negated. Pass `1` to negate the
* expectation. (default: 0)
*/
public static report<T>(value: T, negated?: i32): void;
/**
* Clear the expected value and release any private memory stored as a global.
*/
public static clear(): void;
}

View File

@ -1,15 +0,0 @@
import {handler} from "../main";
describe("example", () => {
it("can log some values to the console", () => {
log<string>("Hello world!"); // strings!
log<f64>(3.1415); // floats!
log<u8>(244); // integers!
log<u64>(0xFFFFFFFF); // long values!
log<string>(handler('{"hi": "hi"}'));
log<string>(handler('{"action": "Post", "msg": "Hello, Fluence!", "username": "fluencer"}'));
log<string>(handler('{"action": "Post", "msg": "Hello, fluencer!", "username": "John Doe"}'));
log<string>(handler('{"action": "Fetch"}'));
});
});

View File

@ -1,31 +1,25 @@
# Fluid decentralized twitter-like feed built on Fluence with SQLite # Fluid decentralized twitter-like feed built on Fluence with SQLite
## Workshop ## Workshop
### Step 0 Framework Workshop scenario could be found [here](https://fluence.dev/page/workshop-fluid-decentralized-twitter-like-feed-built-on-fluence)
Here you can see the most basic Fluence app just a hello-world.
Take a note of `#invocation_handler` macro, it marks function as an entry-point to your app. ## How to run steps
There's a `run.sh` script in each step, but if you'd like to run it manually, here's the instructions
```shell
### Step 1 JSON API # Go into directory of the desired step (step3 used as example)
Now let's define some API for our feed backend. Let's use JSON as a communication format, and serde_json Rust library for handy serialization and deserialization. cd step3-finished-app
Two enums `Request` and `Response` in [api.rs](step1-json-api/src/api.rs) describe API for interaction with frontend. # Download SQLite WASM module
mkdir wasm
wget https://github.com/fluencelabs/sqlite/releases/download/v0.2.0_w/sqlite3_0.2.0.wasm -O ./wasm/sqlite3_0.2.0.wasm
Method `parse` parses `Request` from JSON, and `serialize` converts `Response` back to JSON string, so it can be returned to frontend. # Build fluid WASM module
cargo +nightly build --target wasm32-unknown-unknown --release
cp target/wasm32-unknown-unknown/release/*.wasm ./wasm/
Here we also define [model.rs](step1-json-api/src/model.rs) that's how we plan to use our database. Currently it is just a prototype, so we can implement our API in it's full. # Run it all on 30000 port with default Fluence API
docker run -it --rm -v $(pwd)/wasm:/code -p 30000:30000 fluencelabs/frun:latest
### Step 2 Database ```
Let's forget for a moment about API, and dive into using SQLite module.
- [ffi.rs](step2-database-only/src/ffi.rs) describes inter-module communication between Fluid and SQLite
- [databse.rs](step2-database-only/src/database.rs) provides us with `query` function that executes SQL query on SQLite module, and returns result as a String
Now, take a look at [lib.rs](step2-database-only/src/lib.rs) a few SQL requests are sent there.
First one creates the table for messages, next goes message insertion, emulating a user who's creating a post. Then, we select all messages and log them.
But we decided to use JSON as a communication format, and SQLite allows us to query results in json. So the last query does exactly that it is the same as usual `SELECT *`, but wraps result in json array.
### Step 3 Tying everything together
Now let's use our SQLite knowledge to implement [model.rs](step3-finished-app/src/model.rs) so our API can really persists and read data. And voilà, we have our backend for almost twitter, but it can be easily decentralized!
Also take a look at how to use frun [here](https://fluence.dev/docs/debugging) and look up HTTP API [here](https://fluence.dev/reference)

View File

@ -1,9 +1,7 @@
use crate::errors::{err_msg, Error}; use crate::errors::{err_msg, AppResult, Error};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::value::RawValue; use serde_json::value::RawValue;
pub type AppResult<T> = ::std::result::Result<T, Box<Error>>;
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(tag = "action")] #[serde(tag = "action")]
pub enum Request { pub enum Request {

View File

@ -14,3 +14,5 @@ impl fmt::Display for Error {
pub fn err_msg(s: &str) -> Box<Error> { pub fn err_msg(s: &str) -> Box<Error> {
Error(s.to_string()).into() Error(s.to_string()).into()
} }
pub type AppResult<T> = ::std::result::Result<T, Box<Error>>;

View File

@ -4,8 +4,8 @@ use serde_json::value::RawValue;
use api::Request; use api::Request;
use api::Response; use api::Response;
use crate::api::AppResult;
use crate::errors::err_msg; use crate::errors::err_msg;
use crate::errors::AppResult;
pub mod api; pub mod api;
pub mod errors; pub mod errors;

View File

@ -1,6 +1,7 @@
use crate::api::AppResult;
use log; use log;
use crate::errors::AppResult;
pub fn create_scheme() -> AppResult<()> { pub fn create_scheme() -> AppResult<()> {
Ok(log::info!("creating scheme")) Ok(log::info!("creating scheme"))
} }

View File

@ -0,0 +1,46 @@
#!/usr/bin/env bash
set -e
mkdir -p wasm
SQLITE="sqlite3_0.2.0.wasm"
if [ ! -f "wasm/$SQLITE" ]; then
echo "Downloading $SQLITE..."
wget -q https://github.com/fluencelabs/sqlite/releases/download/v0.2.0_w/$SQLITE -O ./wasm/$SQLITE
fi
# Build fluid WASM module
echo "Building..."
cargo +nightly build --target wasm32-unknown-unknown --release >/dev/null
cp target/wasm32-unknown-unknown/release/*.wasm ./wasm/
echo
# Run it all on 30000 port with default Fluence API
echo "Running..."
docker rm -f frun &>/dev/null || true
echo 'docker run -d --name frun --rm -v "$(pwd)/wasm:/code" -p 30000:30000 fluencelabs/frun:latest'
docker run -d --name frun --rm -v "$(pwd)/wasm:/code" -p 30000:30000 fluencelabs/frun:latest >/dev/null
echo
# Wait for app to be initialized
sleep 1 && (docker logs -f frun 2>&1 &) | grep -q initialized && sleep 1
# Send our username to the application
echo -e "Sending request..."
# Assign json to a variable using heredoc technique
JSON=$(cat <<JSON
{"action":"Post","message":"I'm nice, you're nice, it's nice!","username":"random_joe"}
JSON
)
# Send json as a request, and receive result
RESPONSE=$(curl -s 'http://localhost:30000/apps/1/tx' --data $'sessionId/0\n'"$JSON" --compressed | jq -r .result.data | base64 -D)
# Parse result as JSON and print to console
echo -e "$RESPONSE" | jq .
# Remove frun container
echo -e "Stopping..."
docker rm -f frun >/dev/null

View File

@ -1,6 +1,7 @@
use log; use log;
use crate::api::AppResult; use crate::errors::AppResult;
use crate::errors::err_msg; use crate::errors::err_msg;
use crate::ffi; use crate::ffi;

View File

@ -14,3 +14,5 @@ impl fmt::Display for Error {
pub fn err_msg(s: &str) -> Box<Error> { pub fn err_msg(s: &str) -> Box<Error> {
Error(s.to_string()).into() Error(s.to_string()).into()
} }
pub type AppResult<T> = ::std::result::Result<T, Box<Error>>;

View File

@ -1,11 +1,4 @@
use fluence::sdk::*; use fluence::sdk::*;
use serde_json::value::RawValue;
use api::Request;
use api::Response;
use crate::api::AppResult;
use crate::errors::err_msg;
pub mod database; pub mod database;
pub mod errors; pub mod errors;

View File

@ -1,9 +1,7 @@
use crate::errors::{err_msg, Error}; use crate::errors::{err_msg, AppResult, Error};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::value::RawValue; use serde_json::value::RawValue;
pub type AppResult<T> = ::std::result::Result<T, Box<Error>>;
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(tag = "action")] #[serde(tag = "action")]
pub enum Request { pub enum Request {

View File

@ -1,7 +1,7 @@
use log; use log;
use crate::api::AppResult;
use crate::errors::err_msg; use crate::errors::err_msg;
use crate::errors::AppResult;
use crate::ffi; use crate::ffi;
// Execute query on SQLite // Execute query on SQLite

View File

@ -14,3 +14,5 @@ impl fmt::Display for Error {
pub fn err_msg(s: &str) -> Box<Error> { pub fn err_msg(s: &str) -> Box<Error> {
Error(s.to_string()).into() Error(s.to_string()).into()
} }
pub type AppResult<T> = ::std::result::Result<T, Box<Error>>;

View File

@ -4,8 +4,8 @@ use serde_json::value::RawValue;
use api::Request; use api::Request;
use api::Response; use api::Response;
use crate::api::AppResult;
use crate::errors::err_msg; use crate::errors::err_msg;
use crate::errors::AppResult;
pub mod api; pub mod api;
pub mod database; pub mod database;

View File

@ -1,8 +1,8 @@
use std::str::FromStr; use std::str::FromStr;
use crate::api::AppResult;
use crate::database; use crate::database;
use crate::errors::err_msg; use crate::errors::err_msg;
use crate::errors::AppResult;
pub fn create_scheme() -> AppResult<()> { pub fn create_scheme() -> AppResult<()> {
database::query("CREATE TABLE messages(msg text, username text)".to_string()) database::query("CREATE TABLE messages(msg text, username text)".to_string())