Files
helder/Tests/HelderCoreTests/Highlight/PHPGrammarTests.swift
T

148 lines
5.8 KiB
Swift

import HelderCore
import Testing
@Suite struct PHPGrammarTests {
private let source = """
<?php
namespace App\\Domain;
use Illuminate\\Support\\Str;
#[Attribute]
final class Agent extends Model implements HasLabel
{
use HasFactory;
public const STATUS_ACTIVE = 'active';
public function label(?string $prefix = null): string
{
// line comment
return Str::upper("{$this->name} $prefix") . self::STATUS_ACTIVE . 0x1F;
}
}
"""
@Test("the open tag and keywords read as keywords")
func keywords() {
#expect(role("<?php", in: source, .php) == .keyword)
#expect(role("final", in: source, .php) == .keyword)
#expect(role("function", in: source, .php) == .keyword)
#expect(role("string", in: source, .php, occurrence: 0) == .keyword)
}
@Test("a namespace path reads as a type path")
func namespacePath() {
#expect(role("App\\Domain", in: source, .php) == .string)
#expect(role("Illuminate\\Support\\", in: source, .php) == .string)
}
@Test("class names read as variables, known ones as symbols", arguments: ["Agent", "Model", "HasLabel", "HasFactory", "Attribute", "Str"])
func classNames(name: String) {
#expect(role(name, in: source, .php) == .variable)
#expect(role(name, in: source, .php, symbols: [name]) == .symbol)
}
@Test("the last segment of an imported path is a class name")
func importedClass() {
#expect(role("Str", in: source, .php, symbols: ["Str"]) == .symbol)
}
@Test("variables, properties and constants get their own roles")
func variablesAndConstants() {
#expect(role("$prefix", in: source, .php) == .variable)
#expect(role("STATUS_ACTIVE", in: source, .php) == .number)
#expect(role("STATUS_ACTIVE", in: source, .php, occurrence: 1) == .number)
#expect(role("null", in: source, .php) == .number)
#expect(role("0x1F", in: source, .php) == .number)
}
@Test("a method call after -> is a function name, a property is a variable")
func arrowAccess() {
let text = "<?php $a->save(); $a->name;"
#expect(role("save", in: text, .php) == .plain)
#expect(role("name", in: text, .php) == .variable)
#expect(role("->", in: text, .php) == .keyword)
}
@Test("strings interpolate variables, single quotes do not")
func interpolation() {
let text = "<?php $a = \"hi $name!\"; $b = 'hi $name';"
#expect(role("\"hi ", in: text, .php) == .string)
#expect(role("$name", in: text, .php, occurrence: 0) == .variable)
#expect(role("'hi $name'", in: text, .php) == .string)
#expect(role("{$this->name}", in: source, .php) == .variable)
}
@Test("comments: //, # and block comments")
func comments() {
#expect(role("// line comment", in: source, .php) == .comment)
#expect(role("# hash", in: "<?php # hash\n$a;", .php) == .comment)
let block = "<?php /* one\n two */ $x;"
#expect(role("two */", in: block, .php) == .comment)
#expect(role("$x", in: block, .php) == .variable)
}
@Test("a multi-line string carries over lines")
func multiLineString() {
let text = "<?php $sql = \"select *\n from users\n where id = 1\";\n$next = 2;"
#expect(role(" from users", in: text, .php) == .string)
#expect(role("$next", in: text, .php) == .variable)
}
@Test("a heredoc runs to its closing marker, a nowdoc does not interpolate")
func heredoc() {
let text = "<?php\n$a = <<<EOT\n Hello $name\n EOT;\n$b = <<<'RAW'\n $raw\nRAW;\n$c = 1;"
#expect(role(" Hello ", in: text, .php) == .string)
#expect(role("$name", in: text, .php) == .variable)
#expect(role("$raw", in: text, .php) == .string)
#expect(role("$c", in: text, .php) == .variable)
#expect(role("1", in: text, .php, occurrence: 0) == .number)
}
@Test("a file with <?php is HTML with PHP islands")
func template() {
let text = "<div class=\"<?= $cls ?>\">\n<?php if ($x): ?>\n <b>hi</b>\n<?php endif; ?>\n</div>"
#expect(role("div", in: text, .php) == .number)
#expect(role("$cls", in: text, .php) == .variable)
#expect(role("?>", in: text, .php) == .keyword)
#expect(role("if", in: text, .php) == .keyword)
#expect(role("hi", in: text, .php) == .plain)
#expect(role("endif", in: text, .php) == .keyword)
}
@Test("a PHP line comment ends at ?>")
func lineCommentEndsAtCloseTag() {
let text = "<p><?php // note ?> after</p>"
#expect(role("// note ", in: text, .php) == .comment)
#expect(role("?>", in: text, .php) == .keyword)
#expect(role("after", in: text, .php) == .plain)
}
@Test("a Blade file without <?php is read as PHP")
func bladeReadsAsPHP() {
let text = "@if($user)\n {{ $user->name }}\n@endif"
#expect(role("$user", in: text, .php) == .variable)
}
@Test("static, self and parent stay keywords before ::")
func scopeKeywords() {
let text = "<?php static::make(); self::X; parent::boot(); Carbon::now();"
#expect(role("static", in: text, .php) == .keyword)
#expect(role("self", in: text, .php) == .keyword)
#expect(role("parent", in: text, .php) == .keyword)
#expect(role("Carbon", in: text, .php, symbols: ["Carbon"]) == .symbol)
}
@Test("a qualified name marks only its last segment as the class")
func qualifiedName() {
let text = "<?php new \\App\\Models\\Agent();"
#expect(role("\\App\\Models\\", in: text, .php) == .string)
#expect(role("Agent", in: text, .php, symbols: ["Agent"]) == .symbol)
}
@Test("PHP keywords are case-insensitive")
func caseInsensitiveKeywords() {
#expect(role("NEW", in: "<?php $a = NEW Foo;", .php) == .keyword)
}
}