Skip to content

Commit 60dc900

Browse files
committed
add Autograph tests
1 parent b55c31c commit 60dc900

File tree

5 files changed

+145
-5
lines changed

5 files changed

+145
-5
lines changed

Sources/MarkdownPluginSwift/Signatures/Signature.Expanded (ext).swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ extension Signature.Expanded
55
{
66
@inlinable public
77
init(_ fragments:__shared some Collection<Signature<Scalar>.Fragment>,
8-
sugarArray:Scalar,
98
sugarDictionary:Scalar,
9+
sugarArray:Scalar,
1010
sugarOptional:Scalar,
1111
landmarks:inout SignatureLandmarks)
1212
{

Sources/MarkdownPluginSwift/Signatures/SignatureSyntax.Autographer.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ extension SignatureSyntax.Autographer
7777
else if
7878
let type:TupleTypeSyntax = type.as(TupleTypeSyntax.self)
7979
{
80+
if let only:TupleTypeElementSyntax = type.elements.first, type.elements.count == 1
81+
{
82+
self.encode(type: only.type)
83+
return
84+
}
85+
8086
self.autograph.append("(")
8187

8288
var first:Bool = true
@@ -162,5 +168,27 @@ extension SignatureSyntax.Autographer
162168
}
163169

164170
self.autograph.append(name.text)
171+
172+
if let generics:GenericArgumentListSyntax
173+
{
174+
self.autograph.append("<")
175+
176+
var first:Bool = true
177+
for generic:GenericArgumentSyntax in generics
178+
{
179+
if first
180+
{
181+
first = false
182+
}
183+
else
184+
{
185+
self.autograph.append(",")
186+
}
187+
188+
self.encode(type: generic.argument)
189+
}
190+
191+
self.autograph.append(">")
192+
}
165193
}
166194
}

Sources/MarkdownPluginSwift/Signatures/SignatureSyntax.SugarMap.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ extension SignatureSyntax
33
@frozen @usableFromInline
44
struct SugarMap
55
{
6-
@usableFromInline
7-
var arrays:Set<Int>
86
@usableFromInline
97
var dictionaries:Set<Int>
108
@usableFromInline
9+
var arrays:Set<Int>
10+
@usableFromInline
1111
var optionals:Set<Int>
1212

1313
@inlinable
1414
init()
1515
{
16-
self.arrays = []
1716
self.dictionaries = []
17+
self.arrays = []
1818
self.optionals = []
1919
}
2020
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import Signatures
2+
import Symbols
3+
import Testing
4+
5+
@_spi(testable)
6+
import MarkdownPluginSwift
7+
8+
@Suite
9+
struct Autographs
10+
{
11+
private
12+
var landmarks:SignatureLandmarks
13+
14+
init()
15+
{
16+
self.landmarks = .init()
17+
}
18+
19+
@Test mutating
20+
func Func1()
21+
{
22+
let decl:String = "func f(a: Int, b: [Float]) -> String?"
23+
let _:Signature<Never>.Expanded = .init(decl, landmarks: &self.landmarks)
24+
25+
#expect(self.landmarks.inputs == ["Int", "[Float]"])
26+
#expect(self.landmarks.output == ["String?"])
27+
}
28+
29+
@Test mutating
30+
func Func2()
31+
{
32+
let decl:String = "func f(a: Int, b: [Float]) -> (String?, Set<Int>)"
33+
let _:Signature<Never>.Expanded = .init(decl, landmarks: &self.landmarks)
34+
35+
#expect(self.landmarks.inputs == ["Int", "[Float]"])
36+
#expect(self.landmarks.output == ["String?", "Set<Int>"])
37+
}
38+
39+
@Test mutating
40+
func Func3()
41+
{
42+
let decl:String = """
43+
func f(
44+
a: @Sendable @escaping (Int, Int) -> Int,
45+
b: inout ([Float]),
46+
c: (x: Double, y: Double)) -> (String?, (Set<Int>) async throws -> Int)
47+
"""
48+
let _:Signature<Never>.Expanded = .init(decl, landmarks: &self.landmarks)
49+
50+
#expect(self.landmarks.inputs == ["(Int,Int)->Int", "[Float]", "(Double,Double)"])
51+
#expect(self.landmarks.output == ["String?", "(Set<Int>)->Int"])
52+
}
53+
54+
@Test mutating
55+
func Subscript1()
56+
{
57+
let decl:String = "subscript(a: some Equatable, b: [Float: Bool]!) -> (String)?"
58+
let _:Signature<Never>.Expanded = .init(decl, landmarks: &self.landmarks)
59+
60+
#expect(self.landmarks.inputs == ["_", "[Float:Bool]!"])
61+
#expect(self.landmarks.output == ["String?"])
62+
}
63+
64+
@Test mutating
65+
func Subscript2()
66+
{
67+
let decl:String = "subscript<T>(a: T, b: [T]) -> (T?, Set<T>) where T: Hashable"
68+
let _:Signature<Never>.Expanded = .init(decl, landmarks: &self.landmarks)
69+
70+
#expect(self.landmarks.inputs == ["T", "[T]"])
71+
#expect(self.landmarks.output == ["T?", "Set<T>"])
72+
}
73+
74+
@Test mutating
75+
func Subscript3()
76+
{
77+
let decl:String = """
78+
subscript(
79+
a: @escaping ((Int, Int) -> Int),
80+
b: [Unicode.Scalar].Type) -> (Int) -> (Int) -> Int
81+
"""
82+
let _:Signature<Never>.Expanded = .init(decl, landmarks: &self.landmarks)
83+
84+
#expect(self.landmarks.inputs == ["(Int,Int)->Int", "[Unicode.Scalar].Type"])
85+
#expect(self.landmarks.output == ["(Int)->(Int)->Int"])
86+
}
87+
88+
@Test mutating
89+
func Resugaring()
90+
{
91+
let dictionary:Signature<Symbol.Decl>.Fragment = .init("Dictionary", referent: .sSD)
92+
let array:Signature<Symbol.Decl>.Fragment = .init("Array", referent: .sSa)
93+
let optional:Signature<Symbol.Decl>.Fragment = .init("Optional", referent: .sSq)
94+
95+
let _:Signature<Symbol.Decl>.Expanded = .init([
96+
"func f<T, U, V>(\n a: (",
97+
array,
98+
"<T>),\n b: ",
99+
dictionary,
100+
"<U, V>.Type) -> ",
101+
optional,
102+
"<V>",
103+
],
104+
sugarDictionary: .sSD,
105+
sugarArray: .sSa,
106+
sugarOptional: .sSq,
107+
landmarks: &self.landmarks)
108+
109+
#expect(self.landmarks.inputs == ["[T]", "[U:V].Type"])
110+
#expect(self.landmarks.output == ["V?"])
111+
}
112+
}

Sources/SymbolGraphParts/Vertices/SymbolGraphPart.Vertex.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ extension SymbolGraphPart.Vertex
8888
{
8989
abridged = .init(fragments)
9090
expanded = .init(fragments,
91-
sugarArray: .sSa,
9291
sugarDictionary: .sSD,
92+
sugarArray: .sSa,
9393
sugarOptional: .sSq,
9494
landmarks: &landmarks)
9595
}

0 commit comments

Comments
 (0)