Skip to content

Commit

Permalink
Merge 0761da9 into a9075c4
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jan 17, 2019
2 parents a9075c4 + 0761da9 commit a6f2af4
Show file tree
Hide file tree
Showing 43 changed files with 556 additions and 333 deletions.
1 change: 0 additions & 1 deletion .eslintignore
@@ -1,4 +1,3 @@
/documentation/
/images/
/build/
/vendor/
Expand Down
18 changes: 18 additions & 0 deletions documentation/.eslintrc.js
@@ -0,0 +1,18 @@
module.exports = {
root: false,
plugins: ['markdown'],
globals: {
expect: true // One of the snippets overwrites the global
},
rules: {
// Some snippets reference variables from previous ones
// but eslint-plugin-markdown lints them independently.
// Disable the rules that this causes trouble with:
'no-unused-vars': 0,
'no-undef': 0,
'import/no-extraneous-dependencies': 0 // Avoid complaints about require('unexpected')
},
parserOptions: {
sourceType: 'script' // Otherwise globalReturn won't work, we use that for the async snippets
}
};
3 changes: 2 additions & 1 deletion documentation/api/UnexpectedError.md
Expand Up @@ -18,7 +18,8 @@ shortcut you can also just pass the output or format directly.
An alternative to calling `getErrorMessage(output)` with an output,
you can append the error message to an output the following way:

```js#evaluate:false
<!-- unexpected-markdown evaluate:false -->
```js
output.appendErrorMessage(error);
```

Expand Down
23 changes: 17 additions & 6 deletions documentation/api/addAssertion.md
Expand Up @@ -63,8 +63,9 @@ type and pattern of the assertion.

So in this case, when `expect` is called the following way:

```js#evaluate:false
expect([3,2,1], 'to be sorted', reverse);
<!-- unexpected-markdown evaluate:false -->
```js
expect([3, 2, 1], 'to be sorted', reverse);
```

The handler to our assertion will be called with the values the
Expand Down Expand Up @@ -286,8 +287,13 @@ It would be pretty nice if we could use
even if the retrieval is delayed. Then we would be able to do stuff
like this:

```js#evaluate:false
return expect(new Timelock('Hello world'), 'to satisfy', expect.it('have length', 11));
<!-- unexpected-markdown evaluate:false -->
```js
return expect(
new Timelock('Hello world'),
'to satisfy',
expect.it('have length', 11)
);
```

First we need to define a [type](../addType/) for handling the `Timelock`:
Expand Down Expand Up @@ -322,8 +328,13 @@ expect.addAssertion('<Timelock> to satisfy <any>', function(

Let's see how it works:

```js#async:true
return expect(new Timelock('Hello world!', 5), 'to satisfy', expect.it('not to match', /!/));
<!-- unexpected-markdown async:true -->
```js
return expect(
new Timelock('Hello world!', 5),
'to satisfy',
expect.it('not to match', /!/)
);
```

```output
Expand Down
201 changes: 110 additions & 91 deletions documentation/api/addType.md
Expand Up @@ -94,20 +94,22 @@ That is already quite helpful, but it would be even nicer if the
stringification of `Person` instances could read as valid calls to the
constructor. We can fix that by implementing an `inspect` method on the type.

```js#freshExpect:true
<!-- unexpected-markdown freshExpect:true -->
```js
expect.addType({
name: 'Person',
base: 'object',
identify: function (value) {
return value instanceof Person;
},
inspect: function (person, depth, output, inspect) {
output.text('new Person(')
.append(inspect(person.name, depth))
.text(', ')
.append(inspect(person.age, depth))
.text(')');
}
name: 'Person',
base: 'object',
identify: function(value) {
return value instanceof Person;
},
inspect: function(person, depth, output, inspect) {
output
.text('new Person(')
.append(inspect(person.name, depth))
.text(', ')
.append(inspect(person.age, depth))
.text(')');
}
});
```

Expand Down Expand Up @@ -147,50 +149,58 @@ same depth to the `inspect` function.
Let's say we wanted `Person` instances only to be compared by name and not by
age. Then we need to override the `equal` method:

```js#freshExpect:true
<!-- unexpected-markdown freshExpect:true -->
```js
expect.addType({
name: 'Person',
base: 'object',
identify: function (value) {
return value instanceof Person;
},
inspect: function (person, depth, output, inspect) {
output.text('new Person(')
.append(inspect(person.name, depth))
.text(', ')
.append(inspect(person.age, depth))
.text(')');
},
equal: function (a, b, equal) {
return a === b || equal(a.name, b.name);
}
name: 'Person',
base: 'object',
identify: function(value) {
return value instanceof Person;
},
inspect: function(person, depth, output, inspect) {
output
.text('new Person(')
.append(inspect(person.name, depth))
.text(', ')
.append(inspect(person.age, depth))
.text(')');
},
equal: function(a, b, equal) {
return a === b || equal(a.name, b.name);
}
});
```

This will produce the same output as above, but that means the diff if
wrong. It states that the age should be changed. We can fix that the
following way:

```js#freshExpect:true
<!-- unexpected-markdown freshExpect:true -->
```js
expect.addType({
name: 'Person',
base: 'object',
identify: function (value) {
return value instanceof Person;
},
inspect: function (person, depth, output, inspect) {
output.text('new Person(')
.append(inspect(person.name, depth))
.text(', ')
.append(inspect(person.age, depth))
.text(')');
},
equal: function (a, b, equal) {
return a === b || equal(a.name, b.name);
},
diff: function (actual, expected, output, diff, inspect) {
return this.baseType.diff({name: actual.name}, {name: expected.name}, output);
}
name: 'Person',
base: 'object',
identify: function(value) {
return value instanceof Person;
},
inspect: function(person, depth, output, inspect) {
output
.text('new Person(')
.append(inspect(person.name, depth))
.text(', ')
.append(inspect(person.age, depth))
.text(')');
},
equal: function(a, b, equal) {
return a === b || equal(a.name, b.name);
},
diff: function(actual, expected, output, diff, inspect) {
return this.baseType.diff(
{ name: actual.name },
{ name: expected.name },
output
);
}
});
```

Expand Down Expand Up @@ -218,53 +228,62 @@ on the base directly when you know it is the one you need.

You could also do something really custom as seen below:

```js#freshExpect:true
<!-- unexpected-markdown freshExpect:true -->
```js
var inlineDiff = true; // used to change inlining in a later example

expect.addType({
name: 'Person',
base: 'object',
identify: function (value) {
return value instanceof Person;
},
inspect: function (person, depth, output, inspect) {
output.text('new Person(')
.append(inspect(person.name, depth))
.text(', ')
.append(inspect(person.age, depth))
.text(')');
},
equal: function (a, b, equal) {
return a === b || equal(a.name, b.name);
},
diff: function (actual, expected, output, diff, inspect) {
output.inline = inlineDiff;
var nameDiff = diff(actual.name, expected.name);
output.text('new Person(')
.nl()
.indentLines();
if (nameDiff && nameDiff.inline) {
output.append(nameDiff);
} else {
output.i().append(inspect(actual.name)).text(',').sp()
.annotationBlock(function () {
this.error('should be ').append(inspect(expected.name));
if (nameDiff) {
this.nl().append(nameDiff);
}
})
.nl();
}
output.i().append(inspect(actual.age))
.outdentLines()
.nl()
.text(')');
return output;
name: 'Person',
base: 'object',
identify: function(value) {
return value instanceof Person;
},
inspect: function(person, depth, output, inspect) {
output
.text('new Person(')
.append(inspect(person.name, depth))
.text(', ')
.append(inspect(person.age, depth))
.text(')');
},
equal: function(a, b, equal) {
return a === b || equal(a.name, b.name);
},
diff: function(actual, expected, output, diff, inspect) {
output.inline = inlineDiff;
var nameDiff = diff(actual.name, expected.name);

output
.text('new Person(')
.nl()
.indentLines();

if (nameDiff && nameDiff.inline) {
output.append(nameDiff);
} else {
output
.i()
.append(inspect(actual.name))
.text(',')
.sp()
.annotationBlock(function() {
this.error('should be ').append(inspect(expected.name));
if (nameDiff) {
this.nl().append(nameDiff);
}
})
.nl();
}

output
.i()
.append(inspect(actual.age))
.outdentLines()
.nl()
.text(')');

return output;
}
});
```

Expand Down
14 changes: 8 additions & 6 deletions documentation/api/expect.md
Expand Up @@ -26,9 +26,10 @@ does some unholy trickery so it also works in Jasmine.
Note that if the assertion is asynchronous, you'll have to return the promise
to the `it` block:

```js#eval:false
it('should call the callback', function () {
return expect(setImmediate, 'to call the callback');
<!-- unexpected-markdown evaluate:false -->
```js
it('should call the callback', function() {
return expect(setImmediate, 'to call the callback');
});
```

Expand All @@ -52,8 +53,9 @@ expect('abc', 'to be a string').and('to have length', 3);
Again, note that you need to return the value returned by `expect` to your `it`
block if any of the assertions are asynchronous:

```js#eval:false
it('should do the right thing', function () {
return expect(setImmediate, 'to be a function').and('to call the callback');
<!-- unexpected-markdown evaluate: false -->
```js
it('should do the right thing', function() {
return expect(setImmediate, 'to be a function').and('to call the callback');
});
```
42 changes: 26 additions & 16 deletions documentation/api/promise-all.md
Expand Up @@ -36,32 +36,42 @@ expect.promise works.
The following code snippet creates a promise that is fulfilled when all the
promises in the nested structure are fulfilled.

```js#async:true
<!-- unexpected-markdown async:true -->
```js
return expect.promise.all({
foo: [
expect(42, 'to be a number after a short delay')
],
bar: expect([0, 1, 2], 'to have items satisfying',
expect.it('to be a number after a short delay')),
foo: [expect(42, 'to be a number after a short delay')],
bar: expect(
[0, 1, 2],
'to have items satisfying',
expect.it('to be a number after a short delay')
),

baz: expect({ a: 1, b: 2 }, 'to have values satisfying',
'to be a number after a short delay')
baz: expect(
{ a: 1, b: 2 },
'to have values satisfying',
'to be a number after a short delay'
)
});
```

The following code snippet creates a promise that is rejected when one
of the promises in the nested structure is rejected.

```js#async:true
<!-- unexpected-markdown async:true -->
```js
return expect.promise.all({
foo: [
expect(42, 'to be a number after a short delay')
],
bar: expect([0, 1, 2], 'to have items satisfying',
expect.it('to be a number after a short delay')),
foo: [expect(42, 'to be a number after a short delay')],
bar: expect(
[0, 1, 2],
'to have items satisfying',
expect.it('to be a number after a short delay')
),

baz: expect({ a: '0', b: 1 }, 'to have values satisfying',
'to be a number after a short delay')
baz: expect(
{ a: '0', b: 1 },
'to have values satisfying',
'to be a number after a short delay'
)
});
```

Expand Down

0 comments on commit a6f2af4

Please sign in to comment.