From 4d7e8125ae2f1e85aea56937768a99e9b0e02822 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 1 May 2022 16:11:24 +0000 Subject: [PATCH] Tests: Add unit tests for `wp_fuzzy_number_match()`. Props pbearne, costdev, mukesh27, hellofromTonya, SergeyBiryukov. Fixes #54239. git-svn-id: https://develop.svn.wordpress.org/trunk@53321 602fd350-edb4-49c9-b593-d223f7449a82 --- .../tests/functions/wpFuzzyNumberMatch.php | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 tests/phpunit/tests/functions/wpFuzzyNumberMatch.php diff --git a/tests/phpunit/tests/functions/wpFuzzyNumberMatch.php b/tests/phpunit/tests/functions/wpFuzzyNumberMatch.php new file mode 100644 index 0000000000..85034165ce --- /dev/null +++ b/tests/phpunit/tests/functions/wpFuzzyNumberMatch.php @@ -0,0 +1,112 @@ +assertSame( $result, wp_fuzzy_number_match( $expected, $actual, $precision ) ); + } + + /** + * Data provider. + * + * @return array[] Test parameters { + * @type int|float $expected The expected value. + * @type int|float $actual The actual number. + * @type int|float $precision The allowed variation. + * @type bool $result Whether the numbers match within the specified precision. + * } + */ + public function data_wp_fuzzy_number_match() { + return array( + 'expected 1 int, actual 1 int' => array( + 'expected' => 1, + 'actual' => 1, + 'precision' => 1, + 'result' => true, + ), + 'expected 1 int, actual 2 int' => array( + 'expected' => 1, + 'actual' => 2, + 'precision' => 1, + 'result' => true, + ), + 'expected 1 int, actual 3 int' => array( + 'expected' => 1, + 'actual' => 3, + 'precision' => 1, + 'result' => false, + ), + 'expected 1 int, actual 1 string' => array( + 'expected' => 1, + 'actual' => '1', + 'precision' => 1, + 'result' => true, + ), + 'expected 1 int, actual 11 int, precision 10' => array( + 'expected' => 1, + 'actual' => 11, + 'precision' => 10, + 'result' => true, + ), + 'expected 1 int, actual 12 int, precision 10' => array( + 'expected' => 1, + 'actual' => 12, + 'precision' => 10, + 'result' => false, + ), + 'expected 1.234 float, actual 1 int' => array( + 'expected' => 1.234, + 'actual' => 1, + 'precision' => 1, + 'result' => true, + ), + 'expected 2.234 float, actual 2 int' => array( + 'expected' => 1.234, + 'actual' => 2, + 'precision' => 1, + 'result' => true, + ), + 'expected 1 int, actual 2.0001 float' => array( + 'expected' => 1, + 'actual' => 2.0001, + 'precision' => 1, + 'result' => false, + ), + 'expected 1 int, actual 3.23 float' => array( + 'expected' => 1, + 'actual' => 3.234, + 'precision' => 1, + 'result' => false, + ), + 'expected 1.2e1 float (12), actual 1.3e1 float (13)' => array( + 'expected' => 1.2e1, + 'actual' => 1.3e1, + 'precision' => 1, + 'result' => true, + ), + 'expected 1.2e3 float (1200), actual 1.2e3 float, precision 1000' => array( + 'expected' => 1.2e3, + 'actual' => 1.2e3, + 'precision' => 1000, + 'result' => true, + ), + ); + } + +}