Can someone confirm for me that I’m not losing my mind? It seems like the exact same code does two different things, and I cannot understand why. Does anyone else observe the same when you do this? (I’m on macOS 10.15.6 with Swift 5.2.4 at the moment.)
-
Create an empty package:
% mkdir Experiment % cd Experiment % swift package init --type executable
-
Delete all but the manifest and the two module sources.
Package.swift Sources/Experiment/main.swift Tests/ExperimentTests/ExperimentTests.swift
-
Reduce the manifest to this:
// swift-tools-version:5.2 import PackageDescription let package = Package( name: "Experiment", targets: [ .target(name: "Experiment", dependencies: []), .testTarget(name: "ExperimentTests", dependencies: []), ] )
-
Overwrite
main.swift
with the following:import XCTest let string = "ש\u{5C1}\u{5B8}" let result = string.decomposedStringWithCompatibilityMapping let scalars = Array(result.unicodeScalars) let sorted = scalars.sorted(by: { $0.properties.canonicalCombiningClass < $1.properties.canonicalCombiningClass }) assert(scalars == sorted, "\(scalars) ≠ \(sorted)")
-
Strip the test case file down to this:
import XCTest final class ExperimentTests: XCTestCase { func testReordering() { } }
-
Copy and paste the contents of
main.swift
(except the import) verbatim intotestReordering()
. It should then look like this:import XCTest final class ExperimentTests: XCTestCase { func testReordering() { let string = "ש\u{5C1}\u{5B8}" let result = string.decomposedStringWithCompatibilityMapping let scalars = Array(result.unicodeScalars) let sorted = scalars.sorted(by: { $0.properties.canonicalCombiningClass < $1.properties.canonicalCombiningClass }) assert(scalars == sorted, "\(scalars) ≠ \(sorted)") } }
-
Try the executable variant:
% swift run [3/3] Linking Experiment Assertion failed: ["\u{05E9}", "\u{05C1}", "\u{05B8}"] ≠ ["\u{05E9}", "\u{05B8}", "\u{05C1}"]: file [...]/Experiment/Sources/Experiment/main.swift, line 10 zsh: illegal hardware instruction swift run
-
Try the test variant:
% swift test [3/3] Linking ExperimentPackageTests Test Suite 'All tests' started at 2020-08-25 16:29:07.998 Test Suite 'ExperimentPackageTests.xctest' started at 2020-08-25 16:29:07.999 Test Suite 'ExperimentTests' started at 2020-08-25 16:29:07.999 Test Case '-[ExperimentTests.ExperimentTests testReordering]' started. Test Case '-[ExperimentTests.ExperimentTests testReordering]' passed (0.088 seconds). Test Suite 'ExperimentTests' passed at 2020-08-25 16:29:08.087. Executed 1 test, with 0 failures (0 unexpected) in 0.088 (0.088) seconds Test Suite 'ExperimentPackageTests.xctest' passed at 2020-08-25 16:29:08.088. Executed 1 test, with 0 failures (0 unexpected) in 0.088 (0.089) seconds Test Suite 'All tests' passed at 2020-08-25 16:29:08.088. Executed 1 test, with 0 failures (0 unexpected) in 0.088 (0.090) seconds
How is it even possible that one traps and not the other?!? (That is what happens on your machine too, right? Or is the universe playing some sort of joke on me?)