44
55namespace Maml \Tests ;
66
7+ use Maml \Ast \ObjectNode ;
78use Maml \Maml ;
89use PHPUnit \Framework \TestCase ;
910
@@ -20,4 +21,44 @@ public function testParseAndStringify(): void
2021 $ reparsed = Maml::parse ($ stringified );
2122 $ this ->assertSame ($ parsed , $ reparsed );
2223 }
24+
25+ public function testErrorSnippetPointsAtPosition (): void
26+ {
27+ $ source = "{ \n name: \"test \"\n timeout: -1 \n} " ;
28+ $ doc = Maml::parseAst ($ source );
29+ $ this ->assertInstanceOf (ObjectNode::class, $ doc ->value );
30+ $ node = $ doc ->value ->properties [1 ]->value ;
31+
32+ $ result = Maml::errorSnippet ($ source , $ node ->span ->start , 'Invalid value ' );
33+ $ this ->assertStringContainsString ('Invalid value on line 3. ' , $ result );
34+ $ this ->assertStringContainsString ('timeout: -1 ' , $ result );
35+ $ this ->assertStringContainsString ('^ ' , $ result );
36+ }
37+
38+ public function testErrorSnippetAtStartOfSource (): void
39+ {
40+ $ source = 'null ' ;
41+ $ doc = Maml::parseAst ($ source );
42+
43+ $ result = Maml::errorSnippet ($ source , $ doc ->value ->span ->start , 'Expected object ' );
44+ $ this ->assertStringContainsString ('Expected object on line 1. ' , $ result );
45+ $ this ->assertStringContainsString ('null ' , $ result );
46+ $ this ->assertStringContainsString ('^ ' , $ result );
47+ }
48+
49+ public function testErrorSnippetOnNestedNode (): void
50+ {
51+ $ source = "{ \n items: [ \n {name: \"x \", count: 0} \n ] \n} " ;
52+ $ doc = Maml::parseAst ($ source );
53+ $ this ->assertInstanceOf (ObjectNode::class, $ doc ->value );
54+ $ items = $ doc ->value ->properties [0 ]->value ;
55+ $ this ->assertInstanceOf (\Maml \Ast \ArrayNode::class, $ items );
56+ $ inner = $ items ->elements [0 ]->value ;
57+ $ this ->assertInstanceOf (ObjectNode::class, $ inner );
58+ $ countNode = $ inner ->properties [1 ]->value ;
59+
60+ $ result = Maml::errorSnippet ($ source , $ countNode ->span ->start , 'Count must be positive ' );
61+ $ this ->assertStringContainsString ('Count must be positive on line 3. ' , $ result );
62+ $ this ->assertStringContainsString ('^ ' , $ result );
63+ }
2364}
0 commit comments