Skip to content

Commit cb9cf4b

Browse files
authored
test: create unit test cases for the bytes library
1 parent ac6dace commit cb9cf4b

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

lib/src/rand.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:async';
12
import 'dart:math';
23

34
import 'text.dart';
@@ -6,7 +7,7 @@ import 'text.dart';
67
class Rand extends Text {
78
/// Random text having [src] as the source of eligible characters, [len] as
89
/// the length, and [index] as the index randomizer.
9-
Rand(int len, Future<String> src, [Random? index])
10+
Rand(int len, FutureOr<String> src, [Random? index])
1011
: super(
1112
Future(() async {
1213
final buffer = StringBuffer();
@@ -20,12 +21,8 @@ class Rand extends Text {
2021
);
2122

2223
/// Random digits [0–9] of length [len].
23-
Rand.dig(int len, [Random? index]) : this.str(len, '0123456789', index);
24+
Rand.dig(int len, [Random? index]) : this(len, '0123456789', index);
2425

2526
/// Random hex digits [0–9a-f] of length [len].
26-
Rand.hex(int len, [Random? index]) : this.str(len, '0123456789abcdef', index);
27-
28-
/// Random string of length [len] from [src].
29-
Rand.str(int len, String src, [Random? index])
30-
: this(len, Future.value(src), index);
27+
Rand.hex(int len, [Random? index]) : this(len, '0123456789abcdef', index);
3128
}

test/bytes_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'dart:convert';
2+
import 'dart:io';
3+
import 'dart:typed_data';
4+
5+
import 'package:dartoos/src/bytes.dart';
6+
import 'package:test/test.dart';
7+
8+
Future<void> main() async {
9+
group('library bytes', () {
10+
group('BytesOf', () {
11+
const bytes = <int>[0x00, 0x01, 0x3d];
12+
test('from bytes', () async {
13+
expect(await BytesOf(Uint8List.fromList(bytes)), bytes);
14+
});
15+
test('from list', () async {
16+
expect(await BytesOf.list(bytes), bytes);
17+
});
18+
test('from utf8', () async {
19+
final String str = utf8.decode(bytes);
20+
expect(await BytesOf.utf8(str), bytes);
21+
expect(await BytesOf.utf8('abc'), utf8.encode('abc'));
22+
});
23+
test('from file', () async {
24+
final tempDir = Directory.systemTemp.createTempSync();
25+
final tempFile = File("${tempDir.path}/$pid.test");
26+
try {
27+
tempFile.createSync();
28+
tempFile.writeAsBytesSync(bytes);
29+
expect(await BytesOf.file(tempFile), bytes);
30+
} finally {
31+
tempDir.deleteSync(recursive: true);
32+
}
33+
});
34+
});
35+
});
36+
}

test/rand_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ Future<void> main() async {
3737
});
3838
group('Rand.str', () {
3939
test('length', () async {
40-
final one = await Rand.str(1, 'abc');
40+
final one = await Rand(1, 'abc');
4141
expect(one.length, 1);
4242
});
4343
test('custom characters source', () async {
44-
final custom = await Rand.str(100, 'a', SeqIndexRandom());
44+
final custom = await Rand(100, 'a', SeqIndexRandom());
4545
expect(custom.length, 100);
4646

4747
/// Must not contain any character other than 'a'

0 commit comments

Comments
 (0)