Skip to content

Commit

Permalink
test: 100% coverage on TypeCheck class
Browse files Browse the repository at this point in the history
  • Loading branch information
ali77gh committed Mar 19, 2023
1 parent d316672 commit c9b9c9a
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 17 deletions.
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:
shared_preferences: ^2.0.15

dev_dependencies:
test: ^1.14.4
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
Expand Down
22 changes: 5 additions & 17 deletions test/telescope_test.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
// void main() {
// test('adds one to input values', () {
// runApp(MyApp());
// });
// }
// class MyApp extends StatelessWidget {
//
// @override
// Widget build(BuildContext context) {
//
// return MaterialApp(
// theme: ThemeData(fontFamily: 'IranSans'),
// debugShowCheckedModeBanner: false,
// home: MainLayout()
// );
// }
// }

// Unit tests are here
// Integration tests are in /examples

void main() {}
134 changes: 134 additions & 0 deletions test/type_check_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import 'package:flutter/material.dart';
import 'package:telescope/src/telescope.dart';
import 'package:telescope/src/type_check.dart';
import 'package:test/test.dart';


void main(){
typeCheckBuiltIn();
typeCheckNonBuiltIn();
implementsHashCodeTest();
checkValidTypeTest();
checkValidTypeItemsTest();
}

void typeCheckBuiltIn(){
group("TypeCheck built-in", (){

test("string", () {
expect(TypeCheck.isBuiltIn<String>(), true);
});

test("bool", () {
expect(TypeCheck.isBuiltIn<bool>(), true);
});

test("double", () {
expect(TypeCheck.isBuiltIn<double>(), true);
});

test("int", () {
expect(TypeCheck.isBuiltIn<int>(), true);
});
});
}

void typeCheckNonBuiltIn(){
group("TypeCheck not built-in", (){

test("Widget", () {
expect(TypeCheck.isBuiltIn<Widget>(), false);
});

test("Telescope", () {
expect(TypeCheck.isBuiltIn<Telescope>(), false);
});

test("StatefulWidget", () {
expect(TypeCheck.isBuiltIn<StatefulWidget>(), false);
});

test("StatelessWidget", () {
expect(TypeCheck.isBuiltIn<StatelessWidget>(), false);
});
});
}


class Human{
int age=18;
}
class HashCodeHuman{
int age=18;
@override
int get hashCode=>age.hashCode;
}
void implementsHashCodeTest(){
group("implements hash code", (){

test("null", (){
expect(TypeCheck.implementsHashCodeProperty<String?>(null), true);
});

test("human", (){
expect(TypeCheck.implementsHashCodeProperty(Human()), false);
});

test("hash code human", (){
expect(TypeCheck.implementsHashCodeProperty(HashCodeHuman()), true);
});

});
}

void checkValidTypeTest(){
group("check valid type test", (){

test("supported String", (){
expect(TypeCheck.checkIsValidType<String>("string", false), null);
});

test("supported Human", (){
expect(TypeCheck.checkIsValidType<HashCodeHuman>(HashCodeHuman(), false), null);
});

test("not supported Human", (){
expect((){
TypeCheck.checkIsValidType<Human>(Human(), false);
}, throwsA(TypeMatcher()));
});

test("not supported Human iWillCallNotifyAll", (){
expect(TypeCheck.checkIsValidType<Human>(Human(), true), null);
});

});
}

void checkValidTypeItemsTest(){

group("check valid type items test", (){

test("supported empty list", (){
expect(TypeCheck.checkIsValidTypeForItems([], false), null);
});

test("supported String list", (){
expect(TypeCheck.checkIsValidTypeForItems(["1","2"], false), null);
});

test("supported Human list", (){
expect(TypeCheck.checkIsValidTypeForItems([HashCodeHuman(),HashCodeHuman()], false), null);
});

test("not supported Human", (){
expect((){
TypeCheck.checkIsValidTypeForItems([Human(),Human()], false);
}, throwsA(TypeMatcher()));
});

test("not supported Human list iWillCallNotifyAll", (){
expect(TypeCheck.checkIsValidTypeForItems([Human(),Human()], true), null);
});
});
}

0 comments on commit c9b9c9a

Please sign in to comment.