10BC0 Add "using_tolerance" for easier float comparisons · Issue #81 · assertpy/assertpy · GitHub
[go: up one dir, main page]

Skip to content

Add "using_tolerance" for easier float comparisons #81

@j2kun

Description

@j2kun

I've been using assertpy for a while (it's great!!) and the one pesky thing that I wish assertpy had is to enable tolerant floating point comparisons for the life of an assertion.

A simple but illustrative example is when using nested tuples, sets, lists, and dictionaries involving floats.

expected_point_list = tested_fn()
actual_point_list = [(4, -5.1), (1.2, 4), (5.5, 4)]

assert_that(expected_point_list).using_tolerance(1e-9).is_close_to(actual_point_list)

To do this without such a method requires many loops

expected_point_list = tested_fn()
actual_point_list = [(4, -5.1), (1.2, 4), (5.5, 4)]

for (p1, p2) in zip(expected_point_list, actual_point_list):
  for (a, b) in zip(p1, p2):
    assert_that(a).is_close_to(b, 1e-9)

It's more complicated to do this when using sets, where iteration isn't deterministic. At each extra level of complication, it's harder to read, easier to mix up the expected and actual values, and harder to debug. If the above fails on the value 4, which index does it correspond to? This feature would enable that.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0