Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/src/cli/utils/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Constants {
static const String KEY_PROJECT_NAME = '[PROJECT_NAME]';
static const String KEY_PACKAGE = '[PACKAGE]';
static const String TEST_IMPORT = '[TEST_IMPORT]';
static const String TEST_IMPORTS = '[TEST_IMPORTS]';
static const String KEY_FILE_NAME = '[FILE_NAME]';
static const String KEY_TESTS = '[TESTS]';
static const String KEY_CLASS_NAME = '[CLASS_NAME]';
Expand Down
11 changes: 9 additions & 2 deletions lib/src/data/test_template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ String get timeStampTemplate => '/// Generated by spider on [TIME]\n\n';
/// A template for generating test files.
String get testTemplate => '''import 'dart:io';

import 'package:[TEST_IMPORT]/[TEST_IMPORT].dart';
import 'package:[PROJECT_NAME]/[PACKAGE]/[IMPORT_FILE_NAME]';
[TEST_IMPORTS]

void main() {
test('[FILE_NAME] assets test', () {
[TESTS]
});
}''';

/// A template for importing test package
String get testPackageImport =>
"import 'package:[TEST_IMPORT]/[TEST_IMPORT].dart';";

/// A template for importing project import file
String get projectPackageImport =>
"import 'package:[PROJECT_NAME]/[PACKAGE]/[IMPORT_FILE_NAME]';";

/// A template for generating single test expect statement.
String get expectTestTemplate =>
'expect(File([CLASS_NAME].[ASSET_NAME]).existsSync(), isTrue);';
Expand Down
13 changes: 9 additions & 4 deletions lib/src/generation_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,23 @@ String getTestClass({
required String importFileName,
required String testImport,
}) {
var importsContent = [
testPackageImport.replaceAll(Constants.TEST_IMPORT, testImport),
projectPackageImport
.replaceAll(Constants.KEY_PROJECT_NAME, project)
.replaceAll(Constants.KEY_PACKAGE, package)
.replaceAll(Constants.KEY_IMPORT_FILE_NAME, importFileName)
]..sort();

var content = '';
if (!noComments) {
content += timeStampTemplate.replaceAll(
Constants.KEY_TIME, DateTime.now().toString());
}
content += testTemplate
.replaceAll(Constants.KEY_PROJECT_NAME, project)
.replaceAll(Constants.KEY_PACKAGE, package)
.replaceAll(Constants.KEY_FILE_NAME, fileName)
.replaceAll(Constants.KEY_IMPORT_FILE_NAME, importFileName)
.replaceAll(Constants.KEY_TESTS, tests)
.replaceAll(Constants.TEST_IMPORT, testImport);
.replaceAll(Constants.TEST_IMPORTS, importsContent.join('\n'));
return content;
}

Expand Down