Skip to content

Commit

Permalink
Added tests and readme, small tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Feb 19, 2018
1 parent 276b294 commit 259ed0a
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name" : "@detox/utils",
"description" : "",
"description" : "Various utility functions shared between other Detox libraries",
"keywords" : [
"detox",
"utils"
],
"version" : "",
"version" : "1.0.0",
"homepage" : "https://github.com/Detox/utils",
"author" : "Nazar Mokrynskyi <nazar@mokrynskyi.com>",
"repository" : {
Expand Down
52 changes: 52 additions & 0 deletions readme.md
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
10 changes: 5 additions & 5 deletions src/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/index.ls
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if typeof crypto != 'undefined'
*
* @return {!Uint8Array}
*/
randombytes = (size) ->
random_bytes = (size) ->
array = new Uint8Array(size)
crypto.getRandomValues(array)
array
Expand All @@ -19,15 +19,15 @@ else
*
* @return {!Uint8Array}
*/
randombytes = require('crypto').randomBytes
random_bytes = require('crypto').randomBytes
/**
* @param {number} min
* @param {number} max
*
* @return {number}
*/
function random_int (min, max)
bytes = randombytes(4)
bytes = random_bytes(4)
uint32_number = (new Uint32Array(bytes.buffer))[0]
Math.floor(uint32_number / 2**32 * (max - min + 1)) + min
/**
Expand Down Expand Up @@ -146,7 +146,7 @@ function error_handler (error)

function Wrapper
{
'randombytes' : randombytes
'random_bytes' : random_bytes
'random_int' : random_int
'pull_random_item_from_array' : pull_random_item_from_array
'array2hex' : array2hex
Expand Down
2 changes: 1 addition & 1 deletion src/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 87 additions & 0 deletions tests/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions tests/index.ls
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
)

0 comments on commit 259ed0a

Please sign in to comment.