together-ai-sdk
Version:
A typescript SDK for the Together AI API
53 lines (52 loc) • 1.88 kB
JavaScript
;
/**
* This is a Jest utility file that makes creating multiple test cases
* for a single function very simple. Start by creating an array of test
* cases that are formatted as a tuple.
*
* This example will create three unit tests and
* test the parseInt function with each input
*
* const testCases = [
* ['1234567', 1234567], // means parseInt('1234567') should be 1234567 (number)
* ['-12', -12],
* ['0', 0]
* ]
*
* testmap(testCases, 'Test parseInt with %s', parseInt)
*
* The message will replace all instances of %s with the input for that case
* The parameter can also be a callback that takes the input and creates the name
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.testmap = void 0;
/**
* Tests a function (map) on a whole list of tests
* @param tests - An array of tests in the format of [[input, output],...]
* @param message - A string using %s to name the tests, or a function that returns a string on input
* @param map - The function to test, takes in an input and produces an output to test against
*/
const testmap = (tests, message, map) => {
tests.forEach(([input, output]) => {
const msg = getMessage(input, message);
test(msg, () => {
expect(map(input)).toEqual(output);
});
});
};
exports.testmap = testmap;
/**
* Gets the message that is used as the name of the test
* @param input - the input to the map for the test case
* @param message - the string or callback used to create the message
* @returns the message
*/
function getMessage(input, message) {
if (typeof message === 'string') {
if (typeof input !== 'string') {
throw new Error("If testmap's message is a string formatter, the input type must be a string");
}
return message.replace('%s', input);
}
return message(input);
}