From 9c64bdd22b919721a5af27df8c4b5c06a2df2f19 Mon Sep 17 00:00:00 2001 From: Tzvi Melamed Date: Tue, 29 Nov 2022 16:03:38 -0500 Subject: [PATCH 1/7] Add common code path for test configs --- lib/src/config_provider/config.dart | 2 +- .../decl_type_name_collision_test.dart | 6 ++-- test/config_tests/compiler_opts_test.dart | 7 +++-- .../exclude_all_by_default_test.dart | 9 +++--- test/config_tests/include_exclude_test.dart | 3 +- .../packed_struct_override_test.dart | 17 ++++------- test/config_tests/unknown_keys_warn_test.dart | 5 ++-- test/example_tests/cjson_example_test.dart | 6 ++-- .../example_tests/ffinative_example_test.dart | 6 ++-- test/example_tests/libclang_example_test.dart | 6 ++-- .../objective_c_example_test.dart | 23 ++++++++++----- .../shared_bindings_example_test.dart | 10 +++---- test/example_tests/simple_example_test.dart | 6 ++-- test/example_tests/swift_example_test.dart | 28 +++++++++++++++---- .../comment_markup_test.dart | 6 ++-- .../header_parser_tests/dart_handle_test.dart | 6 ++-- .../forward_decl_test.dart | 6 ++-- .../function_n_struct_test.dart | 6 ++-- test/header_parser_tests/functions_test.dart | 6 ++-- test/header_parser_tests/globals_test.dart | 6 ++-- .../imported_types_test.dart | 6 ++-- test/header_parser_tests/macros_test.dart | 6 ++-- .../native_func_typedef_test.dart | 6 ++-- .../nested_parsing_test.dart | 6 ++-- .../opaque_dependencies_test.dart | 6 ++-- .../packed_structs_test.dart | 6 ++-- .../header_parser_tests/regress_384_test.dart | 6 ++-- .../separate_definition_test.dart | 5 ++-- test/header_parser_tests/typedef_test.dart | 6 ++-- test/header_parser_tests/unions_test.dart | 6 ++-- .../unnamed_enums_test.dart | 6 ++-- test/large_integration_tests/large_test.dart | 14 ++++------ test/native_objc_test/util.dart | 7 +++-- test/native_test/native_test.dart | 5 ++-- test/rename_tests/rename_test.dart | 6 ++-- test/test_utils.dart | 6 ++++ 36 files changed, 123 insertions(+), 150 deletions(-) diff --git a/lib/src/config_provider/config.dart b/lib/src/config_provider/config.dart index 6290bac8..e012e56a 100644 --- a/lib/src/config_provider/config.dart +++ b/lib/src/config_provider/config.dart @@ -19,7 +19,7 @@ final _logger = Logger('ffigen.config_provider.config'); /// Provides configurations to other modules. /// -/// Handles validation, extraction of confiurations from yaml file. +/// Handles validation, extraction of configurations from a yaml file. class Config { /// Input filename. String? get filename => _filename; diff --git a/test/collision_tests/decl_type_name_collision_test.dart b/test/collision_tests/decl_type_name_collision_test.dart index 40d9f88d..19e9497f 100644 --- a/test/collision_tests/decl_type_name_collision_test.dart +++ b/test/collision_tests/decl_type_name_collision_test.dart @@ -3,12 +3,10 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -18,7 +16,7 @@ void main() { setUpAll(() { logWarnings(Level.SEVERE); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Decl type name collision test' ${strings.output}: 'unused' @@ -27,7 +25,7 @@ ${strings.headers}: - 'test/collision_tests/decl_type_name_collision.h' ${strings.preamble}: | // ignore_for_file: non_constant_identifier_names, - ''') as yaml.YamlMap), + '''), ); }); diff --git a/test/config_tests/compiler_opts_test.dart b/test/config_tests/compiler_opts_test.dart index 5193d4db..0b711acd 100644 --- a/test/config_tests/compiler_opts_test.dart +++ b/test/config_tests/compiler_opts_test.dart @@ -6,7 +6,8 @@ import 'package:ffigen/ffigen.dart'; import 'package:ffigen/src/config_provider/spec_utils.dart'; import 'package:ffigen/src/strings.dart' as strings; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; + +import '../test_utils.dart'; late Library actual, expected; @@ -28,7 +29,7 @@ void main() { ); }); test('Compiler Opts Automatic', () { - final config = Config.fromYaml(yaml.loadYaml(''' + final config = testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Compiler Opts Test' ${strings.output}: 'unused' @@ -38,7 +39,7 @@ ${strings.headers}: ${strings.compilerOptsAuto}: ${strings.macos}: ${strings.includeCStdLib}: false - ''') as yaml.YamlMap); + '''); expect(config.compilerOpts.isEmpty, true); }); }); diff --git a/test/config_tests/exclude_all_by_default_test.dart b/test/config_tests/exclude_all_by_default_test.dart index 40bf1b29..361e4b27 100644 --- a/test/config_tests/exclude_all_by_default_test.dart +++ b/test/config_tests/exclude_all_by_default_test.dart @@ -6,14 +6,13 @@ import 'package:ffigen/ffigen.dart'; import 'package:ffigen/src/code_generator.dart'; import 'package:ffigen/src/strings.dart' as strings; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; void main() { group('exclude_all_by_default', () { test('exclude_all_by_default test flag false', () { - final config = Config.fromYaml(yaml.loadYaml(''' + final config = testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'exclude_all_by_default test' ${strings.output}: 'unused' @@ -21,7 +20,7 @@ ${strings.excludeAllByDefault}: false ${strings.headers}: ${strings.entryPoints}: - 'test/config_tests/exclude_all_by_default.h' -''') as yaml.YamlMap); +'''); final library = parse(config); expect(library.getBinding('func'), isA()); @@ -34,7 +33,7 @@ ${strings.headers}: }); test('exclude_all_by_default test flag true', () { - final config = Config.fromYaml(yaml.loadYaml(''' + final config = testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'exclude_all_by_default test' ${strings.output}: 'unused' @@ -42,7 +41,7 @@ ${strings.excludeAllByDefault}: true ${strings.headers}: ${strings.entryPoints}: - 'test/config_tests/exclude_all_by_default.h' -''') as yaml.YamlMap); +'''); final library = parse(config); expect(() => library.getBinding('func'), throwsException); diff --git a/test/config_tests/include_exclude_test.dart b/test/config_tests/include_exclude_test.dart index 5cc1156d..1f278859 100644 --- a/test/config_tests/include_exclude_test.dart +++ b/test/config_tests/include_exclude_test.dart @@ -5,7 +5,6 @@ import 'package:ffigen/ffigen.dart'; import 'package:ffigen/src/strings.dart' as strings; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -69,6 +68,6 @@ $field: } } - final config = Config.fromYaml(yaml.loadYaml(templateString) as yaml.YamlMap); + final config = testConfig(templateString); return config; } diff --git a/test/config_tests/packed_struct_override_test.dart b/test/config_tests/packed_struct_override_test.dart index cb0bf3c3..68f89541 100644 --- a/test/config_tests/packed_struct_override_test.dart +++ b/test/config_tests/packed_struct_override_test.dart @@ -6,7 +6,6 @@ import 'package:ffigen/ffigen.dart'; import 'package:ffigen/src/code_generator.dart'; import 'package:ffigen/src/strings.dart' as strings; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -24,21 +23,15 @@ ${strings.headers}: ${strings.structs}: ${strings.structPack}: '''; - expect( - () => Config.fromYaml( - yaml.loadYaml("$baseYaml'.*': null") as yaml.YamlMap), + expect(() => testConfig("$baseYaml'.*': null"), throwsA(TypeMatcher())); - expect( - () => Config.fromYaml( - yaml.loadYaml("$baseYaml'.*': 3") as yaml.YamlMap), + expect(() => testConfig("$baseYaml'.*': 3"), throwsA(TypeMatcher())); - expect( - () => Config.fromYaml( - yaml.loadYaml("$baseYaml'.*': 32") as yaml.YamlMap), + expect(() => testConfig("$baseYaml'.*': 32"), throwsA(TypeMatcher())); }); test('Override values', () { - final config = Config.fromYaml(yaml.loadYaml(''' + final config = testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Packed Struct Override Test' ${strings.output}: 'unused' @@ -50,7 +43,7 @@ ${strings.structs}: 'Normal.*': 1 'StructWithAttr': 2 'PackedAttr': none - ''') as yaml.YamlMap); + '''); final library = parse(config); diff --git a/test/config_tests/unknown_keys_warn_test.dart b/test/config_tests/unknown_keys_warn_test.dart index 4fcb680e..3dd04517 100644 --- a/test/config_tests/unknown_keys_warn_test.dart +++ b/test/config_tests/unknown_keys_warn_test.dart @@ -6,7 +6,6 @@ import 'package:ffigen/ffigen.dart'; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -18,7 +17,7 @@ void main() { setUpAll(() { final logArr = []; logWarningsToArray(logArr, Level.WARNING); - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Warn for unknown keys.' ${strings.output}: 'unused' @@ -35,7 +34,7 @@ ${strings.structs}: 'warn-5': 'skip' ${strings.unions}: 'warn-6': 'skip' - ''') as yaml.YamlMap); + '''); logString = logArr.join("\n"); }); test('Warn for unknown keys.', () { diff --git a/test/example_tests/cjson_example_test.dart b/test/example_tests/cjson_example_test.dart index 44246155..cd09f38b 100644 --- a/test/example_tests/cjson_example_test.dart +++ b/test/example_tests/cjson_example_test.dart @@ -2,12 +2,10 @@ // 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 'package:ffigen/src/config_provider/config.dart'; import 'package:ffigen/src/header_parser.dart'; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart'; import '../test_utils.dart'; @@ -17,7 +15,7 @@ void main() { logWarnings(Level.SEVERE); }); test('c_json', () { - final config = Config.fromYaml(loadYaml(''' + final config = testConfig(''' ${strings.output}: 'cjson_generated_bindings.dart' ${strings.name}: 'CJson' ${strings.description}: 'Holds bindings to cJSON.' @@ -49,7 +47,7 @@ ${strings.preamble}: | // THE SOFTWARE. // ignore_for_file: camel_case_types, non_constant_identifier_names -''') as YamlMap); +'''); final library = parse(config); matchLibraryWithExpected( diff --git a/test/example_tests/ffinative_example_test.dart b/test/example_tests/ffinative_example_test.dart index 779b41a0..cf3c1586 100644 --- a/test/example_tests/ffinative_example_test.dart +++ b/test/example_tests/ffinative_example_test.dart @@ -2,12 +2,10 @@ // 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 'package:ffigen/src/config_provider/config.dart'; import 'package:ffigen/src/header_parser.dart'; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart'; import '../test_utils.dart'; @@ -18,7 +16,7 @@ void main() { }); test('ffinative', () { - final config = Config.fromYaml(loadYaml(''' + final config = testConfig(''' ${strings.name}: NativeLibrary ${strings.ffiNative}: ${strings.description}: Bindings to `headers/example.h`. @@ -28,7 +26,7 @@ ${strings.headers}: - 'example/ffinative/headers/example.h' ${strings.preamble}: | // ignore_for_file: deprecated_member_use -''') as YamlMap); +'''); final library = parse(config); matchLibraryWithExpected( diff --git a/test/example_tests/libclang_example_test.dart b/test/example_tests/libclang_example_test.dart index c226c4cf..d3b13e91 100644 --- a/test/example_tests/libclang_example_test.dart +++ b/test/example_tests/libclang_example_test.dart @@ -2,12 +2,10 @@ // 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 'package:ffigen/src/config_provider/config.dart'; import 'package:ffigen/src/header_parser.dart'; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart'; import '../test_utils.dart'; @@ -17,7 +15,7 @@ void main() { logWarnings(Level.SEVERE); }); test('libclang-example', () { - final config = Config.fromYaml(loadYaml(''' + final config = testConfig(''' ${strings.output}: 'generated_bindings.dart' ${strings.headers}: ${strings.entryPoints}: @@ -69,7 +67,7 @@ ${strings.preamble}: | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // ignore_for_file: camel_case_types, non_constant_identifier_names -''') as YamlMap); +'''); final library = parse(config); matchLibraryWithExpected( diff --git a/test/example_tests/objective_c_example_test.dart b/test/example_tests/objective_c_example_test.dart index b53f3418..6d363194 100644 --- a/test/example_tests/objective_c_example_test.dart +++ b/test/example_tests/objective_c_example_test.dart @@ -5,13 +5,10 @@ // Objective C support is only available on mac. @TestOn('mac-os') -import 'dart:io'; - -import 'package:ffigen/src/config_provider/config.dart'; import 'package:ffigen/src/header_parser.dart'; +import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart'; import '../test_utils.dart'; @@ -22,9 +19,21 @@ void main() { }); test('objective_c', () { - final pubspecFile = File('example/objective_c/pubspec.yaml'); - final pubspecYaml = loadYaml(pubspecFile.readAsStringSync()) as YamlMap; - final config = Config.fromYaml(pubspecYaml['ffigen'] as YamlMap); + final config = testConfig(''' +${strings.name}: AVFAudio +${strings.description}: Bindings for AVFAudio. +${strings.language}: objc +${strings.output}: 'avf_audio_bindings.dart' +${strings.excludeAllByDefault}: true +${strings.objcInterfaces}: + ${strings.include}: + - 'AVAudioPlayer' +${strings.headers}: + ${strings.entryPoints}: + - '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AVFAudio.framework/Headers/AVAudioPlayer.h' +${strings.preamble}: | + // ignore_for_file: camel_case_types, non_constant_identifier_names, unused_element, unused_field, return_of_invalid_type, void_checks, annotate_overrides, no_leading_underscores_for_local_identifiers, library_private_types_in_public_api +'''); final output = parse(config).generate(); // Verify that the output contains all the methods and classes that the diff --git a/test/example_tests/shared_bindings_example_test.dart b/test/example_tests/shared_bindings_example_test.dart index 290ea409..098aade1 100644 --- a/test/example_tests/shared_bindings_example_test.dart +++ b/test/example_tests/shared_bindings_example_test.dart @@ -2,12 +2,10 @@ // 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 'package:ffigen/src/config_provider/config.dart'; import 'package:ffigen/src/header_parser.dart'; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart'; import '../test_utils.dart'; @@ -18,7 +16,7 @@ void main() { }); test('a_shared_base bindings', () { - final config = Config.fromYaml(loadYaml(''' + final config = testConfig(''' ${strings.name}: NativeLibraryASharedB ${strings.description}: Bindings to `headers/a.h` with shared definitions from `headers/base.h`. ${strings.output}: 'lib/generated/a_shared_b_gen.dart' @@ -30,7 +28,7 @@ ${strings.import}: - 'example/shared_bindings/lib/generated/base_symbols.yaml' ${strings.preamble}: | // ignore_for_file: non_constant_identifier_names, camel_case_types -''') as YamlMap); +'''); final library = parse(config); matchLibraryWithExpected( @@ -41,7 +39,7 @@ ${strings.preamble}: | }); test('base symbol file output', () { - final config = Config.fromYaml(loadYaml(''' + final config = testConfig(''' ${strings.name}: NativeLibraryBase ${strings.description}: Bindings to `headers/base.h`. ${strings.output}: @@ -54,7 +52,7 @@ ${strings.headers}: - 'example/shared_bindings/headers/base.h' ${strings.preamble}: | // ignore_for_file: non_constant_identifier_names, camel_case_types -''') as YamlMap); +'''); final library = parse(config); matchLibrarySymbolFileWithExpected( library, diff --git a/test/example_tests/simple_example_test.dart b/test/example_tests/simple_example_test.dart index b79b576f..4039bf5e 100644 --- a/test/example_tests/simple_example_test.dart +++ b/test/example_tests/simple_example_test.dart @@ -2,12 +2,10 @@ // 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 'package:ffigen/src/config_provider/config.dart'; import 'package:ffigen/src/header_parser.dart'; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart'; import '../test_utils.dart'; @@ -18,14 +16,14 @@ void main() { }); test('simple', () { - final config = Config.fromYaml(loadYaml(''' + final config = testConfig(''' ${strings.name}: NativeLibrary ${strings.description}: Bindings to `headers/example.h`. ${strings.output}: 'generated_bindings.dart' ${strings.headers}: ${strings.entryPoints}: - 'example/simple/headers/example.h' -''') as YamlMap); +'''); final library = parse(config); matchLibraryWithExpected( diff --git a/test/example_tests/swift_example_test.dart b/test/example_tests/swift_example_test.dart index 05c854cb..02b285bd 100644 --- a/test/example_tests/swift_example_test.dart +++ b/test/example_tests/swift_example_test.dart @@ -8,12 +8,11 @@ import 'dart:async'; import 'dart:io'; -import 'package:ffigen/src/config_provider/config.dart'; import 'package:ffigen/src/header_parser.dart'; +import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:path/path.dart' as p; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart'; import '../test_utils.dart'; @@ -44,10 +43,27 @@ void main() { final result = await process.exitCode; expect(result, 0); - final pubspecFile = File('example/swift/pubspec.yaml'); - final pubspecYaml = loadYaml(pubspecFile.readAsStringSync()) as YamlMap; - final config = Config.fromYaml(pubspecYaml['ffigen'] as YamlMap, - filename: pubspecFile.path); + final config = testConfig(''' +${strings.name}: SwiftLibrary +${strings.description}: Bindings for swift_api. +${strings.language}: objc +${strings.output}: 'swift_api_bindings.dart' +${strings.excludeAllByDefault}: true +${strings.objcInterfaces}: + ${strings.include}: + - 'SwiftClass' + ${strings.objcModule}: + 'SwiftClass': 'swift_module' +${strings.headers}: + ${strings.entryPoints}: + - 'example/swift/swift_api.h' +${strings.preamble}: | + // ignore_for_file: camel_case_types, non_constant_identifier_names + // ignore_for_file: unused_element, unused_field, return_of_invalid_type + // ignore_for_file: void_checks, annotate_overrides + // ignore_for_file: no_leading_underscores_for_local_identifiers + // ignore_for_file: library_private_types_in_public_api +'''); final output = parse(config).generate(); // Verify that the output contains all the methods and classes that the diff --git a/test/header_parser_tests/comment_markup_test.dart b/test/header_parser_tests/comment_markup_test.dart index b7e5cfa6..e31098aa 100644 --- a/test/header_parser_tests/comment_markup_test.dart +++ b/test/header_parser_tests/comment_markup_test.dart @@ -3,12 +3,10 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -18,7 +16,7 @@ void main() { setUpAll(() { logWarnings(Level.SEVERE); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Comment Markup Test' ${strings.output}: 'unused' @@ -28,7 +26,7 @@ ${strings.headers}: ${strings.comments}: ${strings.style}: ${strings.any} ${strings.length}: ${strings.full} - ''') as yaml.YamlMap), + '''), ); }); diff --git a/test/header_parser_tests/dart_handle_test.dart b/test/header_parser_tests/dart_handle_test.dart index dec4deca..484d8b0a 100644 --- a/test/header_parser_tests/dart_handle_test.dart +++ b/test/header_parser_tests/dart_handle_test.dart @@ -4,12 +4,10 @@ import 'package:cli_util/cli_util.dart'; import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:path/path.dart' as path; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -20,7 +18,7 @@ void main() { setUpAll(() { logWarnings(); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Dart_Handle Test' ${strings.output}: 'unused' @@ -31,7 +29,7 @@ ${strings.headers}: - 'test/header_parser_tests/dart_handle.h' ${strings.includeDirectives}: - '**dart_handle.h' - ''') as yaml.YamlMap), + '''), ); }); test('Expected Bindings', () { diff --git a/test/header_parser_tests/forward_decl_test.dart b/test/header_parser_tests/forward_decl_test.dart index f8f5894e..73b9f321 100644 --- a/test/header_parser_tests/forward_decl_test.dart +++ b/test/header_parser_tests/forward_decl_test.dart @@ -3,12 +3,10 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -18,14 +16,14 @@ void main() { setUpAll(() { logWarnings(Level.SEVERE); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Forward Declaration Test' ${strings.output}: 'unused' ${strings.headers}: ${strings.entryPoints}: - 'test/header_parser_tests/forward_decl.h' - ''') as yaml.YamlMap), + '''), ); }); diff --git a/test/header_parser_tests/function_n_struct_test.dart b/test/header_parser_tests/function_n_struct_test.dart index 98dfafec..b61070c5 100644 --- a/test/header_parser_tests/function_n_struct_test.dart +++ b/test/header_parser_tests/function_n_struct_test.dart @@ -3,12 +3,10 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -20,7 +18,7 @@ void main() { logWarnings(Level.SEVERE); expected = expectedLibrary(); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Function And Struct Test' ${strings.output}: 'unused' @@ -28,7 +26,7 @@ ${strings.output}: 'unused' ${strings.headers}: ${strings.entryPoints}: - 'test/header_parser_tests/function_n_struct.h' - ''') as yaml.YamlMap), + '''), ); }); diff --git a/test/header_parser_tests/functions_test.dart b/test/header_parser_tests/functions_test.dart index b2718aa1..97685b0a 100644 --- a/test/header_parser_tests/functions_test.dart +++ b/test/header_parser_tests/functions_test.dart @@ -3,11 +3,9 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -18,7 +16,7 @@ void main() { setUpAll(() { logWarnings(); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Functions Test' ${strings.output}: 'unused' @@ -40,7 +38,7 @@ ${strings.functions}: ${strings.preamble}: | // ignore_for_file: camel_case_types - ''') as yaml.YamlMap), + '''), ); }); test('Expected Bindings', () { diff --git a/test/header_parser_tests/globals_test.dart b/test/header_parser_tests/globals_test.dart index 1872ced9..55184a86 100644 --- a/test/header_parser_tests/globals_test.dart +++ b/test/header_parser_tests/globals_test.dart @@ -3,11 +3,9 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -19,7 +17,7 @@ void main() { logWarnings(); expected = expectedLibrary(); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Globals Test' ${strings.output}: 'unused' @@ -37,7 +35,7 @@ ${strings.globals}: - pointerToLongDouble - globalStruct ${strings.compilerOpts}: '-Wno-nullability-completeness' - ''') as yaml.YamlMap), + '''), ); }); diff --git a/test/header_parser_tests/imported_types_test.dart b/test/header_parser_tests/imported_types_test.dart index 2d26c1f0..fc02cff7 100644 --- a/test/header_parser_tests/imported_types_test.dart +++ b/test/header_parser_tests/imported_types_test.dart @@ -3,11 +3,9 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -18,7 +16,7 @@ void main() { setUpAll(() { logWarnings(); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Imported types test' ${strings.output}: 'unused' @@ -31,7 +29,7 @@ ${strings.headers}: ${strings.preamble}: | // ignore_for_file: camel_case_types - ''') as yaml.YamlMap), + '''), ); }); test('Expected Bindings', () { diff --git a/test/header_parser_tests/macros_test.dart b/test/header_parser_tests/macros_test.dart index 689a2898..70439ef4 100644 --- a/test/header_parser_tests/macros_test.dart +++ b/test/header_parser_tests/macros_test.dart @@ -3,12 +3,10 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -20,7 +18,7 @@ void main() { logWarnings(Level.WARNING); expected = expectedLibrary(); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Macros Test' ${strings.output}: 'unused' @@ -29,7 +27,7 @@ ${strings.headers}: - 'test/header_parser_tests/macros.h' ${strings.includeDirectives}: - '**macros.h' - ''') as yaml.YamlMap), + '''), ); }); test('Total bindings count', () { diff --git a/test/header_parser_tests/native_func_typedef_test.dart b/test/header_parser_tests/native_func_typedef_test.dart index e576080b..180ff4d2 100644 --- a/test/header_parser_tests/native_func_typedef_test.dart +++ b/test/header_parser_tests/native_func_typedef_test.dart @@ -3,12 +3,10 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -18,14 +16,14 @@ void main() { setUpAll(() { logWarnings(Level.SEVERE); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Native Func Typedef Test.' ${strings.output}: 'unused' ${strings.headers}: ${strings.entryPoints}: - 'test/header_parser_tests/native_func_typedef.h' - ''') as yaml.YamlMap), + '''), ); }); diff --git a/test/header_parser_tests/nested_parsing_test.dart b/test/header_parser_tests/nested_parsing_test.dart index 813c7472..75803a0b 100644 --- a/test/header_parser_tests/nested_parsing_test.dart +++ b/test/header_parser_tests/nested_parsing_test.dart @@ -3,11 +3,9 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -19,7 +17,7 @@ void main() { logWarnings(); expected = expectedLibrary(); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Nested Parsing Test' ${strings.output}: 'unused' @@ -29,7 +27,7 @@ ${strings.headers}: ${strings.structs}: ${strings.exclude}: - Struct2 - ''') as yaml.YamlMap), + '''), ); }); diff --git a/test/header_parser_tests/opaque_dependencies_test.dart b/test/header_parser_tests/opaque_dependencies_test.dart index c2572d57..fc73cfe8 100644 --- a/test/header_parser_tests/opaque_dependencies_test.dart +++ b/test/header_parser_tests/opaque_dependencies_test.dart @@ -3,12 +3,10 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -18,7 +16,7 @@ void main() { setUpAll(() { logWarnings(Level.SEVERE); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Opaque Dependencies Test' ${strings.output}: 'unused' @@ -33,7 +31,7 @@ ${strings.unions}: ${strings.include}: - 'UE' ${strings.dependencyOnly}: ${strings.opaqueCompoundDependencies} - ''') as yaml.YamlMap), + '''), ); }); test('Expected bindings', () { diff --git a/test/header_parser_tests/packed_structs_test.dart b/test/header_parser_tests/packed_structs_test.dart index bbd1ea7f..62b70757 100644 --- a/test/header_parser_tests/packed_structs_test.dart +++ b/test/header_parser_tests/packed_structs_test.dart @@ -3,12 +3,10 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -18,14 +16,14 @@ void main() { setUpAll(() { logWarnings(Level.SEVERE); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Packed Structs Test' ${strings.output}: 'unused' ${strings.headers}: ${strings.entryPoints}: - 'test/header_parser_tests/packed_structs.h' - ''') as yaml.YamlMap), + '''), ); }); diff --git a/test/header_parser_tests/regress_384_test.dart b/test/header_parser_tests/regress_384_test.dart index 054474c7..b5751d10 100644 --- a/test/header_parser_tests/regress_384_test.dart +++ b/test/header_parser_tests/regress_384_test.dart @@ -3,12 +3,10 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -18,7 +16,7 @@ void main() { setUpAll(() { logWarnings(Level.SEVERE); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Regression test for #384' ${strings.output}: 'unused' @@ -26,7 +24,7 @@ ${strings.headers}: ${strings.entryPoints}: - 'test/header_parser_tests/regress_384_header_1.h' - 'test/header_parser_tests/regress_384_header_2.h' - ''') as yaml.YamlMap), + '''), ); }); diff --git a/test/header_parser_tests/separate_definition_test.dart b/test/header_parser_tests/separate_definition_test.dart index 6b62adee..663bebff 100644 --- a/test/header_parser_tests/separate_definition_test.dart +++ b/test/header_parser_tests/separate_definition_test.dart @@ -8,7 +8,6 @@ import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -38,13 +37,13 @@ Config _makeConfig(List entryPoints) { for (final ep in entryPoints) { entryPointBuilder.writeln(" - $ep"); } - final config = Config.fromYaml(yaml.loadYaml(''' + final config = testConfig(''' ${strings.name}: 'Bindings' ${strings.output}: 'unused' ${strings.headers}: ${strings.entryPoints}: ${entryPointBuilder.toString()} -''') as yaml.YamlMap); +'''); return config; } diff --git a/test/header_parser_tests/typedef_test.dart b/test/header_parser_tests/typedef_test.dart index 0c75f308..51b90e65 100644 --- a/test/header_parser_tests/typedef_test.dart +++ b/test/header_parser_tests/typedef_test.dart @@ -3,12 +3,10 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -19,7 +17,7 @@ void main() { setUpAll(() { logWarnings(Level.SEVERE); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'Bindings' ${strings.output}: 'unused' @@ -40,7 +38,7 @@ ${strings.typeMap}: dart-type: 'int' ${strings.preamble}: | // ignore_for_file: unused_element, unused_field - ''') as yaml.YamlMap), + '''), ); }); diff --git a/test/header_parser_tests/unions_test.dart b/test/header_parser_tests/unions_test.dart index 8e8949ea..e248dfb8 100644 --- a/test/header_parser_tests/unions_test.dart +++ b/test/header_parser_tests/unions_test.dart @@ -3,12 +3,10 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -18,14 +16,14 @@ void main() { setUpAll(() { logWarnings(Level.SEVERE); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Unions Test' ${strings.output}: 'unused' ${strings.headers}: ${strings.entryPoints}: - 'test/header_parser_tests/unions.h' - ''') as yaml.YamlMap), + '''), ); }); diff --git a/test/header_parser_tests/unnamed_enums_test.dart b/test/header_parser_tests/unnamed_enums_test.dart index ea9f32d3..eae9cf3a 100644 --- a/test/header_parser_tests/unnamed_enums_test.dart +++ b/test/header_parser_tests/unnamed_enums_test.dart @@ -3,11 +3,9 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -19,7 +17,7 @@ void main() { logWarnings(); expected = expectedLibrary(); actual = parser.parse( - Config.fromYaml(yaml.loadYaml(''' + testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Unnamed Enums Test' ${strings.output}: 'unused' @@ -32,7 +30,7 @@ ${strings.enums}: ${strings.unnamedEnums}: ${strings.exclude}: - B - ''') as yaml.YamlMap), + '''), ); }); diff --git a/test/large_integration_tests/large_test.dart b/test/large_integration_tests/large_test.dart index 77fe1ec3..257ff264 100644 --- a/test/large_integration_tests/large_test.dart +++ b/test/large_integration_tests/large_test.dart @@ -2,13 +2,11 @@ // 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 'package:ffigen/src/config_provider/config.dart'; import 'package:ffigen/src/header_parser.dart'; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:path/path.dart' as path; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart'; import '../test_utils.dart'; @@ -18,7 +16,7 @@ void main() { logWarnings(Level.SEVERE); }); test('Libclang test', () { - final config = Config.fromYaml(loadYaml(''' + final config = testConfig(''' ${strings.name}: LibClang ${strings.description}: Bindings to LibClang. ${strings.output}: unused @@ -45,7 +43,7 @@ ${strings.typeMap}: dart-type: 'int' ${strings.preamble}: | // ignore_for_file: camel_case_types, non_constant_identifier_names - ''') as YamlMap); + '''); final library = parse(config); matchLibraryWithExpected( @@ -60,7 +58,7 @@ ${strings.preamble}: | }); test('CJSON test', () { - final config = Config.fromYaml(loadYaml(''' + final config = testConfig(''' ${strings.name}: CJson ${strings.description}: Bindings to Cjson. ${strings.output}: unused @@ -73,7 +71,7 @@ ${strings.headers}: - '**cJSON.h' ${strings.preamble}: | // ignore_for_file: camel_case_types, non_constant_identifier_names - ''') as YamlMap); + '''); final library = parse(config); matchLibraryWithExpected( @@ -86,7 +84,7 @@ ${strings.preamble}: | test('SQLite test', () { // Excluding functions that use 'va_list' because it can either be a // Pointer<__va_list_tag> or int depending on the OS. - final config = Config.fromYaml(loadYaml(''' + final config = testConfig(''' ${strings.name}: SQLite ${strings.description}: Bindings to SQLite. ${strings.output}: unused @@ -105,7 +103,7 @@ ${strings.functions}: - sqlite3_str_vappendf ${strings.preamble}: | // ignore_for_file: camel_case_types, non_constant_identifier_names - ''') as YamlMap); + '''); final library = parse(config); matchLibraryWithExpected( diff --git a/test/native_objc_test/util.dart b/test/native_objc_test/util.dart index 1eb07469..7ef14bf6 100644 --- a/test/native_objc_test/util.dart +++ b/test/native_objc_test/util.dart @@ -7,16 +7,17 @@ import 'dart:io'; import 'package:ffigen/ffigen.dart'; import 'package:path/path.dart' as path; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart'; + +import '../test_utils.dart'; void generateBindingsForCoverage(String testName) { // The ObjC test bindings are generated in setup.dart (see #362), which means // that the ObjC related bits of ffigen are missed by test coverage. So this // function just regenerates those bindings. It doesn't test anything except // that the generation succeeded, by asserting the file exists. - final config = Config.fromYaml(loadYaml( + final config = testConfig( File(path.join('test', 'native_objc_test', '${testName}_config.yaml')) - .readAsStringSync()) as YamlMap); + .readAsStringSync()); final library = parse(config); final file = File( path.join('test', 'debug_generated', '${testName}_test.dart'), diff --git a/test/native_test/native_test.dart b/test/native_test/native_test.dart index 4365214f..f7578540 100644 --- a/test/native_test/native_test.dart +++ b/test/native_test/native_test.dart @@ -9,7 +9,6 @@ import 'dart:math'; import 'package:ffigen/ffigen.dart'; import 'package:path/path.dart' as path; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart'; import '../test_utils.dart'; import 'native_test_bindings.dart'; @@ -30,9 +29,9 @@ void main() { }); test('generate_bindings', () { - final config = Config.fromYaml(loadYaml( + final config = testConfig( File(path.join('test', 'native_test', 'config.yaml')) - .readAsStringSync()) as YamlMap); + .readAsStringSync()); final library = parse(config); final file = File( path.join('test', 'debug_generated', 'native_test_bindings.dart'), diff --git a/test/rename_tests/rename_test.dart b/test/rename_tests/rename_test.dart index 0b976a89..24a136a0 100644 --- a/test/rename_tests/rename_test.dart +++ b/test/rename_tests/rename_test.dart @@ -3,11 +3,9 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/header_parser.dart' as parser; import 'package:ffigen/src/strings.dart' as strings; import 'package:test/test.dart'; -import 'package:yaml/yaml.dart' as yaml; import '../test_utils.dart'; @@ -22,7 +20,7 @@ void main() { setUpAll(() { logWarnings(); expected = expectedLibrary(); - actual = parser.parse(Config.fromYaml(yaml.loadYaml(''' + actual = parser.parse(testConfig(''' ${strings.name}: 'NativeLibrary' ${strings.description}: 'Rename Test' ${strings.output}: 'unused' @@ -78,7 +76,7 @@ ${strings.macros}: ${strings.typedefs}: ${strings.rename}: 'Struct5_Alias': 'Struct5_Alias_Renamed' - ''') as yaml.YamlMap)); + ''')); }); test('Function addPrefix', () { diff --git a/test/test_utils.dart b/test/test_utils.dart index 93ea2957..ad81300f 100644 --- a/test/test_utils.dart +++ b/test/test_utils.dart @@ -5,10 +5,12 @@ import 'dart:io'; import 'package:ffigen/src/code_generator.dart'; +import 'package:ffigen/src/config_provider.dart'; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:path/path.dart' as path; import 'package:test/test.dart'; +import 'package:yaml/yaml.dart' as yaml; extension LibraryTestExt on Library { /// Get a [Binding]'s generated string with a given name. @@ -133,3 +135,7 @@ void logWarningsToArray(List logArr, [Level level = Level.WARNING]) { logArr.add('${record.level.name.padRight(8)}: ${record.message}'); }); } + +Config testConfig(String yamlBody) { + return Config.fromYaml(yaml.loadYaml(yamlBody) as yaml.YamlMap); +} From 6d6d8a4cf378569ae068f1331912137484591bbe Mon Sep 17 00:00:00 2001 From: Tzvi Melamed Date: Wed, 30 Nov 2022 12:51:19 -0500 Subject: [PATCH 2/7] share example configs with tests --- example/c_json/README.md | 2 +- example/c_json/ffigen.yaml | 31 ++++++++ example/c_json/pubspec.yaml | 35 +-------- example/ffinative/README.md | 2 +- example/ffinative/ffigen.yaml | 10 +++ example/ffinative/pubspec.yaml | 12 --- example/libclang-example/ffigen.yaml | 73 ++++++++++++++++++ example/libclang-example/pubspec.yaml | 77 +------------------ example/libclang-example/readme.md | 2 +- example/objective_c/README.md | 2 +- example/objective_c/ffigen.yaml | 13 ++++ example/objective_c/pubspec.yaml | 15 ---- example/simple/README.md | 2 +- example/simple/ffigen.yaml | 6 ++ example/simple/pubspec.yaml | 7 -- example/swift/README.md | 2 +- example/swift/ffigen.yaml | 19 +++++ example/swift/pubspec.yaml | 21 ----- test/example_tests/cjson_example_test.dart | 39 +--------- .../example_tests/ffinative_example_test.dart | 17 +--- test/example_tests/libclang_example_test.dart | 59 +------------- .../objective_c_example_test.dart | 19 +---- .../shared_bindings_example_test.dart | 45 ++++------- test/example_tests/simple_example_test.dart | 17 ++-- test/example_tests/swift_example_test.dart | 31 ++------ test/test_utils.dart | 15 +++- 26 files changed, 216 insertions(+), 357 deletions(-) create mode 100644 example/c_json/ffigen.yaml create mode 100644 example/ffinative/ffigen.yaml create mode 100644 example/libclang-example/ffigen.yaml create mode 100644 example/objective_c/ffigen.yaml create mode 100644 example/simple/ffigen.yaml create mode 100644 example/swift/ffigen.yaml diff --git a/example/c_json/README.md b/example/c_json/README.md index 229d65b7..bdd567ec 100644 --- a/example/c_json/README.md +++ b/example/c_json/README.md @@ -15,7 +15,7 @@ make ## Generating bindings At the root of this example (`example/c_json`), run - ``` -dart run ffigen +dart run ffigen --config ffigen.yaml ``` This will generate bindings in a file: [cjson_generated_bindings.dart](./cjson_generated_bindings.dart) diff --git a/example/c_json/ffigen.yaml b/example/c_json/ffigen.yaml new file mode 100644 index 00000000..08c5891a --- /dev/null +++ b/example/c_json/ffigen.yaml @@ -0,0 +1,31 @@ +output: 'cjson_generated_bindings.dart' +name: 'CJson' +description: 'Holds bindings to cJSON.' +headers: + entry-points: + - '../../third_party/cjson_library/cJSON.h' + include-directives: + - '**cJSON.h' +comments: false +preamble: | + // Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + // + // Permission is hereby granted, free of charge, to any person obtaining a copy + // of this software and associated documentation files (the "Software"), to deal + // in the Software without restriction, including without limitation the rights + // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + // copies of the Software, and to permit persons to whom the Software is + // furnished to do so, subject to the following conditions: + // + // The above copyright notice and this permission notice shall be included in + // all copies or substantial portions of the Software. + // + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + // THE SOFTWARE. + + // ignore_for_file: camel_case_types, non_constant_identifier_names \ No newline at end of file diff --git a/example/c_json/pubspec.yaml b/example/c_json/pubspec.yaml index 7b76a5c2..9d2d6843 100644 --- a/example/c_json/pubspec.yaml +++ b/example/c_json/pubspec.yaml @@ -14,37 +14,4 @@ dependencies: dev_dependencies: ffigen: path: '../../' - lints: ^2.0.1 - -ffigen: - output: 'cjson_generated_bindings.dart' - name: 'CJson' - description: 'Holds bindings to cJSON.' - headers: - entry-points: - - '../../third_party/cjson_library/cJSON.h' - include-directives: - - '**cJSON.h' - comments: false - preamble: | - // Copyright (c) 2009-2017 Dave Gamble and cJSON contributors - // - // Permission is hereby granted, free of charge, to any person obtaining a copy - // of this software and associated documentation files (the "Software"), to deal - // in the Software without restriction, including without limitation the rights - // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - // copies of the Software, and to permit persons to whom the Software is - // furnished to do so, subject to the following conditions: - // - // The above copyright notice and this permission notice shall be included in - // all copies or substantial portions of the Software. - // - // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - // THE SOFTWARE. - - // ignore_for_file: camel_case_types, non_constant_identifier_names + lints: ^1.0.1 diff --git a/example/ffinative/README.md b/example/ffinative/README.md index c1469402..b9c8316a 100644 --- a/example/ffinative/README.md +++ b/example/ffinative/README.md @@ -5,6 +5,6 @@ A simple example generating `FfiNative` bindings for a very small header file (` ## Generating bindings At the root of this example (`example/simple`), run - ``` -dart run ffigen +dart run ffigen --config ffigen.yaml ``` This will generate bindings in a file: [generated_bindings.dart](./generated_bindings.dart). diff --git a/example/ffinative/ffigen.yaml b/example/ffinative/ffigen.yaml new file mode 100644 index 00000000..18aeb3f6 --- /dev/null +++ b/example/ffinative/ffigen.yaml @@ -0,0 +1,10 @@ +name: NativeLibrary +ffi-native: + # asset: 'assetname' # (optional) +description: Bindings to `headers/example.h`. +output: 'generated_bindings.dart' +headers: + entry-points: + - 'headers/example.h' +preamble: | + // ignore_for_file: deprecated_member_use \ No newline at end of file diff --git a/example/ffinative/pubspec.yaml b/example/ffinative/pubspec.yaml index 9747ec7d..2247de40 100644 --- a/example/ffinative/pubspec.yaml +++ b/example/ffinative/pubspec.yaml @@ -13,15 +13,3 @@ dev_dependencies: ffigen: path: '../../' lints: ^2.0.0 - -ffigen: - name: NativeLibrary - ffi-native: - # asset: 'assetname' # (optional) - description: Bindings to `headers/example.h`. - output: 'generated_bindings.dart' - headers: - entry-points: - - 'headers/example.h' - preamble: | - // ignore_for_file: deprecated_member_use diff --git a/example/libclang-example/ffigen.yaml b/example/libclang-example/ffigen.yaml new file mode 100644 index 00000000..a1ebcdf4 --- /dev/null +++ b/example/libclang-example/ffigen.yaml @@ -0,0 +1,73 @@ +output: 'generated_bindings.dart' + +# This will sort the bindings alphabetically. +# sort: true + +# This is required if LLVM can't be found in default locations by ffigen. +# llvm-path: +# - '/usr/local/opt/llvm' + +# Bash style Glob matching is also supported. +# TODO(11): Globs dont work on windows if they begin with '.' or '..'. +headers: + entry-points: + - ../../third_party/libclang/include/clang-c/Index.h + include-directives: # use glob syntax to match with header file path. + - '**CXString.h' + - '**Index.h' + +compiler-opts: + - '-I../../third_party/libclang/include' + - '-Wno-nullability-completeness' +functions: + include: + - 'clang_.*' # Can be a regexp, '.' matches any character. + symbol-address: + include: + - 'clang_.*' # Symbol Address for these functions will be exposed via addresses. + expose-typedefs: + include: + - 'clang_.*' # Typedefs for Native and Dart type for these functions will be generated. +structs: + include: + - 'CX.*' +enums: + include: + - 'CXTypeKind' # Full names are given higher priority than regexp. + - 'CXGlobalOptFlags' + +library-imports: + custom_import: 'custom_import.dart' + +# These can be used to map any typedef to a specific native type. +type-map: + typedefs: + 'time_t': + lib: 'ffi' + c-type: 'Int64' + dart-type: 'int' + structs: + 'CXCursorSetImpl': + lib: 'custom_import' + c-type: 'CXCursorSetImpl' + dart-type: 'CXCursorSetImpl' + +# Default is 'NativeLibrary' +name: 'LibClang' + +description: 'Holds bindings to LibClang.' + +# Doc Comments for generated binings. +# Comments can be disabled by using comments: false +comments: + style: doxygen # Options - doxygen(default) / any. + length: full # Options - brief / full(default). + +# The header of the file, this is pasted as it is. +preamble: | + // Part of the LLVM Project, under the Apache License v2.0 with LLVM + // Exceptions. + // See https://llvm.org/LICENSE.txt for license information. + // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + + // ignore_for_file: camel_case_types, non_constant_identifier_names diff --git a/example/libclang-example/pubspec.yaml b/example/libclang-example/pubspec.yaml index bff5a3cb..2e1d9c34 100644 --- a/example/libclang-example/pubspec.yaml +++ b/example/libclang-example/pubspec.yaml @@ -12,79 +12,4 @@ dependencies: dev_dependencies: ffigen: path: '../../' - lints: ^2.0.1 - -ffigen: - output: 'generated_bindings.dart' - - # This will sort the bindings alphabetically. - # sort: true - - # This is required if LLVM can't be found in default locations by ffigen. - # llvm-path: - # - '/usr/local/opt/llvm' - - # Bash style Glob matching is also supported. - # TODO(11): Globs dont work on windows if they begin with '.' or '..'. - headers: - entry-points: - - ../../third_party/libclang/include/clang-c/Index.h - include-directives: # use glob syntax to match with header file path. - - '**CXString.h' - - '**Index.h' - - compiler-opts: - - '-I../../third_party/libclang/include' - - '-Wno-nullability-completeness' - functions: - include: - - 'clang_.*' # Can be a regexp, '.' matches any character. - symbol-address: - include: - - 'clang_.*' # Symbol Address for these functions will be exposed via addresses. - expose-typedefs: - include: - - 'clang_.*' # Typedefs for Native and Dart type for these functions will be generated. - structs: - include: - - 'CX.*' - enums: - include: - - 'CXTypeKind' # Full names are given higher priority than regexp. - - 'CXGlobalOptFlags' - - library-imports: - custom_import: 'custom_import.dart' - - # These can be used to map any typedef to a specific native type. - type-map: - typedefs: - 'time_t': - lib: 'ffi' - c-type: 'Int64' - dart-type: 'int' - structs: - 'CXCursorSetImpl': - lib: 'custom_import' - c-type: 'CXCursorSetImpl' - dart-type: 'CXCursorSetImpl' - - # Default is 'NativeLibrary' - name: 'LibClang' - - description: 'Holds bindings to LibClang.' - - # Doc Comments for generated binings. - # Comments can be disabled by using comments: false - comments: - style: doxygen # Options - doxygen(default) / any. - length: full # Options - brief / full(default). - - # The header of the file, this is pasted as it is. - preamble: | - // Part of the LLVM Project, under the Apache License v2.0 with LLVM - // Exceptions. - // See https://llvm.org/LICENSE.txt for license information. - // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - - // ignore_for_file: camel_case_types, non_constant_identifier_names + lints: ^1.0.1 diff --git a/example/libclang-example/readme.md b/example/libclang-example/readme.md index dab0bdf3..e6b08d6c 100644 --- a/example/libclang-example/readme.md +++ b/example/libclang-example/readme.md @@ -6,6 +6,6 @@ The C header source files for libclang are in [third_party/libclang](/third_part ## Generating bindings At the root of this example (`example/libclang-example`), run - ``` -dart run ffigen +dart run ffigen --config ffigen.yaml ``` This will generate bindings in a file: [generated_bindings.dart](./generated_bindings.dart). diff --git a/example/objective_c/README.md b/example/objective_c/README.md index 1d629721..42cac421 100644 --- a/example/objective_c/README.md +++ b/example/objective_c/README.md @@ -38,7 +38,7 @@ any of the fields or methods of `AVAudioPlayer`, so we're still able to use At the root of this example (`example/objective_c`), run: ``` -dart run ffigen +dart run ffigen --config ffigen.yaml ``` This will generate [avf_audio_bindings.dart](./avf_audio_bindings.dart). diff --git a/example/objective_c/ffigen.yaml b/example/objective_c/ffigen.yaml new file mode 100644 index 00000000..94764ca4 --- /dev/null +++ b/example/objective_c/ffigen.yaml @@ -0,0 +1,13 @@ +name: AVFAudio +description: Bindings for AVFAudio. +language: objc +output: 'avf_audio_bindings.dart' +exclude-all-by-default: true +objc-interfaces: + include: + - 'AVAudioPlayer' +headers: + entry-points: + - '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AVFAudio.framework/Headers/AVAudioPlayer.h' +preamble: | + // ignore_for_file: camel_case_types, non_constant_identifier_names, unused_element, unused_field, return_of_invalid_type, void_checks, annotate_overrides, no_leading_underscores_for_local_identifiers, library_private_types_in_public_api diff --git a/example/objective_c/pubspec.yaml b/example/objective_c/pubspec.yaml index ab1e3956..33b0c319 100644 --- a/example/objective_c/pubspec.yaml +++ b/example/objective_c/pubspec.yaml @@ -13,18 +13,3 @@ dev_dependencies: ffigen: path: '../../' lints: ^2.0.0 - -ffigen: - name: AVFAudio - description: Bindings for AVFAudio. - language: objc - output: 'avf_audio_bindings.dart' - exclude-all-by-default: true - objc-interfaces: - include: - - 'AVAudioPlayer' - headers: - entry-points: - - '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AVFAudio.framework/Headers/AVAudioPlayer.h' - preamble: | - // ignore_for_file: camel_case_types, non_constant_identifier_names, unused_element, unused_field, return_of_invalid_type, void_checks, annotate_overrides, no_leading_underscores_for_local_identifiers, library_private_types_in_public_api diff --git a/example/simple/README.md b/example/simple/README.md index 026ae9f6..75f1e37f 100644 --- a/example/simple/README.md +++ b/example/simple/README.md @@ -5,6 +5,6 @@ A very simple example, generates bindings for a very small header file (`headers ## Generating bindings At the root of this example (`example/simple`), run - ``` -dart run ffigen +dart run ffigen --config ffigen.yaml ``` This will generate bindings in a file: [generated_bindings.dart](./generated_bindings.dart). diff --git a/example/simple/ffigen.yaml b/example/simple/ffigen.yaml new file mode 100644 index 00000000..82ed9251 --- /dev/null +++ b/example/simple/ffigen.yaml @@ -0,0 +1,6 @@ +name: NativeLibrary +description: Bindings to `headers/example.h`. +output: 'generated_bindings.dart' +headers: + entry-points: + - 'headers/example.h' \ No newline at end of file diff --git a/example/simple/pubspec.yaml b/example/simple/pubspec.yaml index 33b93eba..d59f53e0 100644 --- a/example/simple/pubspec.yaml +++ b/example/simple/pubspec.yaml @@ -14,10 +14,3 @@ dev_dependencies: path: '../../' lints: ^2.0.1 -ffigen: - name: NativeLibrary - description: Bindings to `headers/example.h`. - output: 'generated_bindings.dart' - headers: - entry-points: - - 'headers/example.h' diff --git a/example/swift/README.md b/example/swift/README.md index 05c1c344..b168a49b 100644 --- a/example/swift/README.md +++ b/example/swift/README.md @@ -22,7 +22,7 @@ Once you have an Objective-C wrapper header, ffigen can parse it like any other header: ```shell -dart run ffigen +dart run ffigen --config ffigen.yaml ``` This will generate [swift_api_bindings.dart](./swift_api_bindings.dart), diff --git a/example/swift/ffigen.yaml b/example/swift/ffigen.yaml new file mode 100644 index 00000000..e1a83225 --- /dev/null +++ b/example/swift/ffigen.yaml @@ -0,0 +1,19 @@ +name: SwiftLibrary +description: Bindings for swift_api. +language: objc +output: 'swift_api_bindings.dart' +exclude-all-by-default: true +objc-interfaces: + include: + - 'SwiftClass' + module: + 'SwiftClass': 'swift_module' +headers: + entry-points: + - 'swift_api.h' +preamble: | + // ignore_for_file: camel_case_types, non_constant_identifier_names + // ignore_for_file: unused_element, unused_field, return_of_invalid_type + // ignore_for_file: void_checks, annotate_overrides + // ignore_for_file: no_leading_underscores_for_local_identifiers + // ignore_for_file: library_private_types_in_public_api \ No newline at end of file diff --git a/example/swift/pubspec.yaml b/example/swift/pubspec.yaml index 8bd5e710..d00ffa91 100644 --- a/example/swift/pubspec.yaml +++ b/example/swift/pubspec.yaml @@ -13,24 +13,3 @@ dev_dependencies: ffigen: path: '../../' lints: ^2.0.0 - -ffigen: - name: SwiftLibrary - description: Bindings for swift_api. - language: objc - output: 'swift_api_bindings.dart' - exclude-all-by-default: true - objc-interfaces: - include: - - 'SwiftClass' - module: - 'SwiftClass': 'swift_module' - headers: - entry-points: - - 'swift_api.h' - preamble: | - // ignore_for_file: camel_case_types, non_constant_identifier_names - // ignore_for_file: unused_element, unused_field, return_of_invalid_type - // ignore_for_file: void_checks, annotate_overrides - // ignore_for_file: no_leading_underscores_for_local_identifiers - // ignore_for_file: library_private_types_in_public_api diff --git a/test/example_tests/cjson_example_test.dart b/test/example_tests/cjson_example_test.dart index cd09f38b..3fb87555 100644 --- a/test/example_tests/cjson_example_test.dart +++ b/test/example_tests/cjson_example_test.dart @@ -3,8 +3,8 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/header_parser.dart'; -import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; +import 'package:path/path.dart' as path; import 'package:test/test.dart'; import '../test_utils.dart'; @@ -15,45 +15,14 @@ void main() { logWarnings(Level.SEVERE); }); test('c_json', () { - final config = testConfig(''' -${strings.output}: 'cjson_generated_bindings.dart' -${strings.name}: 'CJson' -${strings.description}: 'Holds bindings to cJSON.' -${strings.headers}: - ${strings.entryPoints}: - - 'third_party/cjson_library/cJSON.h' - ${strings.includeDirectives}: - - '**cJSON.h' -${strings.comments}: false -${strings.preamble}: | - // Copyright (c) 2009-2017 Dave Gamble and cJSON contributors - // - // Permission is hereby granted, free of charge, to any person obtaining a copy - // of this software and associated documentation files (the "Software"), to deal - // in the Software without restriction, including without limitation the rights - // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - // copies of the Software, and to permit persons to whom the Software is - // furnished to do so, subject to the following conditions: - // - // The above copyright notice and this permission notice shall be included in - // all copies or substantial portions of the Software. - // - // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - // THE SOFTWARE. - - // ignore_for_file: camel_case_types, non_constant_identifier_names -'''); + final config = + testConfigFromPath(path.join('example', 'c_json', 'ffigen.yaml')); final library = parse(config); matchLibraryWithExpected( library, 'example_c_json.dart', - ['example', 'c_json', config.output], + [config.output], ); }); }); diff --git a/test/example_tests/ffinative_example_test.dart b/test/example_tests/ffinative_example_test.dart index cf3c1586..3c39980d 100644 --- a/test/example_tests/ffinative_example_test.dart +++ b/test/example_tests/ffinative_example_test.dart @@ -3,8 +3,8 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/header_parser.dart'; -import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; +import 'package:path/path.dart' as path; import 'package:test/test.dart'; import '../test_utils.dart'; @@ -16,23 +16,14 @@ void main() { }); test('ffinative', () { - final config = testConfig(''' -${strings.name}: NativeLibrary -${strings.ffiNative}: -${strings.description}: Bindings to `headers/example.h`. -${strings.output}: 'generated_bindings.dart' -${strings.headers}: - ${strings.entryPoints}: - - 'example/ffinative/headers/example.h' -${strings.preamble}: | - // ignore_for_file: deprecated_member_use -'''); + final config = + testConfigFromPath(path.join('example', 'ffinative', 'ffigen.yaml')); final library = parse(config); matchLibraryWithExpected( library, 'example_ffinative.dart', - ['example', 'ffinative', config.output], + [config.output], ); }); }); diff --git a/test/example_tests/libclang_example_test.dart b/test/example_tests/libclang_example_test.dart index d3b13e91..4c2c8150 100644 --- a/test/example_tests/libclang_example_test.dart +++ b/test/example_tests/libclang_example_test.dart @@ -3,8 +3,8 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/header_parser.dart'; -import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; +import 'package:path/path.dart' as path; import 'package:test/test.dart'; import '../test_utils.dart'; @@ -15,65 +15,14 @@ void main() { logWarnings(Level.SEVERE); }); test('libclang-example', () { - final config = testConfig(''' -${strings.output}: 'generated_bindings.dart' -${strings.headers}: - ${strings.entryPoints}: - - third_party/libclang/include/clang-c/Index.h - ${strings.includeDirectives}: - - '**CXString.h' - - '**Index.h' - -${strings.compilerOpts}: '-Ithird_party/libclang/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/ -Wno-nullability-completeness' -${strings.functions}: - ${strings.include}: - - 'clang_.*' - ${strings.symbolAddress}: - ${strings.include}: - - 'clang_.*' - ${strings.exposeFunctionTypedefs}: - ${strings.include}: - - 'clang_.*' -${strings.structs}: - ${strings.include}: - - 'CX.*' -${strings.enums}: - ${strings.include}: - - 'CXTypeKind' - - 'CXGlobalOptFlags' -${strings.libraryImports}: - custom_import: 'custom_import.dart' -${strings.typeMap}: - ${strings.typeMapTypedefs}: - 'time_t': - lib: 'ffi' - c-type: 'Int64' - dart-type: 'int' - ${strings.typeMapStructs}: - 'CXCursorSetImpl': - lib: 'custom_import' - c-type: 'CXCursorSetImpl' - dart-type: 'CXCursorSetImpl' -${strings.name}: 'LibClang' -${strings.description}: 'Holds bindings to LibClang.' -${strings.comments}: - ${strings.style}: ${strings.doxygen} - ${strings.length}: ${strings.full} - -${strings.preamble}: | - // Part of the LLVM Project, under the Apache License v2.0 with LLVM - // Exceptions. - // See https://llvm.org/LICENSE.txt for license information. - // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - - // ignore_for_file: camel_case_types, non_constant_identifier_names -'''); + final config = testConfigFromPath( + path.join('example', 'libclang-example', 'ffigen.yaml')); final library = parse(config); matchLibraryWithExpected( library, 'example_libclang.dart', - ['example', 'libclang-example', config.output], + [config.output], ); }); }); diff --git a/test/example_tests/objective_c_example_test.dart b/test/example_tests/objective_c_example_test.dart index 6d363194..f1dc762a 100644 --- a/test/example_tests/objective_c_example_test.dart +++ b/test/example_tests/objective_c_example_test.dart @@ -6,8 +6,8 @@ @TestOn('mac-os') import 'package:ffigen/src/header_parser.dart'; -import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; +import 'package:path/path.dart' as path; import 'package:test/test.dart'; import '../test_utils.dart'; @@ -19,21 +19,8 @@ void main() { }); test('objective_c', () { - final config = testConfig(''' -${strings.name}: AVFAudio -${strings.description}: Bindings for AVFAudio. -${strings.language}: objc -${strings.output}: 'avf_audio_bindings.dart' -${strings.excludeAllByDefault}: true -${strings.objcInterfaces}: - ${strings.include}: - - 'AVAudioPlayer' -${strings.headers}: - ${strings.entryPoints}: - - '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/AVFAudio.framework/Headers/AVAudioPlayer.h' -${strings.preamble}: | - // ignore_for_file: camel_case_types, non_constant_identifier_names, unused_element, unused_field, return_of_invalid_type, void_checks, annotate_overrides, no_leading_underscores_for_local_identifiers, library_private_types_in_public_api -'''); + final config = testConfigFromPath( + path.join('example', 'objective_c', 'ffigen.yaml')); final output = parse(config).generate(); // Verify that the output contains all the methods and classes that the diff --git a/test/example_tests/shared_bindings_example_test.dart b/test/example_tests/shared_bindings_example_test.dart index 098aade1..0d4e39b1 100644 --- a/test/example_tests/shared_bindings_example_test.dart +++ b/test/example_tests/shared_bindings_example_test.dart @@ -3,8 +3,8 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/header_parser.dart'; -import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; +import 'package:path/path.dart' as path; import 'package:test/test.dart'; import '../test_utils.dart'; @@ -16,48 +16,33 @@ void main() { }); test('a_shared_base bindings', () { - final config = testConfig(''' -${strings.name}: NativeLibraryASharedB -${strings.description}: Bindings to `headers/a.h` with shared definitions from `headers/base.h`. -${strings.output}: 'lib/generated/a_shared_b_gen.dart' -${strings.headers}: - ${strings.entryPoints}: - - 'example/shared_bindings/headers/a.h' -${strings.import}: - ${strings.symbolFilesImport}: - - 'example/shared_bindings/lib/generated/base_symbols.yaml' -${strings.preamble}: | - // ignore_for_file: non_constant_identifier_names, camel_case_types -'''); + final config = testConfigFromPath(path.join( + 'example', + 'shared_bindings', + 'ffigen_configs', + 'a_shared_base.yaml', + )); final library = parse(config); matchLibraryWithExpected( library, 'example_shared_bindings.dart', - ['example', 'shared_bindings', config.output], + [config.output], ); }); test('base symbol file output', () { - final config = testConfig(''' -${strings.name}: NativeLibraryBase -${strings.description}: Bindings to `headers/base.h`. -${strings.output}: - ${strings.bindings}: 'lib/generated/base_gen.dart' - ${strings.symbolFile}: - ${strings.output}: 'lib/generated/base_symbols.yaml' - ${strings.importPath}: 'package:shared_bindings/generated/base_gen.dart' -${strings.headers}: - ${strings.entryPoints}: - - 'example/shared_bindings/headers/base.h' -${strings.preamble}: | - // ignore_for_file: non_constant_identifier_names, camel_case_types -'''); + final config = testConfigFromPath(path.join( + 'example', + 'shared_bindings', + 'ffigen_configs', + 'base.yaml', + )); final library = parse(config); matchLibrarySymbolFileWithExpected( library, 'example_shared_bindings.yaml', - ['example', 'shared_bindings', config.symbolFile!.output], + [config.symbolFile!.output], config.symbolFile!.importPath, ); }); diff --git a/test/example_tests/simple_example_test.dart b/test/example_tests/simple_example_test.dart index 4039bf5e..c3c877e9 100644 --- a/test/example_tests/simple_example_test.dart +++ b/test/example_tests/simple_example_test.dart @@ -3,8 +3,8 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/header_parser.dart'; -import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; +import 'package:path/path.dart' as path; import 'package:test/test.dart'; import '../test_utils.dart'; @@ -16,20 +16,17 @@ void main() { }); test('simple', () { - final config = testConfig(''' -${strings.name}: NativeLibrary -${strings.description}: Bindings to `headers/example.h`. -${strings.output}: 'generated_bindings.dart' -${strings.headers}: - ${strings.entryPoints}: - - 'example/simple/headers/example.h' -'''); + final config = testConfigFromPath(path.join( + 'example', + 'simple', + 'ffigen.yaml', + )); final library = parse(config); matchLibraryWithExpected( library, 'example_simple.dart', - ['example', 'simple', config.output], + [config.output], ); }); }); diff --git a/test/example_tests/swift_example_test.dart b/test/example_tests/swift_example_test.dart index 02b285bd..688f0260 100644 --- a/test/example_tests/swift_example_test.dart +++ b/test/example_tests/swift_example_test.dart @@ -9,9 +9,8 @@ import 'dart:async'; import 'dart:io'; import 'package:ffigen/src/header_parser.dart'; -import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; -import 'package:path/path.dart' as p; +import 'package:path/path.dart' as path; import 'package:test/test.dart'; import '../test_utils.dart'; @@ -37,33 +36,17 @@ void main() { '-o', 'libswiftapi.dylib', ], - workingDirectory: p.join(Directory.current.path, 'example/swift')); + workingDirectory: path.join(Directory.current.path, 'example/swift')); unawaited(stdout.addStream(process.stdout)); unawaited(stderr.addStream(process.stderr)); final result = await process.exitCode; expect(result, 0); - final config = testConfig(''' -${strings.name}: SwiftLibrary -${strings.description}: Bindings for swift_api. -${strings.language}: objc -${strings.output}: 'swift_api_bindings.dart' -${strings.excludeAllByDefault}: true -${strings.objcInterfaces}: - ${strings.include}: - - 'SwiftClass' - ${strings.objcModule}: - 'SwiftClass': 'swift_module' -${strings.headers}: - ${strings.entryPoints}: - - 'example/swift/swift_api.h' -${strings.preamble}: | - // ignore_for_file: camel_case_types, non_constant_identifier_names - // ignore_for_file: unused_element, unused_field, return_of_invalid_type - // ignore_for_file: void_checks, annotate_overrides - // ignore_for_file: no_leading_underscores_for_local_identifiers - // ignore_for_file: library_private_types_in_public_api -'''); + final config = testConfigFromPath(path.join( + 'example', + 'swift', + 'ffigen.yaml', + )); final output = parse(config).generate(); // Verify that the output contains all the methods and classes that the diff --git a/test/test_utils.dart b/test/test_utils.dart index ad81300f..bcdddd6d 100644 --- a/test/test_utils.dart +++ b/test/test_utils.dart @@ -46,7 +46,7 @@ void verifySetupFile(File file) { // Remove '\r' for Windows compatibility, then apply user's normalizer. String _normalizeGeneratedCode( String generated, String Function(String)? codeNormalizer) { - final noCR = generated.replaceAll('\r', ''); + final noCR = generated.replaceAll('\r', '').replaceAll(RegExp(r'\n+'), '\n'); if (codeNormalizer == null) return noCR; return codeNormalizer(noCR); } @@ -136,6 +136,15 @@ void logWarningsToArray(List logArr, [Level level = Level.WARNING]) { }); } -Config testConfig(String yamlBody) { - return Config.fromYaml(yaml.loadYaml(yamlBody) as yaml.YamlMap); +Config testConfig(String yamlBody, {String? filename}) { + return Config.fromYaml( + yaml.loadYaml(yamlBody) as yaml.YamlMap, + filename: filename, + ); +} + +Config testConfigFromPath(String path) { + final file = File(path); + final yamlBody = file.readAsStringSync(); + return testConfig(yamlBody, filename: path); } From eb06160936f1e2cfe08386199064c736742e0652 Mon Sep 17 00:00:00 2001 From: Tzvi Melamed Date: Sun, 4 Dec 2022 16:26:43 -0500 Subject: [PATCH 3/7] separate configs for testing --- example/c_json/README.md | 2 +- example/c_json/cjson_generated_bindings.dart | 1 - example/c_json/{ffigen.yaml => config.yaml} | 0 example/ffinative/README.md | 2 +- .../ffinative/{ffigen.yaml => config.yaml} | 0 .../{ffigen.yaml => config.yaml} | 1 + .../libclang-example/generated_bindings.dart | 101 ++++-------------- example/libclang-example/readme.md | 2 +- example/objective_c/README.md | 2 +- .../objective_c/{ffigen.yaml => config.yaml} | 0 .../ffigen_configs/a_shared_base.yaml | 2 +- example/simple/README.md | 2 +- example/simple/{ffigen.yaml => config.yaml} | 0 example/swift/README.md | 2 +- example/swift/{ffigen.yaml => config.yaml} | 0 example/swift/swift_api.h | 49 ++++++++- test/example_tests/cjson_example_test.dart | 2 +- .../example_tests/ffinative_example_test.dart | 2 +- test/example_tests/libclang_example_test.dart | 16 ++- .../objective_c_example_test.dart | 2 +- test/example_tests/simple_example_test.dart | 2 +- test/example_tests/swift_example_test.dart | 2 +- test/native_test/config.yaml | 4 +- test/regen.dart | 64 +++++------ test/test_utils.dart | 17 ++- 25 files changed, 138 insertions(+), 139 deletions(-) rename example/c_json/{ffigen.yaml => config.yaml} (100%) rename example/ffinative/{ffigen.yaml => config.yaml} (100%) rename example/libclang-example/{ffigen.yaml => config.yaml} (94%) rename example/objective_c/{ffigen.yaml => config.yaml} (100%) rename example/simple/{ffigen.yaml => config.yaml} (100%) rename example/swift/{ffigen.yaml => config.yaml} (100%) diff --git a/example/c_json/README.md b/example/c_json/README.md index bdd567ec..f222cc2e 100644 --- a/example/c_json/README.md +++ b/example/c_json/README.md @@ -15,7 +15,7 @@ make ## Generating bindings At the root of this example (`example/c_json`), run - ``` -dart run ffigen --config ffigen.yaml +dart run ffigen --config config.yaml ``` This will generate bindings in a file: [cjson_generated_bindings.dart](./cjson_generated_bindings.dart) diff --git a/example/c_json/cjson_generated_bindings.dart b/example/c_json/cjson_generated_bindings.dart index 0571e621..c520c20b 100644 --- a/example/c_json/cjson_generated_bindings.dart +++ b/example/c_json/cjson_generated_bindings.dart @@ -19,7 +19,6 @@ // THE SOFTWARE. // ignore_for_file: camel_case_types, non_constant_identifier_names - // AUTO GENERATED FILE, DO NOT EDIT. // // Generated by `package:ffigen`. diff --git a/example/c_json/ffigen.yaml b/example/c_json/config.yaml similarity index 100% rename from example/c_json/ffigen.yaml rename to example/c_json/config.yaml diff --git a/example/ffinative/README.md b/example/ffinative/README.md index b9c8316a..82b0e88f 100644 --- a/example/ffinative/README.md +++ b/example/ffinative/README.md @@ -5,6 +5,6 @@ A simple example generating `FfiNative` bindings for a very small header file (` ## Generating bindings At the root of this example (`example/simple`), run - ``` -dart run ffigen --config ffigen.yaml +dart run ffigen --config config.yaml ``` This will generate bindings in a file: [generated_bindings.dart](./generated_bindings.dart). diff --git a/example/ffinative/ffigen.yaml b/example/ffinative/config.yaml similarity index 100% rename from example/ffinative/ffigen.yaml rename to example/ffinative/config.yaml diff --git a/example/libclang-example/ffigen.yaml b/example/libclang-example/config.yaml similarity index 94% rename from example/libclang-example/ffigen.yaml rename to example/libclang-example/config.yaml index a1ebcdf4..f6517c08 100644 --- a/example/libclang-example/ffigen.yaml +++ b/example/libclang-example/config.yaml @@ -18,6 +18,7 @@ headers: compiler-opts: - '-I../../third_party/libclang/include' + - '-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/' - '-Wno-nullability-completeness' functions: include: diff --git a/example/libclang-example/generated_bindings.dart b/example/libclang-example/generated_bindings.dart index 800c5807..a84d3959 100644 --- a/example/libclang-example/generated_bindings.dart +++ b/example/libclang-example/generated_bindings.dart @@ -27,50 +27,6 @@ class LibClang { lookup) : _lookup = lookup; - /// Retrieve the character data associated with the given string. - ffi.Pointer clang_getCString( - CXString string, - ) { - return _clang_getCString( - string, - ); - } - - late final _clang_getCStringPtr = - _lookup>('clang_getCString'); - late final _clang_getCString = - _clang_getCStringPtr.asFunction(); - - /// Free the given string. - void clang_disposeString( - CXString string, - ) { - return _clang_disposeString( - string, - ); - } - - late final _clang_disposeStringPtr = - _lookup>( - 'clang_disposeString'); - late final _clang_disposeString = - _clang_disposeStringPtr.asFunction(); - - /// Free the given string set. - void clang_disposeStringSet( - ffi.Pointer set1, - ) { - return _clang_disposeStringSet( - set1, - ); - } - - late final _clang_disposeStringSetPtr = - _lookup>( - 'clang_disposeStringSet'); - late final _clang_disposeStringSet = - _clang_disposeStringSetPtr.asFunction(); - /// Provides a shared context for creating translation units. /// /// It provides two options: @@ -6833,12 +6789,6 @@ class LibClang { class _SymbolAddresses { final LibClang _library; _SymbolAddresses(this._library); - ffi.Pointer> - get clang_getCString => _library._clang_getCStringPtr; - ffi.Pointer> - get clang_disposeString => _library._clang_disposeStringPtr; - ffi.Pointer> - get clang_disposeStringSet => _library._clang_disposeStringSetPtr; ffi.Pointer> get clang_createIndex => _library._clang_createIndexPtr; ffi.Pointer> @@ -7641,36 +7591,6 @@ class _SymbolAddresses { get clang_Type_visitFields => _library._clang_Type_visitFieldsPtr; } -/// A character string. -/// -/// The \c CXString type is used to return strings from the interface when -/// the ownership of that string might differ from one call to the next. -/// Use \c clang_getCString() to retrieve the string data and, once finished -/// with the string data, call \c clang_disposeString() to free the string. -class CXString extends ffi.Struct { - external ffi.Pointer data; - - @ffi.UnsignedInt() - external int private_flags; -} - -class CXStringSet extends ffi.Struct { - external ffi.Pointer Strings; - - @ffi.UnsignedInt() - external int Count; -} - -typedef NativeClang_getCString = ffi.Pointer Function( - CXString string); -typedef DartClang_getCString = ffi.Pointer Function(CXString string); -typedef NativeClang_disposeString = ffi.Void Function(CXString string); -typedef DartClang_disposeString = void Function(CXString string); -typedef NativeClang_disposeStringSet = ffi.Void Function( - ffi.Pointer set1); -typedef DartClang_disposeStringSet = void Function( - ffi.Pointer set1); - class CXTargetInfoImpl extends ffi.Opaque {} class CXTranslationUnitImpl extends ffi.Opaque {} @@ -7759,6 +7679,19 @@ typedef NativeClang_CXIndex_setInvocationEmissionPathOption = ffi.Void Function( typedef DartClang_CXIndex_setInvocationEmissionPathOption = void Function( CXIndex arg0, ffi.Pointer Path); +/// A character string. +/// +/// The \c CXString type is used to return strings from the interface when +/// the ownership of that string might differ from one call to the next. +/// Use \c clang_getCString() to retrieve the string data and, once finished +/// with the string data, call \c clang_disposeString() to free the string. +class CXString extends ffi.Struct { + external ffi.Pointer data; + + @ffi.UnsignedInt() + external int private_flags; +} + /// A particular source file that is part of a translation unit. typedef CXFile = ffi.Pointer; typedef NativeClang_getFileName = CXString Function(CXFile SFile); @@ -9925,6 +9858,14 @@ typedef NativeClang_Cursor_getBriefCommentText = CXString Function(CXCursor C); typedef DartClang_Cursor_getBriefCommentText = CXString Function(CXCursor C); typedef NativeClang_Cursor_getMangling = CXString Function(CXCursor arg0); typedef DartClang_Cursor_getMangling = CXString Function(CXCursor arg0); + +class CXStringSet extends ffi.Struct { + external ffi.Pointer Strings; + + @ffi.UnsignedInt() + external int Count; +} + typedef NativeClang_Cursor_getCXXManglings = ffi.Pointer Function( CXCursor arg0); typedef DartClang_Cursor_getCXXManglings = ffi.Pointer Function( diff --git a/example/libclang-example/readme.md b/example/libclang-example/readme.md index e6b08d6c..39f24747 100644 --- a/example/libclang-example/readme.md +++ b/example/libclang-example/readme.md @@ -6,6 +6,6 @@ The C header source files for libclang are in [third_party/libclang](/third_part ## Generating bindings At the root of this example (`example/libclang-example`), run - ``` -dart run ffigen --config ffigen.yaml +dart run ffigen --config config.yaml ``` This will generate bindings in a file: [generated_bindings.dart](./generated_bindings.dart). diff --git a/example/objective_c/README.md b/example/objective_c/README.md index 42cac421..846b77a2 100644 --- a/example/objective_c/README.md +++ b/example/objective_c/README.md @@ -38,7 +38,7 @@ any of the fields or methods of `AVAudioPlayer`, so we're still able to use At the root of this example (`example/objective_c`), run: ``` -dart run ffigen --config ffigen.yaml +dart run ffigen --config config.yaml ``` This will generate [avf_audio_bindings.dart](./avf_audio_bindings.dart). diff --git a/example/objective_c/ffigen.yaml b/example/objective_c/config.yaml similarity index 100% rename from example/objective_c/ffigen.yaml rename to example/objective_c/config.yaml diff --git a/example/shared_bindings/ffigen_configs/a_shared_base.yaml b/example/shared_bindings/ffigen_configs/a_shared_base.yaml index 0adcc204..5873f77e 100644 --- a/example/shared_bindings/ffigen_configs/a_shared_base.yaml +++ b/example/shared_bindings/ffigen_configs/a_shared_base.yaml @@ -11,6 +11,6 @@ headers: import: symbol-files: # Both package Uri and file paths are supported here. - - 'package:shared_bindings/generated/base_symbols.yaml' + - '../lib/generated/base_symbols.yaml' preamble: | // ignore_for_file: non_constant_identifier_names, camel_case_types diff --git a/example/simple/README.md b/example/simple/README.md index 75f1e37f..81d95692 100644 --- a/example/simple/README.md +++ b/example/simple/README.md @@ -5,6 +5,6 @@ A very simple example, generates bindings for a very small header file (`headers ## Generating bindings At the root of this example (`example/simple`), run - ``` -dart run ffigen --config ffigen.yaml +dart run ffigen --config config.yaml ``` This will generate bindings in a file: [generated_bindings.dart](./generated_bindings.dart). diff --git a/example/simple/ffigen.yaml b/example/simple/config.yaml similarity index 100% rename from example/simple/ffigen.yaml rename to example/simple/config.yaml diff --git a/example/swift/README.md b/example/swift/README.md index b168a49b..d53778e1 100644 --- a/example/swift/README.md +++ b/example/swift/README.md @@ -22,7 +22,7 @@ Once you have an Objective-C wrapper header, ffigen can parse it like any other header: ```shell -dart run ffigen --config ffigen.yaml +dart run ffigen --config config.yaml ``` This will generate [swift_api_bindings.dart](./swift_api_bindings.dart), diff --git a/example/swift/ffigen.yaml b/example/swift/config.yaml similarity index 100% rename from example/swift/ffigen.yaml rename to example/swift/config.yaml diff --git a/example/swift/swift_api.h b/example/swift/swift_api.h index 45e5010f..6a42130b 100644 --- a/example/swift/swift_api.h +++ b/example/swift/swift_api.h @@ -1,4 +1,4 @@ -// Generated by Apple Swift version 5.5.2 (swiftlang-1300.0.40.106 clang-1300.0.29.21) +// Generated by Apple Swift version 5.7 (swiftlang-5.7.0.127.4 clang-1400.0.29.50) #ifndef SWIFT_MODULE_SWIFT_H #define SWIFT_MODULE_SWIFT_H #pragma clang diagnostic push @@ -21,11 +21,20 @@ # include #endif +#pragma clang diagnostic ignored "-Wduplicate-method-match" #pragma clang diagnostic ignored "-Wauto-import" +#if defined(__OBJC__) #include +#endif +#if defined(__cplusplus) +#include +#include +#include +#else #include #include #include +#endif #if !defined(SWIFT_TYPEDEFS) # define SWIFT_TYPEDEFS 1 @@ -181,9 +190,40 @@ typedef unsigned int swift_uint4 __attribute__((__ext_vector_type__(4))); #else # define SWIFT_DEPRECATED_OBJC(Msg) SWIFT_DEPRECATED_MSG(Msg) #endif +#if defined(__OBJC__) #if !defined(IBSegueAction) # define IBSegueAction #endif +#endif +#if !defined(SWIFT_EXTERN) +# if defined(__cplusplus) +# define SWIFT_EXTERN extern "C" +# else +# define SWIFT_EXTERN extern +# endif +#endif +#if !defined(SWIFT_CALL) +# define SWIFT_CALL __attribute__((swiftcall)) +#endif +#if defined(__cplusplus) +#if !defined(SWIFT_NOEXCEPT) +# define SWIFT_NOEXCEPT noexcept +#endif +#else +#if !defined(SWIFT_NOEXCEPT) +# define SWIFT_NOEXCEPT +#endif +#endif +#if defined(__cplusplus) +#if !defined(SWIFT_CXX_INT_DEFINED) +#define SWIFT_CXX_INT_DEFINED +namespace swift { +using Int = ptrdiff_t; +using UInt = size_t; +} +#endif +#endif +#if defined(__OBJC__) #if __has_feature(modules) #if __has_warning("-Watimport-in-framework-header") #pragma clang diagnostic ignored "-Watimport-in-framework-header" @@ -191,6 +231,7 @@ typedef unsigned int swift_uint4 __attribute__((__ext_vector_type__(4))); @import ObjectiveC; #endif +#endif #pragma clang diagnostic ignored "-Wproperty-attribute-mismatch" #pragma clang diagnostic ignored "-Wduplicate-method-arg" #if __has_warning("-Wpragma-clang-attribute") @@ -198,6 +239,7 @@ typedef unsigned int swift_uint4 __attribute__((__ext_vector_type__(4))); #endif #pragma clang diagnostic ignored "-Wunknown-pragmas" #pragma clang diagnostic ignored "-Wnullability" +#pragma clang diagnostic ignored "-Wdollar-in-identifier-extension" #if __has_attribute(external_source_symbol) # pragma push_macro("any") @@ -206,8 +248,8 @@ typedef unsigned int swift_uint4 __attribute__((__ext_vector_type__(4))); # pragma pop_macro("any") #endif +#if defined(__OBJC__) @class NSString; -@class NSNumber; SWIFT_CLASS("_TtC12swift_module10SwiftClass") @interface SwiftClass : NSObject @@ -216,6 +258,9 @@ SWIFT_CLASS("_TtC12swift_module10SwiftClass") - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER; @end +#endif +#if defined(__cplusplus) +#endif #if __has_attribute(external_source_symbol) # pragma clang attribute pop #endif diff --git a/test/example_tests/cjson_example_test.dart b/test/example_tests/cjson_example_test.dart index 3fb87555..714cb70b 100644 --- a/test/example_tests/cjson_example_test.dart +++ b/test/example_tests/cjson_example_test.dart @@ -16,7 +16,7 @@ void main() { }); test('c_json', () { final config = - testConfigFromPath(path.join('example', 'c_json', 'ffigen.yaml')); + testConfigFromPath(path.join('example', 'c_json', 'config.yaml')); final library = parse(config); matchLibraryWithExpected( diff --git a/test/example_tests/ffinative_example_test.dart b/test/example_tests/ffinative_example_test.dart index 3c39980d..28588b16 100644 --- a/test/example_tests/ffinative_example_test.dart +++ b/test/example_tests/ffinative_example_test.dart @@ -17,7 +17,7 @@ void main() { test('ffinative', () { final config = - testConfigFromPath(path.join('example', 'ffinative', 'ffigen.yaml')); + testConfigFromPath(path.join('example', 'ffinative', 'config.yaml')); final library = parse(config); matchLibraryWithExpected( diff --git a/test/example_tests/libclang_example_test.dart b/test/example_tests/libclang_example_test.dart index 4c2c8150..8414e499 100644 --- a/test/example_tests/libclang_example_test.dart +++ b/test/example_tests/libclang_example_test.dart @@ -2,6 +2,10 @@ // 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:ffigen/src/code_generator/library.dart'; +import 'package:ffigen/src/config_provider/config.dart'; import 'package:ffigen/src/header_parser.dart'; import 'package:logging/logging.dart'; import 'package:path/path.dart' as path; @@ -15,9 +19,15 @@ void main() { logWarnings(Level.SEVERE); }); test('libclang-example', () { - final config = testConfigFromPath( - path.join('example', 'libclang-example', 'ffigen.yaml')); - final library = parse(config); + final configYaml = + File(path.join('example', 'libclang-example', 'config.yaml')) + .absolute; + late Config config; + late Library library; + withChDir(configYaml.path, () { + config = testConfigFromPath(configYaml.path); + library = parse(config); + }); matchLibraryWithExpected( library, diff --git a/test/example_tests/objective_c_example_test.dart b/test/example_tests/objective_c_example_test.dart index f1dc762a..b291edbc 100644 --- a/test/example_tests/objective_c_example_test.dart +++ b/test/example_tests/objective_c_example_test.dart @@ -20,7 +20,7 @@ void main() { test('objective_c', () { final config = testConfigFromPath( - path.join('example', 'objective_c', 'ffigen.yaml')); + path.join('example', 'objective_c', 'config.yaml')); final output = parse(config).generate(); // Verify that the output contains all the methods and classes that the diff --git a/test/example_tests/simple_example_test.dart b/test/example_tests/simple_example_test.dart index c3c877e9..343bf1cc 100644 --- a/test/example_tests/simple_example_test.dart +++ b/test/example_tests/simple_example_test.dart @@ -19,7 +19,7 @@ void main() { final config = testConfigFromPath(path.join( 'example', 'simple', - 'ffigen.yaml', + 'config.yaml', )); final library = parse(config); diff --git a/test/example_tests/swift_example_test.dart b/test/example_tests/swift_example_test.dart index 688f0260..02ed00ad 100644 --- a/test/example_tests/swift_example_test.dart +++ b/test/example_tests/swift_example_test.dart @@ -45,7 +45,7 @@ void main() { final config = testConfigFromPath(path.join( 'example', 'swift', - 'ffigen.yaml', + 'config.yaml', )); final output = parse(config).generate(); diff --git a/test/native_test/config.yaml b/test/native_test/config.yaml index 6d8e4752..6f73a509 100644 --- a/test/native_test/config.yaml +++ b/test/native_test/config.yaml @@ -8,10 +8,10 @@ name: NativeLibrary description: 'Native tests.' -output: 'test/native_test/native_test_bindings.dart' +output: './native_test_bindings.dart' headers: entry-points: - - 'test/native_test/native_test.c' + - './native_test.c' include-directives: - '**native_test.c' diff --git a/test/regen.dart b/test/regen.dart index 5b0fc2cd..1ee0df3c 100644 --- a/test/regen.dart +++ b/test/regen.dart @@ -8,7 +8,7 @@ import 'dart:io'; import 'package:args/args.dart'; import 'package:ffigen/ffigen.dart'; import 'package:logging/logging.dart'; -import 'package:yaml/yaml.dart'; +import './test_utils.dart'; const usage = r'''Regenerates the Dart FFI bindings used in tests and examples. @@ -18,31 +18,12 @@ e.g. with this command: $ dart run test/setup.dart && dart run test/regen.dart && dart test '''; -Future _regenConfig(File yamlConfig, File bindingOutput, - {bool chDir = false}) async { +Future _regenConfig(File yamlConfig, File bindingOutput) async { yamlConfig = yamlConfig.absolute; bindingOutput = bindingOutput.absolute; - - Directory? oldDir; - var yaml = loadYaml(await yamlConfig.readAsString()) as YamlMap; - - if (chDir) { - oldDir = Directory.current; - Directory.current = yamlConfig.parent; - } - try { - if (yaml.containsKey("ffigen")) { - yaml = yaml["ffigen"] as YamlMap; - } - - final config = Config.fromYaml(yaml); - final library = parse(config); - library.generateFile(bindingOutput); - } finally { - if (oldDir != null) { - Directory.current = oldDir; - } - } + final config = testConfigFromPath(yamlConfig.path); + final library = parse(config); + library.generateFile(bindingOutput); } Future main(List args) async { @@ -69,18 +50,25 @@ Future main(List args) async { print('${record.level.name}: ${record.time}: ${record.message}'); }); - await _regenConfig(File('test/native_test/config.yaml'), - File('test/native_test/native_test_bindings.dart')); - - await _regenConfig(File('example/libclang-example/pubspec.yaml'), - File('example/libclang-example/generated_bindings.dart'), - chDir: true); - - await _regenConfig(File('example/simple/pubspec.yaml'), - File('example/simple/generated_bindings.dart'), - chDir: true); - - await _regenConfig(File('example/c_json/pubspec.yaml'), - File('example/c_json/cjson_generated_bindings.dart'), - chDir: true); + final nativeTestConfig = File('test/native_test/config.yaml').absolute; + final nativeTestOut = + File('test/native_test/native_test_bindings.dart').absolute; + await withChDir(nativeTestConfig.path, + () => _regenConfig(nativeTestConfig, nativeTestOut)); + + final libclangConfig = File('example/libclang-example/config.yaml').absolute; + final libclangOut = + File('example/libclang-example/generated_bindings.dart').absolute; + await withChDir( + libclangConfig.path, () => _regenConfig(libclangConfig, libclangOut)); + + final simpleConfig = File('example/simple/config.yaml').absolute; + final simpleOut = File('example/simple/generated_bindings.dart').absolute; + await withChDir( + simpleConfig.path, () => _regenConfig(simpleConfig, simpleOut)); + + final cJsonConfig = File('example/c_json/config.yaml').absolute; + final cJsonOut = + File('example/c_json/cjson_generated_bindings.dart').absolute; + await withChDir(cJsonConfig.path, () => _regenConfig(cJsonConfig, cJsonOut)); } diff --git a/test/test_utils.dart b/test/test_utils.dart index bcdddd6d..f4e33a17 100644 --- a/test/test_utils.dart +++ b/test/test_utils.dart @@ -5,7 +5,7 @@ import 'dart:io'; import 'package:ffigen/src/code_generator.dart'; -import 'package:ffigen/src/config_provider.dart'; +import 'package:ffigen/src/config_provider/config.dart'; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; import 'package:path/path.dart' as path; @@ -148,3 +148,18 @@ Config testConfigFromPath(String path) { final yamlBody = file.readAsStringSync(); return testConfig(yamlBody, filename: path); } + +T withChDir(String path, T Function() inner) { + final oldDir = Directory.current; + Directory.current = File(path).parent; + + late T result; + + try { + result = inner(); + } finally { + Directory.current = oldDir; + } + + return result; +} From 2d9eaf5cda8c02ad47d5edcace6855d9fce06d7a Mon Sep 17 00:00:00 2001 From: Tzvi Melamed Date: Sun, 4 Dec 2022 23:28:05 -0500 Subject: [PATCH 4/7] use chDir for native test --- test/native_test/native_test.dart | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/test/native_test/native_test.dart b/test/native_test/native_test.dart index f7578540..9fdb8281 100644 --- a/test/native_test/native_test.dart +++ b/test/native_test/native_test.dart @@ -29,26 +29,31 @@ void main() { }); test('generate_bindings', () { - final config = testConfig( - File(path.join('test', 'native_test', 'config.yaml')) - .readAsStringSync()); - final library = parse(config); - final file = File( + final configFile = + File(path.join('test', 'native_test', 'config.yaml')).absolute; + final outFile = File( path.join('test', 'debug_generated', 'native_test_bindings.dart'), - ); - library.generateFile(file); + ).absolute; + + late Config config; + late Library library; + withChDir(configFile.path, () { + config = testConfigFromPath(configFile.path); + library = parse(config); + }); + library.generateFile(outFile); try { - final actual = file.readAsStringSync().replaceAll('\r', ''); + final actual = outFile.readAsStringSync().replaceAll('\r', ''); final expected = File(path.join(config.output)) .readAsStringSync() .replaceAll('\r', ''); expect(actual, expected); - if (file.existsSync()) { - file.delete(); + if (outFile.existsSync()) { + outFile.delete(); } } catch (e) { - print('Failed test: Debug generated file: ${file.absolute.path}'); + print('Failed test: Debug generated file: ${outFile.absolute.path}'); rethrow; } }); From b7535d47008ea5fe133316e4f50a3b264962e7d0 Mon Sep 17 00:00:00 2001 From: Tzvi Melamed Date: Sun, 4 Dec 2022 23:41:02 -0500 Subject: [PATCH 5/7] temp --- example/c_json/pubspec.yaml | 2 +- example/libclang-example/pubspec.yaml | 2 +- example/shared_bindings/ffigen_configs/a_shared_base.yaml | 2 +- test/native_test/config.yaml | 4 ++-- test/native_test/native_test.dart | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/example/c_json/pubspec.yaml b/example/c_json/pubspec.yaml index 9d2d6843..1733b5fc 100644 --- a/example/c_json/pubspec.yaml +++ b/example/c_json/pubspec.yaml @@ -14,4 +14,4 @@ dependencies: dev_dependencies: ffigen: path: '../../' - lints: ^1.0.1 + lints: ^2.0.1 diff --git a/example/libclang-example/pubspec.yaml b/example/libclang-example/pubspec.yaml index 2e1d9c34..daef35bc 100644 --- a/example/libclang-example/pubspec.yaml +++ b/example/libclang-example/pubspec.yaml @@ -12,4 +12,4 @@ dependencies: dev_dependencies: ffigen: path: '../../' - lints: ^1.0.1 + lints: ^2.0.1 diff --git a/example/shared_bindings/ffigen_configs/a_shared_base.yaml b/example/shared_bindings/ffigen_configs/a_shared_base.yaml index 5873f77e..0adcc204 100644 --- a/example/shared_bindings/ffigen_configs/a_shared_base.yaml +++ b/example/shared_bindings/ffigen_configs/a_shared_base.yaml @@ -11,6 +11,6 @@ headers: import: symbol-files: # Both package Uri and file paths are supported here. - - '../lib/generated/base_symbols.yaml' + - 'package:shared_bindings/generated/base_symbols.yaml' preamble: | // ignore_for_file: non_constant_identifier_names, camel_case_types diff --git a/test/native_test/config.yaml b/test/native_test/config.yaml index 6f73a509..901e5597 100644 --- a/test/native_test/config.yaml +++ b/test/native_test/config.yaml @@ -8,10 +8,10 @@ name: NativeLibrary description: 'Native tests.' -output: './native_test_bindings.dart' +output: 'native_test_bindings.dart' headers: entry-points: - - './native_test.c' + - 'native_test.c' include-directives: - '**native_test.c' diff --git a/test/native_test/native_test.dart b/test/native_test/native_test.dart index 9fdb8281..7a19c2a2 100644 --- a/test/native_test/native_test.dart +++ b/test/native_test/native_test.dart @@ -36,11 +36,11 @@ void main() { ).absolute; late Config config; - late Library library; withChDir(configFile.path, () { config = testConfigFromPath(configFile.path); - library = parse(config); }); + final library = parse(config); + library.generateFile(outFile); try { From 7eb24ace6110bf73ef25f3a9aeb48182c4cd5a8e Mon Sep 17 00:00:00 2001 From: Tzvi Melamed Date: Thu, 6 Apr 2023 12:53:31 -0400 Subject: [PATCH 6/7] Ensure local package paths work in tests --- test/test_utils.dart | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_utils.dart b/test/test_utils.dart index f4e33a17..50cb30e3 100644 --- a/test/test_utils.dart +++ b/test/test_utils.dart @@ -8,6 +8,7 @@ import 'package:ffigen/src/code_generator.dart'; import 'package:ffigen/src/config_provider/config.dart'; import 'package:ffigen/src/strings.dart' as strings; import 'package:logging/logging.dart'; +import 'package:package_config/package_config_types.dart'; import 'package:path/path.dart' as path; import 'package:test/test.dart'; import 'package:yaml/yaml.dart' as yaml; @@ -140,6 +141,12 @@ Config testConfig(String yamlBody, {String? filename}) { return Config.fromYaml( yaml.loadYaml(yamlBody) as yaml.YamlMap, filename: filename, + packageConfig: PackageConfig([ + Package( + 'shared_bindings', + Uri.file(path.join(path.current, 'example', 'shared_bindings', 'lib/')), + ), + ]), ); } From 350535d936623a1eb769268015abed5651be50f0 Mon Sep 17 00:00:00 2001 From: Tzvi Melamed Date: Thu, 6 Apr 2023 12:59:05 -0400 Subject: [PATCH 7/7] regen + concurrency=1 to fix race conditions --- .github/workflows/test-package.yml | 6 +- .../libclang-example/generated_bindings.dart | 101 ++++++++++++++---- example/swift/swift_api.h | 2 +- 3 files changed, 84 insertions(+), 25 deletions(-) diff --git a/.github/workflows/test-package.yml b/.github/workflows/test-package.yml index e1c1ad06..8bcf6e64 100644 --- a/.github/workflows/test-package.yml +++ b/.github/workflows/test-package.yml @@ -52,7 +52,7 @@ jobs: - name: Build test dylib and bindings run: dart test/setup.dart - name: Run VM tests - run: dart test --platform vm + run: dart test --platform vm --concurrency=1 test-mac: needs: analyze @@ -67,7 +67,7 @@ jobs: - name: Build test dylib and bindings run: dart test/setup.dart - name: Run VM tests - run: dart test --platform vm + run: dart test --platform vm --concurrency=1 - name: Collect coverage run: ./tool/coverage.sh - name: Upload coverage @@ -89,7 +89,7 @@ jobs: - name: Build test dylib and bindings run: dart test/setup.dart - name: Run VM tests - run: dart test --platform vm + run: dart test --platform vm --concurrency=1 # Sanity check the latest `flutter create --template plugin_ffi`. # This will break if we change the Flutter template or the generated code. diff --git a/example/libclang-example/generated_bindings.dart b/example/libclang-example/generated_bindings.dart index a84d3959..800c5807 100644 --- a/example/libclang-example/generated_bindings.dart +++ b/example/libclang-example/generated_bindings.dart @@ -27,6 +27,50 @@ class LibClang { lookup) : _lookup = lookup; + /// Retrieve the character data associated with the given string. + ffi.Pointer clang_getCString( + CXString string, + ) { + return _clang_getCString( + string, + ); + } + + late final _clang_getCStringPtr = + _lookup>('clang_getCString'); + late final _clang_getCString = + _clang_getCStringPtr.asFunction(); + + /// Free the given string. + void clang_disposeString( + CXString string, + ) { + return _clang_disposeString( + string, + ); + } + + late final _clang_disposeStringPtr = + _lookup>( + 'clang_disposeString'); + late final _clang_disposeString = + _clang_disposeStringPtr.asFunction(); + + /// Free the given string set. + void clang_disposeStringSet( + ffi.Pointer set1, + ) { + return _clang_disposeStringSet( + set1, + ); + } + + late final _clang_disposeStringSetPtr = + _lookup>( + 'clang_disposeStringSet'); + late final _clang_disposeStringSet = + _clang_disposeStringSetPtr.asFunction(); + /// Provides a shared context for creating translation units. /// /// It provides two options: @@ -6789,6 +6833,12 @@ class LibClang { class _SymbolAddresses { final LibClang _library; _SymbolAddresses(this._library); + ffi.Pointer> + get clang_getCString => _library._clang_getCStringPtr; + ffi.Pointer> + get clang_disposeString => _library._clang_disposeStringPtr; + ffi.Pointer> + get clang_disposeStringSet => _library._clang_disposeStringSetPtr; ffi.Pointer> get clang_createIndex => _library._clang_createIndexPtr; ffi.Pointer> @@ -7591,6 +7641,36 @@ class _SymbolAddresses { get clang_Type_visitFields => _library._clang_Type_visitFieldsPtr; } +/// A character string. +/// +/// The \c CXString type is used to return strings from the interface when +/// the ownership of that string might differ from one call to the next. +/// Use \c clang_getCString() to retrieve the string data and, once finished +/// with the string data, call \c clang_disposeString() to free the string. +class CXString extends ffi.Struct { + external ffi.Pointer data; + + @ffi.UnsignedInt() + external int private_flags; +} + +class CXStringSet extends ffi.Struct { + external ffi.Pointer Strings; + + @ffi.UnsignedInt() + external int Count; +} + +typedef NativeClang_getCString = ffi.Pointer Function( + CXString string); +typedef DartClang_getCString = ffi.Pointer Function(CXString string); +typedef NativeClang_disposeString = ffi.Void Function(CXString string); +typedef DartClang_disposeString = void Function(CXString string); +typedef NativeClang_disposeStringSet = ffi.Void Function( + ffi.Pointer set1); +typedef DartClang_disposeStringSet = void Function( + ffi.Pointer set1); + class CXTargetInfoImpl extends ffi.Opaque {} class CXTranslationUnitImpl extends ffi.Opaque {} @@ -7679,19 +7759,6 @@ typedef NativeClang_CXIndex_setInvocationEmissionPathOption = ffi.Void Function( typedef DartClang_CXIndex_setInvocationEmissionPathOption = void Function( CXIndex arg0, ffi.Pointer Path); -/// A character string. -/// -/// The \c CXString type is used to return strings from the interface when -/// the ownership of that string might differ from one call to the next. -/// Use \c clang_getCString() to retrieve the string data and, once finished -/// with the string data, call \c clang_disposeString() to free the string. -class CXString extends ffi.Struct { - external ffi.Pointer data; - - @ffi.UnsignedInt() - external int private_flags; -} - /// A particular source file that is part of a translation unit. typedef CXFile = ffi.Pointer; typedef NativeClang_getFileName = CXString Function(CXFile SFile); @@ -9858,14 +9925,6 @@ typedef NativeClang_Cursor_getBriefCommentText = CXString Function(CXCursor C); typedef DartClang_Cursor_getBriefCommentText = CXString Function(CXCursor C); typedef NativeClang_Cursor_getMangling = CXString Function(CXCursor arg0); typedef DartClang_Cursor_getMangling = CXString Function(CXCursor arg0); - -class CXStringSet extends ffi.Struct { - external ffi.Pointer Strings; - - @ffi.UnsignedInt() - external int Count; -} - typedef NativeClang_Cursor_getCXXManglings = ffi.Pointer Function( CXCursor arg0); typedef DartClang_Cursor_getCXXManglings = ffi.Pointer Function( diff --git a/example/swift/swift_api.h b/example/swift/swift_api.h index 6a42130b..0928c492 100644 --- a/example/swift/swift_api.h +++ b/example/swift/swift_api.h @@ -1,4 +1,4 @@ -// Generated by Apple Swift version 5.7 (swiftlang-5.7.0.127.4 clang-1400.0.29.50) +// Generated by Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51) #ifndef SWIFT_MODULE_SWIFT_H #define SWIFT_MODULE_SWIFT_H #pragma clang diagnostic push