Skip to content

Add unit tests for grid and polygon shapes, and enhance star shape tests#3955

Open
tarone-saloni wants to merge 1 commit intoGraphiteEditor:masterfrom
tarone-saloni:add-tests/polygon-star-grid-snapper
Open

Add unit tests for grid and polygon shapes, and enhance star shape tests#3955
tarone-saloni wants to merge 1 commit intoGraphiteEditor:masterfrom
tarone-saloni:add-tests/polygon-star-grid-snapper

Conversation

@tarone-saloni
Copy link
Copy Markdown

  • Adds #[cfg(test)] coverage to three previously untested files in the tool system.
    Tests cover polygon/star draw, modifier keys (Shift/Alt), RMB cancel, radius correctness,
    and pure unit tests for grid spacing math in calculate_grid_params.

Fixed #3954

Screenshot From 2026-03-26 19-22-04

Signed-off-by: Saloni Tarone <taronesaloni@gmail.com>
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the robustness of the editor's shape tools by adding extensive unit test coverage for grid, polygon, and star shapes. These tests validate core drawing logic, modifier key interactions, and cancellation behavior, ensuring greater stability and preventing regressions in future development.

Highlights

  • Grid Shape Tests: Added comprehensive unit tests for the grid_shape.rs file, specifically for the calculate_grid_params function, covering various grid drawing scenarios.
  • Polygon Shape Tests: Introduced new unit tests for polygon_shape.rs to validate polygon drawing behavior, including modifier keys, radius calculations, and cancellation.
  • Star Shape Tests: Enhanced the existing test suite for star_shape.rs with new unit tests to ensure correct star drawing, radius calculations, and cancellation logic.
  • Issue Resolution: Resolved issue Add tests for polygon shape, star shape, and grid snapping tools #3954, which likely pertained to the lack of test coverage for these shape tools.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@tarone-saloni
Copy link
Copy Markdown
Author

/gemini summary

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces new test cases for grid_shape.rs, polygon_shape.rs, and star_shape.rs to validate their drawing and parameter calculation logic, including aspects like ratio locking, centering, negative drags, and isometric modes. The review comments suggest improvements such as using glam's abs_diff_eq for more concise floating-point vector comparisons, reducing the tolerance for floating-point assertions in tests to improve precision, and correcting a misleading test name for polygon default sides.

Comment on lines +292 to +293
assert!((translation.x - 10.).abs() < 1e-10, "Expected translation.x=10, got {}", translation.x);
assert!((translation.y - 10.).abs() < 1e-10, "Expected translation.y=10, got {}", translation.y);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

For comparing floating-point vectors, it's cleaner to use abs_diff_eq from the glam crate. This allows you to assert the near-equality of the entire DVec2 in a single line, making the test more concise and readable.

Suggested change
assert!((translation.x - 10.).abs() < 1e-10, "Expected translation.x=10, got {}", translation.x);
assert!((translation.y - 10.).abs() < 1e-10, "Expected translation.y=10, got {}", translation.y);
assert!(translation.abs_diff_eq(DVec2::splat(10.), 1e-9), "Expected translation to be close to (10, 10), got {}", translation);

assert_eq!(editor.active_document().metadata().all_layers().count(), 1);
let (sides, radius) = get_polygon_inputs(&editor).expect("Polygon node should exist after draw");
assert!(sides >= 3, "Polygon should have at least 3 sides, got {sides}");
assert!((radius - 50.).abs() < 1., "Expected radius ≈ 50 for 100×100 drag, got {radius}");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The tolerance of 1.0 for this floating-point comparison seems quite large. While some tolerance is expected in integration tests due to floating-point arithmetic, a value this high might mask potential inaccuracies. Consider using a smaller tolerance, like 1e-9, to ensure the calculation is as precise as expected. This also applies to other similar assertions in this test module.

Suggested change
assert!((radius - 50.).abs() < 1., "Expected radius ≈ 50 for 100×100 drag, got {radius}");
assert!((radius - 50.).abs() < 1e-9, "Expected radius ≈ 50 for 100×100 drag, got {radius}");

}

#[tokio::test]
async fn polygon_default_six_sides() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The test name polygon_default_six_sides is misleading as the test asserts that the default number of sides is 5. To improve clarity and prevent confusion, the test name should be updated to reflect the actual assertion.

Suggested change
async fn polygon_default_six_sides() {
async fn polygon_default_five_sides() {

draw_star(&mut editor, 0., 0., 100., 100., ModifierKeys::empty()).await;

let (_, outer_radius, _) = get_star_inputs(&editor).expect("Star node should exist");
assert!((outer_radius - 50.).abs() < 1., "Expected outer radius ~50 for 100x100 drag, got {outer_radius}");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The tolerance of 1.0 for this floating-point comparison seems quite large. While some tolerance is expected in integration tests, a value this high might mask potential inaccuracies. Consider using a smaller tolerance, like 1e-9, to ensure the calculation is as precise as expected.

Suggested change
assert!((outer_radius - 50.).abs() < 1., "Expected outer radius ~50 for 100x100 drag, got {outer_radius}");
assert!((outer_radius - 50.).abs() < 1e-9, "Expected outer radius ~50 for 100x100 drag, got {outer_radius}");

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

This pull request significantly enhances the stability and reliability of the editor's shape tools by introducing extensive unit test coverage for grid, polygon, and star shapes. The new tests validate core drawing logic, interactions with modifier keys, radius calculations, and cancellation behaviors, ensuring that these fundamental functionalities work as expected and preventing future regressions.

Highlights

  • New Unit Tests for Grid Shape: Added comprehensive unit tests for the calculate_grid_params function in grid_shape.rs, covering various scenarios like basic rectangle, lock ratio, center draw, negative drag, and isometric grids.
  • New Unit Tests for Polygon Shape: Introduced unit tests for polygon_shape.rs to validate polygon drawing behavior, including simple drawing, handling non-square dimensions, shift-lock ratio, default side count, and right-mouse-button cancellation.
  • Enhanced Unit Tests for Star Shape: Expanded the test suite for star_shape.rs with new unit tests to ensure correct star drawing, accurate inner and outer radius calculations, and proper right-mouse-button cancellation.
  • Issue Resolution: Resolved issue Add tests for polygon shape, star shape, and grid snapping tools #3954, which aimed to improve test coverage for shape tools.
Changelog
  • editor/src/messages/tool/common_functionality/shapes/grid_shape.rs
    • Added unit tests for calculate_grid_params function, covering various grid drawing scenarios.
    • Included tests for basic rectangle, lock ratio, center draw, negative drag, and isometric grid calculations.
  • editor/src/messages/tool/common_functionality/shapes/polygon_shape.rs
    • Added unit tests for polygon drawing functionality.
    • Tests cover simple polygon draw, behavior with non-square dimensions, shift-key lock ratio, default number of sides, and right-mouse-button cancellation.
  • editor/src/messages/tool/common_functionality/shapes/star_shape.rs
    • Added unit tests for star drawing functionality.
    • Tests include simple star draw, verification of inner and outer radius calculations, and right-mouse-button cancellation.
Activity
  • gemini-code-assist[bot] provided an initial summary of the changes.
  • tarone-saloni requested a summary.
  • gemini-code-assist[bot] suggested using abs_diff_eq for DVec2 comparisons in grid_shape.rs for cleaner floating-point assertions.
  • gemini-code-assist[bot] recommended using a smaller tolerance (e.g., 1e-9) for floating-point comparisons in polygon_shape.rs to improve precision.
  • gemini-code-assist[bot] suggested renaming the test polygon_default_six_sides to polygon_default_five_sides in polygon_shape.rs to accurately reflect its assertion.
  • gemini-code-assist[bot] recommended using a smaller tolerance (e.g., 1e-9) for floating-point comparisons in star_shape.rs to improve precision.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 3 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Add one-off context when rerunning by tagging @cubic-dev-ai with guidance or docs links (including llms.txt)
  • Ask questions if you need clarification on any suggestion

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.

Add tests for polygon shape, star shape, and grid snapping tools

1 participant