Skip to content

Commit 9285965

Browse files
authored
Merge pull request #41413 from lorentey/schmactual
[StdlibUnittest] expectEqual: Use less opinionated argument names
2 parents 6509eff + 7ce8544 commit 9285965

File tree

2 files changed

+107
-67
lines changed

2 files changed

+107
-67
lines changed

stdlib/private/StdlibUnittest/StdlibUnittest.swift

Lines changed: 93 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -181,106 +181,133 @@ public func identityComp(_ element: MinimalComparableValue)
181181
return element
182182
}
183183

184-
public func expectEqual<T : Equatable>(_ expected: T, _ actual: T,
184+
public func expectEqual<T : Equatable>(
185+
_ first: T,
186+
_ second: T,
185187
_ message: @autoclosure () -> String = "",
186188
stackTrace: SourceLocStack = SourceLocStack(),
187189
showFrame: Bool = true,
188-
file: String = #file, line: UInt = #line) {
189-
expectEqualTest(expected, actual, message(),
190+
file: String = #file, line: UInt = #line
191+
) {
192+
expectEqualTest(first, second, message(),
190193
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line), showFrame: false) {$0 == $1}
191194
}
192195

193196
public func expectEqual<T : Equatable, U : Equatable>(
194-
_ expected: (T, U), _ actual: (T, U),
197+
_ first: (T, U),
198+
_ second: (T, U),
195199
_ message: @autoclosure () -> String = "",
196200
stackTrace: SourceLocStack = SourceLocStack(),
197201
showFrame: Bool = true,
198-
file: String = #file, line: UInt = #line) {
199-
expectEqualTest(expected.0, actual.0, message(),
202+
file: String = #file, line: UInt = #line
203+
) {
204+
expectEqualTest(first.0, second.0, message(),
200205
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line), showFrame: false) {$0 == $1}
201-
expectEqualTest(expected.1, actual.1, message(),
206+
expectEqualTest(first.1, second.1, message(),
202207
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line), showFrame: false) {$0 == $1}
203208
}
204209

205210
public func expectEqual<T : Equatable, U : Equatable, V : Equatable>(
206-
_ expected: (T, U, V), _ actual: (T, U, V),
211+
_ first: (T, U, V),
212+
_ second: (T, U, V),
207213
_ message: @autoclosure () -> String = "",
208214
stackTrace: SourceLocStack = SourceLocStack(),
209215
showFrame: Bool = true,
210-
file: String = #file, line: UInt = #line) {
211-
expectEqualTest(expected.0, actual.0, message(),
216+
file: String = #file, line: UInt = #line
217+
) {
218+
expectEqualTest(first.0, second.0, message(),
212219
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line), showFrame: false) {$0 == $1}
213-
expectEqualTest(expected.1, actual.1, message(),
220+
expectEqualTest(first.1, second.1, message(),
214221
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line), showFrame: false) {$0 == $1}
215-
expectEqualTest(expected.2, actual.2, message(),
222+
expectEqualTest(first.2, second.2, message(),
216223
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line), showFrame: false) {$0 == $1}
217224
}
218225

219226
public func expectEqual<T : Equatable, U : Equatable, V : Equatable, W : Equatable>(
220-
_ expected: (T, U, V, W), _ actual: (T, U, V, W),
227+
_ first: (T, U, V, W),
228+
_ second: (T, U, V, W),
221229
_ message: @autoclosure () -> String = "",
222230
stackTrace: SourceLocStack = SourceLocStack(),
223231
showFrame: Bool = true,
224-
file: String = #file, line: UInt = #line) {
225-
expectEqualTest(expected.0, actual.0, message(),
232+
file: String = #file, line: UInt = #line
233+
) {
234+
expectEqualTest(first.0, second.0, message(),
226235
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line), showFrame: false) {$0 == $1}
227-
expectEqualTest(expected.1, actual.1, message(),
236+
expectEqualTest(first.1, second.1, message(),
228237
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line), showFrame: false) {$0 == $1}
229-
expectEqualTest(expected.2, actual.2, message(),
238+
expectEqualTest(first.2, second.2, message(),
230239
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line), showFrame: false) {$0 == $1}
231-
expectEqualTest(expected.3, actual.3, message(),
240+
expectEqualTest(first.3, second.3, message(),
232241
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line), showFrame: false) {$0 == $1}
233242
}
234243

235-
public func expectEqual(_ expected: String, _ actual: Substring,
244+
public func expectEqual(
245+
_ first: String,
246+
_ second: Substring,
236247
_ message: @autoclosure () -> String = "",
237248
stackTrace: SourceLocStack = SourceLocStack(),
238249
showFrame: Bool = true,
239-
file: String = #file, line: UInt = #line) {
240-
if !(expected == actual) {
250+
file: String = #file, line: UInt = #line
251+
) {
252+
if !(first == second) {
241253
expectationFailure(
242-
"expected: \(String(reflecting: expected)) (of type \(String(reflecting: type(of: expected))))\n"
243-
+ "actual: \(String(reflecting: actual)) (of type \(String(reflecting: type(of: actual))))",
254+
"""
255+
first: \(String(reflecting: first)) (of type \(String(reflecting: type(of: first))))
256+
second: \(String(reflecting: second)) (of type \(String(reflecting: type(of: second))))
257+
""",
244258
trace: message(),
245259
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line)
246260
)
247261
}
248262
}
249-
public func expectEqual(_ expected: Substring, _ actual: String,
263+
public func expectEqual(
264+
_ first: Substring,
265+
_ second: String,
250266
_ message: @autoclosure () -> String = "",
251267
stackTrace: SourceLocStack = SourceLocStack(),
252268
showFrame: Bool = true,
253-
file: String = #file, line: UInt = #line) {
254-
if !(expected == actual) {
269+
file: String = #file, line: UInt = #line
270+
) {
271+
if !(first == second) {
255272
expectationFailure(
256-
"expected: \(String(reflecting: expected)) (of type \(String(reflecting: type(of: expected))))\n"
257-
+ "actual: \(String(reflecting: actual)) (of type \(String(reflecting: type(of: actual))))",
273+
"""
274+
first: \(String(reflecting: first)) (of type \(String(reflecting: type(of: first))))
275+
second: \(String(reflecting: second)) (of type \(String(reflecting: type(of: second))))
276+
""",
258277
trace: message(),
259278
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line)
260279
)
261280
}
262281
}
263-
public func expectEqual(_ expected: String, _ actual: String,
282+
public func expectEqual(
283+
_ first: String,
284+
_ second: String,
264285
_ message: @autoclosure () -> String = "",
265286
stackTrace: SourceLocStack = SourceLocStack(),
266287
showFrame: Bool = true,
267-
file: String = #file, line: UInt = #line) {
268-
if !(expected == actual) {
288+
file: String = #file, line: UInt = #line
289+
) {
290+
if !(first == second) {
269291
expectationFailure(
270-
"expected: \(String(reflecting: expected)) (of type \(String(reflecting: type(of: expected))))\n"
271-
+ "actual: \(String(reflecting: actual)) (of type \(String(reflecting: type(of: actual))))",
292+
"""
293+
first: \(String(reflecting: first)) (of type \(String(reflecting: type(of: first))))
294+
second: \(String(reflecting: second)) (of type \(String(reflecting: type(of: second))))
295+
""",
272296
trace: message(),
273297
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line)
274298
)
275299
}
276300
}
277301

278-
public func expectEqualReference(_ expected: AnyObject?, _ actual: AnyObject?,
302+
public func expectEqualReference(
303+
_ first: AnyObject?,
304+
_ second: AnyObject?,
279305
_ message: @autoclosure () -> String = "",
280306
stackTrace: SourceLocStack = SourceLocStack(),
281307
showFrame: Bool = true,
282-
file: String = #file, line: UInt = #line) {
283-
expectEqualTest(expected, actual, message(),
308+
file: String = #file, line: UInt = #line
309+
) {
310+
expectEqualTest(first, second, message(),
284311
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line), showFrame: false) {$0 === $1}
285312
}
286313

@@ -298,61 +325,74 @@ public func expectationFailure(
298325
// See <rdar://26058520> Generic type constraints incorrectly applied to
299326
// functions with the same name
300327
public func expectEqualTest<T>(
301-
_ expected: T, _ actual: T,
328+
_ first: T,
329+
_ second: T,
302330
_ message: @autoclosure () -> String = "",
303331
stackTrace: SourceLocStack = SourceLocStack(),
304332
showFrame: Bool = true,
305-
file: String = #file, line: UInt = #line, sameValue equal: (T, T) -> Bool
333+
file: String = #file, line: UInt = #line,
334+
sameValue equal: (T, T) -> Bool
306335
) {
307-
if !equal(expected, actual) {
336+
if !equal(first, second) {
308337
expectationFailure(
309-
"expected: \(String(reflecting: expected)) (of type \(String(reflecting: type(of: expected))))\n"
310-
+ "actual: \(String(reflecting: actual)) (of type \(String(reflecting: type(of: actual))))",
338+
"""
339+
first: \(String(reflecting: first)) (of type \(String(reflecting: type(of: first))))
340+
second: \(String(reflecting: second)) (of type \(String(reflecting: type(of: second))))
341+
""",
311342
trace: message(),
312343
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line)
313344
)
314345
}
315346
}
316347

317-
public func expectNotEqual<T : Equatable>(_ expected: T, _ actual: T,
348+
public func expectNotEqual<T : Equatable>(
349+
_ first: T,
350+
_ second: T,
318351
_ message: @autoclosure () -> String = "",
319352
stackTrace: SourceLocStack = SourceLocStack(),
320353
showFrame: Bool = true,
321-
file: String = #file, line: UInt = #line) {
322-
if expected == actual {
354+
file: String = #file, line: UInt = #line
355+
) {
356+
if first == second {
323357
expectationFailure(
324-
"unexpected value: \"\(actual)\" (of type \(String(reflecting: type(of: actual))))",
358+
"unexpected value: \"\(second)\" (of type \(String(reflecting: type(of: second))))",
325359
trace: message(),
326360
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line)
327361
)
328362
}
329363
}
330364

331365
public func expectOptionalEqual<T>(
332-
_ expected: T, _ actual: T?,
366+
_ first: T,
367+
_ second: T?,
333368
_ message: @autoclosure () -> String = "",
334369
stackTrace: SourceLocStack = SourceLocStack(),
335370
showFrame: Bool = true,
336-
file: String = #file, line: UInt = #line, sameValue equal: (T, T) -> Bool
371+
file: String = #file, line: UInt = #line,
372+
sameValue equal: (T, T) -> Bool
337373
) {
338-
if (actual == nil) || !equal(expected, actual!) {
374+
if (second == nil) || !equal(first, second!) {
339375
expectationFailure(
340-
"expected: \"\(expected)\" (of type \(String(reflecting: type(of: expected))))\n"
341-
+ "actual: \"\(actual.debugDescription)\" (of type \(String(reflecting: type(of: actual))))",
376+
"""
377+
first: \"\(first)\" (of type \(String(reflecting: type(of: first))))
378+
second: \"\(second.debugDescription)\" (of type \(String(reflecting: type(of: second))))
379+
""",
342380
trace: message(),
343381
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line))
344382
}
345383
}
346384

347385
public func expectEqual(
348-
_ expected: Any.Type, _ actual: Any.Type,
386+
_ first: Any.Type, _ second: Any.Type,
349387
_ message: @autoclosure () -> String = "",
350388
stackTrace: SourceLocStack = SourceLocStack(),
351389
showFrame: Bool = true,
352390
file: String = #file, line: UInt = #line
353391
) {
354-
expectEqualTest(expected, actual, message(),
355-
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line), showFrame: false) { $0 == $1 }
392+
expectEqualTest(
393+
first, second, message(),
394+
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line), showFrame: false
395+
) { $0 == $1 }
356396
}
357397

358398
public func expectLT<T : Comparable>(_ lhs: T, _ rhs: T,

validation-test/StdlibUnittest/Common.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ AssertionsTestSuite.test("expectFailure/Pass") {
438438
}
439439
// CHECK: [ RUN ] Assertions.expectFailure/Pass
440440
// CHECK-NEXT: stdout>>> check failed at {{.*}}{{[/\\]}}StdlibUnittest{{[/\\]}}Common.swift, line
441-
// CHECK: stdout>>> expected: 1 (of type Swift.Int)
442-
// CHECK: stdout>>> actual: 2 (of type Swift.Int)
441+
// CHECK: stdout>>> first: 1 (of type Swift.Int)
442+
// CHECK: stdout>>> second: 2 (of type Swift.Int)
443443
// CHECK: [ OK ] Assertions.expectFailure/Pass
444444

445445
AssertionsTestSuite.test("expectFailure/UXPass")
@@ -452,8 +452,8 @@ AssertionsTestSuite.test("expectFailure/UXPass")
452452
}
453453
// CHECK: [ RUN ] Assertions.expectFailure/UXPass ({{X}}FAIL: [Custom(reason: test)])
454454
// CHECK-NEXT: stdout>>> check failed at {{.*}}{{[/\\]}}StdlibUnittest{{[/\\]}}Common.swift, line
455-
// CHECK: stdout>>> expected: 1 (of type Swift.Int)
456-
// CHECK: stdout>>> actual: 2 (of type Swift.Int)
455+
// CHECK: stdout>>> first: 1 (of type Swift.Int)
456+
// CHECK: stdout>>> second: 2 (of type Swift.Int)
457457
// CHECK: [ UXPASS ] Assertions.expectFailure/UXPass
458458

459459
AssertionsTestSuite.test("expectFailure/Fail") {
@@ -489,11 +489,11 @@ AssertionsTestSuite.test("expectFailure/AfterFailure/Fail") {
489489
}
490490
// CHECK: [ RUN ] Assertions.expectFailure/AfterFailure/Fail
491491
// CHECK-NEXT: stdout>>> check failed at {{.*}}{{[/\\]}}StdlibUnittest{{[/\\]}}Common.swift, line
492-
// CHECK: stdout>>> expected: 1 (of type Swift.Int)
493-
// CHECK: stdout>>> actual: 2 (of type Swift.Int)
492+
// CHECK: stdout>>> first: 1 (of type Swift.Int)
493+
// CHECK: stdout>>> second: 2 (of type Swift.Int)
494494
// CHECK: stdout>>> check failed at {{.*}}{{[/\\]}}StdlibUnittest{{[/\\]}}Common.swift, line
495-
// CHECK: stdout>>> expected: 3 (of type Swift.Int)
496-
// CHECK: stdout>>> actual: 4 (of type Swift.Int)
495+
// CHECK: stdout>>> first: 3 (of type Swift.Int)
496+
// CHECK: stdout>>> second: 4 (of type Swift.Int)
497497
// CHECK: [ FAIL ] Assertions.expectFailure/AfterFailure/Fail
498498

499499
AssertionsTestSuite.test("expectFailure/AfterFailure/XFail")
@@ -507,11 +507,11 @@ AssertionsTestSuite.test("expectFailure/AfterFailure/XFail")
507507
}
508508
// CHECK: [ RUN ] Assertions.expectFailure/AfterFailure/XFail ({{X}}FAIL: [Custom(reason: test)])
509509
// CHECK-NEXT: stdout>>> check failed at {{.*}}{{[/\\]}}StdlibUnittest{{[/\\]}}Common.swift, line
510-
// CHECK: stdout>>> expected: 1 (of type Swift.Int)
511-
// CHECK: stdout>>> actual: 2 (of type Swift.Int)
510+
// CHECK: stdout>>> first: 1 (of type Swift.Int)
511+
// CHECK: stdout>>> second: 2 (of type Swift.Int)
512512
// CHECK: stdout>>> check failed at {{.*}}{{[/\\]}}StdlibUnittest{{[/\\]}}Common.swift, line
513-
// CHECK: stdout>>> expected: 3 (of type Swift.Int)
514-
// CHECK: stdout>>> actual: 4 (of type Swift.Int)
513+
// CHECK: stdout>>> first: 3 (of type Swift.Int)
514+
// CHECK: stdout>>> second: 4 (of type Swift.Int)
515515
// CHECK: [ XFAIL ] Assertions.expectFailure/AfterFailure/XFail
516516

517517
AssertionsTestSuite.test("expectUnreachable") {
@@ -605,8 +605,8 @@ TestSuiteLifetimeTracked.test("failsIfLifetimeTrackedAreLeaked") {
605605
}
606606
// CHECK: [ RUN ] TestSuiteLifetimeTracked.failsIfLifetimeTrackedAreLeaked
607607
// CHECK-NEXT: stdout>>> check failed at {{.*}}.swift, line [[@LINE-4]]
608-
// CHECK: stdout>>> expected: 0 (of type Swift.Int)
609-
// CHECK: stdout>>> actual: 1 (of type Swift.Int)
608+
// CHECK: stdout>>> first: 0 (of type Swift.Int)
609+
// CHECK: stdout>>> second: 1 (of type Swift.Int)
610610
// CHECK: [ FAIL ] TestSuiteLifetimeTracked.failsIfLifetimeTrackedAreLeaked
611611

612612
TestSuiteLifetimeTracked.test("passesIfLifetimeTrackedAreResetAfterFailure") {}

0 commit comments

Comments
 (0)