Skip to content

Commit

Permalink
chore(ui/peer) : add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
iwanbk committed Nov 20, 2024
1 parent 8dd57a7 commit fe0035d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 30 deletions.
90 changes: 90 additions & 0 deletions test/peers_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:myceliumflut/main.dart'; // Adjust the import based on your project structure

void main() {
group('preprocessPeers', () {
test('removes empty elements', () {
final peers = [
'tcp://192.168.0.1:9651',
'',
'tcp://192.168.0.2:9651',
''
];
final result = preprocessPeers(peers);
expect(result, ['tcp://192.168.0.1:9651', 'tcp://192.168.0.2:9651']);
});

test('removes duplicated elements', () {
final peers = [
'tcp://192.168.0.1:9651',
'tcp://192.168.0.2:9651',
'tcp://192.168.0.1:9651'
];
final result = preprocessPeers(peers);
expect(result, ['tcp://192.168.0.1:9651', 'tcp://192.168.0.2:9651']);
});

test('removes empty and duplicated elements', () {
final peers = [
'tcp://192.168.0.1:9651',
'',
'tcp://192.168.0.2:9651',
'tcp://192.168.0.1:9651',
''
];
final result = preprocessPeers(peers);
expect(result, ['tcp://192.168.0.1:9651', 'tcp://192.168.0.2:9651']);
});

test('returns empty list if all elements are empty', () {
final peers = ['', '', ''];
final result = preprocessPeers(peers);
expect(result, []);
});

test('returns the same list if no empty or duplicated elements', () {
final peers = [
'tcp://192.168.0.1:9651',
'tcp://192.168.0.2:9651',
'peer3:9651'
];
final result = preprocessPeers(peers);
expect(result,
['tcp://192.168.0.1:9651', 'tcp://192.168.0.2:9651', 'peer3:9651']);
});
});

group('isValidPeers', () {
test('returns error if peers list is empty', () {
final peers = <String>[];
final result = isValidPeers(peers);
expect(result, "peers can't be empty");
});

test('returns error if peers list contains only an empty string', () {
final peers = [''];
final result = isValidPeers(peers);
expect(result, "peers can't be empty");
});

test('returns error for invalid peer without tcp prefix', () {
final peers = ['tcp://192.168.0.1:9651', '192.168.0.3:9651'];
final result = isValidPeers(peers);
expect(result,
'invalid peer:`192.168.0.3:9651` peer must start with tcp://');
});

test('returns error for invalid peer without port', () {
final peers = ['tcp://192.168.0.1:9651', 'tcp://192.168.0.3'];
final result = isValidPeers(peers);
expect(
result, 'invalid peer:`tcp://192.168.0.3` peer must end with :9651');
});

test('returns null for valid peers', () {
final peers = ['tcp://192.168.0.1:9651', 'tcp://192.168.0.2:9651'];
final result = isValidPeers(peers);
expect(result, null);
});
});
}
30 changes: 0 additions & 30 deletions test/widget_test.dart

This file was deleted.

0 comments on commit fe0035d

Please sign in to comment.