|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ahc\Json\Test; |
| 4 | + |
| 5 | +use Ahc\Json\Comment; |
| 6 | + |
| 7 | +/** @coversDefaultClass \Ahc\Json\Comment */ |
| 8 | +class CommentTest extends \PHPUnit_Framework_TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @dataProvider theTests |
| 12 | + * @covers \Ahc\Json\Comment::strip |
| 13 | + */ |
| 14 | + public function testStrip($json, $expect) |
| 15 | + { |
| 16 | + $this->assertSame($expect, (new Comment)->strip($json)); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * @dataProvider theTests |
| 21 | + * @covers \Ahc\Json\Comment::decode |
| 22 | + */ |
| 23 | + public function testDecode($json) |
| 24 | + { |
| 25 | + $actual = (new Comment)->decode($json, true); |
| 26 | + |
| 27 | + $this->assertNotEmpty($actual); |
| 28 | + $this->assertArrayHasKey('a', $actual); |
| 29 | + $this->assertArrayHasKey('b', $actual); |
| 30 | + |
| 31 | + $this->assertSame(JSON_ERROR_NONE, json_last_error()); |
| 32 | + } |
| 33 | + |
| 34 | + public function theTests() |
| 35 | + { |
| 36 | + return [ |
| 37 | + 'without comment' => [ |
| 38 | + 'json' => '{"a":1,"b":2}', |
| 39 | + 'expect' => '{"a":1,"b":2}', |
| 40 | + ], |
| 41 | + 'single line comment' => [ |
| 42 | + 'json' => '{"a":1, |
| 43 | + // comment |
| 44 | + "b":2, |
| 45 | + // comment |
| 46 | + "c":3}', |
| 47 | + 'expect' => '{"a":1, |
| 48 | + "b":2, |
| 49 | + "c":3}', |
| 50 | + ], |
| 51 | + 'single line comment at end' => [ |
| 52 | + 'json' => '{"a":1, |
| 53 | + "b":2,// comment |
| 54 | + "c":3}', |
| 55 | + 'expect' => '{"a":1, |
| 56 | + "b":2, |
| 57 | + "c":3}', |
| 58 | + ], |
| 59 | + 'real multiline comment' => [ |
| 60 | + 'json' => '{"a":1, |
| 61 | + /* |
| 62 | + * comment |
| 63 | + */ |
| 64 | + "b":2, "c":3}', |
| 65 | + 'expect' => '{"a":1, |
| 66 | + ' . ' |
| 67 | + "b":2, "c":3}', |
| 68 | + ], |
| 69 | + 'inline multiline comment' => [ |
| 70 | + 'json' => '{"a":1, |
| 71 | + /* comment */ "b":2, "c":3}', |
| 72 | + 'expect' => '{"a":1, |
| 73 | + "b":2, "c":3}', |
| 74 | + ], |
| 75 | + 'inline multiline comment at end' => [ |
| 76 | + 'json' => '{"a":1, "b":2, "c":3/* comment */}', |
| 77 | + 'expect' => '{"a":1, "b":2, "c":3}', |
| 78 | + ], |
| 79 | + 'comment inside string' => [ |
| 80 | + 'json' => '{"a": "a//b", "b":"a/* not really comment */b"}', |
| 81 | + 'expect' => '{"a": "a//b", "b":"a/* not really comment */b"}', |
| 82 | + ], |
| 83 | + ]; |
| 84 | + } |
| 85 | +} |
0 commit comments