Fix the weakened assertion finder for test edits
Your task
A test failed after a feature change, and an agent was told to "make the test pass". It did, by loosening the assertions: status_code == 200 became status_code in (200, 201, 202), and total == 42 became total is not None. Your team now runs a check over every test edit that reports assertions made weaker.
An assistant wrote weakened_assertions(before, after). It is in the editor. It has a bug. Find it and fix it.
Input and output:
before: the assertion lines before the edit.after: the assertion lines after the edit, at the same positions.- Return the positions in
beforewhose assertion got weaker, in increasing order.
Rules the tests check:
- Score each line by how much it pins down. Look at the line with whitespace trimmed from both ends, in lowercase. The first rule that matches gives the score:
- The line is empty: 0. The check is gone.
- It contains
is not none: 1. - It contains
in (orin [, with the space beforein: 2. A set of accepted values. - It contains
==: 3. - Anything else: 1.
- Compare
before[i]withafter[i]. A position missing fromaftercounts as an empty line. Extra lines inafterare ignored. - Report position i when the score of
after[i]is lower than the score ofbefore[i]. A new expected value with the same score, like 42 becoming 50, is not reported.
Examples
weakened_assertions(before: list[str], after: list[str]) -> list[int]Example 1
Input
weakened_assertions(["assert response.status_code == 200", "assert response.json()[\"total\"] == 42"], ["assert response.status_code in (200, 201, 202)", "assert response.json().get(\"total\") is not None"])Output
[0, 1]Example 2
Input
weakened_assertions(["assert total == 42"], ["assert total == 50"])Output
[]Example 3
Input
weakened_assertions(["assert a == 1", "assert b == 2"], ["assert a == 1"])Output
[1]