-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test setup and test for type utils (#26)
- Loading branch information
1 parent
cadce2a
commit e2eeb90
Showing
2 changed files
with
130 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import 'package:floor_generator/misc/type_utils.dart' as type_utils; | ||
import 'package:test/test.dart'; | ||
|
||
import '../test_utils.dart'; | ||
|
||
void main() { | ||
group('assert type', () { | ||
test('is string', () async { | ||
final type = await getDartType("'123'"); | ||
|
||
final actual = type_utils.isString(type); | ||
|
||
expect(actual, isTrue); | ||
}); | ||
|
||
test('is not string', () async { | ||
final type = await getDartType(1); | ||
|
||
final actual = type_utils.isString(type); | ||
|
||
expect(actual, isFalse); | ||
}); | ||
|
||
test('is bool', () async { | ||
final type = await getDartType(true); | ||
|
||
final actual = type_utils.isBool(type); | ||
|
||
expect(actual, isTrue); | ||
}); | ||
|
||
test('is not bool', () async { | ||
final type = await getDartType(1); | ||
|
||
final actual = type_utils.isBool(type); | ||
|
||
expect(actual, isFalse); | ||
}); | ||
|
||
test('is int', () async { | ||
final type = await getDartType(1); | ||
|
||
final actual = type_utils.isInt(type); | ||
|
||
expect(actual, isTrue); | ||
}); | ||
|
||
test('is not int', () async { | ||
final type = await getDartType(1.1); | ||
|
||
final actual = type_utils.isInt(type); | ||
|
||
expect(actual, isFalse); | ||
}); | ||
|
||
test('is double', () async { | ||
final type = await getDartType(1.1); | ||
|
||
final actual = type_utils.isDouble(type); | ||
|
||
expect(actual, isTrue); | ||
}); | ||
|
||
test('is not double', () async { | ||
final type = await getDartType(1); | ||
|
||
final actual = type_utils.isDouble(type); | ||
|
||
expect(actual, isFalse); | ||
}); | ||
|
||
test('is list', () async { | ||
final type = await getDartType([1, 2, 3]); | ||
|
||
final actual = type_utils.isList(type); | ||
|
||
expect(actual, isTrue); | ||
}); | ||
|
||
test('is not list', () async { | ||
final type = await getDartType(1); | ||
|
||
final actual = type_utils.isList(type); | ||
|
||
expect(actual, isFalse); | ||
}); | ||
|
||
test('is supported type', () async { | ||
final type = await getDartType(1); | ||
|
||
final actual = type_utils.isSupportedType(type); | ||
|
||
expect(actual, isTrue); | ||
}); | ||
}); | ||
} |
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,34 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:analyzer/dart/element/type.dart'; | ||
import 'package:build/build.dart'; | ||
import 'package:build_test/build_test.dart'; | ||
import 'package:path/path.dart' as path; | ||
import 'package:source_gen/source_gen.dart'; | ||
|
||
/// Creates a [LibraryReader] of the [sourceFile]. | ||
Future<LibraryReader> resolveCompilationUnit(String sourceFile) async { | ||
final files = [File(sourceFile)]; | ||
|
||
final fileMap = Map<String, String>.fromEntries(files.map( | ||
(f) => MapEntry('a|lib/${path.basename(f.path)}', f.readAsStringSync()))); | ||
|
||
final library = await resolveSources(fileMap, (item) async { | ||
final assetId = AssetId.parse(fileMap.keys.first); | ||
return item.libraryFor(assetId); | ||
}); | ||
|
||
return LibraryReader(library); | ||
} | ||
|
||
Future<DartType> getDartType(dynamic value) async { | ||
final source = ''' | ||
library test; | ||
final value = $value; | ||
'''; | ||
return resolveSource(source, (item) async { | ||
final libraryReader = LibraryReader(await item.findLibraryByName('test')); | ||
return (libraryReader.allElements.elementAt(1) as VariableElement).type; | ||
}); | ||
} |