From 81ab2fdf77f9cefa29ff133cdbfb98e8d82b9cb2 Mon Sep 17 00:00:00 2001 From: netkas Date: Tue, 24 Sep 2024 00:43:06 -0400 Subject: [PATCH] Added PhpUnit tests --- .idea/php.xml | 5 +++ tests/OptsLib/ParseTest.php | 70 +++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 tests/OptsLib/ParseTest.php diff --git a/.idea/php.xml b/.idea/php.xml index fe09da7..b20d972 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -20,6 +20,11 @@ + + + + + diff --git a/tests/OptsLib/ParseTest.php b/tests/OptsLib/ParseTest.php new file mode 100644 index 0000000..367ee22 --- /dev/null +++ b/tests/OptsLib/ParseTest.php @@ -0,0 +1,70 @@ + 'value1', + 'f' => 'value2', + 'unmatched_string' => true, + ]; + + $this->assertEquals($expected_result, Parse::parseArgument($input)); + } + + /** + * Testing parseArgument method when input is an array type + */ + public function testParseArgumentWithArrayInput() + { + // Array argument will be transformed into '--flag1 value1 "-"f" value2" unmatched_string' + $input = ['--flag1', 'value1', '-', 'f', 'value2', 'unmatched_string']; + $expected_result = [ + 'flag1' => 'value1', + 'unmatched_string' => true, + 'f' => true, + 'value2' => true + ]; + + $this->assertEquals($expected_result, Parse::parseArgument($input)); + } + + /** + * Testing parseArgument method when input contains escaped quotes in a string argument + */ + public function testParseArgumentWithEscapedQuotes() + { + $input = "--flag1=\"value1 with some \\\"escaped quotes\\\"\" -f 'value2 with some \\'escaped quotes\\'' unmatched_string"; + $expected_result = [ + 'flag1' => 'value1 with some "escaped quotes"', + 'f' => 'value2 with some \'escaped quotes\'', + 'unmatched_string' => true, + ]; + + $this->assertEquals($expected_result, Parse::parseArgument($input)); + } + + /** + * Testing parseArgument method only takes maximum number of arguments specified + */ + public function testParseArgumentWithMaxArguments() + { + $input = '--flag1=value1 -f value2 unmatched_string1 unmatched_string2'; + $expected_result = [ + 'flag1' => 'value1', + 'f' => 'value2', + 'unmatched_string1' => true + ]; // It should only parse maximum 3 arguments + + $this->assertEquals($expected_result, Parse::parseArgument($input, 2)); + } +} \ No newline at end of file