diff --git a/package.json b/package.json index 20b22bd859..3ce4876c48 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "class-transformer": "^0.5.1", "class-validator": "^0.14.1", "crypto-browserify": "^3.12.0", + "deep-is": "^0.1.4", "esbuild": "^0.19.3", "esbuild-plugin-tsc": "^0.4.0", "ethers": "^6.11.1", @@ -59,7 +60,6 @@ "@typescript-eslint/eslint-plugin": "^6.13.0", "@typescript-eslint/parser": "^6.13.0", "deep-equal": "^2.2.3", - "deep-is": "^0.1.4", "eslint": "^8.54.0", "eslint-config-prettier": "^8.5.0", "eslint-plugin-simple-import-sort": "^10.0.0", diff --git a/property_tests/tests/blob/test/generate_tests.ts b/property_tests/tests/blob/test/generate_tests.ts index 807b248a0f..68d64967c1 100644 --- a/property_tests/tests/blob/test/generate_tests.ts +++ b/property_tests/tests/blob/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -29,9 +29,7 @@ export function generateTests( ) ); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/bool/test/generate_tests.ts b/property_tests/tests/bool/test/generate_tests.ts index 5e433736d1..3c94860698 100644 --- a/property_tests/tests/bool/test/generate_tests.ts +++ b/property_tests/tests/bool/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -24,9 +24,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/canister_methods/http_request/test/generate_tests.ts b/property_tests/tests/canister_methods/http_request/test/generate_tests.ts index 3a25432547..2caeaa058d 100644 --- a/property_tests/tests/canister_methods/http_request/test/generate_tests.ts +++ b/property_tests/tests/canister_methods/http_request/test/generate_tests.ts @@ -1,8 +1,8 @@ import { HttpRequest, HttpResponse } from 'azle/experimental'; -import { deepEqual, Named } from 'azle/property_tests'; +import { Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { HttpResponseAgentResponseValue } from 'azle/property_tests/arbitraries/http/response_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; import { fletch } from './fletch'; @@ -42,12 +42,11 @@ export function generateTests( ...expectedResponse, headers: sortedExpectedHeaders }; - const valuesAreEqual = deepEqual( + + return testEquality( processedResponse, processedExpectedResponse ); - - return { Ok: valuesAreEqual }; } } ] diff --git a/property_tests/tests/canister_methods/http_request_update/test/generate_tests.ts b/property_tests/tests/canister_methods/http_request_update/test/generate_tests.ts index fae0d69a50..3fa58cbd20 100644 --- a/property_tests/tests/canister_methods/http_request_update/test/generate_tests.ts +++ b/property_tests/tests/canister_methods/http_request_update/test/generate_tests.ts @@ -1,8 +1,8 @@ import { HttpRequest, HttpResponse } from 'azle/experimental'; -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { HttpResponseAgentResponseValue } from 'azle/property_tests/arbitraries/http/response_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; import { fletch } from './fletch'; @@ -26,9 +26,7 @@ export function generateTests( const result = await actor['get_state'](); - return { - Ok: deepEqual(result, 0) - }; + return testEquality(result, 0); } }, { @@ -54,12 +52,11 @@ export function generateTests( ...expectedResponse, headers: sortedExpectedHeaders }; - const valuesAreEqual = deepEqual( + + return testEquality( processedResponse, processedExpectedResponse ); - - return { Ok: valuesAreEqual }; } }, { @@ -69,9 +66,7 @@ export function generateTests( const result = await actor['get_state'](); - return { - Ok: deepEqual(result, 1) - }; + return testEquality(result, 1); } } ] diff --git a/property_tests/tests/canister_methods/init/test/generate_tests.ts b/property_tests/tests/canister_methods/init/test/generate_tests.ts index 24c808b365..284743bfb8 100644 --- a/property_tests/tests/canister_methods/init/test/generate_tests.ts +++ b/property_tests/tests/canister_methods/init/test/generate_tests.ts @@ -1,7 +1,7 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { CorrespondingJSType } from 'azle/property_tests/arbitraries/candid/corresponding_js_type'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( _functionName: string, @@ -20,13 +20,7 @@ export function generateTests( const actor = getActor(__dirname); const result = await actor.getInitValues(); - const valuesAreEqual = deepEqual(result, expectedResult); - - return valuesAreEqual - ? { Ok: true } - : { - Err: `\n Incorrect return value\n expected: ${expectedResult}\n received: ${result}` - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/canister_methods/inspect_message/test/generate_tests.ts b/property_tests/tests/canister_methods/inspect_message/test/generate_tests.ts index 84f80b08e4..b97656bc1a 100644 --- a/property_tests/tests/canister_methods/inspect_message/test/generate_tests.ts +++ b/property_tests/tests/canister_methods/inspect_message/test/generate_tests.ts @@ -1,9 +1,9 @@ import { Agent } from '@dfinity/agent'; -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidReturnType } from 'azle/property_tests/arbitraries/candid/candid_return_type_arb'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { CorrespondingJSType } from 'azle/property_tests/arbitraries/candid/corresponding_js_type'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; import { InspectMessageBehavior } from './test'; @@ -48,7 +48,7 @@ function generateTest( const result = await actor[functionName](...paramValues); if (behavior === 'ACCEPT') { - return { Ok: deepEqual(result, expectedResult) }; + return testEquality(result, expectedResult); } return { @@ -56,16 +56,18 @@ function generateTest( }; } catch (error: any) { if (behavior === 'RETURN') { - return { - Ok: error.message.includes('rejected the message') - }; + return testEquality( + error.message.includes('rejected the message'), + true + ); } if (behavior === 'THROW') { const expectedError = `Method \\"${functionName}\\" not allowed`; - return { - Ok: error.message.includes(expectedError) - }; + return testEquality( + error.message.includes(expectedError), + true + ); } throw error; diff --git a/property_tests/tests/canister_methods/post_upgrade/test/generate_init_tests.ts b/property_tests/tests/canister_methods/post_upgrade/test/generate_init_tests.ts index 956cc11e1c..ba37fbcf4c 100644 --- a/property_tests/tests/canister_methods/post_upgrade/test/generate_init_tests.ts +++ b/property_tests/tests/canister_methods/post_upgrade/test/generate_init_tests.ts @@ -1,7 +1,7 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { CorrespondingJSType } from 'azle/property_tests/arbitraries/candid/corresponding_js_type'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( _functionName: string, @@ -23,19 +23,10 @@ export function generateTests( const isPostUpgradeCalled = await actor.isPostUpgradeCalled(); - const valuesAreEqual = - deepEqual(initValues, expectedResult) && - isPostUpgradeCalled === false; - - return valuesAreEqual - ? { Ok: true } - : { - Err: `\n -Incorrect return value - expected: ${expectedResult} - received: ${initValues} - isPostUpgradeCalled: ${isPostUpgradeCalled}` - }; + return testEquality( + [initValues, isPostUpgradeCalled], + [expectedResult, false] + ); } } ] diff --git a/property_tests/tests/canister_methods/post_upgrade/test/generate_post_upgrade_tests.ts b/property_tests/tests/canister_methods/post_upgrade/test/generate_post_upgrade_tests.ts index ac4b0feeb2..8a355563fc 100644 --- a/property_tests/tests/canister_methods/post_upgrade/test/generate_post_upgrade_tests.ts +++ b/property_tests/tests/canister_methods/post_upgrade/test/generate_post_upgrade_tests.ts @@ -1,7 +1,7 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { CorrespondingJSType } from 'azle/property_tests/arbitraries/candid/corresponding_js_type'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( _functionName: string, @@ -24,19 +24,10 @@ export function generateTests( await actor.getPostUpgradeValues(); const isInitCalled = await actor.isInitCalled(); - const valuesAreEqual = - deepEqual(postUpgradeValues, expectedResult) && - isInitCalled === false; - - return valuesAreEqual - ? { Ok: true } - : { - Err: `\n -Incorrect return value -expected: ${expectedResult} -received: ${postUpgradeValues} -isInitCalled: ${isInitCalled}` - }; + return testEquality( + [postUpgradeValues, isInitCalled], + [expectedResult, false] + ); } } ] diff --git a/property_tests/tests/canister_methods/pre_upgrade/test/test.ts b/property_tests/tests/canister_methods/pre_upgrade/test/test.ts index 9e517424ce..2d35ed19f6 100644 --- a/property_tests/tests/canister_methods/pre_upgrade/test/test.ts +++ b/property_tests/tests/canister_methods/pre_upgrade/test/test.ts @@ -1,4 +1,4 @@ -import { deepEqual, getActor, runPropTests } from 'azle/property_tests'; +import { getActor, runPropTests } from 'azle/property_tests'; import { CandidReturnTypeArb } from 'azle/property_tests/arbitraries/candid/candid_return_type_arb'; import { CandidValueAndMetaArb } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { CorrespondingJSType } from 'azle/property_tests/arbitraries/candid/corresponding_js_type'; @@ -12,6 +12,7 @@ import { QueryMethodArb } from 'azle/property_tests/arbitraries/canister_methods/query_method_arb'; import { UpdateMethodArb } from 'azle/property_tests/arbitraries/canister_methods/update_method_arb'; +import { testEquality } from 'azle/test'; import fc from 'fast-check'; const SimplePreUpgradeArb = PreUpgradeMethodArb({ @@ -95,7 +96,7 @@ function generateGetPreUpgradeExecutedCanisterMethod(): QueryMethod { const actor = getActor(__dirname); const result = await actor.getPreUpgradeExecuted(); - return { Ok: deepEqual(result, []) }; + return testEquality(result, []); } } ], @@ -106,7 +107,7 @@ function generateGetPreUpgradeExecutedCanisterMethod(): QueryMethod { const actor = getActor(__dirname); const result = await actor.getPreUpgradeExecuted(); - return { Ok: deepEqual(result, [true]) }; + return testEquality(result, [true]); } } ] diff --git a/property_tests/tests/canister_methods/query/test/generate_tests.ts b/property_tests/tests/canister_methods/query/test/generate_tests.ts index d4359e5115..9b6f86be00 100644 --- a/property_tests/tests/canister_methods/query/test/generate_tests.ts +++ b/property_tests/tests/canister_methods/query/test/generate_tests.ts @@ -1,8 +1,8 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidReturnType } from 'azle/property_tests/arbitraries/candid/candid_return_type_arb'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { CorrespondingJSType } from 'azle/property_tests/arbitraries/candid/corresponding_js_type'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -21,13 +21,7 @@ export function generateTests( test: async () => { const actor = getActor(__dirname); const result = await actor[functionName](...paramValues); - const valuesAreEqual = deepEqual(result, expectedResult); - - return valuesAreEqual - ? { Ok: true } - : { - Err: `\n Incorrect return value\n expected: ${expectedResult}\n received: ${result}` - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/canister_methods/update/test/generate_tests.ts b/property_tests/tests/canister_methods/update/test/generate_tests.ts index 29bff5bae6..d2acc41815 100644 --- a/property_tests/tests/canister_methods/update/test/generate_tests.ts +++ b/property_tests/tests/canister_methods/update/test/generate_tests.ts @@ -1,8 +1,8 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidReturnType } from 'azle/property_tests/arbitraries/candid/candid_return_type_arb'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { CorrespondingJSType } from 'azle/property_tests/arbitraries/candid/corresponding_js_type'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -21,13 +21,8 @@ export function generateTests( test: async () => { const actor = getActor(__dirname); const result = await actor[functionName](...paramValues); - const valuesAreEqual = deepEqual(result, expectedResult); - return valuesAreEqual - ? { Ok: true } - : { - Err: `\n Incorrect return value\n expected: ${expectedResult}\n received: ${result}` - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/float32/test/generate_tests.ts b/property_tests/tests/float32/test/generate_tests.ts index c24b4f746f..727cc21ec4 100644 --- a/property_tests/tests/float32/test/generate_tests.ts +++ b/property_tests/tests/float32/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -23,9 +23,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/float64/test/generate_tests.ts b/property_tests/tests/float64/test/generate_tests.ts index 921fb0cba3..7c0c4befc4 100644 --- a/property_tests/tests/float64/test/generate_tests.ts +++ b/property_tests/tests/float64/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -27,9 +27,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/func/test/generate_tests.ts b/property_tests/tests/func/test/generate_tests.ts index ad13f8691e..bd1f7504ba 100644 --- a/property_tests/tests/func/test/generate_tests.ts +++ b/property_tests/tests/func/test/generate_tests.ts @@ -1,7 +1,7 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Func } from 'azle/property_tests/arbitraries/candid/reference/func_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -21,12 +21,10 @@ export function generateTests( ) ); - return { - Ok: deepEqual( - result, - returnFunc.value.agentResponseValue - ) - }; + return testEquality( + result, + returnFunc.value.agentResponseValue + ); } } ] diff --git a/property_tests/tests/int/test/generate_tests.ts b/property_tests/tests/int/test/generate_tests.ts index f5ee4c664c..0d356e15bf 100644 --- a/property_tests/tests/int/test/generate_tests.ts +++ b/property_tests/tests/int/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -24,9 +24,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/int16/test/generate_tests.ts b/property_tests/tests/int16/test/generate_tests.ts index c71b2e8368..62725742e7 100644 --- a/property_tests/tests/int16/test/generate_tests.ts +++ b/property_tests/tests/int16/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -27,9 +27,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/int32/test/generate_tests.ts b/property_tests/tests/int32/test/generate_tests.ts index a26a235294..f95ad9b64e 100644 --- a/property_tests/tests/int32/test/generate_tests.ts +++ b/property_tests/tests/int32/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -27,9 +27,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/int64/test/generate_tests.ts b/property_tests/tests/int64/test/generate_tests.ts index 01edd1730e..2be510faaf 100644 --- a/property_tests/tests/int64/test/generate_tests.ts +++ b/property_tests/tests/int64/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -26,9 +26,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/int8/test/generate_tests.ts b/property_tests/tests/int8/test/generate_tests.ts index b63b263e52..6ff0af87c6 100644 --- a/property_tests/tests/int8/test/generate_tests.ts +++ b/property_tests/tests/int8/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -26,9 +26,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/nat/test/generate_tests.ts b/property_tests/tests/nat/test/generate_tests.ts index 98c25e8312..a24868b63e 100644 --- a/property_tests/tests/nat/test/generate_tests.ts +++ b/property_tests/tests/nat/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -24,9 +24,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/nat16/test/generate_tests.ts b/property_tests/tests/nat16/test/generate_tests.ts index a2919564d7..75bd87c367 100644 --- a/property_tests/tests/nat16/test/generate_tests.ts +++ b/property_tests/tests/nat16/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -27,9 +27,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/nat32/test/generate_tests.ts b/property_tests/tests/nat32/test/generate_tests.ts index 7ff0d4244d..709ba84432 100644 --- a/property_tests/tests/nat32/test/generate_tests.ts +++ b/property_tests/tests/nat32/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -27,9 +27,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/nat64/test/generate_tests.ts b/property_tests/tests/nat64/test/generate_tests.ts index 56bc57c16c..e608ab1f8b 100644 --- a/property_tests/tests/nat64/test/generate_tests.ts +++ b/property_tests/tests/nat64/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -26,9 +26,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/nat8/test/generate_tests.ts b/property_tests/tests/nat8/test/generate_tests.ts index fbbf4e7a98..a31a97d302 100644 --- a/property_tests/tests/nat8/test/generate_tests.ts +++ b/property_tests/tests/nat8/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -27,9 +27,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/null/test/generate_tests.ts b/property_tests/tests/null/test/generate_tests.ts index da9350041c..8d5d0ad651 100644 --- a/property_tests/tests/null/test/generate_tests.ts +++ b/property_tests/tests/null/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -20,9 +20,7 @@ export function generateTests( ) ); - return { - Ok: deepEqual(result, null) - }; + return testEquality(result, null); } } ] diff --git a/property_tests/tests/opt/test/generate_tests.ts b/property_tests/tests/opt/test/generate_tests.ts index 2574e48dd7..bae894a4a4 100644 --- a/property_tests/tests/opt/test/generate_tests.ts +++ b/property_tests/tests/opt/test/generate_tests.ts @@ -1,7 +1,7 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Opt } from 'azle/property_tests/arbitraries/candid/constructed/opt_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -23,9 +23,7 @@ export function generateTests( const result = await actor[functionName](...params); - return { - Ok: deepEqual(expectedResult, result) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/principal/test/generate_tests.ts b/property_tests/tests/principal/test/generate_tests.ts index adccf33806..a0c95cdf65 100644 --- a/property_tests/tests/principal/test/generate_tests.ts +++ b/property_tests/tests/principal/test/generate_tests.ts @@ -1,7 +1,7 @@ import { Principal } from '@dfinity/principal'; -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -25,7 +25,7 @@ export function generateTests( ) ); - return { Ok: deepEqual(result, expectedResult) }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/record/test/generate_tests.ts b/property_tests/tests/record/test/generate_tests.ts index 3b5bcd1c84..2a0dc710cb 100644 --- a/property_tests/tests/record/test/generate_tests.ts +++ b/property_tests/tests/record/test/generate_tests.ts @@ -1,7 +1,7 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Record } from 'azle/property_tests/arbitraries/candid/constructed/record_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -21,12 +21,10 @@ export function generateTests( ) ); - return { - Ok: deepEqual( - result, - returnRecord.value.agentResponseValue - ) - }; + return testEquality( + result, + returnRecord.value.agentResponseValue + ); } } ] diff --git a/property_tests/tests/recursive/test/generate_tests.ts b/property_tests/tests/recursive/test/generate_tests.ts index 234c5ec79a..9c27802fc5 100644 --- a/property_tests/tests/recursive/test/generate_tests.ts +++ b/property_tests/tests/recursive/test/generate_tests.ts @@ -1,7 +1,7 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Recursive } from 'azle/property_tests/arbitraries/candid/recursive'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -21,12 +21,10 @@ export function generateTests( const result = await actor[functionName](...params); - return { - Ok: deepEqual( - result, - returnRecursive.value.agentResponseValue - ) - }; + return testEquality( + result, + returnRecursive.value.agentResponseValue + ); } } ] diff --git a/property_tests/tests/service/test/generate_tests.ts b/property_tests/tests/service/test/generate_tests.ts index 4abe1e70ee..7356346c22 100644 --- a/property_tests/tests/service/test/generate_tests.ts +++ b/property_tests/tests/service/test/generate_tests.ts @@ -1,7 +1,7 @@ import { Principal } from '@dfinity/principal'; import { Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; import { execSync } from 'child_process'; export function generateTests( @@ -31,11 +31,10 @@ export function generateTests( .toString() .trim(); - return { - Ok: - result === - `(service "${returnService.value.agentArgumentValue.toText()}")` - }; + return testEquality( + result, + `(service "${returnService.value.agentArgumentValue.toText()}")` + ); } } ] diff --git a/property_tests/tests/text/test/generate_tests.ts b/property_tests/tests/text/test/generate_tests.ts index 18090ce7cd..1876c12bfd 100644 --- a/property_tests/tests/text/test/generate_tests.ts +++ b/property_tests/tests/text/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -24,9 +24,7 @@ export function generateTests( const result = await actor[functionName](...paramValues); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/tuple/test/generate_tests.ts b/property_tests/tests/tuple/test/generate_tests.ts index 5740c9a8fc..1d8d7747b1 100644 --- a/property_tests/tests/tuple/test/generate_tests.ts +++ b/property_tests/tests/tuple/test/generate_tests.ts @@ -1,10 +1,10 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { ReturnTuple, Tuple } from 'azle/property_tests/arbitraries/candid/constructed/tuple_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -26,7 +26,7 @@ export function generateTests( ) ); - return { Ok: deepEqual(result, expectedResult) }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/variant/test/generate_tests.ts b/property_tests/tests/variant/test/generate_tests.ts index 700fe5cf93..78834fa68f 100644 --- a/property_tests/tests/variant/test/generate_tests.ts +++ b/property_tests/tests/variant/test/generate_tests.ts @@ -1,7 +1,7 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; import { Variant } from 'azle/property_tests/arbitraries/candid/constructed/variant_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -23,9 +23,7 @@ export function generateTests( ) ); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/property_tests/tests/vec/test/generate_tests.ts b/property_tests/tests/vec/test/generate_tests.ts index 162e75c72c..0d80090cc0 100644 --- a/property_tests/tests/vec/test/generate_tests.ts +++ b/property_tests/tests/vec/test/generate_tests.ts @@ -1,6 +1,6 @@ -import { deepEqual, getActor, Named } from 'azle/property_tests'; +import { getActor, Named } from 'azle/property_tests'; import { CandidValueAndMeta } from 'azle/property_tests/arbitraries/candid/candid_value_and_meta_arb'; -import { Test } from 'azle/test'; +import { Test, testEquality } from 'azle/test'; export function generateTests( functionName: string, @@ -21,9 +21,7 @@ export function generateTests( ); const result = await actor[functionName](...params); - return { - Ok: deepEqual(result, expectedResult) - }; + return testEquality(result, expectedResult); } } ] diff --git a/src/lib/server.ts b/src/lib/server.ts index e7df029783..2960ede3ff 100644 --- a/src/lib/server.ts +++ b/src/lib/server.ts @@ -6,29 +6,25 @@ import { IncomingMessageForServer } from 'http'; // @ts-ignore import httpMessageParser from 'http-message-parser'; -import { - blob, - bool, - CandidType, - Canister, - Func, - ic, - init, - Manual, - nat16, - None, - Opt, - postUpgrade, - query, - Record, - Some, - text, - Tuple, - update, - Variant, - Vec -} from '.'; +import { CandidType } from './candid/candid_type'; +import { Manual } from './candid/manual'; +import { blob } from './candid/types/constructed/blob'; +import { None, Opt, Some } from './candid/types/constructed/opt'; +import { Record } from './candid/types/constructed/record'; +import { Tuple } from './candid/types/constructed/tuple'; +import { Variant } from './candid/types/constructed/variant'; +import { Vec } from './candid/types/constructed/vec'; +import { bool } from './candid/types/primitive/bool'; +import { nat16 } from './candid/types/primitive/nats/nat16'; +import { text } from './candid/types/primitive/text'; +import { Func } from './candid/types/reference/func'; +import { Canister } from './candid/types/reference/service/'; import { CanisterOptions } from './candid/types/reference/service/canister_function'; +import { init } from './canister_methods/methods/init'; +import { postUpgrade } from './canister_methods/methods/post_upgrade'; +import { query } from './canister_methods/methods/query'; +import { update } from './canister_methods/methods/update'; +import { ic } from './ic'; export type HeaderField = [text, text]; export const HeaderField = Tuple(text, text); diff --git a/test/index.ts b/test/index.ts index a35dbf1761..72b76fdcf7 100644 --- a/test/index.ts +++ b/test/index.ts @@ -1,32 +1,26 @@ -import { execSync } from 'child_process'; +// TODO import deepEqual from 'deep-is' works for some +// TODO import { deepEqual } from 'deep-is' works for others +// TODO require seems to work for all of them +// eslint-disable-next-line @typescript-eslint/no-var-requires +const deepEqual = require('deep-is'); -export type Test = { +import { jsonStringify } from '../src/lib/json'; + +export type Test<> = { name: string; skip?: boolean; wait?: number; - prep?: () => Promise; - test?: () => Promise>; -}; - -// export type Variant = Partial; - -export type AzleResult = Partial<{ - Ok: T; - Err: E; -}>; - -export type Ok = { - Ok: T; + prep?: () => Promise; + test?: () => Promise; }; -// TODO let's get rid of this function in all tests and use match instead -export function ok(azle_result: AzleResult): azle_result is Ok { - if (azle_result.Err === undefined) { - return true; - } else { - return false; - } -} +// TODO get rid of this union once the jest migration is complete +type AzleResult = + | Partial<{ + Ok: { isSuccessful: boolean; message?: string }; + Err: string; + }> + | Partial<{ Ok: boolean; Err: string }>; // TODO should this just return a boolean? // TODO then the function calling can decide to throw or not @@ -37,42 +31,41 @@ export async function runTests( for (const test of tests) { try { if (test.skip === true) { - console.log(`Skipping: ${test.name}`); + console.info(`Skipping: ${test.name}`); continue; } - console.log(); + console.info(); if (test.prep !== undefined || test.wait !== undefined) { - console.log(`\n${test.name}\n`); + console.info(`\n${test.name}\n`); } else { - console.log(`\nRunning test: ${test.name}\n`); + console.info(`\nRunning test: ${test.name}\n`); } if (test.wait !== undefined) { - console.log(`waiting ${test.wait} milliseconds`); + console.info(`waiting ${test.wait} milliseconds`); await new Promise((resolve) => setTimeout(resolve, test.wait)); - console.log('done waiting'); + console.info('done waiting'); continue; } if (test.prep !== undefined) { - await test.prep(); continue; } - const result = + const result: AzleResult = test.test !== undefined ? await test.test() : { Err: 'test is not defined' }; - if (!ok(result)) { - console.log('\x1b[31m', `test: ${test.name} failed`); - console.log('\x1b[31m', `${result.Err}`); - console.log('\x1b[0m'); + if (result.Err !== undefined || result.Ok === undefined) { + console.info('\x1b[31m', `test: ${test.name} failed`); + console.info('\x1b[31m', `${result.Err}`); + console.info('\x1b[0m'); if (exitProcess) { process.exit(1); @@ -81,9 +74,22 @@ export async function runTests( } } - if (result.Ok !== true) { - console.log('\x1b[31m', `test: ${test.name} failed`); - console.log('\x1b[0m'); + // TODO replace this with the below commented out code once jest migration is complete + const message = + typeof result.Ok === 'object' && result.Ok !== null + ? result.Ok.message + : undefined; + const successful = + typeof result.Ok === 'boolean' + ? result.Ok + : result.Ok.isSuccessful; + + if (successful !== true) { + console.info('\x1b[31m', `test: ${test.name} failed`); + if (message !== undefined) { + console.info('\x1b[31m', `${message}`); + } + console.info('\x1b[0m'); if (exitProcess) { process.exit(1); @@ -92,15 +98,30 @@ export async function runTests( } } - console.log('\x1b[32m', `test: ${test.name} passed`); - console.log('\x1b[0m'); + // TODO bring this back once jest migration is complete + // if (result.Ok.isSuccessful !== true) { + // console.info('\x1b[31m', `test: ${test.name} failed`); + // if (result.Ok.message !== undefined) { + // console.info('\x1b[31m', `${result.Ok.message}`); + // } + // console.info('\x1b[0m'); + + // if (exitProcess) { + // process.exit(1); + // } else { + // return false; + // } + // } + + console.info('\x1b[32m', `test: ${test.name} passed`); + console.info('\x1b[0m'); } catch (error) { - console.log( + console.info( '\x1b[31m', `test ${test.name} failed`, (error as any).toString() ); - console.log('\x1b[0m'); + console.info('\x1b[0m'); if (exitProcess) { process.exit(1); @@ -113,46 +134,17 @@ export async function runTests( return true; } -export function deploy(canisterName: string, argument?: string): Test[] { - return [ - { - // TODO hopefully we can get rid of this: https://forum.dfinity.org/t/generated-declarations-in-node-js-environment-break/12686/16?u=lastmjs - name: 'waiting for createActor fetchRootKey', - wait: 5000 - }, - { - name: `create canister ${canisterName}`, - prep: async () => { - execSync(`dfx canister create ${canisterName}`, { - stdio: 'inherit' - }); - } - }, - { - name: 'clear canister memory', - prep: async () => { - execSync( - `dfx canister uninstall-code ${canisterName} || true`, - { - stdio: 'inherit' - } - ); - } - }, - { - name: `deploy canister ${canisterName}`, - prep: async () => { - execSync( - `dfx deploy${ - argument === undefined ? '' : ` --argument ${argument}` - } ${canisterName}`, - { - stdio: 'inherit' - } - ); - } - } - ]; +// TODO is is better test framework conformity to call this assertEqual? I'll hold off for now, it should be easy to search for all testEquality and change it, easier than assertEqual I think +// TODO so based on this I think I've actually seen this in other testing frameworks, assertEquals will take two and make sure they are equals, and assert will take one boolean. Right now we have test instead of assert but it would be easy to change +export function testEquality(actual: T, expected: T): AzleResult { + if (deepEqual(actual, expected)) { + return { Ok: { isSuccessful: true } }; + } else { + const message = `Expected: ${jsonStringify( + expected + )}, Received: ${jsonStringify(actual)}`; + return { Ok: { isSuccessful: false, message } }; + } } export function createSnakeCaseProxy(