-
Notifications
You must be signed in to change notification settings - Fork 81
[swift2objc] Filtering Support #1730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
liamappelbe
merged 11 commits into
dart-lang:main
from
nikeokoronkwo:swift2objc-filters
Dec 16, 2024
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0c6ae4f
added filter function to config
nikeokoronkwo 6b42b1c
unit tests for filter
nikeokoronkwo d9a926d
Merge branch 'main' into swift2objc-filters
nikeokoronkwo 2af7024
updated config options
nikeokoronkwo 34d05ed
reimplemented unit tests for filtering
nikeokoronkwo 93c7879
Merge branch 'main' into swift2objc-filters
nikeokoronkwo dbe4ba4
updated filtering to include transitive dependencies
nikeokoronkwo 38bbbbc
minor fixes and added deep transitive support
nikeokoronkwo 19e1504
removed dangling semicolon
nikeokoronkwo 4a7de6a
filter non nullable
nikeokoronkwo 94926ae
Resolved all standing conflicts
nikeokoronkwo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,110 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:path/path.dart' as path; | ||
import 'package:swift2objc/src/ast/declarations/compounds/class_declaration.dart'; | ||
import 'package:swift2objc/swift2objc.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('Unit test for filter', () { | ||
final thisDir = path.join(Directory.current.path, 'test/unit'); | ||
|
||
final file = path.join(thisDir, 'filter_test_input.swift'); | ||
test('A: Specific Files', () async { | ||
final output = path.join(thisDir, 'filter_test_output_a.swift'); | ||
final actualOutputFile = path.join(thisDir, '${ | ||
path.basenameWithoutExtension(output)}.test${path.extension(output) | ||
}'); | ||
|
||
await generateWrapper(Config( | ||
input: FilesInputConfig( | ||
files: [Uri.file(file)], | ||
), | ||
outputFile: Uri.file(actualOutputFile), | ||
tempDir: Directory(thisDir).uri, | ||
preamble: '// Test preamble text', | ||
include: (declaration) => declaration.name == 'Engine', | ||
)); | ||
|
||
final actualOutput = await File(actualOutputFile).readAsString(); | ||
final expectedOutput = File(output).readAsStringSync(); | ||
|
||
expect(actualOutput, expectedOutput); | ||
}); | ||
|
||
test('B: Declarations of a specific type', () async { | ||
final output = path.join(thisDir, 'filter_test_output_b.swift'); | ||
final actualOutputFile = path.join(thisDir, '${ | ||
path.basenameWithoutExtension(output)}.test${path.extension(output) | ||
}'); | ||
|
||
await generateWrapper(Config( | ||
input: FilesInputConfig( | ||
files: [Uri.file(file)], | ||
), | ||
outputFile: Uri.file(actualOutputFile), | ||
tempDir: Directory(thisDir).uri, | ||
preamble: '// Test preamble text', | ||
include: (declaration) => declaration is ClassDeclaration, | ||
)); | ||
|
||
final actualOutput = await File(actualOutputFile).readAsString(); | ||
final expectedOutput = File(output).readAsStringSync(); | ||
|
||
expect(actualOutput, expectedOutput); | ||
}); | ||
|
||
test('C: Nonexistent declaration', () async { | ||
final output = path.join(thisDir, 'filter_test_output_c.swift'); | ||
final actualOutputFile = path.join(thisDir, '${ | ||
path.basenameWithoutExtension(output)}.test${path.extension(output) | ||
}'); | ||
|
||
await generateWrapper(Config( | ||
input: FilesInputConfig( | ||
files: [Uri.file(file)], | ||
), | ||
outputFile: Uri.file(actualOutputFile), | ||
tempDir: Directory(thisDir).uri, | ||
preamble: '// Test preamble text', | ||
// The following declaration does not exist, | ||
// so none are produced in output | ||
include: (declaration) => declaration.name == 'Ship', | ||
)); | ||
|
||
final actualOutput = await File(actualOutputFile).readAsString(); | ||
final expectedOutput = File(output).readAsStringSync(); | ||
|
||
expect(actualOutput, expectedOutput); | ||
}); | ||
|
||
tearDown(() { | ||
if (File(path.join(thisDir, 'symbolgraph_module.abi.json')).existsSync()) { | ||
File(path.join(thisDir, 'symbolgraph_module.abi.json')).deleteSync(); | ||
} | ||
if (File(path.join(thisDir, 'symbolgraph_module.swiftdoc')).existsSync()) { | ||
File(path.join(thisDir, 'symbolgraph_module.swiftdoc')).deleteSync(); | ||
} | ||
if (File(path.join(thisDir, 'symbolgraph_module.swiftmodule')).existsSync()) { | ||
File(path.join(thisDir, 'symbolgraph_module.swiftmodule')).deleteSync(); | ||
} | ||
if (File(path.join(thisDir, 'symbolgraph_module.swiftsource')).existsSync()) { | ||
File(path.join(thisDir, 'symbolgraph_module.swiftsource')).deleteSync(); | ||
} | ||
if (File(path.join(thisDir, 'symbolgraph_module.symbols.json')).existsSync()) { | ||
File(path.join(thisDir, 'symbolgraph_module.symbols.json')).deleteSync(); | ||
} | ||
if (File(path.join(thisDir, 'symbolgraph_module.swiftsourceinfo')).existsSync()) { | ||
File(path.join(thisDir, 'symbolgraph_module.swiftsourceinfo')).deleteSync(); | ||
} | ||
|
||
for (final file in Directory(thisDir).listSync().where((t) => path.extension(t.path, 2) == '.test.swift')) { | ||
if (file is File) file.deleteSync(); | ||
} | ||
}); | ||
}); | ||
} |
This file contains hidden or 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,135 @@ | ||
import Foundation | ||
|
||
public struct Engine { | ||
public let type: String | ||
public let horsepower: Int | ||
|
||
public init(type: String, horsepower: Int) { | ||
self.type = type | ||
self.horsepower = horsepower | ||
} | ||
|
||
public func displaySpecs() { | ||
print("Engine: \(type), \(horsepower) HP") | ||
} | ||
} | ||
|
||
|
||
public struct Tire { | ||
public let brand: String | ||
public let size: Int | ||
|
||
public init(brand: String, size: Int) { | ||
self.brand = brand | ||
self.size = size | ||
} | ||
|
||
public func displayInfo() { | ||
print("Tire: \(brand), size \(size)") | ||
} | ||
} | ||
|
||
|
||
public struct Dimensions { | ||
public let length: Double | ||
public let width: Double | ||
public let height: Double | ||
|
||
public init(length: Double, width: Double, height: Double) { | ||
self.length = length | ||
self.width = width | ||
self.height = height | ||
} | ||
|
||
public func displayDimensions() { | ||
print("Dimensions (LxWxH): \(length) x \(width) x \(height) meters") | ||
} | ||
} | ||
|
||
|
||
public class Vehicle { | ||
public var make: String | ||
public var model: String | ||
public var engine: Engine | ||
public var dimensions: Dimensions | ||
|
||
public init(make: String, model: String, engine: Engine, dimensions: Dimensions) { | ||
self.make = make | ||
self.model = model | ||
self.engine = engine | ||
self.dimensions = dimensions | ||
} | ||
|
||
public func displayInfo() { | ||
print("Vehicle: \(make) \(model)") | ||
engine.displaySpecs() | ||
dimensions.displayDimensions() | ||
} | ||
} | ||
|
||
|
||
public class Car: Vehicle { | ||
public var numberOfDoors: Int | ||
public var tires: [Tire] | ||
|
||
public init(make: String, model: String, engine: Engine, dimensions: Dimensions, numberOfDoors: Int, tires: [Tire]) { | ||
self.numberOfDoors = numberOfDoors | ||
self.tires = tires | ||
super.init(make: make, model: model, engine: engine, dimensions: dimensions) | ||
} | ||
|
||
public func honk() { | ||
print("Car \(make) \(model) goes 'Beep Beep!'") | ||
} | ||
} | ||
|
||
|
||
public class ElectricCar: Car { | ||
public var batteryCapacity: Int // in kWh | ||
|
||
public init(make: String, model: String, dimensions: Dimensions, numberOfDoors: Int, tires: [Tire], batteryCapacity: Int) { | ||
self.batteryCapacity = batteryCapacity | ||
let electricEngine = Engine(type: "Electric", horsepower: batteryCapacity * 3) // Example calculation | ||
super.init(make: make, model: model, engine: electricEngine, dimensions: dimensions, numberOfDoors: numberOfDoors, tires: tires) | ||
} | ||
|
||
public func chargeBattery() { | ||
print("Charging \(make) \(model)... Battery capacity: \(batteryCapacity) kWh") | ||
} | ||
} | ||
|
||
public class Bicycle { | ||
public var brand: String | ||
public var gearCount: Int | ||
public var dimensions: Dimensions | ||
|
||
public init(brand: String, gearCount: Int, dimensions: Dimensions) { | ||
self.brand = brand | ||
self.gearCount = gearCount | ||
self.dimensions = dimensions | ||
} | ||
|
||
public func pedal() { | ||
print("\(brand) bicycle is pedaling with \(gearCount) gears.") | ||
dimensions.displayDimensions() | ||
} | ||
} | ||
|
||
|
||
public class Garage { | ||
private var vehicles: [Vehicle] = [] | ||
|
||
public init() {} | ||
|
||
public func addVehicle(_ vehicle: Vehicle) { | ||
vehicles.append(vehicle) | ||
print("Added \(vehicle.make) \(vehicle.model) to the garage.") | ||
} | ||
|
||
public func listVehicles() { | ||
print("Garage contains:") | ||
for vehicle in vehicles { | ||
print("- \(vehicle.make) \(vehicle.model)") | ||
} | ||
} | ||
} |
This file contains hidden or 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,31 @@ | ||
// Test preamble text | ||
|
||
import Foundation | ||
|
||
@objc public class EngineWrapper: NSObject { | ||
var wrappedInstance: Engine | ||
|
||
@objc public var horsepower: Int { | ||
get { | ||
wrappedInstance.horsepower | ||
} | ||
} | ||
|
||
@objc public var type: String { | ||
get { | ||
wrappedInstance.type | ||
} | ||
} | ||
|
||
init(_ wrappedInstance: Engine) { | ||
self.wrappedInstance = wrappedInstance | ||
} | ||
|
||
@objc init(type: String, horsepower: Int) { | ||
wrappedInstance = Engine(type: type, horsepower: horsepower) | ||
} | ||
|
||
@objc public func displaySpecs() { | ||
wrappedInstance.displaySpecs() | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.