Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds an assertion assertEOSErrorIncludesMessage #97

Conversation

dallasjohnson
Copy link
Contributor

This should assert the error message for an expected eosio_assert_message_exception includes the given description message.

  • The purpose of this is to allow more thorough testing of expected errors and ensure that one error is not masked by another error and potentially leaving logical branches of code untested.
  • It is also intended to check the error message is included by the thrown error message rather than equals which allows for as error message to be safely changed in the contract while maintaining a constant key (possibly a localised key used by a client.)

eg. A contract may include:

check(existing == statstable.end(), "ERR::CREATE_EXISITNG_SYMBOL::token with symbol already exists");

and a safe test assertion may only check for the ERR::CREATE_EXISITNG_SYMBOL part which allows for the contract developer to safely evolve the error message for debugging purposes without breaking the tests.

Copy link
Collaborator

@MitchPierias MitchPierias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this idea, and actually have it integrated locally. Consider factoring it directly into the assertEOSError method with a simple determiner within;

if (error.json && error.json.error && error.json.error.name) {
    // Compare error and fail if the error doesn't match the expected
    assert(
      error.json.error.name === eosErrorName,
      `Expected ${eosErrorName}, got ${error.json.error.name} instead.`
    );
    // Check the error details contain the optional message
    if (withMessage && error.json.error.details.length > 0) {
      assert.deepEqual(error.json.error.details[0], {
       message: `assertion failure with message: ${withMessage}`,
      });
    }
    return;
}

Then adding a withMessage argument like so;

export const assertEOSError = async (operation: Promise<any>, eosErrorName:string, withMessage?: string) {}

You can also add some icing to the default assert like;

// Fail if no exception thrown
assert.fail(`Expected ${withMessage || 'error'} but operation completed successfully.`);

Thoughts?

@dallasjohnson
Copy link
Contributor Author

I was thinking that having a specific assert would mean that the èosErrorNameparam could be omitted from the call and be internally hardcoded since for custom errors generated viacheck(...)` in a contract they are always the same AFAIK.

@MitchPierias
Copy link
Collaborator

MitchPierias commented Oct 8, 2019

Ahh yes I see what your going for there! Good idea, much cleaner! I think I ran into this problem too. Can I suggest you add this determiner to assertEOSError and then simplify your assertEOSErrorIncludesMessage(operation:Promimse<any>, withMessage:string)
to simply call assertEOSError with the message and hard coded eosErrorName;

export const assertEOSErrorMessage = async (operation: Promise<any>, message: string) => {
    // Pass our operation and message onto assertEOSError
    return assertEOSError(operation, 'eosio_assert_message_exception', message);
}

This will minimize duplicate code. Also please add yourself to the authors on Kevin's assertEOSError as well if you go with this option

Am I still missing things?

@dallasjohnson
Copy link
Contributor Author

I had deliberately not checked for equals on the message but instead check the message includes the error. This allows for partial checking of the error string as a showed in the example above.What are your thoughts on that?

@MitchPierias
Copy link
Collaborator

I agree, great idea! Would it be a good idea to consolidate your changes into assertEOSError and make assertEOSErrorMessage be a shorthand method, or no? @thekevinbrown thoughts too please?

@dallasjohnson dallasjohnson force-pushed the feature/Error-message-include-assertion branch from 13764ef to 3aa2704 Compare October 8, 2019 23:38
@thekevinbrown
Copy link
Member

I feel like we can be consistent on how we assert EOS errors and this can be a flavour of an EOS error we assert (e.g. shorthand like you're saying)

Copy link
Member

@thekevinbrown thekevinbrown left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of minor things I noticed. Thanks again for the contribution!

src/utils.ts Outdated Show resolved Hide resolved
src/utils.ts Outdated Show resolved Hide resolved
src/utils.ts Outdated Show resolved Hide resolved
src/utils.ts Outdated Show resolved Hide resolved
@dallasjohnson dallasjohnson force-pushed the feature/Error-message-include-assertion branch from f1b14a0 to 570cd9b Compare October 10, 2019 08:06
…e error message for an expected `eosio_assert_message_exception` includes the given description.
… assertions by creating a private function for other assertions to call into.
@dallasjohnson dallasjohnson force-pushed the feature/Error-message-include-assertion branch from 570cd9b to 3d3572b Compare October 12, 2019 00:00
@MitchPierias MitchPierias merged commit 9d8441f into CoinageCrypto:master Oct 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants