assertPerfectSquare method

void assertPerfectSquare({
  1. String? message,
})

Asserts that this number is a perfect square.

A perfect square is an integer that is the square of another integer.

Throws a test failure if this number is not a perfect square.

Example:

1.assertPerfectSquare();   // Passes (1 = 1²)
4.assertPerfectSquare();   // Passes (4 = 2²)
9.assertPerfectSquare();   // Passes (9 = 3²)
16.assertPerfectSquare();  // Passes (16 = 4²)
25.assertPerfectSquare();  // Passes (25 = 5²)
2.assertPerfectSquare();   // Fails
3.assertPerfectSquare();   // Fails
10.assertPerfectSquare();  // Fails
  • message: Optional custom message for the failure reason.

Implementation

void assertPerfectSquare({String? message}) {
  expect(sqrt(this) % 1 == 0, isTrue,
      reason: message ?? 'Expected $this to be a perfect square');
}