Files

115 lines
5.6 KiB
Swift

import HelderCore
import Testing
@Suite struct PHPSymbolsTests {
@Test("every declaration keyword is read", arguments: [
("class Agent", "class", "Agent"),
("interface Searchable {", "interface", "Searchable"),
("trait HasVault", "trait", "HasVault"),
("enum Status: string", "enum", "Status"),
])
func declarationKinds(line: String, kind: String, name: String) {
let declaration = PHPSymbols.parseDeclaration(line)
#expect(declaration?.kind == kind)
#expect(declaration?.name == name)
}
@Test("parents and interfaces come off the declaration line")
func parents() {
let declaration = PHPSymbols.parseDeclaration("final class VaultSearch extends Base implements Searchable, Arrayable, Jsonable {")
#expect(declaration?.parents == ["Base"])
#expect(declaration?.interfaces == ["Searchable", "Arrayable", "Jsonable"])
}
@Test("a fully qualified parent is reduced to its short name")
func qualifiedParents() {
let declaration = PHPSymbols.parseDeclaration("class A extends \\App\\Base implements \\App\\Contracts\\Runs")
#expect(declaration?.parents == ["Base"])
#expect(declaration?.interfaces == ["Runs"])
}
@Test("an interface may extend several interfaces, an enum may implement one")
func interfaceAndEnum() {
#expect(PHPSymbols.parseDeclaration("interface A extends B, C")?.parents == ["B", "C"])
#expect(PHPSymbols.parseDeclaration("enum Status: string implements HasLabel")?.interfaces == ["HasLabel"])
}
@Test("without a clause the lists are empty")
func noClause() {
#expect(PHPSymbols.parseDeclaration("class Plain {") == PHPDeclaration(kind: "class", name: "Plain", parents: [], interfaces: []))
}
@Test("the modifiers PHP allows may precede the keyword", arguments: [
"final class A extends B", "abstract class A", "final readonly class A", " class A", "\tabstract final class A",
])
func modifiers(line: String) {
#expect(PHPSymbols.parseDeclaration(line)?.name == "A")
}
@Test("a mention that is not a declaration is ignored", arguments: [
"$x = Agent::class;", "// class Agent lives here", "use App\\Models\\Agent;", "return $a instanceof Agent;", "classy Thing",
])
func notDeclarations(line: String) {
#expect(PHPSymbols.parseDeclaration(line) == nil)
}
@Test("an implements list wrapped onto the next line yields fewer interfaces, never wrong ones")
func wrappedImplements() {
#expect(PHPSymbols.parseDeclaration("class A extends B implements")?.interfaces == [])
#expect(PHPSymbols.parseDeclaration("class A extends B implements")?.parents == ["B"])
}
@Test("the namespace of a file")
func namespaces() {
#expect(PHPSymbols.parseNamespace("namespace App\\Console\\Commands;") == "App\\Console\\Commands")
#expect(PHPSymbols.parseNamespace("namespace App;") == "App")
#expect(PHPSymbols.parseNamespace(" namespace \\App\\X ;") == "App\\X")
#expect(PHPSymbols.parseNamespace("use App\\Models\\Agent;") == nil)
#expect(PHPSymbols.parseNamespace("class A {") == nil)
#expect(PHPSymbols.parseNamespace("namespace App {") == nil)
}
@Test("an indented use mixes in one or more traits")
func traitUse() {
#expect(PHPSymbols.parseTraitUse(" use HasFactory;") == ["HasFactory"])
#expect(PHPSymbols.parseTraitUse(" use HasFactory, Notifiable, SoftDeletes;") == ["HasFactory", "Notifiable", "SoftDeletes"])
#expect(PHPSymbols.parseTraitUse("\tuse App\\Support\\HasVault;") == ["HasVault"])
#expect(PHPSymbols.parseTraitUse(" use A, B { A::run insteadof B; }") == ["A", "B"])
}
@Test("what is not a trait use gives nothing", arguments: [
"use App\\Models\\Agent;", " use function App\\tap;", " use const App\\X;", " $f = function () use ($x) {};",
])
func notTraitUse(line: String) {
#expect(PHPSymbols.parseTraitUse(line) == [])
}
@Test("a namespace import is noise for reference search, a trait use is not")
func importLines() {
#expect(PHPSymbols.isImportLine("use App\\Models\\Agent;"))
#expect(PHPSymbols.isImportLine("use App\\Support\\Str as S;"))
#expect(PHPSymbols.isImportLine("use function App\\Support\\tap;"))
#expect(!PHPSymbols.isImportLine(" use HasFactory;"))
#expect(!PHPSymbols.isImportLine("\tuse HasFactory;"))
#expect(!PHPSymbols.isImportLine("$f = function () use ($agent) {};"))
#expect(!PHPSymbols.isImportLine("// use Agent for this"))
}
@Test("the word under the caret", arguments: [
("return new Agent($x);", 12, "Agent"), ("return new Agent($x);", 11, "Agent"), ("return new Agent($x);", 16, "Agent"),
("return new Agent($x);", 17, ""), (" ", 1, ""), ("$x = 42;", 6, ""), ("Agent", 0, "Agent"), ("Agent", 5, "Agent"),
("Agent", 9, ""), ("Agent", -1, ""),
])
func wordAt(line: String, offset: Int, word: String) {
#expect(PHPSymbols.word(in: line, at: offset) == word)
}
@Test("project declarations sort before vendor ones, then by path, capped")
func sortDefinitions() {
let defs = ["vendor/z.php", "b.php", "a.php"].map { SymbolDef(name: "A", path: $0, line: 1, kind: "class") }
#expect(PHPSymbols.sortDefinitions(defs).map(\.path) == ["a.php", "b.php", "vendor/z.php"])
let many = (0..<40).map { SymbolDef(name: "A", path: "f\($0).php", line: 1, kind: "class") }
#expect(PHPSymbols.sortDefinitions(many).count == SymbolLimits.maxDefinitions)
}
}