-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
316 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import 'package:checks/checks.dart'; | ||
import 'package:code_builder/code_builder.dart'; | ||
import 'package:file/memory.dart'; | ||
import 'package:test/test.dart'; | ||
import 'package:you_cli/src/cli_core.dart'; | ||
|
||
import '../../bin/cli.dart'; | ||
|
||
void main() { | ||
late MemoryFileSystem fs; | ||
late YouCli cli; | ||
|
||
setUp(() { | ||
fs = MemoryFileSystem(); | ||
cli = YouCli(projectDir: fs.directory("/app")); | ||
|
||
// YouCli需要 | ||
fs.file("/app/pubspec.yaml") | ||
..createSync(recursive: true) | ||
..writeAsString("""name: app"""); | ||
}); | ||
group("Cmd_gen_routes_g_dart", () { | ||
void checkBuilder(String? expected, {required bool async}) { | ||
Cmd_gen_routes_g_dart cmd = Cmd_gen_routes_g_dart.libMode(fs: fs, async: async, dir: fs.directory("/app/lib/routes")); | ||
var result = cmd.builderExpression(cli.rootRoute); | ||
if (expected == null) { | ||
check(result).isNull(); | ||
} else { | ||
check(cmd.builderExpression(cli.rootRoute)!.accept(DartEmitter()).toString().split("\n").join()).equals(expected); | ||
} | ||
} | ||
|
||
test('builderExpression: no page + no layout', () { | ||
checkBuilder(async: false, null); | ||
checkBuilder(async: true, null); | ||
}); | ||
|
||
test('builderExpression: page + no layout', () { | ||
fs.file("/app/lib/routes/page.dart").createSync(recursive: true); | ||
|
||
checkBuilder(async: false, "root_.build"); | ||
checkBuilder(async: true, "() async { await root_.loadLibrary();return root_.build; } "); | ||
}); | ||
|
||
test('builderExpression: page + layout', () { | ||
fs.file("/app/lib/routes/page.dart").createSync(recursive: true); | ||
fs.file("/app/lib/routes/layout.dart").createSync(recursive: true); | ||
|
||
checkBuilder(async: false, "root__.layout(root_.build)"); | ||
checkBuilder(async: true, "() async { await root_.loadLibrary(); await root__.loadLibrary();return root__.layout(root_.build); } "); | ||
}); | ||
}); | ||
} | ||
|
||
Expression genRouteExpression(RouteNode rootPage) { | ||
final $To = refer('To', 'package:you_flutter/router.dart'); | ||
return $To.newInstance([ | ||
literalString(rootPage.dir.basename) | ||
], { | ||
"builderAsync": refer(rootPage.flatName).property('build'), | ||
// "children": literalList(rootPage.children.map((e)=>createRoutes(e))), | ||
"children": literalList(rootPage.children.map((e) => genRouteExpression(e)).toList()), | ||
}); | ||
} | ||
|
||
String genRouteString(RouteNode rootPage) { | ||
if (rootPage.children.isEmpty) { | ||
return '''To("${rootPage.dir.basename}", builderAsync: ${rootPage.flatName}.build)'''; | ||
} | ||
return '''To("${rootPage.dir.basename}", builderAsync: ${rootPage.flatName}.build, children:[ | ||
${rootPage.children.map((e) => genRouteString(e)).join(",")} | ||
])'''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'package:checks/checks.dart'; | ||
import 'package:file/memory.dart'; | ||
import 'package:test/test.dart'; | ||
import 'package:you_cli/src/cli_core.dart'; | ||
|
||
void main() { | ||
group("RouteNode", () { | ||
test('create from directory', () { | ||
late MemoryFileSystem fs = MemoryFileSystem(); | ||
fs.directory("/note/lib/routes/notes/page_1").createSync(recursive: true); | ||
fs.directory("/note/lib/routes/notes/page_1/page_1_1").createSync(recursive: true); | ||
fs.directory("/note/lib/routes/notes/page_1/page_1_2").createSync(recursive: true); | ||
|
||
YouCli cli = YouCli(projectDir: fs.directory("/note")); | ||
check(cli.rootRoute.toList().map((e) => e.routePath)).deepEquals([ | ||
"/", | ||
"/notes", | ||
"/notes/page_1", | ||
"/notes/page_1/page_1_1", | ||
"/notes/page_1/page_1_2", | ||
]); | ||
}); | ||
}); | ||
|
||
} |
Oops, something went wrong.