Skip to content

Commit

Permalink
Fix analysis and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
liamappelbe committed Apr 16, 2024
1 parent 8eb98a5 commit b145c8f
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 9 deletions.
5 changes: 3 additions & 2 deletions pkgs/objective_c/ffigen_c.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Generate bindings for the C headers.
# Run with `dart run ffigen --config ffigen_c.yaml`.
# Regenerate bindings with `dart run tool/generate_code.dart`.
name: CBindings
output: 'lib/src/c_bindings_generated.dart'
headers:
Expand Down Expand Up @@ -33,8 +33,9 @@ preamble: |
// BSD-style license that can be found in the LICENSE file.
// Bindings for `src/objective_c.h` and `src/objective_c_runtime.h`.
// Regenerate bindings with `dart run ffigen --config ffigen_c.yaml`.
// Regenerate bindings with `dart run tool/generate_code.dart`.
// ignore_for_file: always_specify_types
// ignore_for_file: camel_case_types
// ignore_for_file: non_constant_identifier_names
// coverage:ignore-file
7 changes: 5 additions & 2 deletions pkgs/objective_c/ffigen_objc.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Generate bindings for the ObjC headers.
# Run with `dart run ffigen --config ffigen_objc.yaml`.
# Regenerate bindings with `dart run tool/generate_code.dart`.
name: ObjectiveCBindings
language: objc
output: 'lib/src/objective_c_bindings_generated.dart'
Expand Down Expand Up @@ -46,8 +46,11 @@ preamble: |
// BSD-style license that can be found in the LICENSE file.
// Bindings for `src/foundation.h`.
// Regenerate bindings with `dart run ffigen --config ffigen_objc.yaml`.
// Regenerate bindings with `dart run tool/generate_code.dart`.
// ignore_for_file: always_specify_types
// ignore_for_file: camel_case_types
// ignore_for_file: non_constant_identifier_names
// coverage:ignore-file
import 'package:ffi/ffi.dart' as pkg_ffi;
2 changes: 2 additions & 0 deletions pkgs/objective_c/lib/objective_c.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export 'src/c_bindings_generated.dart'
objectRelease,
blockCopy,
blockRelease;

// Keep in sync with pkgs/objective_c/ffigen_objc.yaml.
export 'src/objective_c_bindings_generated.dart'
show
NSArray,
Expand Down
3 changes: 2 additions & 1 deletion pkgs/objective_c/lib/src/c_bindings_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
// BSD-style license that can be found in the LICENSE file.

// Bindings for `src/objective_c.h` and `src/objective_c_runtime.h`.
// Regenerate bindings with `dart run ffigen --config ffigen_c.yaml`.
// Regenerate bindings with `dart run tool/generate_code.dart`.

// ignore_for_file: always_specify_types
// ignore_for_file: camel_case_types
// ignore_for_file: non_constant_identifier_names
// coverage:ignore-file

// AUTO GENERATED FILE, DO NOT EDIT.
//
Expand Down
6 changes: 4 additions & 2 deletions pkgs/objective_c/lib/src/objective_c_bindings_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
// BSD-style license that can be found in the LICENSE file.

// Bindings for `src/foundation.h`.
// Regenerate bindings with `dart run ffigen --config ffigen_objc.yaml`.
// Regenerate bindings with `dart run tool/generate_code.dart`.

// ignore_for_file: always_specify_types
// ignore_for_file: camel_case_types
// ignore_for_file: non_constant_identifier_names
// coverage:ignore-file

import 'package:ffi/ffi.dart' as pkg_ffi;

// AUTO GENERATED FILE, DO NOT EDIT.
//
// Generated by `package:ffigen`.
// ignore_for_file: type=lint
import 'dart:ffi' as ffi;
import 'package:ffi/ffi.dart' as pkg_ffi;
import '../objective_c.dart' as objc;

class NSObject extends objc.ObjCObjectBase {
Expand Down
2 changes: 2 additions & 0 deletions pkgs/objective_c/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ dependencies:
sdk: flutter
ffi: ^2.1.0
plugin_platform_interface: ^2.0.2
yaml: ^3.1.0

dev_dependencies:
ffigen: ^11.0.0
flutter_test:
sdk: flutter
flutter_lints: ^3.0.0
test: ^1.25.0

dependency_overrides:
ffigen:
Expand Down
53 changes: 53 additions & 0 deletions pkgs/objective_c/test/interface_lists_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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.

// Objective C support is only available on mac.
@TestOn('mac-os')

import 'dart:io';

import 'package:ffigen/src/code_generator/objc_built_in_functions.dart';
import 'package:test/test.dart';
import 'package:yaml/yaml.dart';

void main() {
group('Verify interface lists', () {
late List<String> yamlInterfaces;
setUpAll(() {
final yaml = loadYaml(File('ffigen_objc.yaml').readAsStringSync());
yamlInterfaces = yaml['objc-interfaces']['include']
.map<String>((dynamic i) => i as String)
.toList() as List<String>
..sort();
});

test('ObjCBuiltInFunctions.builtInInterfaces', () {
expect(ObjCBuiltInFunctions.builtInInterfaces, yamlInterfaces);
});

test('package:objective_c exports all the interfaces', () {
final exportFile = File('lib/objective_c.dart').readAsStringSync();
for (final intf in yamlInterfaces) {
expect(exportFile, contains(intf));
}
});

test('All code genned interfaces are included in the list', () {
final classNameRegExp = RegExp(r'^class (\w+) ');
final allClassNames = <String>[];
for (final line in File('lib/src/objective_c_bindings_generated.dart')
.readAsLinesSync()) {
final match = classNameRegExp.firstMatch(line);
if (match != null) {
final className = match[1]!;
if (!className.startsWith('ObjCBlock')) {
allClassNames.add(className);
}
}
}
allClassNames.sort();
expect(allClassNames, yamlInterfaces);
});
});
}
27 changes: 27 additions & 0 deletions pkgs/objective_c/test/nsstring_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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.

// Objective C support is only available on mac.
@TestOn('mac-os')

import 'package:objective_c/objective_c.dart';
import 'package:test/test.dart';

void main() {
group('NSString', () {
for (final s in ['Hello', '🇵🇬', 'Embedded\u0000Null']) {
test('NSString to/from Dart string [$s]', () {
final ns1 = NSString(s);
expect(ns1.length, s.length);
expect(ns1.toString().length, s.length);
expect(ns1.toString(), s);

final ns2 = s.toNSString();
expect(ns2.length, s.length);
expect(ns2.toString().length, s.length);
expect(ns2.toString(), s);
});
}
});
}
2 changes: 0 additions & 2 deletions pkgs/objective_c/tool/generate_code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ Map<String, String> parseExtraMethods(String filename) {
}
}
}
extraMethods["import 'dart:ffi' as ffi;"] =
"import 'package:ffi/ffi.dart' as pkg_ffi;";
return extraMethods;
}

Expand Down

0 comments on commit b145c8f

Please sign in to comment.