|
| 1 | +// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:ffigen/ffigen.dart'; |
| 8 | +import 'package:ffigen/src/code_generator.dart' as cg; |
| 9 | +import 'package:ffigen/src/header_parser.dart' as parser; |
| 10 | +import 'package:test/test.dart'; |
| 11 | + |
| 12 | +import '../test_utils.dart'; |
| 13 | + |
| 14 | +const _cyclicClasses = {'Node', 'Merger', 'Owner', 'Leaf', 'Branch'}; |
| 15 | + |
| 16 | +void main() { |
| 17 | + group('cpp_cyclic_class', () { |
| 18 | + late cg.Library library; |
| 19 | + |
| 20 | + setUpAll(() { |
| 21 | + library = parser.parse( |
| 22 | + testContext( |
| 23 | + FfiGenerator( |
| 24 | + output: Output(dart: DartOutput(path: Uri.file('unused'))), |
| 25 | + input: Input( |
| 26 | + entryPoints: [ |
| 27 | + Uri.file( |
| 28 | + absPath('test/header_parser_tests/cpp_cyclic_class.h'), |
| 29 | + ), |
| 30 | + ], |
| 31 | + compilerOptions: [ |
| 32 | + '-x', |
| 33 | + 'c++', |
| 34 | + '-std=c++17', |
| 35 | + if (Platform.isMacOS) ...['-isysroot', macSdkPath], |
| 36 | + ], |
| 37 | + ), |
| 38 | + cpp: const Cpp(), |
| 39 | + visitors: [ |
| 40 | + Visitor( |
| 41 | + cppClass: (node) => node.isIncluded = _cyclicClasses.contains( |
| 42 | + node.originalName, |
| 43 | + ), |
| 44 | + ), |
| 45 | + ], |
| 46 | + ), |
| 47 | + ), |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + test('classes that name themselves are parsed exactly once', () { |
| 52 | + final names = |
| 53 | + library.bindings |
| 54 | + .whereType<cg.CppClass>() |
| 55 | + .map((c) => c.originalName) |
| 56 | + .toList() |
| 57 | + ..sort(); |
| 58 | + |
| 59 | + // Every class of the header, once each: a member type that closes a cycle |
| 60 | + // must resolve to the class already being parsed, not to a second copy of |
| 61 | + // it. |
| 62 | + expect(names, _cyclicClasses.toList()..sort()); |
| 63 | + }); |
| 64 | + |
| 65 | + test('a cyclic member resolves to the class already being parsed', () { |
| 66 | + cg.CppClass classNamed(String name) => library.bindings |
| 67 | + .whereType<cg.CppClass>() |
| 68 | + .firstWhere((c) => c.originalName == name); |
| 69 | + |
| 70 | + cg.CppClass returnedClass(cg.CppClass cls, String methodName) { |
| 71 | + final method = cls.methods.firstWhere( |
| 72 | + (m) => m.originalName == methodName, |
| 73 | + orElse: () => fail('${cls.originalName} has no method $methodName'), |
| 74 | + ); |
| 75 | + expect(method.returnType, isA<cg.CppClassPointerType>()); |
| 76 | + return (method.returnType as cg.CppClassPointerType).cppClass; |
| 77 | + } |
| 78 | + |
| 79 | + final node = classNamed('Node'); |
| 80 | + final leaf = classNamed('Leaf'); |
| 81 | + final branch = classNamed('Branch'); |
| 82 | + |
| 83 | + expect( |
| 84 | + returnedClass(node, 'clone'), |
| 85 | + same(node), |
| 86 | + reason: 'a cyclic return type must not be a second copy of the class', |
| 87 | + ); |
| 88 | + expect(returnedClass(leaf, 'parent'), same(branch)); |
| 89 | + expect(returnedClass(branch, 'firstLeaf'), same(leaf)); |
| 90 | + }); |
| 91 | + }); |
| 92 | +} |
0 commit comments