| 177 | |
| 178 | |
| 179 | def simplify(case): # TODO this is a hack |
| 180 | if isinstance(case, SON) and "$ref" not in case: |
| 181 | simplified = SON(case) # make a copy! |
| 182 | if random.choice([True, False]): |
| 183 | # delete |
| 184 | simplified_keys = list(simplified) |
| 185 | if not len(simplified_keys): |
| 186 | return (False, case) |
| 187 | simplified.pop(random.choice(simplified_keys)) |
| 188 | return (True, simplified) |
| 189 | else: |
| 190 | # simplify a value |
| 191 | simplified_items = list(simplified.items()) |
| 192 | if not len(simplified_items): |
| 193 | return (False, case) |
| 194 | (key, value) = random.choice(simplified_items) |
| 195 | (success, value) = simplify(value) |
| 196 | simplified[key] = value |
| 197 | return (success, success and simplified or case) |
| 198 | if isinstance(case, list): |
| 199 | simplified = list(case) |
| 200 | if random.choice([True, False]): |
| 201 | # delete |
| 202 | if not len(simplified): |
| 203 | return (False, case) |
| 204 | simplified.pop(random.randrange(len(simplified))) |
| 205 | return (True, simplified) |
| 206 | else: |
| 207 | # simplify an item |
| 208 | if not len(simplified): |
| 209 | return (False, case) |
| 210 | index = random.randrange(len(simplified)) |
| 211 | (success, value) = simplify(simplified[index]) |
| 212 | simplified[index] = value |
| 213 | return (success, success and simplified or case) |
| 214 | return (False, case) |
| 215 | |
| 216 | |
| 217 | def reduce(case, predicate, reductions=0): |