Skip to content

Commit

Permalink
Merge pull request #20 from onezerocompany/lucasilverentand/add-path-…
Browse files Browse the repository at this point in the history
…tests

Add tests for path and path segment related code
  • Loading branch information
lucasilverentand authored Dec 13, 2024
2 parents 916e0ef + a07d076 commit 2301e4a
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 0 deletions.
51 changes: 51 additions & 0 deletions oui/test/router/oui_path_match_segment_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:oui/oui.dart';

void main() {
group('OuiPathMatchSegment', () {
test('constructor initializes fields correctly', () {
final segment = OuiPathSegment.static('home');
final matchSegment = OuiPathMatchSegment(
segment: segment,
original: 'home',
value: 'home',
);

expect(matchSegment.segment, segment);
expect(matchSegment.original, 'home');
expect(matchSegment.content, 'home');
expect(matchSegment.id, 'home');
});

test('content returns original if value is null', () {
final segment = OuiPathSegment.static('home');
final matchSegment = OuiPathMatchSegment(
segment: segment,
original: 'home',
);

expect(matchSegment.content, 'home');
});

test('content returns value if not null', () {
final segment = OuiPathSegment.static('home');
final matchSegment = OuiPathMatchSegment(
segment: segment,
original: 'home',
value: 'home-value',
);

expect(matchSegment.content, 'home-value');
});

test('id returns segment id', () {
final segment = OuiPathSegment.static('home');
final matchSegment = OuiPathMatchSegment(
segment: segment,
original: 'home',
);

expect(matchSegment.id, 'home');
});
});
}
68 changes: 68 additions & 0 deletions oui/test/router/oui_path_match_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:oui/oui.dart';

class TestScreen extends OuiScreen {
@override
final String id;
final Localized<OuiPath> localizedPath;

TestScreen(this.id, this.localizedPath);

@override
Localized<OuiPath> get path => localizedPath;

@override
Widget build(BuildContext context, WidgetRef ref) {
return Container();
}
}

void main() {
group('OuiPathMatch', () {
test('forScreen matches path', () {
final screen = TestScreen(
'test',
Localized(OuiPath([OuiPathSegment.static('test')])),
);
final match = OuiPathMatch.forScreen(screen, ['test'], Locale('en'));
expect(match.isMatch, true);
expect(match.screens.length, 1);
expect(match.screens.first.id, 'test');
});

test('forScreen does not match path', () {
final screen = TestScreen(
'test',
Localized(OuiPath([OuiPathSegment.static('test')])),
);
final match = OuiPathMatch.forScreen(screen, ['no-match'], Locale('en'));
expect(match.isMatch, false);
});

test('add combines matches', () {
final screen1 = TestScreen(
'screen1',
Localized(OuiPath([OuiPathSegment.static('screen1')])),
);
final screen2 = TestScreen(
'screen2',
Localized(OuiPath([OuiPathSegment.static('screen2')])),
);

final match1 = OuiPathMatch.forScreen(screen1, ['screen1'], Locale('en'));
final match2 = OuiPathMatch.forScreen(screen2, ['screen2'], Locale('en'));

final combinedMatch = match1.add(match2);
expect(combinedMatch.isMatch, true);
expect(combinedMatch.screens.length, 2);
expect(combinedMatch.screens[0].id, 'screen1');
expect(combinedMatch.screens[1].id, 'screen2');
});

test('noMatch represents no match', () {
final noMatch = OuiPathMatch.noMatch;
expect(noMatch.isMatch, false);
expect(noMatch.screens.isEmpty, true);
});
});
}
30 changes: 30 additions & 0 deletions oui/test/router/oui_path_segment_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:oui/oui.dart';

void main() {
group('OuiPathSegment', () {
test('static segment', () {
final segment = OuiPathSegment.static('home');
expect(segment.id, 'home');
expect(segment.isDynamic, false);
expect(segment.pattern?.hasMatch('home'), true);
expect(segment.pattern?.hasMatch('about'), false);
});

test('argument segment', () {
final segment = OuiPathSegment.argument('id', pattern: RegExp(r'^\d+$'));
expect(segment.id, 'id');
expect(segment.isDynamic, true);
expect(segment.pattern?.hasMatch('123'), true);
expect(segment.pattern?.hasMatch('abc'), false);
});

test('wildcard segment', () {
final segment = OuiPathSegment.wildcard('path');
expect(segment.id, 'path');
expect(segment.isDynamic, true);
expect(segment.pattern?.hasMatch('anything/goes/here'), true);
expect(segment.pattern?.hasMatch(''), true);
});
});
}
59 changes: 59 additions & 0 deletions oui/test/router/oui_path_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:oui/oui.dart';

void main() {
group('OuiPath', () {
test('matches static segments', () {
final path = OuiPath([
OuiPathSegment.static('home'),
OuiPathSegment.static('about'),
]);

final matches = path.matches(['home', 'about']);
expect(matches.length, 2);
expect(matches[0].content, 'home');
expect(matches[1].content, 'about');
});

test('matches dynamic segments', () {
final path = OuiPath([
OuiPathSegment.static('user'),
OuiPathSegment.argument('id'),
]);

final matches = path.matches(['user', '123']);
expect(matches.length, 2);
expect(matches[0].content, 'user');
expect(matches[1].content, '123');
});

test('matches wildcard segments', () {
final path = OuiPath([
OuiPathSegment.static('files'),
OuiPathSegment.wildcard('path'),
]);

final matches = path.matches(['files', 'documents/report.pdf']);
expect(matches.length, 2);
expect(matches[0].content, 'files');
expect(matches[1].content, 'documents/report.pdf');
});

test('does not match when segments do not align', () {
final path = OuiPath([
OuiPathSegment.static('home'),
OuiPathSegment.static('about'),
]);

final matches = path.matches(['home', 'contact']);
expect(matches.length, 1);
expect(matches[0].content, 'home');
});

test('parses empty path', () {
final path = OuiPath.empty;
final matches = path.matches([]);
expect(matches.length, 0);
});
});
}

0 comments on commit 2301e4a

Please sign in to comment.