Skip to content

Commit

Permalink
TW-1589: Improve unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
hieutbui authored and hoangdat committed Apr 1, 2024
1 parent 96c2716 commit 5bde424
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/pages/search/server_search_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ class ServerSearchController with SearchDebouncerMixin {
void updateSearchCategories(String searchTerm) {
resetNextBatch();

if (searchTerm.isContainsHttpProtocol()) {
searchTerm = searchTerm.removeHttpProtocol();
if (searchTerm.isContainsUrlSeparator()) {
searchTerm = searchTerm.removeUrlSeparatorAndPreceding();
}

_searchCategories = ServerSideSearchCategories(
Expand Down
19 changes: 11 additions & 8 deletions lib/utils/string_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,18 @@ extension StringCasingExtension on String {
return replaceAll(RegExp(r'\D'), '');
}

bool isContainsHttpProtocol() {
final urlRegExp = RegExp(
r'(http://|https://)(www.)?([a-zA-Z0-9]+).[a-zA-Z0-9]*.[a-z]{2,}.?([a-z]+)?',
);
return urlRegExp.hasMatch(this);
bool isContainsUrlSeparator() {
final separatorRegExp = RegExp(r'://');
return separatorRegExp.hasMatch(this);
}

String removeHttpProtocol() {
final httpProtocolRegExp = RegExp(r'(http://|https://)');
return replaceAll(httpProtocolRegExp, '');
String removeUrlSeparatorAndPreceding() {
final separatorRegExp = RegExp(r'\b[^ ]*://');
final standAloneSeparatorRegExp = RegExp(r' *:// *');

var replacedText = replaceAll(separatorRegExp, '');
replacedText = replacedText.replaceAll(standAloneSeparatorRegExp, ' ');

return replacedText;
}
}
125 changes: 119 additions & 6 deletions test/utils/string_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,28 @@ void main() {
'[isContainsHttpProtocol] TEST\n'
'GIVEN a string\n'
'USING isContainsUrl function\n'
'IF the text contains a URL\n'
'IF the string contains a URL\n'
'THEN should return true\n'
'ELSE should return false\n', () {
final testMap = <String, bool>{
'https': false,
'https:': false,
'https:/': false,
'https/': false,
'https//': false,
'https://': true,
'https://www.example.com': true,
'https://www.example.com/': true,
'https://www.example.com/watch?v=ohg5sjyrha0': true,
'https://example.com/test/test-a-link/check/1608?test=1': true,
'https://www.example.com/watch?v=ohg5sjyrha0&feature=related': true,
'https://example.com/test/test-a-link/check/1608': true,
'http': false,
'http:': false,
'http:/': false,
'http/': false,
'http//': false,
'http://': true,
'http://www.example.com': true,
'http://www.example.com/': true,
'http://www.example.com/watch?v=ohg5sjyrha0': true,
Expand Down Expand Up @@ -94,12 +106,49 @@ void main() {
'hello world.com/test/test-a-link/check/1608/?test==1': false,
'hello world.com/test/test-a-link/check/1608/?test=1&': false,
'this is a test string': false,
'ftp': false,
'ftp:': false,
'ftp:/': false,
'ftp//': false,
'ftp://': true,
'ftp://www.example.com': true,
'ftp://www.example.com/': true,
'ftp://www.example.com/watch?v=ohg5sjyrha0': true,
'ftp://example.com/test/test-a-link/check/1608?test=1': true,
'ftp://www.example.com/watch?v=ohg5sjyrha0&feature=related': true,
'ftp://example.com/test/test-a-link/check/1608': true,
'sftp': false,
'sftp:': false,
'sftp:/': false,
'sftp//': false,
'sftp://': true,
'sftp://www.example.com': true,
'sftp://www.example.com/': true,
'sftp://www.example.com/watch?v=ohg5sjyrha0': true,
'sftp://example.com/test/test-a-link/check/1608?test=1': true,
'sftp://www.example.com/watch?v=ohg5sjyrha0&feature=related': true,
'sftp://example.com/test/test-a-link/check/1608': true,
'ssh': false,
'ssh:': false,
'ssh:/': false,
'ssh//': false,
'ssh://': true,
'ssh://www.example.com': true,
'ssh://www.example.com/': true,
'ssh://www.example.com/watch?v=ohg5sjyrha0': true,
'ssh://example.com/test/test-a-link/check/1608?test=1': true,
'ssh://www.example.com/watch?v=ohg5sjyrha0&feature=related': true,
'ssh://example.com/test/test-a-link/check/1608': true,
};

for (final entry in testMap.entries) {
test('Testing: ${entry.key} => Expected: ${entry.value}', () {
final result = entry.key.isContainsHttpProtocol();
expect(result, entry.value);
final result = entry.key.isContainsUrlSeparator();
if (entry.value) {
expect(result, isTrue);
} else {
expect(result, isFalse);
}
});
}
});
Expand All @@ -108,10 +157,15 @@ void main() {
'[removeHttpProtocol] TEST\n'
'GIVEN a string\n'
'USING removeHttpProtocol function\n'
'IF the URL starts with http:// or https://\n'
'IF the string starts with http:// or https://\n'
'THEN should return the URL without the protocol\n'
'ELSE should return the string unchanged\n', () {
final testMap = <String, String>{
'https': 'https',
'https:': 'https:',
'https:/': 'https:/',
'https//': 'https//',
'https://': '',
'https://www.example.com': 'www.example.com',
'https://www.example.com/': 'www.example.com/',
'https://www.example.com/watch?v=ohg5sjyrha0':
Expand All @@ -122,6 +176,11 @@ void main() {
'www.example.com/watch?v=ohg5sjyrha0&feature=related',
'https://example.com/test/test-a-link/check/1608':
'example.com/test/test-a-link/check/1608',
'http': 'http',
'http:': 'http:',
'http:/': 'http:/',
'http//': 'http//',
'http://': '',
'http://www.example.com': 'www.example.com',
'http://www.example.com/': 'www.example.com/',
'http://www.example.com/watch?v=ohg5sjyrha0':
Expand Down Expand Up @@ -168,13 +227,67 @@ void main() {
'hello world.com/test/test-a-link/check/1608/?test==1',
'hello world.com/test/test-a-link/check/1608/?test=1&':
'hello world.com/test/test-a-link/check/1608/?test=1&',
'hello 123://world.com/test/test-a-link/check/1608/?test=1&':
'hello world.com/test/test-a-link/check/1608/?test=1&',
'hello this is the://world.com/test/test-a-link/check/1608/?test=1& for the text contains :// inside it':
'hello this is world.com/test/test-a-link/check/1608/?test=1& for the text contains inside it',
'this is a test string': 'this is a test string',
'ftp': 'ftp',
'ftp:': 'ftp:',
'ftp:/': 'ftp:/',
'ftp//': 'ftp//',
'ftp://': '',
'ftp://www.example.com': 'www.example.com',
'ftp://www.example.com/': 'www.example.com/',
'ftp://www.example.com/watch?v=ohg5sjyrha0':
'www.example.com/watch?v=ohg5sjyrha0',
'ftp://example.com/test/test-a-link/check/1608?test=1':
'example.com/test/test-a-link/check/1608?test=1',
'ftp://www.example.com/watch?v=ohg5sjyrha0&feature=related':
'www.example.com/watch?v=ohg5sjyrha0&feature=related',
'ftp://example.com/test/test-a-link/check/1608':
'example.com/test/test-a-link/check/1608',
'sftp': 'sftp',
'sftp:': 'sftp:',
'sftp:/': 'sftp:/',
'sftp//': 'sftp//',
'sftp://': '',
'sftp://www.example.com': 'www.example.com',
'sftp://www.example.com/': 'www.example.com/',
'sftp://www.example.com/watch?v=ohg5sjyrha0':
'www.example.com/watch?v=ohg5sjyrha0',
'sftp://example.com/test/test-a-link/check/1608?test=1':
'example.com/test/test-a-link/check/1608?test=1',
'sftp://www.example.com/watch?v=ohg5sjyrha0&feature=related':
'www.example.com/watch?v=ohg5sjyrha0&feature=related',
'sftp://example.com/test/test-a-link/check/1608':
'example.com/test/test-a-link/check/1608',
'ssh': 'ssh',
'ssh:': 'ssh:',
'ssh:/': 'ssh:/',
'ssh//': 'ssh//',
'ssh://': '',
'ssh://www.example.com': 'www.example.com',
'ssh://www.example.com/': 'www.example.com/',
'ssh://www.example.com/watch?v=ohg5sjyrha0':
'www.example.com/watch?v=ohg5sjyrha0',
'ssh://example.com/test/test-a-link/check/1608?test=1':
'example.com/test/test-a-link/check/1608?test=1',
'ssh://www.example.com/watch?v=ohg5sjyrha0&feature=related':
'www.example.com/watch?v=ohg5sjyrha0&feature=related',
'ssh://example.com/test/test-a-link/check/1608':
'example.com/test/test-a-link/check/1608',
};

for (final entry in testMap.entries) {
test('Testing: ${entry.key} => Expected: ${entry.value}', () {
final result = entry.key.removeHttpProtocol();
expect(result, entry.value);
final result = entry.key.removeUrlSeparatorAndPreceding();
if (entry.value.isNotEmpty) {
expect(result, isNotEmpty);
expect(result, equals(entry.value));
} else {
expect(result, isEmpty);
}
});
}
});
Expand Down

0 comments on commit 5bde424

Please sign in to comment.