-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests to all macro examples
- Loading branch information
Showing
21 changed files
with
1,453 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...es/Tests/MacroExamples/Implementation/ComplexMacros/DictionaryIndirectionMacroTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import MacroExamplesImplementation | ||
import SwiftSyntaxMacros | ||
import SwiftSyntaxMacrosTestSupport | ||
import XCTest | ||
|
||
final class DictionaryStorageMacroTests: XCTestCase { | ||
private let macros: [String: Macro.Type] = [ | ||
"DictionaryStorage": DictionaryStorageMacro.self, | ||
"DictionaryStorageProperty": DictionaryStoragePropertyMacro.self, | ||
] | ||
|
||
func testExpansionConvertsStoredProperties() { | ||
assertMacroExpansion( | ||
""" | ||
@DictionaryStorage | ||
struct Point { | ||
var x: Int = 1 | ||
var y: Int = 2 | ||
} | ||
""", | ||
expandedSource: """ | ||
struct Point { | ||
var x: Int = 1 { | ||
get { | ||
_storage["x", default: 1] as! Int | ||
} | ||
set { | ||
_storage["x"] = newValue | ||
} | ||
} | ||
var y: Int = 2 { | ||
get { | ||
_storage["y", default: 2] as! Int | ||
} | ||
set { | ||
_storage["y"] = newValue | ||
} | ||
} | ||
var _storage: [String: Any] = [:] | ||
} | ||
""", | ||
macros: macros | ||
) | ||
} | ||
|
||
func testExpansionWithoutInitializersEmitsError() { | ||
assertMacroExpansion( | ||
""" | ||
@DictionaryStorage | ||
class Point { | ||
let x: Int | ||
let y: Int | ||
} | ||
""", | ||
expandedSource: """ | ||
class Point { | ||
let x: Int | ||
let y: Int | ||
var _storage: [String: Any] = [:] | ||
} | ||
""", | ||
diagnostics: [ | ||
DiagnosticSpec(message: "stored property must have an initializer", line: 3, column: 3), | ||
DiagnosticSpec(message: "stored property must have an initializer", line: 4, column: 3), | ||
], | ||
macros: macros | ||
) | ||
} | ||
|
||
func testExpansionIgnoresComputedProperties() { | ||
assertMacroExpansion( | ||
""" | ||
@DictionaryStorage | ||
struct Test { | ||
var value: Int { | ||
get { return 0 } | ||
set {} | ||
} | ||
} | ||
""", | ||
expandedSource: """ | ||
struct Test { | ||
var value: Int { | ||
get { return 0 } | ||
set {} | ||
} | ||
var _storage: [String: Any] = [:] | ||
} | ||
""", | ||
macros: macros | ||
) | ||
} | ||
} |
140 changes: 140 additions & 0 deletions
140
Examples/Tests/MacroExamples/Implementation/ComplexMacros/ObservableMacroTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import MacroExamplesImplementation | ||
import SwiftSyntaxMacros | ||
import SwiftSyntaxMacrosTestSupport | ||
import XCTest | ||
|
||
final class ObservableMacroTests: XCTestCase { | ||
private let macros: [String: Macro.Type] = [ | ||
"Observable": ObservableMacro.self, | ||
"ObservableProperty": ObservablePropertyMacro.self, | ||
] | ||
|
||
func testExpansion() { | ||
assertMacroExpansion( | ||
""" | ||
@Observable | ||
final class Dog { | ||
var name: String? | ||
var treat: Treat? | ||
var isHappy: Bool = true | ||
init() {} | ||
func bark() { | ||
print("bork bork") | ||
} | ||
} | ||
""", | ||
expandedSource: #""" | ||
final class Dog { | ||
var name: String? { | ||
get { | ||
_registrar.beginAccess(\.name) | ||
defer { | ||
_registrar.endAccess() | ||
} | ||
return _storage.name | ||
} | ||
set { | ||
_registrar.beginAccess(\.name) | ||
_registrar.register(observable: self, willSet: \.name, to: newValue) | ||
defer { | ||
_registrar.register(observable: self, didSet: \.name) | ||
_registrar.endAccess() | ||
} | ||
_storage.name = newValue | ||
} | ||
} | ||
var treat: Treat? { | ||
get { | ||
_registrar.beginAccess(\.treat) | ||
defer { | ||
_registrar.endAccess() | ||
} | ||
return _storage.treat | ||
} | ||
set { | ||
_registrar.beginAccess(\.treat) | ||
_registrar.register(observable: self, willSet: \.treat, to: newValue) | ||
defer { | ||
_registrar.register(observable: self, didSet: \.treat) | ||
_registrar.endAccess() | ||
} | ||
_storage.treat = newValue | ||
} | ||
} | ||
var isHappy: Bool = true { | ||
get { | ||
_registrar.beginAccess(\.isHappy) | ||
defer { | ||
_registrar.endAccess() | ||
} | ||
return _storage.isHappy | ||
} | ||
set { | ||
_registrar.beginAccess(\.isHappy) | ||
_registrar.register(observable: self, willSet: \.isHappy, to: newValue) | ||
defer { | ||
_registrar.register(observable: self, didSet: \.isHappy) | ||
_registrar.endAccess() | ||
} | ||
_storage.isHappy = newValue | ||
} | ||
} | ||
init() {} | ||
func bark() { | ||
print("bork bork") | ||
} | ||
let _registrar = ObservationRegistrar<Dog >() | ||
public nonisolated func addObserver(_ observer: some Observer<Dog >) { | ||
_registrar.addObserver(observer) | ||
} | ||
public nonisolated func removeObserver(_ observer: some Observer<Dog >) { | ||
_registrar.removeObserver(observer) | ||
} | ||
private func withTransaction<T>(_ apply: () throws -> T) rethrows -> T { | ||
_registrar.beginAccess() | ||
defer { | ||
_registrar.endAccess() | ||
} | ||
return try apply() | ||
} | ||
private struct Storage { | ||
var name: String? | ||
var treat: Treat? | ||
var isHappy: Bool = true | ||
} | ||
private var _storage = Storage() | ||
} | ||
extension Dog: Observable { | ||
} | ||
"""#, | ||
macros: macros | ||
) | ||
} | ||
} |
Oops, something went wrong.