Skip to content

Commit

Permalink
Add test setup and test for type utils (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitusortner authored Feb 7, 2019
1 parent cadce2a commit e2eeb90
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
96 changes: 96 additions & 0 deletions floor_generator/test/misc/type_utils_test.dart
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);
});
});
}
34 changes: 34 additions & 0 deletions floor_generator/test/test_utils.dart
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;
});
}

0 comments on commit e2eeb90

Please sign in to comment.