From 8e520ec0bf6565ffbb025bd08de62eabc9e17732 Mon Sep 17 00:00:00 2001 From: Michal Freriks Date: Tue, 15 Jul 2025 15:22:45 +0200 Subject: [PATCH 1/2] Add unit tests for string extension methods --- .../extensions/string_extensions_test.dart | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/packages/netglade_utils/test/src/extensions/string_extensions_test.dart b/packages/netglade_utils/test/src/extensions/string_extensions_test.dart index 41b0574..c76a481 100644 --- a/packages/netglade_utils/test/src/extensions/string_extensions_test.dart +++ b/packages/netglade_utils/test/src/extensions/string_extensions_test.dart @@ -303,4 +303,122 @@ void main() { }); }); }); + + group('normalizeUrl', () { + for (final testCase in [ + 'https://aaa.cz', + 'aaa.cz', + 'https://aaa.cz/', + 'aaa.cz/', + ' https://aaa.cz/', + ' aaa.cz/', + 'https://aaa.cz/// ', + 'aaa.cz/// ', + ]) { + test('URL $testCase should be normalized', () { + final result = testCase.normalizeUrl(); + + expect(result, equals('https://aaa.cz')); + }); + } + }); + + group('rtrim', () { + test('should remove trailing slash', () { + final result = 'https://aaa.cz/'.rtrim('/'); + expect(result, equals('https://aaa.cz')); + }); + test('should remove trailing slashes', () { + final result = 'https://aaa.cz///'.rtrim('/'); + expect(result, equals('https://aaa.cz')); + }); + }); + + group('capitalize', () { + test('should capitalize first letter', () { + final result = 'hello world'.capitalize(); + expect(result, equals('Hello world')); + }); + + test('should not change already capitalized string', () { + final result = 'Hello world'.capitalize(); + expect(result, equals('Hello world')); + }); + + test('single character is not capitalized', () { + final result = 'h'.capitalize(); + expect(result, equals('h')); + }); + + test('Should capitalize first letter when two characters', () { + final result = 'hh'.capitalize(); + expect(result, equals('Hh')); + }); + + test('should handle empty string', () { + final result = ''.capitalize(); + expect(result, equals('')); + }); + }); + + group('shorten', () { + test('should shorten string to max length', () { + final result = 'Hello, world!'.shorten(5); + expect(result, equals('Hello...')); + }); + + test('should not shorten if length is less than max length', () { + final result = 'Hi'.shorten(5); + expect(result, equals('Hi')); + }); + + test('should handle empty string', () { + final result = ''.shorten(5); + expect(result, equals('')); + }); + }); + + group('stripOuterQuotes', () { + test('should strip outer quotes from string', () { + final result = '"Hello, world!"'.stripOuterQuotes(); + expect(result, equals('Hello, world!')); + }); + + test('should handle string without quotes', () { + final result = 'Hello, world!'.stripOuterQuotes(); + expect(result, equals('Hello, world!')); + }); + + test('string with single quote is not handled', () { + final result = "'Hello, world!'".stripOuterQuotes(); + expect(result, equals("'Hello, world!'")); + }); + + test('should handle empty string', () { + final result = ''.stripOuterQuotes(); + expect(result, equals('')); + }); + }); + + group('stripNewLines', () { + test('should replace new lines with space', () { + final result = 'Hello\nWorld'.stripNewLines(); + expect(result, equals('Hello World')); + }); + + test('should replace multiple new lines with single space', () { + final result = 'Hello\n\nWorld'.stripNewLines(); + expect(result, equals('Hello World')); + }); + + test('should handle empty string', () { + final result = ''.stripNewLines(); + expect(result, equals('')); + }); + + test('should handle string without new lines', () { + final result = 'Hello World'.stripNewLines(); + expect(result, equals('Hello World')); + }); + }); } From 55cf9e9bd2ff7b31c4c020be8284b6da2f4bb10d Mon Sep 17 00:00:00 2001 From: Michal Freriks Date: Tue, 15 Jul 2025 15:28:06 +0200 Subject: [PATCH 2/2] Add missing string extensions tests to changelog --- packages/netglade_utils/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/netglade_utils/CHANGELOG.md b/packages/netglade_utils/CHANGELOG.md index 7b2e9e8..54411b5 100644 --- a/packages/netglade_utils/CHANGELOG.md +++ b/packages/netglade_utils/CHANGELOG.md @@ -1,6 +1,7 @@ ## Unreleased - Require Dart SDK 3.8.0 or later. - Add `maybeSuccess` and `maybeError` to `Result` type. +- Add string extensions tests. ## 2.5.0 - Dependencies update.