Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make must extensible #24

Closed
estilles opened this issue May 23, 2015 · 6 comments
Closed

Make must extensible #24

estilles opened this issue May 23, 2015 · 6 comments

Comments

@estilles
Copy link

Allow must to be extended using community developed assertion/matcher plugins. This would allow for consumers to easily add either their own or community provided matchers, without bloating the must core library. We could easily add assertions for promises (see issue #22), Express request/response objects, etc.

I propose using the typical .use() syntax. For example:

var must = require("must")
var mustPromise = require('must-promise')
must.use(mustPromise);

... or something similar.

@moll
Copy link
Owner

moll commented May 23, 2015

Hey!

I'm with you. Allowing extensibility is most sensible.

We might need to provide a function or two to ease asserting and throwing an error to simplify creating custom matchers. Internally I added a function for that at

function insist(ok, message, expected, opts) {
, but that's not part of the public API right now.

Also, I'm not 100% sure does Must need to know when its being extended[1] vs just allowing extensions to do whatever they please[2]. Something like a promise plugin could just be called last and it would iterate over existing matcher functions to wrap them for promise support.

  1. Must.use
  2. require("must-promise")(require("must"))

@estilles
Copy link
Author

@moll ... I'm open to either solution.

I completely agree that must would need to expose the existing private insist() function, but also the chain() function (could be useful in certain use-cases).

Which ever solution you decide, I'll be glad to pitch in. :-)

Already have a few plugins in mind, so I hope this lands soon!

@estilles
Copy link
Author

@moll, I create a simple proof of concept at JohnnyEstilles/must-promise. Let me know what you think.

@estilles
Copy link
Author

@moll, updated my proof of concept with some additional matchers. Let me know what you think.

@moll
Copy link
Owner

moll commented Jun 14, 2015

Hey,

I pondered over the insist function a little and ended up with the idea to export it as Must.prototype.assert (

js-must/must.js

Line 1028 in 77616e8

Must.prototype.assert = function assert(ok, message, opts) {
). That was the thing I wanted to chat with you about. Would like to hear your thoughts on it.

In short, it works like any assert function ever:

Must.prototype.truthy = function() {
  this.assert(this.actual, "be truthy")
}

Given an object with {expected} as the third argument sets the expected property on the assertion error and concatenates its stringified value to the end of the message string:

Must.prototype.null = function() {
  this.assert(this.actual === null, "be", {expected: null})
}

If you give a function instead of a string, that function will be called for the message should the assertion fail. You can still pass expected in an options object, but instead of concatenating, it'll only be for AssertionError.

So, give me a call and-or reply here when you get around to it. Thanks!

@moll
Copy link
Owner

moll commented Jun 14, 2015

Let's continue this conversation over at #12.

@moll moll closed this as completed Jun 14, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants