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

review: test: add tests for substitution class #4018

Merged
merged 6 commits into from
Jul 5, 2021
Merged

review: test: add tests for substitution class #4018

merged 6 commits into from
Jul 5, 2021

Conversation

Rohitesh-Kumar-Jain
Copy link
Contributor

#1818

Hi, I have added 3 new tests for methods of the Substitution class :

  1. testSubstituteMethodBody should kill this mutation :

Screenshot 2021-06-30 at 4 01 07 PM

  1. testSubstituteStatement should kill this mutation :

Screenshot 2021-06-30 at 4 02 09 PM

  1. testSubstituteFieldDefaultExpressionshould kill this mutation :

Screenshot 2021-06-30 at 4 03 34 PM

Link to #3967

@slarse slarse self-requested a review June 30, 2021 11:05
Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

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

Hi @Rohitesh-Kumar-Jain,

These tests are a little bit tell-tale of the "chasing coverage" problem I've mentioned a few times. They don't really test the core functionality of these methods. Your tests are designed to essentially test the methods as getters (as the contracts say). But the substitute methods are not plain getters, but rather as the name implies, they are meant to perform substitutions.

I would urge you to carefully study the templating documentation to get a better idea of what the purpose of these methods is.

It is a bit hard to understand how to use these things. To get you started, here's one complete test of substituteStatement that meaningfully tests the actual substitution.

@Test
public void testSubstituteStatementWithTemplatedInitializer() {
    // contract: Given a statement with a templated initializer, substituteStatement should
    // return a new statement with the initializer replaced with the value bound to the template
    // parameter

    // arrange
    Factory factory = createFactoryWithTemplates();
    CtClass<?> targetClass = factory.Class().create("TargetClass");

    String templateExecutableName = "executable";
    int templateVariableIndex = 0;
    String templateVariableName = "s";
    String initializerToSubstitute = "My chosen initializer";
    CtStatement expectedStatement = factory.createLocalVariable(
            factory.Type().stringType(), templateVariableName, factory.createLiteral(initializerToSubstitute)
    );

    StatementWithTemplatedInitializer template = new StatementWithTemplatedInitializer();
    template._initializer_ = factory.createLiteral(initializerToSubstitute);

    // act
    CtStatement substitutedStatement = Substitution.substituteStatement(targetClass, template, templateVariableIndex, templateExecutableName);

    // assert
    assertEquals(expectedStatement, substitutedStatement);
}

private static class StatementWithTemplatedInitializer extends ExtensionTemplate {
    TemplateParameter<String> _initializer_;

    public void executable() {
        String s = _initializer_.S();
    }
}

To be clear, I would like you to rewrite these tests to actually test some form of substitution.

@Rohitesh-Kumar-Jain
Copy link
Contributor Author

I would urge you to carefully study the templating documentation to get a better idea of what the purpose of these methods is.

Thanks for sharing this, I will go through this.

here's one complete test of substituteStatement that meaningfully tests the actual substitution.

I am glad that you showed me how to correctly test this. I will rewrite tests after going through the resources that you shared

@Rohitesh-Kumar-Jain
Copy link
Contributor Author

Should I refactor the wrongly extracted test (that I extracted in #4016) in this PR only ?

Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

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

Now the tests are much better! Some minor comments on this revision, see code.

src/test/java/spoon/test/template/SubstitutionTest.java Outdated Show resolved Hide resolved
src/test/java/spoon/test/template/SubstitutionTest.java Outdated Show resolved Hide resolved
src/test/java/spoon/test/template/SubstitutionTest.java Outdated Show resolved Hide resolved
src/test/java/spoon/test/template/SubstitutionTest.java Outdated Show resolved Hide resolved
@Rohitesh-Kumar-Jain
Copy link
Contributor Author

As all the suggestions are resolved for tests, should I refactor the wrongly extracted test (that I extracted in #4016)

Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

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

Nice, LGTM!

As all the suggestions are resolved for tests, should I refactor the wrongly extracted test (that I extracted in #4016)

Let's keep this PR clean and to the point, and move that test in a separate PR.

@slarse slarse merged commit 3bc0b7d into INRIA:master Jul 5, 2021
@Rohitesh-Kumar-Jain Rohitesh-Kumar-Jain deleted the add-tests-for-substitution-class branch July 5, 2021 09:48
@monperrus monperrus mentioned this pull request Aug 19, 2021
woutersmeenk pushed a commit to woutersmeenk/spoon that referenced this pull request Aug 29, 2021
algomaster99 added a commit to algomaster99/spoon that referenced this pull request Nov 11, 2021
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.

2 participants