Skip to content

Commit

Permalink
add "checking" section to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
veigaribo committed Oct 11, 2020
1 parent 295a31d commit 767b807
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ class Number {

The only extra step is to define a static method called `generateData`, that
will take any number of random numbers in the range [0, 1] as parameters
and should return a random instance value of the constructor.
and should return a random instance value.

```javascript
@instance(addable)
class Number {
...
// ...

static generateData(n) {
// this is quite a trivial example, we just wrap the n
Expand All @@ -118,14 +118,24 @@ class Number {
}
```

## Checking

You may also check whether a value is of an instance of a class using the
`isInstance` function:

```javascript
const n = new Number(50)
isInstance(n, addable) // true, because Numbers are addable
```

## How it works

When you define your constructor using the `@instance` decorator, a sample
of random instance values of your constructor will be generated using your
constructor's `generateData`, and each property will be tested using those.
If any of the laws fails to be asserted, an error is thrown, and you may be
sure that the constructor in question is not an instance of the class you
declared in the decorator.
of random instance values will be generated using your constructor's
`generateData`, and each property will be tested using those. If any of the
laws fails to be asserted, an error is thrown, and you may be sure that the
constructor in question is not an instance of the class you declared in the
decorator.

In case it passes, you may have a high confidence that it is.

Expand All @@ -136,7 +146,7 @@ function:

```javascript
class Number {
...
// ...
}

// will throw if anything goes bad
Expand Down
7 changes: 6 additions & 1 deletion test/readme.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
test('Readme examples work', () => {
expect(() => {
const { Class, all, instance, obey } = require('../lib/index')
const { Class, all, instance, isInstance, obey } = require('../lib/index')

const eq = new Class({
name: 'Eq',
Expand Down Expand Up @@ -60,5 +60,10 @@ test('Readme examples work', () => {
}

instance(addable)(Number)

const n = new Number(50)
const is = isInstance(n, addable) // true, because Numbers are addable

expect(is).toBe(true)
}).not.toThrow()
})

0 comments on commit 767b807

Please sign in to comment.