200 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			200 lines
		
	
	
		
			5.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| namespace nulib\app\args;
 | |
| 
 | |
| use nulib\app\args\SimpleArgsParser;
 | |
| use nulib\tests\TestCase;
 | |
| 
 | |
| class SimpleArgsParserTest extends TestCase {
 | |
|   const NORMALIZE_ARGS = [
 | |
|     ["-a"],
 | |
|     ["--longb"],
 | |
|     ["-c", "--longc"],
 | |
|     ["-x", "--x1"],
 | |
|     ["-x", "--x2"],
 | |
| 
 | |
|     ["-n", "--none"],
 | |
|     ["-m:", "--mandatory"],
 | |
|     ["-o::", "--optional"],
 | |
|     ["--mo02:", "args" => [["value", "value"]]],
 | |
|     ["--mo12:", "args" => ["value", ["value"]]],
 | |
|     ["--mo22:", "args" => ["value", "value"]],
 | |
|   ];
 | |
|   const NORMALIZE_TESTS = [
 | |
|     [], ["--"],
 | |
|     ["--"], ["--"],
 | |
|     ["--", "--"], ["--", "--"],
 | |
|     ["-aa"], ["-a", "-a", "--"],
 | |
|     ["a", "b"], ["--", "a", "b"],
 | |
|     ["-a", "--ma", "x", "a", "--ma=y", "b"], ["-a", "--mandatory", "x", "--mandatory", "y", "--", "a", "b"],
 | |
|     ["-mx", "-m", "y"], ["-m", "x", "-m", "y", "--"],
 | |
|     ["-ox", "-o", "y"], ["-ox", "-o", "--", "y"],
 | |
|     ["-a", "--", "-a", "-c"], ["-a", "--", "-a", "-c"],
 | |
| 
 | |
|     # -a et -b doivent être considérés comme arguments, -n comme option
 | |
|     ["--mo02"],                   ["--mo02", "--", "--"],
 | |
|     ["--mo02", "-a"],             ["--mo02", "-a", "--", "--"],
 | |
|     ["--mo02", "--"],             ["--mo02", "--", "--"],
 | |
|     ["--mo02", "--", "-n"],       ["--mo02", "--", "-n", "--"],
 | |
|     ["--mo02", "--", "--", "-b"], ["--mo02", "--", "--", "-b"],
 | |
|     #
 | |
|     ["--mo02", "-a"],                   ["--mo02", "-a", "--", "--"],
 | |
|     ["--mo02", "-a", "-a"],             ["--mo02", "-a", "-a", "--"],
 | |
|     ["--mo02", "-a", "--"],             ["--mo02", "-a", "--", "--"],
 | |
|     ["--mo02", "-a", "--", "-n"],       ["--mo02", "-a", "--", "-n", "--"],
 | |
|     ["--mo02", "-a", "--", "--", "-b"], ["--mo02", "-a", "--", "--", "-b"],
 | |
| 
 | |
|     [
 | |
|       "--mo02", "--",
 | |
|       "--mo02", "x", "--",
 | |
|       "--mo02", "x", "y",
 | |
|       "--mo12", "x", "--",
 | |
|       "--mo12", "x", "y",
 | |
|       "--mo22", "x", "y",
 | |
|       "z",
 | |
|     ], [
 | |
|       "--mo02", "--",
 | |
|       "--mo02", "x", "--",
 | |
|       "--mo02", "x", "y",
 | |
|       "--mo12", "x", "--",
 | |
|       "--mo12", "x", "y",
 | |
|       "--mo22", "x", "y",
 | |
|       "--",
 | |
|       "z",
 | |
|     ],
 | |
|   ];
 | |
| 
 | |
|   function testNormalize() {
 | |
|     $parser = new SimpleArgsParser(self::NORMALIZE_ARGS);
 | |
|     $count = count(self::NORMALIZE_TESTS);
 | |
|     for ($i = 0; $i < $count; $i += 2) {
 | |
|       $args = self::NORMALIZE_TESTS[$i];
 | |
|       $expected = self::NORMALIZE_TESTS[$i + 1];
 | |
|       $normalized = $parser->normalize($args);
 | |
|       self::assertSame($expected, $normalized
 | |
|         , "for ".var_export($args, true)
 | |
|         .", normalized is ".var_export($normalized, true)
 | |
|       );
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   function testArgsNone() {
 | |
|     $parser = new SimpleArgsParser([
 | |
|       ["-z"],
 | |
|       ["-a"],
 | |
|       ["-b"],
 | |
|       ["-c",],
 | |
|       ["-d", "value" => 42],
 | |
|     ]);
 | |
| 
 | |
|     $dest = []; $parser->parse($dest, ["-a", "-bb", "-ccc", "-dddd"]);
 | |
|     self::assertSame(null, $dest["z"] ?? null);
 | |
|     self::assertSame(1, $dest["a"] ?? null);
 | |
|     self::assertSame(2, $dest["b"] ?? null);
 | |
|     self::assertSame(3, $dest["c"] ?? null);
 | |
|     self::assertSame(42, $dest["d"] ?? null);
 | |
| 
 | |
|     self::assertTrue(true);
 | |
|   }
 | |
| 
 | |
|   function testArgsMandatory() {
 | |
|     $parser = new SimpleArgsParser([
 | |
|       ["-z:"],
 | |
|       ["-a:"],
 | |
|       ["-b:"],
 | |
|       ["-c:", "value" => 42],
 | |
|     ]);
 | |
| 
 | |
|     $dest = []; $parser->parse($dest, [
 | |
|       "-a",
 | |
|       "-bb",
 | |
|       "-c",
 | |
|       "-c15",
 | |
|       "-c30",
 | |
|       "-c45",
 | |
|     ]);
 | |
|     self::assertSame(null, $dest["z"] ?? null);
 | |
|     self::assertSame("-bb", $dest["a"] ?? null);
 | |
|     self::assertSame(null, $dest["b"] ?? null);
 | |
|     self::assertSame("45", $dest["c"] ?? null);
 | |
| 
 | |
|     self::assertTrue(true);
 | |
|   }
 | |
| 
 | |
|   function testArgsOptional() {
 | |
|     $parser = new SimpleArgsParser([
 | |
|       ["-z::"],
 | |
|       ["-a::"],
 | |
|       ["-b::"],
 | |
|       ["-c::", "value" => 42],
 | |
|       ["-d::", "value" => 42],
 | |
|     ]);
 | |
| 
 | |
|     $dest = []; $parser->parse($dest, [
 | |
|       "-a",
 | |
|       "-bb",
 | |
|       "-c",
 | |
|       "-d15",
 | |
|       "-d30",
 | |
|     ]);
 | |
|     self::assertSame(null, $dest["z"] ?? null);
 | |
|     self::assertSame(null, $dest["a"] ?? null);
 | |
|     self::assertSame("b", $dest["b"] ?? null);
 | |
|     self::assertSame(42, $dest["c"] ?? null);
 | |
|     self::assertSame("30", $dest["d"] ?? null);
 | |
| 
 | |
|     self::assertTrue(true);
 | |
|   }
 | |
| 
 | |
|   function testRemains() {
 | |
|     $parser = new SimpleArgsParser([]);
 | |
|     $dest = []; $parser->parse($dest, ["x", "y"]);
 | |
|     self::assertSame(["x", "y"], $dest["args"] ?? null);
 | |
|   }
 | |
| 
 | |
|   function test() {
 | |
|     $parser = new SimpleArgsParser([
 | |
|       ["-n", "--none"],
 | |
|       ["-m:", "--mandatory"],
 | |
|       ["-o::", "--optional"],
 | |
|       ["--mo02:", "args" => [["value", "value"]]],
 | |
|       ["--mo12:", "args" => ["value", ["value"]]],
 | |
|       ["--mo22:", "args" => ["value", "value"]],
 | |
|     ]);
 | |
|     $parser->parse($dest, [
 | |
|       "--mo02", "--",
 | |
|       "--mo02", "x", "--",
 | |
|       "--mo02", "x", "y",
 | |
|       "--mo12", "x", "--",
 | |
|       "--mo12", "x", "y",
 | |
|       "--mo22", "x", "y",
 | |
|       "z",
 | |
|     ]);
 | |
| 
 | |
|     self::assertTrue(true);
 | |
|   }
 | |
| 
 | |
|   function testAutono() {
 | |
|     $parser = new SimpleArgsParser([
 | |
|       ["-a", "--plouf"],
 | |
|       ["-b", "--no-plouf"],
 | |
|     ]);
 | |
|     $dest = [];
 | |
|     $parser->parse($dest, ["-aabb"]);
 | |
|     self::assertSame(["plouf" => 0, "args" => []], $dest);
 | |
| 
 | |
|     $parser = new SimpleArgsParser([
 | |
|       ["-a", "--plouf", "value" => true],
 | |
|       ["-b", "--no-plouf", "value" => false],
 | |
|     ]);
 | |
|     $dest = ["plouf" => null];
 | |
|     $parser->parse($dest, []);
 | |
|     self::assertSame(["plouf" => null, "args" => []], $dest);
 | |
|     $dest = ["plouf" => null];
 | |
|     $parser->parse($dest, ["-a"]);
 | |
|     self::assertSame(["plouf" => true, "args" => []], $dest);
 | |
|     $dest = ["plouf" => null];
 | |
|     $parser->parse($dest, ["-b"]);
 | |
|     self::assertSame(["plouf" => false, "args" => []], $dest);
 | |
|   }
 | |
| }
 |