Skip to content

Commit 697c4e7

Browse files
authored
feat: Rand class
Closes #9
2 parents 12bf06e + 63ed64c commit 697c4e7

File tree

9 files changed

+143
-22
lines changed

9 files changed

+143
-22
lines changed

CHANGELOG.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
<!--todo: release initial version -->
22

3-
## 0.0.1
3+
# Changelog
44

5-
- Initial version.
5+
All notable changes to this project will be documented in this file.
6+
7+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
8+
and this project adheres to [Dart Package Versioning](https://dart.dev/tools/pub/versioning).
9+
10+
## [Unreleased]
11+
12+
### Added
13+
14+
- FutureWrap class: instead of returning a value, subclasses will
15+
themselves be the value.
16+
- Text abstract class
17+
- Rand class for generating random string patterns —
18+
[9](https://g]]]ithub.com/dartoos-dev/dartoos/issues/9).
19+
20+
## [0.0.1]

example/dartoos_example.dart

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
import 'package:dartoos/dartoos.dart';
2-
3-
void main() {
4-
final awesome = Awesome();
5-
// ignore: avoid_print
6-
print('awesome: ${awesome.isAwesome}');
7-
}
1+
void main() {}

lib/dartoos.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/// More dartdocs go here.
44
library dartoos;
55

6-
export 'src/dartoos_base.dart';
76
export 'src/future_wrap.dart';
8-
7+
export 'text.dart';
98
// @todo: #3 Export any libraries intended for clients of this package.

lib/src/dartoos_base.dart

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/src/text.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import 'future_wrap.dart';
2+
3+
/// Represents a Future<String> value.
4+
abstract class Text extends FutureWrap<String> {
5+
/// Constructs a Text from a Future<String>
6+
Text(Future<String> text) : super(text);
7+
8+
/// Text from value.
9+
Text.value(String value) : super.value(value);
10+
}

lib/src/text/rand.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'dart:math';
2+
3+
import '../text.dart';
4+
5+
/// Randomized text.
6+
class Rand extends Text {
7+
/// Random text having [src] as the source of eligible characters, [len] as
8+
/// the length, and [index] as the index randomizer.
9+
Rand(int len, Future<String> src, [Random? index])
10+
: super(
11+
Future(() async {
12+
final buffer = StringBuffer();
13+
final randIndex = index ?? Random();
14+
final chars = await src;
15+
for (int count = 0; count < len; count++) {
16+
buffer.write(chars[randIndex.nextInt(chars.length)]);
17+
}
18+
return buffer.toString();
19+
}),
20+
);
21+
22+
/// Random digits [0–9] of length [len].
23+
Rand.dig(int len, [Random? index]) : this.str(len, '0123456789', index);
24+
25+
/// 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);
31+
}

lib/text.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// Collection of text-related classes
2+
library text;
3+
4+
export 'src/text.dart';
5+
export 'src/text/rand.dart';
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import 'package:dartoos/dartoos.dart';
21
import 'package:test/test.dart';
32

43
void main() {
54
group('A group of tests', () {
6-
final awesome = Awesome();
7-
85
setUp(() {
96
// Additional setup goes here.
107
});
118

129
test('First Test', () {
13-
expect(awesome.isAwesome, isTrue);
10+
expect(true, true);
1411
});
1512
});
1613
}

test/text/rand_test.dart

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import 'dart:math';
2+
3+
import 'package:dartoos/src/text/rand.dart';
4+
import 'package:test/test.dart';
5+
6+
Future<void> main() async {
7+
group('Rand.dig', () {
8+
test('length', () async {
9+
final four = await Rand.dig(4, Random.secure());
10+
expect(four.length, 4);
11+
});
12+
test('digits only', () async {
13+
final digits = await Rand.dig(10, SeqIndexRandom());
14+
// Regexp '\D' means non-digit characters
15+
// the generated value cannot contain any non-digit character
16+
expect(digits.contains(RegExp(r'\D')), false);
17+
});
18+
test('decimal digits [0–9]', () async {
19+
final decimalDigits = await Rand.dig(10, SeqIndexRandom());
20+
expect(decimalDigits, '0123456789');
21+
});
22+
});
23+
group('Rand.hex', () {
24+
test('length', () async {
25+
expect((await Rand.hex(1)).length, 1);
26+
expect((await Rand.hex(5)).length, 5);
27+
});
28+
test('hex digits only', () async {
29+
final hex = await Rand.hex(10, SeqIndexRandom());
30+
// the generated value cannot contain any non-hex character
31+
expect(hex.contains(RegExp('[^0-9a-f]')), false);
32+
});
33+
test('hexadecimal digits [0–9a–f]', () async {
34+
final hexDigits = await Rand.hex(16, SeqIndexRandom());
35+
expect(hexDigits, '0123456789abcdef');
36+
});
37+
});
38+
group('Rand.str', () {
39+
test('length', () async {
40+
final one = await Rand.str(1, 'abc');
41+
expect(one.length, 1);
42+
});
43+
test('custom characters source', () async {
44+
final custom = await Rand.str(100, 'a', SeqIndexRandom());
45+
expect(custom.length, 100);
46+
47+
/// Must not contain any character other than 'a'
48+
expect(custom.contains(RegExp('[^a]')), false);
49+
});
50+
});
51+
}
52+
53+
/// Sequentially generated integer values; quite the opposite of
54+
/// random!!!
55+
class SeqIndexRandom implements Random {
56+
int _counter = 1;
57+
int _currBound = 1;
58+
59+
@override
60+
bool nextBool() => throw UnsupportedError('nextBool not implemented');
61+
@override
62+
double nextDouble() => throw UnsupportedError('nextDouble not implemented');
63+
64+
/// Generates sequential integers from 0 up to bound - 1.
65+
/// If [bound] changes, the counter is reset so that the first
66+
/// value for the new max is 0.
67+
@override
68+
int nextInt(int bound) {
69+
if (_currBound != bound) {
70+
_currBound = bound;
71+
_counter = bound;
72+
}
73+
final value = _counter % _currBound;
74+
++_counter;
75+
return value;
76+
}
77+
}

0 commit comments

Comments
 (0)