Comment by anthonypasq96

7 hours ago

youre verifying std lib function call counts in unit tests? lmao.

You can do that with mocks if it's important that something is only called once, or likely there's some unintended side effect of calling it twice and tests woukd catch the bug

  • i know you could do it, im asking why on earth you would feel its vital to verify stream.filter() was called twice in a function

You're not verifying the observable behavior of your application? lmao

  • How would you suggest tests around:

        void func() {
            printEvens(someCall().stream().filter(n -> n % 2 == 0).toList());
        }
    
        void printEvens(List<Integer> nums) {
            nums.stream().filter(n -> n % 2 == 0).forEach(n -> System.out.println(n));
        }
    

    The first filter is redundant in this example. Duplicate code checkers are checking for exactly matching lines.

    I am unaware of any linter or static analyzer that would flag this.

    What's more, unit tests to test the code for printEvens (there exists one) pass because they're working properly... and the unit test that calls the calling function passes because it is working properly too.

    Alternatively, write the failing test for this code.

    • Idk how exactly to do it in cpp becasue I'm not familiar with the tooling

      You could write a test that makes sure the output of someCall is passed directly to printeven without being modified.

      The example as you wrote is hard to test in general. It's probably not something you would write if your serious about testing.

      1 reply →

  • A redundant filter() isn't observable (except in execution time).

    You could pick it up if you were to explicitly track whether it's being called redundantly but it'd be very hard and by the time you'd thought of doing that you'd certainly have already manually checked the code for it.