Skip to content

Commit f08fec7

Browse files
[NFC] BridgeJS: Move out default argument tests into a separate suite (swiftwasm#679)
BridgeJS: Move out default argument tests into a separate suite
1 parent 9660392 commit f08fec7

File tree

6 files changed

+3782
-3382
lines changed

6 files changed

+3782
-3382
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import XCTest
2+
import JavaScriptKit
3+
4+
@JSClass struct DefaultArgumentImports {
5+
@JSFunction static func runJsDefaultArgumentTests() throws(JSException)
6+
}
7+
8+
final class DefaultArgumentTests: XCTestCase {
9+
func testRunJsDefaultArgumentTests() throws {
10+
try DefaultArgumentImports.runJsDefaultArgumentTests()
11+
}
12+
}
13+
14+
// MARK: - Default Parameters
15+
16+
@JS enum DefaultArgumentExports {
17+
@JS static func testStringDefault(message: String = "Hello World") -> String {
18+
return message
19+
}
20+
21+
@JS static func testIntDefault(count: Int = 42) -> Int {
22+
return count
23+
}
24+
25+
@JS static func testBoolDefault(flag: Bool = true) -> Bool {
26+
return flag
27+
}
28+
29+
@JS static func testOptionalDefault(name: String? = nil) -> String? {
30+
return name
31+
}
32+
33+
@JS static func testMultipleDefaults(
34+
title: String = "Default Title",
35+
count: Int = -10,
36+
enabled: Bool = false
37+
) -> String {
38+
return "\(title): \(count) (\(enabled))"
39+
}
40+
41+
@JS static func testSimpleEnumDefault(status: Status = .success) -> Status {
42+
return status
43+
}
44+
45+
@JS static func testDirectionDefault(direction: Direction = .north) -> Direction {
46+
return direction
47+
}
48+
49+
@JS static func testRawStringEnumDefault(theme: Theme = .light) -> Theme {
50+
return theme
51+
}
52+
53+
@JS static func testComplexInit(greeter: Greeter = Greeter(name: "DefaultGreeter")) -> String {
54+
return greeter.greet()
55+
}
56+
57+
@JS static func testEmptyInit(_ object: StaticPropertyHolder = StaticPropertyHolder()) -> StaticPropertyHolder {
58+
return object
59+
}
60+
61+
@JS static func createConstructorDefaults(
62+
name: String = "Default",
63+
count: Int = 42,
64+
enabled: Bool = true,
65+
status: Status = .success,
66+
tag: String? = nil
67+
) -> DefaultArgumentConstructorDefaults {
68+
return DefaultArgumentConstructorDefaults(
69+
name: name,
70+
count: count,
71+
enabled: enabled,
72+
status: status,
73+
tag: tag
74+
)
75+
}
76+
77+
@JS static func describeConstructorDefaults(
78+
_ value: DefaultArgumentConstructorDefaults
79+
) -> String {
80+
return value.describe()
81+
}
82+
83+
@JS static func arrayWithDefault(_ values: [Int] = [1, 2, 3]) -> Int {
84+
return values.reduce(0, +)
85+
}
86+
87+
@JS static func arrayWithOptionalDefault(_ values: [Int]? = nil) -> Int {
88+
return values?.reduce(0, +) ?? -1
89+
}
90+
91+
@JS static func arrayMixedDefaults(
92+
prefix: String = "Sum",
93+
values: [Int] = [10, 20],
94+
suffix: String = "!"
95+
) -> String {
96+
return "\(prefix): \(values.reduce(0, +))\(suffix)"
97+
}
98+
}
99+
100+
@JS class DefaultArgumentConstructorDefaults {
101+
@JS var name: String
102+
@JS var count: Int
103+
@JS var enabled: Bool
104+
@JS var status: Status
105+
@JS var tag: String?
106+
107+
@JS init(
108+
name: String = "Default",
109+
count: Int = 42,
110+
enabled: Bool = true,
111+
status: Status = .success,
112+
tag: String? = nil
113+
) {
114+
self.name = name
115+
self.count = count
116+
self.enabled = enabled
117+
self.status = status
118+
self.tag = tag
119+
}
120+
121+
@JS func describe() -> String {
122+
let tagStr = tag ?? "nil"
123+
let statusStr: String
124+
switch status {
125+
case .loading: statusStr = "loading"
126+
case .success: statusStr = "success"
127+
case .error: statusStr = "error"
128+
}
129+
return "\(name):\(count):\(enabled):\(statusStr):\(tagStr)"
130+
}
131+
}

Tests/BridgeJSRuntimeTests/ExportAPITests.swift

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -769,101 +769,6 @@ enum GraphOperations {
769769
}
770770
}
771771

772-
// MARK: - Default Parameters
773-
774-
@JS func testStringDefault(message: String = "Hello World") -> String {
775-
return message
776-
}
777-
778-
@JS func testIntDefault(count: Int = 42) -> Int {
779-
return count
780-
}
781-
782-
@JS func testBoolDefault(flag: Bool = true) -> Bool {
783-
return flag
784-
}
785-
786-
@JS func testOptionalDefault(name: String? = nil) -> String? {
787-
return name
788-
}
789-
790-
@JS func testMultipleDefaults(
791-
title: String = "Default Title",
792-
count: Int = -10,
793-
enabled: Bool = false
794-
) -> String {
795-
return "\(title): \(count) (\(enabled))"
796-
}
797-
798-
@JS func testSimpleEnumDefault(status: Status = .success) -> Status {
799-
return status
800-
}
801-
802-
@JS func testDirectionDefault(direction: Direction = .north) -> Direction {
803-
return direction
804-
}
805-
806-
@JS func testRawStringEnumDefault(theme: Theme = .light) -> Theme {
807-
return theme
808-
}
809-
810-
@JS func testComplexInit(greeter: Greeter = Greeter(name: "DefaultGreeter")) -> String {
811-
return greeter.greet()
812-
}
813-
814-
@JS func testEmptyInit(_ object: StaticPropertyHolder = StaticPropertyHolder()) -> StaticPropertyHolder {
815-
return object
816-
}
817-
818-
@JS class ConstructorDefaults {
819-
@JS var name: String
820-
@JS var count: Int
821-
@JS var enabled: Bool
822-
@JS var status: Status
823-
@JS var tag: String?
824-
825-
@JS init(
826-
name: String = "Default",
827-
count: Int = 42,
828-
enabled: Bool = true,
829-
status: Status = .success,
830-
tag: String? = nil
831-
) {
832-
self.name = name
833-
self.count = count
834-
self.enabled = enabled
835-
self.status = status
836-
self.tag = tag
837-
}
838-
839-
@JS func describe() -> String {
840-
let tagStr = tag ?? "nil"
841-
let statusStr: String
842-
switch status {
843-
case .loading: statusStr = "loading"
844-
case .success: statusStr = "success"
845-
case .error: statusStr = "error"
846-
}
847-
return "\(name):\(count):\(enabled):\(statusStr):\(tagStr)"
848-
}
849-
}
850-
851-
@JS func arrayWithDefault(_ values: [Int] = [1, 2, 3]) -> Int {
852-
return values.reduce(0, +)
853-
}
854-
855-
@JS func arrayWithOptionalDefault(_ values: [Int]? = nil) -> Int {
856-
return values?.reduce(0, +) ?? -1
857-
}
858-
859-
@JS func arrayMixedDefaults(
860-
prefix: String = "Sum",
861-
values: [Int] = [10, 20],
862-
suffix: String = "!"
863-
) -> String {
864-
return "\(prefix): \(values.reduce(0, +))\(suffix)"
865-
}
866-
867772
// MARK: - Static Properties
868773

869774
@JS class StaticPropertyHolder {

0 commit comments

Comments
 (0)