geninq
Version:
A JavaScript version of the Linq library for Generators
41 lines (39 loc) • 946 B
text/typescript
import { It, ItThrows } from "../test-utils";
import "./single";
ItThrows(
"Throws if more than one matching element",
(s) => s.single(),
new Error("Too many matches in sequence")
);
ItThrows(
"Throws if more than one matching element with predicate",
(s) => s.single((i) => i !== 2),
new Error("Too many matches in sequence")
);
It(
"Gets the only element in a sequence that matches a predicate",
(s) => s.single((i) => i === 2),
2
);
It(
"Returns undefined if the sequence is empty",
(s) => s.single("or-default"),
undefined,
[]
);
It(
"Returns undefined if the element does not exist",
(s) => s.single((i) => i === 4, "or-default"),
undefined
);
ItThrows(
"Throws for element that does not exist",
(s) => s.single((i) => i === 4),
new Error("No matching elements in sequence.")
);
ItThrows(
"Throws for an empty sequence",
(s) => s.single(),
new Error("No matching elements in sequence."),
[]
);