|
| 1 | +// Copyright (c) 2024, 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 'package:jnigen/src/elements/elements.dart' as ast; |
| 6 | +import 'package:jnigen/src/elements/j_elements.dart'; |
| 7 | +import 'package:test/test.dart'; |
| 8 | + |
| 9 | +extension on Iterable<ast.Method> { |
| 10 | + List<bool> get isExcludedValues => map((c) => c.isExcluded).toList(); |
| 11 | +} |
| 12 | + |
| 13 | +extension on Iterable<ast.Field> { |
| 14 | + List<bool> get isExcludedValues => map((c) => c.isExcluded).toList(); |
| 15 | +} |
| 16 | + |
| 17 | +// This is customizable by the user |
| 18 | +class UserExcluder extends Visitor { |
| 19 | + @override |
| 20 | + void visitClass(ClassDecl c) { |
| 21 | + if (c.binaryName.contains('y')) { |
| 22 | + c.isExcluded = true; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + @override |
| 27 | + void visitMethod(Method method) { |
| 28 | + if (method.name == 'Bar') { |
| 29 | + method.isExcluded = true; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + @override |
| 34 | + void visitField(Field field) { |
| 35 | + if (field.name == 'Bar') { |
| 36 | + field.isExcluded = true; |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +void main() { |
| 42 | + test('Exclude something using the user excluder, Simple AST', () async { |
| 43 | + final classes = ast.Classes({ |
| 44 | + 'Foo': ast.ClassDecl( |
| 45 | + binaryName: 'Foo', |
| 46 | + declKind: ast.DeclKind.classKind, |
| 47 | + superclass: ast.TypeUsage.object, |
| 48 | + methods: [ |
| 49 | + ast.Method(name: 'foo', returnType: ast.TypeUsage.object), |
| 50 | + ast.Method(name: 'Bar', returnType: ast.TypeUsage.object), |
| 51 | + ast.Method(name: 'foo1', returnType: ast.TypeUsage.object), |
| 52 | + ast.Method(name: 'Bar', returnType: ast.TypeUsage.object), |
| 53 | + ], |
| 54 | + fields: [ |
| 55 | + ast.Field(name: 'foo', type: ast.TypeUsage.object), |
| 56 | + ast.Field(name: 'Bar', type: ast.TypeUsage.object), |
| 57 | + ast.Field(name: 'foo1', type: ast.TypeUsage.object), |
| 58 | + ast.Field(name: 'Bar', type: ast.TypeUsage.object), |
| 59 | + ], |
| 60 | + ), |
| 61 | + 'y.Foo': ast.ClassDecl( |
| 62 | + binaryName: 'y.Foo', |
| 63 | + declKind: ast.DeclKind.classKind, |
| 64 | + superclass: ast.TypeUsage.object, |
| 65 | + methods: [ |
| 66 | + ast.Method(name: 'foo', returnType: ast.TypeUsage.object), |
| 67 | + ast.Method(name: 'Bar', returnType: ast.TypeUsage.object), |
| 68 | + ], |
| 69 | + fields: [ |
| 70 | + ast.Field(name: 'foo', type: ast.TypeUsage.object), |
| 71 | + ast.Field(name: 'Bar', type: ast.TypeUsage.object), |
| 72 | + ]), |
| 73 | + }); |
| 74 | + |
| 75 | + final simpleClasses = Classes(classes); |
| 76 | + simpleClasses.accept(UserExcluder()); |
| 77 | + |
| 78 | + expect(classes.decls['y.Foo']?.isExcluded, true); |
| 79 | + expect(classes.decls['Foo']?.isExcluded, false); |
| 80 | + |
| 81 | + expect(classes.decls['Foo']?.fields.isExcludedValues, |
| 82 | + [false, true, false, true]); |
| 83 | + expect(classes.decls['Foo']?.methods.isExcludedValues, |
| 84 | + [false, true, false, true]); |
| 85 | + }); |
| 86 | +} |
0 commit comments