tunnelvisionlabs / antlr4cs

The original, highly-optimized C# Target for ANTLR 4
Other
450 stars 103 forks source link

Make generated *Context classes mockable #343

Open miloszkukla opened 4 years ago

miloszkukla commented 4 years ago

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 commented 4 years ago

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

miloszkukla commented 4 years ago

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 commented 4 years ago

@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.