Skip to content

Commit

Permalink
Add unit tests for parsing function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
FirentisTFW committed Nov 9, 2023
1 parent 645fb64 commit d2f4e42
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/test/dart_class_parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,45 @@ describe("parseLinesToClassFields", () => {

expect(parseLinesToClassFields(lines)).toEqual([]);
});

test("with fields that are functions, returns them", () => {
const lines = [
"class Button extends StatelessWidget {",
" final void Function() fun1;",
" final CustomClass Function(_) fun2;",
" final CustomClass Function(CustomClass param1, CustomClass param2)? fun3;",
" final (CustomClass, CustomClass2) Function(CustomClass param1, param2)? fun4;",
"",
" const Button({",
" super.key,",
" required this.fun1,",
" required this.fun2,",
" this.fun3,",
" this.fun4,",
" });",
"",
" @override",
" Widget build(BuildContext context) {",
" return const SizedBox.shrink();",
" }",
"}",
];

expect(parseLinesToClassFields(lines)).toEqual([
new DartClassField("fun1", "void Function()"),
new DartClassField("fun2", "CustomClass Function(_)"),
new DartClassField(
"fun3",
"CustomClass Function(CustomClass param1, CustomClass param2)",
true
),
new DartClassField(
"fun4",
"(CustomClass, CustomClass2) Function(CustomClass param1, param2)",
true
),
]);
});
});
describe("when passed a StatefulWidget", () => {
test("with fields after constructor, returns them", () => {
Expand Down
1 change: 0 additions & 1 deletion src/util/dart_class_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ function parseLinesToClassFields(input: Array<string>): Array<DartClassField> {
.filter((line) => line !== "");

return fieldListLines.map((line) => {
// FIXME Add unit tests for functions with multiple parameters
const lineParts = line.trim().split(" ").slice(1);
const name = lineParts.pop()?.replace(";", "") ?? "";
const type = lineParts.join(" ");
Expand Down

0 comments on commit d2f4e42

Please sign in to comment.