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

Make generated *Context classes mockable #343

Open
miloszkukla opened this issue Feb 6, 2020 · 3 comments
Open

Make generated *Context classes mockable #343

miloszkukla opened this issue Feb 6, 2020 · 3 comments

Comments

@miloszkukla
Copy link

miloszkukla commented Feb 6, 2020

I'm wondering would it be feasible for generated Context classes to be mockable by implementing an interface I*Context or making the members virtual?

I'm not sure how would that affect the performance (dynamic dispatch) but if so it could be switchable by an option.

I guess \tool\resources\org\antlr\v4\tool\templates\codegen\CSharp\CSharp.stg would need to be modified and extra step for generating such interfaces would need to be added.

The obvious problem are public fields that are generated in *Context classes - they can't be part of the interface. Could they be replaced with properties assuming JIT would inline them so performance wouldn't be hurt?

@sharwell
Copy link
Member

sharwell commented Feb 6, 2020

Can you provide a more specific example of how the current context objects cannot be tested?

@miloszkukla
Copy link
Author

miloszkukla commented Feb 10, 2020

Ok, we've tried to write example test TestAntlr.zip and concluded that indeed it can be tested by adding children to ParseRuleContext derived objects (ParseRuleContext.AddChild method). Fields representing labels can be just assigned to expected value. Such tests are probably quite fragile to grammar changes but I guess that's unavoidable. Feel free to close this issue :)

@sharwell
Copy link
Member

sharwell commented Feb 10, 2020

@miloszkukla My recommendation would be providing example inputs to the parser (rather than constructing the context objects directly, which may or may not reflect reality), and combining the result with a code coverage tool like codecov.io to ensure you don't lose coverage of parts of the grammar when changes are made in the future.

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

No branches or pull requests

2 participants