Skip to content

Commit d75c6ac

Browse files
committed
Refine errorSnippet pointer alignment logic and improve test assertions
1 parent ebf8db5 commit d75c6ac

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

src/Maml.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static function errorSnippet(string $source, Position $pos, string $messa
8181
$postfix = $postParts[0];
8282

8383
$snippet = " {$lastLine}{$postfix}\n";
84-
$pointer = ' ' . \str_repeat('.', \max(0, \strlen($lastLine) - 1)) . "^\n";
84+
$pointer = ' ' . \str_repeat('.', \strlen($lastLine)) . "^\n";
8585
return "{$message} on line {$pos->line}.\n\n{$snippet}{$pointer}";
8686
}
8787
}

tests/MamlTest.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ public function testErrorSnippetPointsAtPosition(): void
3030
$node = $doc->value->properties[1]->value;
3131

3232
$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);
33+
$this->assertSame(
34+
"Invalid value on line 3.\n\n timeout: -1\n ...........^\n",
35+
$result,
36+
);
3637
}
3738

3839
public function testErrorSnippetAtStartOfSource(): void
@@ -41,9 +42,10 @@ public function testErrorSnippetAtStartOfSource(): void
4142
$doc = Maml::parseAst($source);
4243

4344
$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);
45+
$this->assertSame(
46+
"Expected object on line 1.\n\n null\n ^\n",
47+
$result,
48+
);
4749
}
4850

4951
public function testErrorSnippetOnNestedNode(): void
@@ -58,7 +60,9 @@ public function testErrorSnippetOnNestedNode(): void
5860
$countNode = $inner->properties[1]->value;
5961

6062
$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+
$this->assertSame(
64+
"Count must be positive on line 3.\n\n {name: \"x\", count: 0}\n .......................^\n",
65+
$result,
66+
);
6367
}
6468
}

0 commit comments

Comments
 (0)