Skip to content

Make it easier to have multiple checks for the same values #108

@LeaVerou

Description

@LeaVerou

I just ran into this scenario, which I suspect is pretty common: while testing an object (a subclass of Set), I wanted to test both the values as well as value type (to make sure it was the subclass I was defining and not Set).

One potential solution is to make check() take an array, in which case multiple tests are generated, one for each check.
One tricky bit is that what happens if you want to specify a map() function on only some of them?

Perhaps the solution would be to just utilize nesting which already works:

export default {
	// test params here
	tests: [ 
		// all parent params inherited, so we only specify what changes
		{check: check.is("MyFoo")], 
		{map: v => [...v]}
	],
}

However, this doesn't scale when you have multiple tests.
It seems this is another instance of the more general problem "You have M and Ns and you want to test every combination of M x N", which is also the subject of #106 .

I wonder if the right solution might be some way to basically automate the nested solution, i.e. provide tests that will be automatically added to all leafs under a certain group.
E.g. assuming we call that property leafTests (to be bikeshedded) this means one can do:

export default {
	run (arg) {
		return arg + 1;
	},
	expect: 1,
	leafTests: [ 
		{ /* default equality check */ }
		{ check: check.is("number") }
	],
	tests: [ 
		{ arg: 1 }
		{ arg: Number("1") },
		{ arg: new Number("1") },
	],
}

Which will basically be a macro for doing:

const leafTests = [ 
	{ /* default equality check */ }
	{ check: check.is("number") }
];
export default {
	run (arg) {
		return arg + 1;
	},
	expect: 1,
	tests: [ 
		{ arg: 1, tests: leafTests }
		{ arg: Number("1"), tests: leafTests },
		{ arg: new Number("1"), tests: leafTests },
	],
}

And note that given how nesting works, either of these can be nested, and it should just work.
This allows people providing multiple sets of not just checking functions, but also data, run(), basically any key we support.

I think I'm becoming convinced this is the way to go, but we need to find a good name for this concept.
Some ideas:

  • checks (since in most cases this will be about check/map)
  • childTests
  • autoChildren
  • forEach
  • each
  • cross (since this is basically doing a cross-product)
  • crossTests
  • with
  • withTests

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions