22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5- import 'package:jnigen/jnigen.dart' ;
5+ import 'package:jnigen/jnigen.dart' as jnigen ;
66import 'package:jnigen/src/bindings/linker.dart' ;
77import 'package:jnigen/src/bindings/renamer.dart' ;
88import 'package:jnigen/src/elements/elements.dart' as ast;
@@ -13,6 +13,10 @@ extension on Iterable<ast.Method> {
1313 List <String > get finalNames => map ((m) => m.finalName).toList ();
1414}
1515
16+ extension on Iterable <ast.Param > {
17+ List <String > get finalNames => map ((p) => p.finalName).toList ();
18+ }
19+
1620extension on Iterable <ast.Field > {
1721 List <String > get finalNames => map ((f) => f.finalName).toList ();
1822}
@@ -28,42 +32,43 @@ class UserRenamer extends Visitor {
2832
2933 @override
3034 void visitMethod (Method method) {
31- if (method.name == 'Foo' ) {
32- method.newName = ' Bar' ;
35+ if (method.name. contains ( 'Foo' ) ) {
36+ method.newName = method.name. replaceAll ( 'Foo' , ' Bar') ;
3337 }
3438 }
3539
3640 @override
3741 void visitField (Field field) {
38- if (field.name == 'Foo' ) {
39- field.newName = ' Bar' ;
42+ if (field.name. contains ( 'Foo' ) ) {
43+ field.newName = field.name. replaceAll ( 'Foo' , ' Bar') ;
4044 }
4145 }
4246
4347 @override
4448 void visitParam (Param parameter) {
45- if (parameter.name == 'Foo' ) {
46- parameter.newName = ' Bar' ;
49+ if (parameter.name. contains ( 'Foo' ) ) {
50+ parameter.newName = parameter.name. replaceAll ( 'Foo' , ' Bar') ;
4751 }
4852 }
4953}
5054
5155Future <void > rename (ast.Classes classes) async {
52- final config = Config (
53- outputConfig: OutputConfig (
54- dartConfig: DartCodeOutputConfig (
55- path: Uri .file ('test.dart' ),
56- structure: OutputStructure .singleFile,
56+ final config = jnigen.Config (
57+ outputConfig: jnigen.OutputConfig (
58+ dartConfig: jnigen.DartCodeOutputConfig (
59+ path: Uri .file ('test.dart' ),
60+ structure: jnigen.OutputStructure .singleFile,
61+ ),
5762 ),
58- ),
59- classes: [],
60- );
63+ classes: []
64+ );
6165 await classes.accept (Linker (config));
6266 classes.accept (Renamer (config));
6367}
6468
6569void main () {
66- test ('Exclude something using the user excluder, Simple AST' , () async {
70+ test ('rename classes, fields, methods and params using user visitors' ,
71+ () async {
6772 final classes = ast.Classes ({
6873 'Foo' : ast.ClassDecl (
6974 binaryName: 'Foo' ,
@@ -83,31 +88,33 @@ void main() {
8388 ],
8489 ),
8590 'y.Foo' : ast.ClassDecl (
86- binaryName: 'y.Foo' ,
87- declKind: ast.DeclKind .classKind,
88- superclass: ast.TypeUsage .object,
89- methods: [
90- ast.Method (
91- name: 'Foo' ,
92- returnType: ast.TypeUsage .object,
93- params: [
94- ast.Param (name: 'Foo' , type: ast.TypeUsage .object),
95- ast.Param (name: 'Foo1' , type: ast.TypeUsage .object),
96- ]
97- ),
98- ],
99- ),
91+ binaryName: 'y.Foo' ,
92+ declKind: ast.DeclKind .classKind,
93+ superclass: ast.TypeUsage .object,
94+ methods: [
95+ ast.Method (name: 'Foo' , returnType: ast.TypeUsage .object, params: [
96+ ast.Param (name: 'Foo' , type: ast.TypeUsage .object),
97+ ast.Param (name: 'Foo1' , type: ast.TypeUsage .object),
98+ ]),
99+ ],
100+ ),
100101 });
101102
102103 final simpleClasses = Classes (classes);
103104 simpleClasses.accept (UserRenamer ());
104105
106+ await rename (classes);
107+
105108 expect (classes.decls['y.Foo' ]? .finalName, 'y.Bar' );
106109 expect (classes.decls['Foo' ]? .finalName, 'Bar' );
107110
108111 expect (classes.decls['Foo' ]? .fields.finalNames,
109112 ['Bar' , r'Bar$1' , 'Bar1' , r'Bar1$1' ]);
113+
110114 expect (classes.decls['Foo' ]? .methods.finalNames,
111115 [r'Bar$2' , r'Bar$3' , r'Bar1$2' , r'Bar1$3' ]);
116+
117+ expect (classes.decls['y.Foo' ]? .methods.first.params.finalNames,
118+ ['Bar' , 'Bar1' ]);
112119 });
113120}
0 commit comments