Skip to content

Commit e820406

Browse files
[native_doc_dartifier] Add more end-to-end tests (#2420)
* The initialization and call to an inner Java class inside some outer class. * The use of strings correctly using toJString() and toDartString() * The use of identifiers that start with '_' * The use of identifiers that have some '$'
1 parent 7788b63 commit e820406

File tree

9 files changed

+127
-3
lines changed

9 files changed

+127
-3
lines changed

.github/workflows/native_doc_dartifier.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,15 @@ jobs:
4040
- name: Analyze code
4141
run: dart analyze --fatal-infos
4242
if: always() && steps.install.outcome == 'success'
43-
- name: Run pre-test tool
44-
run: dart run tool/prepare_dartify_test.dart
43+
- name: Compile Java code
44+
run: dart run tool/compile_java.dart
4545
if: always() && steps.install.outcome == 'success'
4646
- name: Run VM tests
4747
run: dart test --platform vm
4848
if: always() && steps.install.outcome == 'success'
49+
- name: Regenerate bindings and dartified snippets
50+
run: dart run tool/prepare_dartify_test.dart
51+
if: always() && steps.install.outcome == 'success'
52+
- name: Run VM tests again
53+
run: dart test --platform vm
54+
if: always() && steps.install.outcome == 'success'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// AUTO GENERATED BY NATIVE_DOC_DARTIFIER. DO NOT EDIT!
2+
// Regenerate this file by running tool/prepare_dartify_test.dart
3+
4+
import '../bindings.dart';
5+
6+
bool identifiersSpecialCases() =>
7+
Example.has$$dollar$$sign + Example.$_startsWithUnderscore == 3;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// AUTO GENERATED BY NATIVE_DOC_DARTIFIER. DO NOT EDIT!
2+
// Regenerate this file by running tool/prepare_dartify_test.dart
3+
4+
import '../bindings.dart';
5+
6+
bool innerClassCall() {
7+
final acc1 = Accumulator$DoublingAccumulator(Accumulator());
8+
acc1.add(10);
9+
acc1.add$1(10, 10);
10+
acc1.add$2(10, 10, 10);
11+
12+
return acc1.accumulator == 120;
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// AUTO GENERATED BY NATIVE_DOC_DARTIFIER. DO NOT EDIT!
2+
// Regenerate this file by running tool/prepare_dartify_test.dart
3+
4+
import 'package:jni/jni.dart';
5+
import '../bindings.dart';
6+
7+
bool backAndForthStrings() {
8+
final name = 'World'.toJString();
9+
final example = Example();
10+
final greeting = example.greet(name)?.toDartString();
11+
print(greeting);
12+
return greeting == 'Hello World';
13+
}

pkgs/native_doc_dartifier/test/dartify_simple_cases/java/com/Accumulator.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
package com;
22

33
public class Accumulator {
4+
5+
public class DoublingAccumulator {
6+
public int accumulator;
7+
8+
public DoublingAccumulator() {
9+
this.accumulator = 0;
10+
}
11+
12+
public void add(int value) {
13+
this.accumulator += value * 2;
14+
}
15+
16+
public void add(int value1, int value2) {
17+
this.accumulator += (value1 + value2) * 2;
18+
}
19+
20+
public void add(int value1, int value2, int value3) {
21+
this.accumulator += (value1 + value2 + value3) * 2;
22+
}
23+
}
24+
425
public int accumulator;
526

627
public Accumulator() {

pkgs/native_doc_dartifier/test/dartify_simple_cases/java_snippets.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,36 @@ Boolean overloadedMethods() {
2121
},
2222
{
2323
'code': '''
24+
Boolean innerClassCall() {
25+
DoublingAccumulator acc1 = new DoublingAccumulator();
26+
acc1.add(10);
27+
acc1.add(10, 10);
28+
acc1.add(10, 10, 10);
29+
30+
return acc1.accumulator == 120;
31+
}''',
32+
'fileName': 'inner_class.dart',
33+
},
34+
{
35+
'code': '''
36+
Boolean backAndForthStrings(){
37+
String name = "World";
38+
Example example = new Example();
39+
String greeting = example.greet(name);
40+
System.out.println(greeting);
41+
return greeting == "Hello World";
42+
}''',
43+
'fileName': 'strings.dart',
44+
},
45+
{
46+
'code': '''
47+
Boolean identifiersSpecialCases(){
48+
return Example.has\$dollar\$sign() + Example._startsWithUnderscore() == 3;
49+
}''',
50+
'fileName': 'identifiers.dart',
51+
},
52+
{
53+
'code': '''
2454
Boolean useEnums() {
2555
Example example = new Example();
2656
Boolean isTrueUsage = example.enumValueToString(Operation.ADD) == "Addition";

pkgs/native_doc_dartifier/test/dartify_simple_cases/runtime_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import 'package:jni/jni.dart';
88
import 'package:test/test.dart';
99

1010
import 'dartified_snippets/enums.dart';
11+
import 'dartified_snippets/identifiers.dart';
1112
import 'dartified_snippets/implement_inline_interface.dart';
1213
import 'dartified_snippets/implement_normal_interface.dart';
14+
import 'dartified_snippets/inner_class.dart';
1315
import 'dartified_snippets/overloaded_methods.dart';
16+
import 'dartified_snippets/strings.dart';
1417

1518
void main() {
1619
setUpAll(() {
@@ -32,6 +35,18 @@ void main() {
3235
expect(overloadedMethods(), isTrue);
3336
});
3437

38+
test('Inner Class Call', () async {
39+
expect(innerClassCall(), isTrue);
40+
});
41+
42+
test('Back and Forth Strings', () async {
43+
expect(backAndForthStrings(), isTrue);
44+
});
45+
46+
test('identifiers has \$ and starts with "_"', () async {
47+
expect(identifiersSpecialCases(), isTrue);
48+
});
49+
3550
test('Implement Inline Interface', () async {
3651
expect(implementInlineInterface(), isTrue);
3752
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import 'prepare_dartify_test.dart';
2+
3+
void main() {
4+
print('Compiling Java package...');
5+
compileJavaPackage();
6+
}

pkgs/native_doc_dartifier/tool/prepare_dartify_test.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,20 @@ void generateDartSnippets() async {
6868
if (!outputFile.parent.existsSync()) {
6969
outputFile.parent.createSync(recursive: true);
7070
}
71-
outputFile.writeAsStringSync('import \'../bindings.dart\';\n\n$dartCode');
71+
final content = '''
72+
// AUTO GENERATED BY NATIVE_DOC_DARTIFIER. DO NOT EDIT!
73+
// Regenerate this file by running tool/prepare_dartify_test.dart
74+
75+
import 'package:jni/jni.dart';
76+
import '../bindings.dart';
77+
78+
$dartCode
79+
''';
80+
outputFile.writeAsStringSync(content);
81+
82+
Process.runSync('dart', ['fix', '--apply', outputFile.absolute.path]);
83+
84+
Process.runSync('dart', ['format', outputFile.absolute.path]);
7285
}
7386
}
7487

0 commit comments

Comments
 (0)