https://github.com/mediacomem/test-value-generator
Utilities to generate incremental and unique values in automated tests.
https://github.com/mediacomem/test-value-generator
Last synced: about 1 year ago
JSON representation
Utilities to generate incremental and unique values in automated tests.
- Host: GitHub
- URL: https://github.com/mediacomem/test-value-generator
- Owner: MediaComem
- License: mit
- Created: 2017-11-24T09:50:27.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-28T12:18:32.000Z (over 8 years ago)
- Last Synced: 2025-03-15T01:03:16.428Z (over 1 year ago)
- Language: JavaScript
- Size: 31.3 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# test-value-generator
Utilities to generate incremental and unique values in automated tests.
[](https://badge.fury.io/js/test-value-generator)
[](https://gemnasium.com/github.com/MediaComem/test-value-generator)
[](https://travis-ci.org/MediaComem/test-value-generator)
[](https://coveralls.io/github/MediaComem/test-value-generator?branch=master)
[](LICENSE.txt)
Developed at the [Media Engineering Institute](http://mei.heig-vd.ch) ([HEIG-VD](https://heig-vd.ch)).
## Usage
This module exposes factory functions that return value generator functions when called:
```js
const testValueGenerator = require('test-value-generator');
// Create a value generator function:
const numberGenerator = testValueGenerator.incremental(i => i);
typeof(numberGenerator); // => "function"
// Generate a value by calling it:
numberGenerator(); // => 0
```
### Generating incremental values
```js
const testValueGenerator = require('test-value-generator');
// An incremental value generator helps you produce values containing
// a number that is incremented each time you use the generator.
const incrementalEmail = testValueGenerator.incremental(i => `email-${i}@example.com`);
incrementalEmail(); // => "email-0@example.com"
incrementalEmail(); // => "email-1@example.com"
incrementalEmail(); // => "email-2@example.com"
```
### Generating unique values
A value generator created with `unique` will call your function until it can
generate a value that it has not generated before. It will throw an error if it
fails after 10 attempts.
```js
const testValueGenerator = require('test-value-generator');
// A unique value generator makes sure to never produces the same value
// twice, and throws an error if it cannot.
const uniqueDiceRoll = testValueGenerator.unique(() => Math.floor(Math.random() * 6 + 1));
uniqueDiceRoll(); // => 3
uniqueDiceRoll(); // => 5
uniqueDiceRoll(); // => 6
uniqueDiceRoll(); // => 2
uniqueDiceRoll(); // => 1
uniqueDiceRoll(); // => 4
try {
uniqueDiceRoll();
} catch(err) {
// Error thrown: Could not generate unique value after 10 attempts.
}
```