From cdfc97d19b88dbefd80e7a72e60404e2d80cea50 Mon Sep 17 00:00:00 2001 From: Shish Date: Mon, 5 Nov 2018 20:53:58 +0000 Subject: [PATCH] begin tests in core --- core/util.inc.php | 2 ++ core/util.test.php | 43 +++++++++++++++++++++++++++++++++++++++++++ tests/phpunit.xml | 5 ++++- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 core/util.test.php diff --git a/core/util.inc.php b/core/util.inc.php index 650b472c..38de0489 100644 --- a/core/util.inc.php +++ b/core/util.inc.php @@ -221,6 +221,8 @@ function parse_shorthand_int(string $limit): int { * @return string */ function to_shorthand_int(int $int): string { + assert($int >= 0); + if($int >= pow(1024, 3)) { return sprintf("%.1fGB", $int / pow(1024, 3)); } diff --git a/core/util.test.php b/core/util.test.php new file mode 100644 index 00000000..3187f682 --- /dev/null +++ b/core/util.test.php @@ -0,0 +1,43 @@ +assertEquals( + html_escape("Foo & "), + "Foo & <waffles>" + ); + + $this->assertEquals( + html_unescape("Foo & <waffles>"), + "Foo & " + ); + + $x = "Foo & <waffles>"; + $this->assertEquals(html_escape(html_unescape($x)), $x); + } + + public function test_int_escape() { + $this->assertEquals(int_escape(""), 0); + $this->assertEquals(int_escape("1"), 1); + $this->assertEquals(int_escape("-1"), -1); + $this->assertEquals(int_escape("-1.5"), -1); + } + + public function test_clamp() { + $this->assertEquals(clamp(0, 5, 10), 5); + $this->assertEquals(clamp(5, 5, 10), 5); + $this->assertEquals(clamp(7, 5, 10), 7); + $this->assertEquals(clamp(10, 5, 10), 10); + $this->assertEquals(clamp(15, 5, 10), 10); + } + + public function test_shorthand_int() { + $this->assertEquals(to_shorthand_int(1231231231), "1.1GB"); + + $this->assertEquals(parse_shorthand_int("foo"), -1); + $this->assertEquals(parse_shorthand_int("32M"), 33554432); + $this->assertEquals(parse_shorthand_int("43.4KB"), 44441); + $this->assertEquals(parse_shorthand_int("1231231231"), 1231231231); + } +} diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 2edbd4df..e972774b 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,6 +1,9 @@ - + + ../core/ + + ../ext/