Skip to content

tests: Add unit tests for Paragraph alignment#4614

Open
Hangeerh wants to merge 3 commits intoManimCommunity:mainfrom
Hangeerh:paragraph-testing
Open

tests: Add unit tests for Paragraph alignment#4614
Hangeerh wants to merge 3 commits intoManimCommunity:mainfrom
Hangeerh:paragraph-testing

Conversation

@Hangeerh
Copy link

Overview:

Added unit tests for Paragraph alignment. The changes implement a portion of features proposed in issue 2839, specifically tests for changes introduced in Fixed bad text slicing for lines in :class:.Paragraph #2721 .

Motivation and Explanation: Why and how do your changes improve the library?

Further Information and Comments

We test for left alignment by getting the underlying VMobjectFromSVGPath for the first character of each line, and comparing them using a custom float comparison funcition.

def test_paragraph_alignment():
    def float_eq(a: float, b: float, delta: float = 1e-2):
        return abs(a - b) <= delta

    # check paragraph left alignment
    par_left = Paragraph("This is", "a left-aligned", "paragraph", alignment="left")
    assert float_eq(par_left[0][0].get_x(LEFT), par_left[1][0].get_x(LEFT))
    assert float_eq(par_left[0][0].get_x(LEFT), par_left[2][0].get_x(LEFT))

Similarly when testing for right alignment, we compare the last letter of each line.

    # check paragraph right alignment
    par_right = Paragraph("This is", "a right-aligned", "paragraph", alignment="right")
    assert float_eq(par_right[0][-1].get_x(RIGHT), par_right[1][-1].get_x(RIGHT))
    assert float_eq(par_right[0][-1].get_x(RIGHT), par_right[2][-1].get_x(RIGHT))

When testing for center alignment, we compute the average of the left of the first character and the right of the last character in each line. This is supposed to give us an estimate of where the middle of the line is. We then compare them for each line.

    par_center = Paragraph(
        "This is", "a center-aligned", "paragraph", alignment="center"
    )
    assert float_eq(
        (par_center[0][0].get_x(LEFT) + par_center[0][-1].get_x(RIGHT)) / 2,
        (par_center[1][0].get_x(LEFT) + par_center[1][-1].get_x(RIGHT)) / 2,
    )
    assert float_eq(
        (par_center[0][0].get_x(LEFT) + par_center[0][-1].get_x(RIGHT)) / 2,
        (par_center[2][0].get_x(LEFT) + par_center[2][-1].get_x(RIGHT)) / 2,
    )

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

Copy link
Member

@behackl behackl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests seem fine to me, thanks for your contribution! I've left one request for a change with the assert statements, but other than that I'm happy to merge this.

Comment on lines +39 to +40
def float_eq(a: float, b: float, delta: float = 1e-2):
return abs(a - b) <= delta
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather use np.testing.assert_almost_equal with digits set to something reasonable over a custom numerical comparison method. Could you please change the asserts accordingly and remove float_eq?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed it to use numpy.testing.assert_almost_equal

This replaces the previous custom float comparison function with a numpy
function.
@Hangeerh
Copy link
Author

I've swapped out the previous custom function to use numpy.testing.assert_almost_equal.

I chose to compare up to two decimal places, because the width of one of the underlying VMobjectFromSVG is around 0.3 to 0.2. Comparing up to two decimal places would catch all errors that are one magnitude smaller than the width of the character. Any smaller difference would not be noticeable, as they correspond to a difference of about one hundredth of the character's width.

@Hangeerh Hangeerh requested a review from behackl March 6, 2026 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants