-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathDefaultTests.swift
More file actions
155 lines (147 loc) · 5.04 KB
/
DefaultTests.swift
File metadata and controls
155 lines (147 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import Foundation
import MetaCodable
import Testing
@testable import PluginCore
@Suite("Default Tests")
struct DefaultTests {
@Test("Reports error for @Default misuse", .tags(.default, .errorHandling, .macroExpansion, .structs))
func misuseOnNonVariableDeclaration() throws {
assertMacroExpansion(
"""
struct SomeCodable {
@Default("some")
func someFunc() {
}
}
""",
expandedSource:
"""
struct SomeCodable {
func someFunc() {
}
}
""",
diagnostics: [
.init(
id: Default.misuseID,
message:
"@Default only applicable to variable declarations",
line: 2, column: 5,
fixIts: [
.init(message: "Remove @Default attribute")
]
)
]
)
}
@Test("Reports error for @Default misuse (DefaultTests #1)", .tags(.default, .errorHandling, .macroExpansion, .structs))
func misuseOnStaticVariable() throws {
assertMacroExpansion(
"""
struct SomeCodable {
@Default("some")
static let value: String
}
""",
expandedSource:
"""
struct SomeCodable {
static let value: String
}
""",
diagnostics: [
.init(
id: Default.misuseID,
message:
"@Default can't be used with static variables declarations",
line: 2, column: 5,
fixIts: [
.init(message: "Remove @Default attribute")
]
)
]
)
}
@Test("Reports error when @Default is applied multiple times", .tags(.default, .errorHandling, .macroExpansion, .structs))
func duplicatedMisuse() throws {
assertMacroExpansion(
"""
struct SomeCodable {
@Default("some")
@Default("other")
let one: String
}
""",
expandedSource:
"""
struct SomeCodable {
let one: String
}
""",
diagnostics: [
.init(
id: Default.misuseID,
message:
"@Default can only be applied once per declaration",
line: 2, column: 5,
fixIts: [
.init(message: "Remove @Default attribute")
]
),
.init(
id: Default.misuseID,
message:
"@Default can only be applied once per declaration",
line: 3, column: 5,
fixIts: [
.init(message: "Remove @Default attribute")
]
),
]
)
}
@Suite("Default - Default Value Behavior")
struct DefaultValueBehavior {
@Codable
struct SomeCodable {
@Default("default_value")
let value: String
@Default(42)
let number: Int
}
@Test("Decodes from JSON successfully (DefaultTests #2)", .tags(.decoding, .default))
func defaultValueUsage() throws {
// Test with missing keys in JSON
let jsonStr = "{}"
let jsonData = try #require(jsonStr.data(using: .utf8))
let decoded = try JSONDecoder().decode(
SomeCodable.self, from: jsonData)
#expect(decoded.value == "default_value")
#expect(decoded.number == 42)
}
@Test("Decodes from JSON successfully (DefaultTests #3)", .tags(.decoding, .default))
func overrideDefaultValues() throws {
// Test with provided values in JSON
let jsonStr = """
{
"value": "custom_value",
"number": 100
}
"""
let jsonData = try #require(jsonStr.data(using: .utf8))
let decoded = try JSONDecoder().decode(
SomeCodable.self, from: jsonData)
#expect(decoded.value == "custom_value")
#expect(decoded.number == 100)
}
@Test("Encodes and decodes successfully (DefaultTests #1)", .tags(.decoding, .default, .encoding))
func encodingWithDefaults() throws {
let original = SomeCodable(value: "test", number: 99)
let encoded = try JSONEncoder().encode(original)
let decoded = try JSONDecoder().decode(
SomeCodable.self, from: encoded)
#expect(decoded.value == "test")
#expect(decoded.number == 99)
}
}
}