-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests and readme, small tweaks
- Loading branch information
Showing
7 changed files
with
243 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Detox utils [![Travis CI](https://img.shields.io/travis/Detox/utils/master.svg?label=Travis%20CI)](https://travis-ci.org/Detox/utils) | ||
Various utility functions shared between other Detox libraries. | ||
|
||
Functions support working in Node.js as well as modern browsers | ||
|
||
## How to install | ||
``` | ||
npm install @detox/utils | ||
``` | ||
|
||
## How to use | ||
Node.js: | ||
```javascript | ||
var detox_utils = require('@detox/utils') | ||
|
||
// Do stuff | ||
``` | ||
Browser: | ||
```javascript | ||
requirejs(['@detox/utils'], function (detox_utils) { | ||
// Do stuff | ||
}) | ||
``` | ||
|
||
## List of exported functions | ||
* `random_bytes()` | ||
* `random_int()` | ||
* `pull_random_item_from_array()` | ||
* `array2hex()` | ||
* `hex2array()` | ||
* `string2array()` | ||
* `array2string()` | ||
* `is_string_equal_to_array()` | ||
* `concat_arrays()` | ||
* `compute_source_id()` | ||
* `timeoutSet()` | ||
* `intervalSet()` | ||
* `error_handler()` | ||
|
||
Look at source code and tests for details and usage examples. | ||
|
||
## Contribution | ||
Feel free to create issues and send pull requests (for big changes create an issue first and link it from the PR), they are highly appreciated! | ||
|
||
When reading LiveScript code make sure to configure 1 tab to be 4 spaces (GitHub uses 8 by default), otherwise code might be hard to read. | ||
|
||
## License | ||
Free Public License 1.0.0 / Zero Clause BSD License | ||
|
||
https://opensource.org/licenses/FPL-1.0.0 | ||
|
||
https://tldrlegal.com/license/bsd-0-clause-license |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* @package Detox utils | ||
* @author Nazar Mokrynskyi <nazar@mokrynskyi.com> | ||
* @license 0BSD | ||
*/ | ||
lib = require('..') | ||
test = require('tape') | ||
|
||
test('Utils', (t) !-> | ||
t.plan(19) | ||
|
||
random1 = lib.random_bytes(10) | ||
random2 = lib.random_bytes(10) | ||
t.ok(random1 instanceof Uint8Array, 'Random bytes are in Uint8Array') | ||
t.equal(random1.length, 10, 'Random bytes of correct length') | ||
t.notEqual(random1.join(','), random2.join(','), 'Random bytes are random') | ||
|
||
random_int1 = lib.random_int(1, 999) | ||
random_int2 = lib.random_int(1, 999) | ||
t.notEqual(random_int1, random_int2, 'Random ints are random') | ||
|
||
array = [1, 2, 3] | ||
random_item = lib.pull_random_item_from_array(array) | ||
t.equal(array.length, 2, 'Pulled item from array') | ||
|
||
hex_array = Uint8Array.of(1, 2, 3, 5, 6) | ||
hex = '0102030506' | ||
t.equal(lib.array2hex(hex_array), hex, 'Array to hex converted correctly') | ||
t.equal(lib.hex2array(hex).join(','), hex_array.join(','), 'Hex to array converted correctly') | ||
|
||
string = 'Hello, world 😊' | ||
string_array = Uint8Array.of(72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 32, 240, 159, 152, 138) | ||
t.equal(lib.string2array(string).join(','), string_array.join(','), 'String to Uint8Array converted correctly') | ||
t.equal(lib.array2string(string_array), string, 'Uint8Array to string converted correctly') | ||
|
||
t.ok(lib.is_string_equal_to_array(hex_array.join(','), hex_array), 'String to Uint8Array comparison succeeded') | ||
t.notOk(lib.is_string_equal_to_array(hex_array.join(','), string_array), 'String to Uint8Array comparison failed') | ||
|
||
array1 = Uint8Array.of(1, 2) | ||
array2 = Uint8Array.of(3, 4) | ||
arrays_result = Uint8Array.of(1, 2, 3, 4) | ||
concatenated = lib.concat_arrays([array1, array2]) | ||
t.equal(concatenated.length, 4, 'Concatenated array has expected length') | ||
t.ok(concatenated instanceof Uint8Array, 'Concatenated array is Uint8Array') | ||
t.equal(concatenated.join(','), arrays_result.join(','), 'Concatenated array has expected contents') | ||
|
||
computed_source = lib.compute_source_id(array1, array2) | ||
t.equal(computed_source, '1,23,4', 'Source id computed correctly') | ||
|
||
x = 0 | ||
lib.timeoutSet(0.001, !-> | ||
++x | ||
) | ||
clearTimeout(lib.timeoutSet(0.001, !-> | ||
++x | ||
)) | ||
setTimeout (!-> | ||
t.equal(x, 1, 'timeoutSet works correctly') | ||
), 50 | ||
|
||
y = 0 | ||
i1 = lib.intervalSet(0.001, !-> | ||
++y | ||
) | ||
clearInterval(lib.intervalSet(0.001, !-> | ||
++x | ||
)) | ||
setTimeout (!-> | ||
clearInterval(i1) | ||
t.notEqual(y, 0, 'intervalSet works correctly') | ||
t.equal(x, 1, 'intervalSet works correctly') | ||
), 100 | ||
|
||
setTimeout (!-> | ||
console_error = console.error | ||
console.error = !-> | ||
console.error = console_error | ||
t.pass('error_handler catches errors') | ||
(new Promise !-> | ||
throw new Error | ||
).catch(lib.error_handler) | ||
), 200 | ||
|
||
setTimeout (!-> | ||
console.error = !-> | ||
console.error = console_error | ||
t.fail("error_handler doesn't catch other things") | ||
(new Promise !-> | ||
throw 'Hello there' | ||
).catch(lib.error_handler) | ||
), 300 | ||
) |