From e3f7043533466bf08b55bb45c3ffb74fcec8b8b5 Mon Sep 17 00:00:00 2001 From: Vadim Gritsenko Date: Fri, 7 Aug 2026 10:09:43 -0400 Subject: [PATCH] Add TimeInterval alias (and a test). --- .../SwiftTypes/SwiftKnownModules.swift | 2 + .../TimeIntervalTests.swift | 126 ++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 Tests/JExtractSwiftTests/TimeIntervalTests.swift diff --git a/Sources/SwiftExtract/SwiftTypes/SwiftKnownModules.swift b/Sources/SwiftExtract/SwiftTypes/SwiftKnownModules.swift index 689c24f61..47b7b342a 100644 --- a/Sources/SwiftExtract/SwiftTypes/SwiftKnownModules.swift +++ b/Sources/SwiftExtract/SwiftTypes/SwiftKnownModules.swift @@ -129,6 +129,8 @@ private let foundationEssentialsSourceFile: SourceFileSyntax = """ public init(timeIntervalSince1970: Double) } + public typealias TimeInterval = Double + public struct UUID {} public struct URL { diff --git a/Tests/JExtractSwiftTests/TimeIntervalTests.swift b/Tests/JExtractSwiftTests/TimeIntervalTests.swift new file mode 100644 index 000000000..6b8038a2f --- /dev/null +++ b/Tests/JExtractSwiftTests/TimeIntervalTests.swift @@ -0,0 +1,126 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2026 Apple Inc. and the Swift.org project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift.org project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import JExtractSwiftLib +import SwiftJavaConfigurationShared +import Testing + +struct TimeIntervalTests { + @Test( + "Import: accept TimeInterval (resolves to Double)", + arguments: [ + ( + JExtractGenerationMode.jni, + /* expected Java chunks */ + [ + """ + public static void delay(double seconds) { + SwiftModule.$delay(seconds); + } + """ + ], + /* expected Swift chunks */ + [ + """ + @_cdecl("Java_com_example_swift_SwiftModule__00024delay__D") + public func Java_com_example_swift_SwiftModule__00024delay__D(environment: UnsafeMutablePointer!, thisClass: jclass, seconds: jdouble) { + SwiftModule.delay(seconds: Double(fromJNI: seconds, in: environment)) + } + """ + ], + ) + ] + ) + func func_accept_timeInterval( + mode: JExtractGenerationMode, + expectedJavaChunks: [String], + expectedSwiftChunks: [String] + ) throws { + let text = + """ + import Foundation + + public func delay(seconds: TimeInterval) + """ + + try assertOutput( + input: text, + mode, + .java, + detectChunkByInitialLines: 1, + expectedChunks: expectedJavaChunks + ) + + try assertOutput( + input: text, + mode, + .swift, + detectChunkByInitialLines: 1, + expectedChunks: expectedSwiftChunks + ) + } + + @Test( + "Import: return TimeInterval (resolves to Double)", + arguments: [ + ( + JExtractGenerationMode.jni, + /* expected Java chunks */ + [ + """ + public static double now() { + return SwiftModule.$now(); + } + """ + ], + /* expected Swift chunks */ + [ + """ + @_cdecl("Java_com_example_swift_SwiftModule__00024now__") + public func Java_com_example_swift_SwiftModule__00024now__(environment: UnsafeMutablePointer!, thisClass: jclass) -> jdouble { + return SwiftModule.now() + } + """ + ] + ) + ] + ) + func func_return_timeInterval( + mode: JExtractGenerationMode, + expectedJavaChunks: [String], + expectedSwiftChunks: [String] + ) throws { + let text = + """ + import Foundation + public func now() -> TimeInterval + """ + + try assertOutput( + input: text, + mode, + .java, + detectChunkByInitialLines: 1, + expectedChunks: expectedJavaChunks + ) + + try assertOutput( + input: text, + mode, + .swift, + detectChunkByInitialLines: 1, + expectedChunks: expectedSwiftChunks + ) + } +}