Skip to content

Commit

Permalink
Add unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed Apr 7, 2018
1 parent fdbcf01 commit a400bcd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ homepage: https://github.com/pd4d10/nanoid

environment:
sdk: ">=2.0.0-dev.32.0 <2.0.0"

dev_dependencies:
test: ^0.12.33
29 changes: 29 additions & 0 deletions test/generate_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:test/test.dart';
import 'package:nanoid/generate.dart';

void main() {
test('has options', () {
expect(generate('a', 5), equals('aaaaa'));
});

test('has flat distribution', () {
var COUNT = 100 * 1000;
var LENGTH = 5;
var ALPHABET = 'abcdefghijklmnopqrstuvwxyz';

var chars = {};
for (var i = 0; i < COUNT; i++) {
var id = generate(ALPHABET, LENGTH);
for (var j = 0; j < id.length; j++) {
var char = id[j];
if (chars[char] == null) chars[char] = 0;
chars[char] += 1;
}
}

chars.forEach((k, _) {
var distribution = (chars[k] * ALPHABET.length) / (COUNT * LENGTH);
expect(distribution, closeTo(1, 1));
});
});
}
25 changes: 25 additions & 0 deletions test/nanoid_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:test/test.dart';
import 'package:nanoid/nanoid.dart';
import 'package:nanoid/url.dart';

void main() {
test('generates URL-friendly IDs', () {
for (var i = 0; i < 10; i++) {
var id = nanoid();
expect(id.length, equals(21));
for (var j = 0; j < id.length; j++) {
expect(url.contains(id[j]), equals(true));
}
}
});

test('has no collisions', () {
var COUNT = 100 * 1000;
var used = {};
for (var i = 0; i < COUNT; i++) {
var id = nanoid();
expect(used[id], equals(null));
used[id] = true;
}
});
}

0 comments on commit a400bcd

Please sign in to comment.