From 2c9b6376a92bbd925d42a2cd76265759fb295b78 Mon Sep 17 00:00:00 2001 From: Mayukha Vadari Date: Thu, 14 Sep 2023 16:26:03 -0400 Subject: [PATCH] switch createValidateTests to JS --- create_validate_tests.py | 174 ---------------- .../models/XChainAccountCreateCommit.test.ts | 16 +- .../XChainAddAccountCreateAttestation.test.ts | 46 ++--- .../models/XChainAddClaimAttestation.test.ts | 43 ++-- packages/xrpl/test/models/XChainClaim.test.ts | 18 +- .../xrpl/test/models/XChainCommit.test.ts | 14 +- .../test/models/XChainCreateBridge.test.ts | 10 +- .../test/models/XChainCreateClaimID.test.ts | 12 +- .../test/models/XChainModifyBridge.test.ts | 8 +- tools/createValidateTests.js | 187 ++++++++++++++++++ 10 files changed, 270 insertions(+), 258 deletions(-) delete mode 100644 create_validate_tests.py create mode 100644 tools/createValidateTests.js diff --git a/create_validate_tests.py b/create_validate_tests.py deleted file mode 100644 index db332a5c30..0000000000 --- a/create_validate_tests.py +++ /dev/null @@ -1,174 +0,0 @@ -""" -Helper script to write `validate` methods for transactions. -""" -import sys -import json - - -NORMAL_TYPES = ["number", "string"] -NUMBERS = ["0", "1"] - -BINARY_CODEC_FIXTURES = "./packages/ripple-binary-codec/test/fixtures/codec-fixtures.json" - -def get_tx(tx_name): - with open(BINARY_CODEC_FIXTURES) as f: - fixtures = json.load(f) - transactions = fixtures["transactions"] - print(len(transactions)) - valid_txs = [tx["json"] for tx in transactions if tx["json"]["TransactionType"] == tx_name] - valid_tx = valid_txs[0] - del valid_tx["TxnSignature"] - del valid_tx["SigningPubKey"] - return json.dumps(valid_tx, indent=2) - - -def main(): - model_name = sys.argv[1] - filename = f"./packages/xrpl/src/models/transactions/{model_name}.ts" - model, tx_name = get_model(filename) - return process_model(model, tx_name) - - -# Extract just the model from the file -def get_model(filename): - model = "" - started = False - ended = False - with open(filename) as f: - for line in f: - if ended: - continue - if not started and not line.startswith("export"): - continue - if not started and "Flags" in line: - continue - if not started: - started = True - model += line - if line == '}\n': - ended = True - - lines = model.split("\n") - name_line = lines[0].split(" ") - tx_name = name_line[2] - return model, tx_name - - -def get_invalid_value(param_types): - if len(param_types) == 1: - param_type = param_types[0] - if param_type == "number": - return "'number'" - elif param_type == "string": - return 123 - elif param_type == "IssuedCurrency": - return {"test": "test"} - elif param_type == "Amount": - return {"currency": "ETH"} - elif param_type == "XChainBridge": - return {"XChainDoor": "test"} - else: - raise Exception(f"{param_type} not supported yet") - - simplified_param_types = {param_types[i] for i in range(len(param_types)) if i % 2 == 0} - if simplified_param_types == {'0', '1'}: - return 2 - elif simplified_param_types == {"number", "string"}: - return {"currency": "ETH"} - else: - raise Exception(f"{simplified_param_types} not supported yet") - -# Process the model and build the tests - -def process_model(model, tx_name): - output = "" - - for line in model.split("\n"): - # skip the things we don't want to test - if line == "": - continue - if line.startswith("export"): - continue - if line == "}": - continue - line = line.strip() - - if line.startswith("TransactionType"): - continue - if line.startswith("Flags"): - # TODO: support flag checking - continue - - split = line.split(" ") - param = split[0].strip("?:") - param_types = split[1:] - optional = split[0].endswith("?:") - - if not optional: - output += f""" it(`throws w/ missing {param}`, function () {{ - delete tx.{param} - - assert.throws( - () => validate{tx_name}(tx), - ValidationError, - '{tx_name}: missing field {param}', - ) - assert.throws( - () => validate(tx), - ValidationError, - '{tx_name}: missing field {param}', - ) - }}) - -""" - - fake_value = get_invalid_value(param_types) - output += f""" it(`throws w/ invalid {param}`, function () {{ - tx.{param} = {fake_value} - - assert.throws( - () => validate{tx_name}(tx), - ValidationError, - '{tx_name}: invalid field {param}', - ) - assert.throws( - () => validate(tx), - ValidationError, - '{tx_name}: invalid field {param}', - ) - }}) - -""" - - output = output[:-2] - output += "})\n" - - output = f"""import {{ assert }} from 'chai' - -import {{ validate, ValidationError }} from '../../src' -import {{ validate{tx_name} }} from '../../src/models/transactions/{tx_name}' - -/** - * {tx_name} Transaction Verification Testing. - * - * Providing runtime verification testing for each specific transaction type. - */ -describe('{tx_name}', function () {{ - let tx - - beforeEach(function () {{ - tx = {get_tx(tx_name)} as any - }}) - - it('verifies valid {tx_name}', function () {{ - assert.doesNotThrow(() => validate{tx_name}(tx)) - assert.doesNotThrow(() => validate(tx)) - }}) - -""" + output - - return output - - -if __name__ == "__main__": - print(main()) diff --git a/packages/xrpl/test/models/XChainAccountCreateCommit.test.ts b/packages/xrpl/test/models/XChainAccountCreateCommit.test.ts index 60465645b0..454458fdae 100644 --- a/packages/xrpl/test/models/XChainAccountCreateCommit.test.ts +++ b/packages/xrpl/test/models/XChainAccountCreateCommit.test.ts @@ -39,7 +39,7 @@ describe('XChainAccountCreateCommit', function () { assert.doesNotThrow(() => validate(tx)) }) - it(`throws w/ missing XChainBridge`, function () { + it('throws w/ missing XChainBridge', function () { delete tx.XChainBridge assert.throws( @@ -54,7 +54,7 @@ describe('XChainAccountCreateCommit', function () { ) }) - it(`throws w/ invalid XChainBridge`, function () { + it('throws w/ invalid XChainBridge', function () { tx.XChainBridge = { XChainDoor: 'test' } assert.throws( @@ -69,7 +69,7 @@ describe('XChainAccountCreateCommit', function () { ) }) - it(`throws w/ missing SignatureReward`, function () { + it('throws w/ missing SignatureReward', function () { delete tx.SignatureReward assert.throws( @@ -84,7 +84,7 @@ describe('XChainAccountCreateCommit', function () { ) }) - it(`throws w/ invalid SignatureReward`, function () { + it('throws w/ invalid SignatureReward', function () { tx.SignatureReward = { currency: 'ETH' } assert.throws( @@ -99,7 +99,7 @@ describe('XChainAccountCreateCommit', function () { ) }) - it(`throws w/ missing Destination`, function () { + it('throws w/ missing Destination', function () { delete tx.Destination assert.throws( @@ -114,7 +114,7 @@ describe('XChainAccountCreateCommit', function () { ) }) - it(`throws w/ invalid Destination`, function () { + it('throws w/ invalid Destination', function () { tx.Destination = 123 assert.throws( @@ -129,7 +129,7 @@ describe('XChainAccountCreateCommit', function () { ) }) - it(`throws w/ missing Amount`, function () { + it('throws w/ missing Amount', function () { delete tx.Amount assert.throws( @@ -144,7 +144,7 @@ describe('XChainAccountCreateCommit', function () { ) }) - it(`throws w/ invalid Amount`, function () { + it('throws w/ invalid Amount', function () { tx.Amount = { currency: 'ETH' } assert.throws( diff --git a/packages/xrpl/test/models/XChainAddAccountCreateAttestation.test.ts b/packages/xrpl/test/models/XChainAddAccountCreateAttestation.test.ts index 8d64c34736..4fb98fd584 100644 --- a/packages/xrpl/test/models/XChainAddAccountCreateAttestation.test.ts +++ b/packages/xrpl/test/models/XChainAddAccountCreateAttestation.test.ts @@ -49,7 +49,7 @@ describe('XChainAddAccountCreateAttestation', function () { assert.doesNotThrow(() => validate(tx)) }) - it(`throws w/ missing Amount`, function () { + it('throws w/ missing Amount', function () { delete tx.Amount assert.throws( @@ -64,7 +64,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ invalid Amount`, function () { + it('throws w/ invalid Amount', function () { tx.Amount = { currency: 'ETH' } assert.throws( @@ -79,7 +79,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ missing AttestationRewardAccount`, function () { + it('throws w/ missing AttestationRewardAccount', function () { delete tx.AttestationRewardAccount assert.throws( @@ -94,7 +94,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ invalid AttestationRewardAccount`, function () { + it('throws w/ invalid AttestationRewardAccount', function () { tx.AttestationRewardAccount = 123 assert.throws( @@ -109,7 +109,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ missing AttestationSignerAccount`, function () { + it('throws w/ missing AttestationSignerAccount', function () { delete tx.AttestationSignerAccount assert.throws( @@ -124,7 +124,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ invalid AttestationSignerAccount`, function () { + it('throws w/ invalid AttestationSignerAccount', function () { tx.AttestationSignerAccount = 123 assert.throws( @@ -139,7 +139,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ missing Destination`, function () { + it('throws w/ missing Destination', function () { delete tx.Destination assert.throws( @@ -154,7 +154,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ invalid Destination`, function () { + it('throws w/ invalid Destination', function () { tx.Destination = 123 assert.throws( @@ -169,7 +169,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ missing OtherChainSource`, function () { + it('throws w/ missing OtherChainSource', function () { delete tx.OtherChainSource assert.throws( @@ -184,7 +184,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ invalid OtherChainSource`, function () { + it('throws w/ invalid OtherChainSource', function () { tx.OtherChainSource = 123 assert.throws( @@ -199,7 +199,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ missing PublicKey`, function () { + it('throws w/ missing PublicKey', function () { delete tx.PublicKey assert.throws( @@ -214,7 +214,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ invalid PublicKey`, function () { + it('throws w/ invalid PublicKey', function () { tx.PublicKey = 123 assert.throws( @@ -229,7 +229,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ missing Signature`, function () { + it('throws w/ missing Signature', function () { delete tx.Signature assert.throws( @@ -244,7 +244,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ invalid Signature`, function () { + it('throws w/ invalid Signature', function () { tx.Signature = 123 assert.throws( @@ -259,7 +259,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ missing SignatureReward`, function () { + it('throws w/ missing SignatureReward', function () { delete tx.SignatureReward assert.throws( @@ -274,7 +274,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ invalid SignatureReward`, function () { + it('throws w/ invalid SignatureReward', function () { tx.SignatureReward = { currency: 'ETH' } assert.throws( @@ -289,7 +289,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ missing WasLockingChainSend`, function () { + it('throws w/ missing WasLockingChainSend', function () { delete tx.WasLockingChainSend assert.throws( @@ -304,7 +304,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ invalid WasLockingChainSend`, function () { + it('throws w/ invalid WasLockingChainSend', function () { tx.WasLockingChainSend = 2 assert.throws( @@ -319,7 +319,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ missing XChainAccountCreateCount`, function () { + it('throws w/ missing XChainAccountCreateCount', function () { delete tx.XChainAccountCreateCount assert.throws( @@ -334,8 +334,8 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ invalid XChainAccountCreateCount`, function () { - tx.XChainAccountCreateCount = true + it('throws w/ invalid XChainAccountCreateCount', function () { + tx.XChainAccountCreateCount = { currency: 'ETH' } assert.throws( () => validateXChainAddAccountCreateAttestation(tx), @@ -349,7 +349,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ missing XChainBridge`, function () { + it('throws w/ missing XChainBridge', function () { delete tx.XChainBridge assert.throws( @@ -364,7 +364,7 @@ describe('XChainAddAccountCreateAttestation', function () { ) }) - it(`throws w/ invalid XChainBridge`, function () { + it('throws w/ invalid XChainBridge', function () { tx.XChainBridge = { XChainDoor: 'test' } assert.throws( diff --git a/packages/xrpl/test/models/XChainAddClaimAttestation.test.ts b/packages/xrpl/test/models/XChainAddClaimAttestation.test.ts index 0fc5898a9a..4e7d1aeb7a 100644 --- a/packages/xrpl/test/models/XChainAddClaimAttestation.test.ts +++ b/packages/xrpl/test/models/XChainAddClaimAttestation.test.ts @@ -25,8 +25,7 @@ describe('XChainAddClaimAttestation', function () { 'ED7541DEC700470F54276C90C333A13CDBB5D341FD43C60CEA12170F6D6D4E1136', Sequence: 9, Signature: - '7C175050B08000AD35EEB2D87E16CD3F95A0AEEBF2A049474275153D9D4DD445' + - '28FE99AA50E71660A15B0B768E1B90E609BBD5DC7AFAFD45D9705D72D40EA10C', + '7C175050B08000AD35EEB2D87E16CD3F95A0AEEBF2A049474275153D9D4DD44528FE99AA50E71660A15B0B768E1B90E609BBD5DC7AFAFD45D9705D72D40EA10C', TransactionType: 'XChainAddClaimAttestation', WasLockingChainSend: 1, XChainBridge: { @@ -48,7 +47,7 @@ describe('XChainAddClaimAttestation', function () { assert.doesNotThrow(() => validate(tx)) }) - it(`throws w/ missing Amount`, function () { + it('throws w/ missing Amount', function () { delete tx.Amount assert.throws( @@ -63,7 +62,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ invalid Amount`, function () { + it('throws w/ invalid Amount', function () { tx.Amount = { currency: 'ETH' } assert.throws( @@ -78,7 +77,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ missing AttestationRewardAccount`, function () { + it('throws w/ missing AttestationRewardAccount', function () { delete tx.AttestationRewardAccount assert.throws( @@ -93,7 +92,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ invalid AttestationRewardAccount`, function () { + it('throws w/ invalid AttestationRewardAccount', function () { tx.AttestationRewardAccount = 123 assert.throws( @@ -108,7 +107,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ missing AttestationSignerAccount`, function () { + it('throws w/ missing AttestationSignerAccount', function () { delete tx.AttestationSignerAccount assert.throws( @@ -123,7 +122,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ invalid AttestationSignerAccount`, function () { + it('throws w/ invalid AttestationSignerAccount', function () { tx.AttestationSignerAccount = 123 assert.throws( @@ -138,7 +137,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ invalid Destination`, function () { + it('throws w/ invalid Destination', function () { tx.Destination = 123 assert.throws( @@ -153,7 +152,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ missing OtherChainSource`, function () { + it('throws w/ missing OtherChainSource', function () { delete tx.OtherChainSource assert.throws( @@ -168,7 +167,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ invalid OtherChainSource`, function () { + it('throws w/ invalid OtherChainSource', function () { tx.OtherChainSource = 123 assert.throws( @@ -183,7 +182,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ missing PublicKey`, function () { + it('throws w/ missing PublicKey', function () { delete tx.PublicKey assert.throws( @@ -198,7 +197,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ invalid PublicKey`, function () { + it('throws w/ invalid PublicKey', function () { tx.PublicKey = 123 assert.throws( @@ -213,7 +212,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ missing Signature`, function () { + it('throws w/ missing Signature', function () { delete tx.Signature assert.throws( @@ -228,7 +227,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ invalid Signature`, function () { + it('throws w/ invalid Signature', function () { tx.Signature = 123 assert.throws( @@ -243,7 +242,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ missing WasLockingChainSend`, function () { + it('throws w/ missing WasLockingChainSend', function () { delete tx.WasLockingChainSend assert.throws( @@ -258,7 +257,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ invalid WasLockingChainSend`, function () { + it('throws w/ invalid WasLockingChainSend', function () { tx.WasLockingChainSend = 2 assert.throws( @@ -273,7 +272,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ missing XChainBridge`, function () { + it('throws w/ missing XChainBridge', function () { delete tx.XChainBridge assert.throws( @@ -288,7 +287,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ invalid XChainBridge`, function () { + it('throws w/ invalid XChainBridge', function () { tx.XChainBridge = { XChainDoor: 'test' } assert.throws( @@ -303,7 +302,7 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ missing XChainClaimID`, function () { + it('throws w/ missing XChainClaimID', function () { delete tx.XChainClaimID assert.throws( @@ -318,8 +317,8 @@ describe('XChainAddClaimAttestation', function () { ) }) - it(`throws w/ invalid XChainClaimID`, function () { - tx.XChainClaimID = ['123'] + it('throws w/ invalid XChainClaimID', function () { + tx.XChainClaimID = { currency: 'ETH' } assert.throws( () => validateXChainAddClaimAttestation(tx), diff --git a/packages/xrpl/test/models/XChainClaim.test.ts b/packages/xrpl/test/models/XChainClaim.test.ts index 8ee8984860..7b854ea786 100644 --- a/packages/xrpl/test/models/XChainClaim.test.ts +++ b/packages/xrpl/test/models/XChainClaim.test.ts @@ -39,7 +39,7 @@ describe('XChainClaim', function () { assert.doesNotThrow(() => validate(tx)) }) - it(`throws w/ missing XChainBridge`, function () { + it('throws w/ missing XChainBridge', function () { delete tx.XChainBridge assert.throws( @@ -54,7 +54,7 @@ describe('XChainClaim', function () { ) }) - it(`throws w/ invalid XChainBridge`, function () { + it('throws w/ invalid XChainBridge', function () { tx.XChainBridge = { XChainDoor: 'test' } assert.throws( @@ -69,7 +69,7 @@ describe('XChainClaim', function () { ) }) - it(`throws w/ missing XChainClaimID`, function () { + it('throws w/ missing XChainClaimID', function () { delete tx.XChainClaimID assert.throws( @@ -84,7 +84,7 @@ describe('XChainClaim', function () { ) }) - it(`throws w/ invalid XChainClaimID`, function () { + it('throws w/ invalid XChainClaimID', function () { tx.XChainClaimID = { currency: 'ETH' } assert.throws( @@ -99,7 +99,7 @@ describe('XChainClaim', function () { ) }) - it(`throws w/ missing Destination`, function () { + it('throws w/ missing Destination', function () { delete tx.Destination assert.throws( @@ -114,7 +114,7 @@ describe('XChainClaim', function () { ) }) - it(`throws w/ invalid Destination`, function () { + it('throws w/ invalid Destination', function () { tx.Destination = 123 assert.throws( @@ -129,7 +129,7 @@ describe('XChainClaim', function () { ) }) - it(`throws w/ invalid DestinationTag`, function () { + it('throws w/ invalid DestinationTag', function () { tx.DestinationTag = 'number' assert.throws( @@ -144,7 +144,7 @@ describe('XChainClaim', function () { ) }) - it(`throws w/ missing Amount`, function () { + it('throws w/ missing Amount', function () { delete tx.Amount assert.throws( @@ -159,7 +159,7 @@ describe('XChainClaim', function () { ) }) - it(`throws w/ invalid Amount`, function () { + it('throws w/ invalid Amount', function () { tx.Amount = { currency: 'ETH' } assert.throws( diff --git a/packages/xrpl/test/models/XChainCommit.test.ts b/packages/xrpl/test/models/XChainCommit.test.ts index e5f179ef64..4963cf1bef 100644 --- a/packages/xrpl/test/models/XChainCommit.test.ts +++ b/packages/xrpl/test/models/XChainCommit.test.ts @@ -38,7 +38,7 @@ describe('XChainCommit', function () { assert.doesNotThrow(() => validate(tx)) }) - it(`throws w/ missing XChainBridge`, function () { + it('throws w/ missing XChainBridge', function () { delete tx.XChainBridge assert.throws( @@ -53,7 +53,7 @@ describe('XChainCommit', function () { ) }) - it(`throws w/ invalid XChainBridge`, function () { + it('throws w/ invalid XChainBridge', function () { tx.XChainBridge = { XChainDoor: 'test' } assert.throws( @@ -68,7 +68,7 @@ describe('XChainCommit', function () { ) }) - it(`throws w/ missing XChainClaimID`, function () { + it('throws w/ missing XChainClaimID', function () { delete tx.XChainClaimID assert.throws( @@ -83,7 +83,7 @@ describe('XChainCommit', function () { ) }) - it(`throws w/ invalid XChainClaimID`, function () { + it('throws w/ invalid XChainClaimID', function () { tx.XChainClaimID = { currency: 'ETH' } assert.throws( @@ -98,7 +98,7 @@ describe('XChainCommit', function () { ) }) - it(`throws w/ invalid OtherChainDestination`, function () { + it('throws w/ invalid OtherChainDestination', function () { tx.OtherChainDestination = 123 assert.throws( @@ -113,7 +113,7 @@ describe('XChainCommit', function () { ) }) - it(`throws w/ missing Amount`, function () { + it('throws w/ missing Amount', function () { delete tx.Amount assert.throws( @@ -128,7 +128,7 @@ describe('XChainCommit', function () { ) }) - it(`throws w/ invalid Amount`, function () { + it('throws w/ invalid Amount', function () { tx.Amount = { currency: 'ETH' } assert.throws( diff --git a/packages/xrpl/test/models/XChainCreateBridge.test.ts b/packages/xrpl/test/models/XChainCreateBridge.test.ts index ee6fc20fae..dfd174058e 100644 --- a/packages/xrpl/test/models/XChainCreateBridge.test.ts +++ b/packages/xrpl/test/models/XChainCreateBridge.test.ts @@ -38,7 +38,7 @@ describe('XChainCreateBridge', function () { assert.doesNotThrow(() => validate(tx)) }) - it(`throws w/ missing XChainBridge`, function () { + it('throws w/ missing XChainBridge', function () { delete tx.XChainBridge assert.throws( @@ -53,7 +53,7 @@ describe('XChainCreateBridge', function () { ) }) - it(`throws w/ invalid XChainBridge`, function () { + it('throws w/ invalid XChainBridge', function () { tx.XChainBridge = { XChainDoor: 'test' } assert.throws( @@ -68,7 +68,7 @@ describe('XChainCreateBridge', function () { ) }) - it(`throws w/ missing SignatureReward`, function () { + it('throws w/ missing SignatureReward', function () { delete tx.SignatureReward assert.throws( @@ -83,7 +83,7 @@ describe('XChainCreateBridge', function () { ) }) - it(`throws w/ invalid SignatureReward`, function () { + it('throws w/ invalid SignatureReward', function () { tx.SignatureReward = { currency: 'ETH' } assert.throws( @@ -98,7 +98,7 @@ describe('XChainCreateBridge', function () { ) }) - it(`throws w/ invalid MinAccountCreateAmount`, function () { + it('throws w/ invalid MinAccountCreateAmount', function () { tx.MinAccountCreateAmount = { currency: 'ETH' } assert.throws( diff --git a/packages/xrpl/test/models/XChainCreateClaimID.test.ts b/packages/xrpl/test/models/XChainCreateClaimID.test.ts index a9a679f44c..503fae0130 100644 --- a/packages/xrpl/test/models/XChainCreateClaimID.test.ts +++ b/packages/xrpl/test/models/XChainCreateClaimID.test.ts @@ -38,7 +38,7 @@ describe('XChainCreateClaimID', function () { assert.doesNotThrow(() => validate(tx)) }) - it(`throws w/ missing XChainBridge`, function () { + it('throws w/ missing XChainBridge', function () { delete tx.XChainBridge assert.throws( @@ -53,7 +53,7 @@ describe('XChainCreateClaimID', function () { ) }) - it(`throws w/ invalid XChainBridge`, function () { + it('throws w/ invalid XChainBridge', function () { tx.XChainBridge = { XChainDoor: 'test' } assert.throws( @@ -68,7 +68,7 @@ describe('XChainCreateClaimID', function () { ) }) - it(`throws w/ missing SignatureReward`, function () { + it('throws w/ missing SignatureReward', function () { delete tx.SignatureReward assert.throws( @@ -83,7 +83,7 @@ describe('XChainCreateClaimID', function () { ) }) - it(`throws w/ invalid SignatureReward`, function () { + it('throws w/ invalid SignatureReward', function () { tx.SignatureReward = { currency: 'ETH' } assert.throws( @@ -98,7 +98,7 @@ describe('XChainCreateClaimID', function () { ) }) - it(`throws w/ missing OtherChainSource`, function () { + it('throws w/ missing OtherChainSource', function () { delete tx.OtherChainSource assert.throws( @@ -113,7 +113,7 @@ describe('XChainCreateClaimID', function () { ) }) - it(`throws w/ invalid OtherChainSource`, function () { + it('throws w/ invalid OtherChainSource', function () { tx.OtherChainSource = 123 assert.throws( diff --git a/packages/xrpl/test/models/XChainModifyBridge.test.ts b/packages/xrpl/test/models/XChainModifyBridge.test.ts index 4f7f2b67f5..57eeb50823 100644 --- a/packages/xrpl/test/models/XChainModifyBridge.test.ts +++ b/packages/xrpl/test/models/XChainModifyBridge.test.ts @@ -38,7 +38,7 @@ describe('XChainModifyBridge', function () { assert.doesNotThrow(() => validate(tx)) }) - it(`throws w/ missing XChainBridge`, function () { + it('throws w/ missing XChainBridge', function () { delete tx.XChainBridge assert.throws( @@ -53,7 +53,7 @@ describe('XChainModifyBridge', function () { ) }) - it(`throws w/ invalid XChainBridge`, function () { + it('throws w/ invalid XChainBridge', function () { tx.XChainBridge = { XChainDoor: 'test' } assert.throws( @@ -68,7 +68,7 @@ describe('XChainModifyBridge', function () { ) }) - it(`throws w/ invalid SignatureReward`, function () { + it('throws w/ invalid SignatureReward', function () { tx.SignatureReward = { currency: 'ETH' } assert.throws( @@ -83,7 +83,7 @@ describe('XChainModifyBridge', function () { ) }) - it(`throws w/ invalid MinAccountCreateAmount`, function () { + it('throws w/ invalid MinAccountCreateAmount', function () { tx.MinAccountCreateAmount = { currency: 'ETH' } assert.throws( diff --git a/tools/createValidateTests.js b/tools/createValidateTests.js new file mode 100644 index 0000000000..570644a619 --- /dev/null +++ b/tools/createValidateTests.js @@ -0,0 +1,187 @@ +const fs = require("fs"); +const fixtures = require("../packages/ripple-binary-codec/test/fixtures/codec-fixtures.json"); + +const NORMAL_TYPES = ["number", "string"]; +const NUMBERS = ["0", "1"]; + +function getTx(txName) { + transactions = fixtures.transactions; + const validTxs = fixtures.transactions + .filter((tx) => tx.json.TransactionType === txName) + .map((tx) => tx.json); + const validTx = validTxs[0]; + delete validTx.TxnSignature; + delete validTx.SigningPubKey; + return JSON.stringify(validTx, null, 2); +} + +function main() { + const modelName = process.argv[2]; + const filename = `./packages/xrpl/src/models/transactions/${modelName}.ts`; + const [model, txName] = getModel(filename); + return processModel(model, txName); +} + +// Extract just the model from the file +function getModel(filename) { + let model = ""; + let started = false; + let ended = false; + const data = fs.readFileSync(filename, "utf8"); + const lines = data.split("\n"); + for (let line of lines) { + if (ended) { + continue; + } + if (!started && !line.startsWith("export")) { + continue; + } + if (!started && line.includes("Flags")) { + continue; + } + if (!started) { + started = true; + } + model += line + "\n"; + if (line === "}") { + ended = true; + } + } + const name_line = model.split("\n")[0].split(" "); + const txName = name_line[2]; + return [model, txName]; +} + +function getInvalidValue(paramTypes) { + if (paramTypes.length === 1) { + const paramType = paramTypes[0]; + if (paramType == "number") { + return "'number'"; + } else if (paramType == "string") { + return 123; + } else if (paramType == "IssuedCurrency") { + return JSON.stringify({ test: "test" }); + } else if (paramType == "Amount") { + return JSON.stringify({ currency: "ETH" }); + } else if (paramType == "XChainBridge") { + return JSON.stringify({ XChainDoor: "test" }); + } else { + throw Error(`${paramType} not supported yet`); + } + } + + const simplifiedParamTypes = paramTypes.filter( + (_paramType, index) => index % 2 == 0 + ); + if (JSON.stringify(simplifiedParamTypes) === '["0","1"]') { + return 2; + } else if (JSON.stringify(simplifiedParamTypes) === '["number","string"]') { + return JSON.stringify({ currency: "ETH" }); + } else { + throw Error(`${simplifiedParamTypes} not supported yet`); + } +} + +// Process the model and build the tests + +function processModel(model, txName) { + let output = ""; + for (let line of model.split("\n")) { + if (line == "") { + continue; + } + if (line.startsWith("export")) { + continue; + } + if (line == "}") { + continue; + } + line = line.trim(); + if (line.startsWith("TransactionType")) { + continue; + } + if (line.startsWith("Flags")) { + // TODO: support flag checking + continue; + } + if (line.startsWith("/**")) { + continue; + } + if (line.startsWith("*")) { + continue; + } + + const split = line.split(" "); + const param = split[0].replace("?:", "").replace(":", "").trim(); + const paramTypes = split.slice(1); + const optional = split[0].endsWith("?:"); + + if (!optional) { + output += ` it("throws w/ missing ${param}", function () { + delete tx.${param} + + assert.throws( + () => validate${txName}(tx), + ValidationError, + '${txName}: missing field ${param}', + ) + assert.throws( + () => validate(tx), + ValidationError, + '${txName}: missing field ${param}', + ) + }) + +`; + } + + const fakeValue = getInvalidValue(paramTypes); + output += ` it('throws w/ invalid ${param}', function () { + tx.${param} = ${fakeValue} + + assert.throws( + () => validate${txName}(tx), + ValidationError, + '${txName}: invalid field ${param}', + ) + assert.throws( + () => validate(tx), + ValidationError, + '${txName}: invalid field ${param}', + ) + }) + +`; + } + + output = output.substring(0, output.length - 2); + output += "\n})\n"; + output = + `import { assert } from 'chai' + +import { validate, ValidationError } from '../../src' +import { validate${txName} } from '../../src/models/transactions/${txName}' + +/** + * ${txName} Transaction Verification Testing. + * + * Providing runtime verification testing for each specific transaction type. + */ +describe('${txName}', function () { + let tx + + beforeEach(function () { + tx = ${getTx(txName)} as any + }) + + it('verifies valid ${txName}', function () { + assert.doesNotThrow(() => validate${txName}(tx)) + assert.doesNotThrow(() => validate(tx)) + }) + +` + output; + + return output; +} + +console.log(main());