GithubHelp home page GithubHelp logo

Comments (11)

hcoles avatar hcoles commented on June 8, 2024 2

This feature is implemented in #1310. In the next release it will be possible to filter these mutations out by adding the filter string "+funmodifiablecollection"

from pitest.

romani avatar romani commented on June 8, 2024 1

@hcoles , please share link to some doc or please share how to use ? Or where to put "+funmodifiablecollection" ? We use maven.

Thanks a lot in advance.
May be this https://pitest.org/quickstart/maven/#features ?

from pitest.

romani avatar romani commented on June 8, 2024

Some internal immutable collection is good for engineers express intend, such details might not be easily testable from public methods.
I think it is same level of restrictions that engineers use by placing final on variable, it is signal for compiler or other engineers that mutation is not recommended or not expected.

Avoiding reporting of survivals would be awesome, may be by options or specific mutator or ....

from pitest.

romani avatar romani commented on June 8, 2024

@hcoles , one more fix is required
Please look at checkstyle/checkstyle#14484 (comment)
Look like you covered the only when code is return ....;

from pitest.

hcoles avatar hcoles commented on June 8, 2024

The filter is restricted to direct returns wrapping return values in this way is wide spread idiom where it might be reasonably argued that writing tests to confirm the behaviour isn't a productive use of time. A failure to wrap a return value can be detected by static an analysis rather than a test.

Filtering the calls in other places makes less sense as it means pitest would not highlight redundant code.

The example code in your link

    public List<Token> getHiddenBefore() {
        List<Token> returnList = null;
        if (hiddenBefore != null) {
            returnList = Collections.unmodifiableList(hiddenBefore);
        }
        return returnList;
    }

Could be more clearly expressed as

    public List<Token> getHiddenBefore() {
        if (hiddenBefore != null) {
            return Collections.unmodifiableList(hiddenBefore);
        }
        return hiddenBefore;
    }

Or

    public List<Token> getHiddenBefore() {
        if (hiddenBefore == null) {
            return null;
        }
        return Collections.unmodifiableList(hiddenBefore);
    }

To emphasize that the method may return null.

from pitest.

romani avatar romani commented on June 8, 2024

there is another evil "single return from method" , but we refactored it to java8 style of code to reconcile.
I pretty sure there will be numerous examples where usage of unmodifiableList without return is reasonable.

ok, what about code like (link):

    public void setHiddenBefore(List<Token> hiddenBefore) {
        this.hiddenBefore = Collections.unmodifiableList(hiddenBefore);
    }

from pitest.

romani avatar romani commented on June 8, 2024

still mutation survival:
https://github.com/checkstyle/checkstyle/actions/runs/7916871513/job/21611685470?pr=14484#step:6:458

Source File: "DetailAstImpl.java"
Class: "com.puppycrawl.tools.checkstyle.DetailAstImpl"
Method: "getHiddenAfter"
Line Contents: ".map(Collections::unmodifiableList)"
Mutator: "org.pitest.mutationtest.engine.gregor.mutators.experimental.NakedReceiverMutator"
Description: "replaced call to java/util/Optional::map with receiver"
Line Number: 514

java:

        return Optional.ofNullable(hiddenBefore)
            .map(Collections::unmodifiableList)
            .orElse(null);

from pitest.

hcoles avatar hcoles commented on June 8, 2024
    public void setHiddenBefore(List<Token> hiddenBefore) {
        this.hiddenBefore = Collections.unmodifiableList(hiddenBefore);
    }

Wrapping on write is a common idiom, and I agree it would make sense to filter this.

Patterns such as creating an Optional in order to avoid writing an if statement, are not so common and it makes sense for pitest to continue to highlight code not justified by a test in these constructs.

If you wish to avoid mutating calls to unmodifiableList etc completely I think this would be better dealt with by a general purpose "don't mutate this" parameter similar to avoidCallTo.

from pitest.

romani avatar romani commented on June 8, 2024

Do you already have parameter by which we config pitest to skip mutation over unmodifiableList at all ?

avoidCallTo do no mutation inside method, but still mutate call of method.

from pitest.

hcoles avatar hcoles commented on June 8, 2024

avoidCallsTo would work, but it works at the class level, so you have to filter out calls to everything in java.util.Collections.

Filtering out calls to individual methods would need a new parameter.

from pitest.

romani avatar romani commented on June 8, 2024

yes, you are right, we already use avoidCallsTo for classes.
https://github.com/checkstyle/checkstyle/blob/c1ca1b903beaed94923f621fac2197394ae011dd/pom.xml#L3079-L3082

but did not want to exclude all methods from this class, as it has bunch of other methods like sort/...... that is better to question their necessity in main code.
that is why we created new class as wrapper to suppress only unmodifiableXxxx. It would be awesome to let users suppress more granular for specific method of class.

from pitest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.