Skip to content

Commit cf59796

Browse files
committed
feature: added unit tests and functionality for login use case
1 parent 6114aff commit cf59796

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

lib/validator.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Validator {
2+
static String? validateEmail(String email) {
3+
if (email.isEmpty) {
4+
return "Required field";
5+
}
6+
7+
//check for a valid email with a Regular Expression
8+
const String emailRegexPattern =
9+
r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9\w-]+\.[a-zA-Z]+";
10+
11+
RegExp regExp = RegExp(emailRegexPattern);
12+
13+
if (!regExp.hasMatch(email)) {
14+
return "Please enter a valid email";
15+
}
16+
return null;
17+
}
18+
19+
static String? validatePassword(String password) {
20+
if (password.isEmpty) {
21+
return "Required field";
22+
}
23+
24+
if (password.length < 8) {
25+
return "Password should be a minimum of 8 characters";
26+
}
27+
28+
return null;
29+
}
30+
}

test/validator_test.dart

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:test_driven_development/validator.dart';
3+
4+
void main() {
5+
group("Validator -", () {
6+
test("validate for empty email", () {
7+
//ARRANGE
8+
String email = '';
9+
10+
//ACT
11+
String? result = Validator.validateEmail(email);
12+
13+
//ASSERT
14+
expect(result, "Required field");
15+
});
16+
17+
test("validate for invalid email", () {
18+
//ARRANGE
19+
String email = 'sdsdsd';
20+
21+
//ACT
22+
String? result = Validator.validateEmail(email);
23+
24+
//ASSERT
25+
expect(result, "Please enter a valid email");
26+
});
27+
28+
test("validate for valid email", () {
29+
//ARRANGE
30+
String email = 'dev.basakk6@gmail.com';
31+
32+
//ACT
33+
String? result = Validator.validateEmail(email);
34+
35+
//ASSERT
36+
expect(result, null);
37+
});
38+
39+
test("validate for empty password", () {
40+
//ARRANGE
41+
String password = '';
42+
43+
//ACT
44+
String? result = Validator.validatePassword(password);
45+
46+
//ASSERT
47+
expect(result, "Required field");
48+
});
49+
50+
test("validate for invalid password", () {
51+
//ARRANGE
52+
String password = '1234567';
53+
54+
//ACT
55+
String? result = Validator.validatePassword(password);
56+
57+
//ASSERT
58+
expect(result, "Password should be a minimum of 8 characters");
59+
});
60+
61+
test("validate for valid password", () {
62+
//ARRANGE
63+
String password = '12345678';
64+
65+
//ACT
66+
String? result = Validator.validatePassword(password);
67+
68+
//ASSERT
69+
expect(result, null);
70+
});
71+
});
72+
}

0 commit comments

Comments
 (0)