Skip to content

Instantly share code, notes, and snippets.

@ddanielbee
Created May 7, 2018 14:55
Show Gist options
  • Save ddanielbee/237af28b58d2a33ff361b0093fdba54f to your computer and use it in GitHub Desktop.
Save ddanielbee/237af28b58d2a33ff361b0093fdba54f to your computer and use it in GitHub Desktop.
Property Based Testing String stuff
const jsc = require("jsverify");
// Write a function that:
// Takes an array of strings
// Removes all instances of a dash character (-)
// Makes sure concatenating the strings will result in a string
// shorter than 200 characters
// (by removing all extra fluff from the tail)
// Makes all strings lowercase
// Returns an array of the resulting strings.
const stringCleaning = xs =>
xs
.join(",")
.replace(/-/g, "")
.substring(0, 200)
.toLowerCase()
.split(",");
const noDashesProperty = xs =>
stringCleaning(xs)
.join("")
.indexOf("-") === -1;
const limitedStringLengthProperty = xs => stringCleaning(xs).join("").length <= 200;
const lowerCaseProperty = xs =>
stringCleaning(xs).reduce((acc, cur) => acc && cur.toLowerCase() === cur, true);
const largeString = jsc.suchthat(jsc.nestring, str => str.length > 5);
describe("Our string manipulation function", () => {
it("should remove all instances of dash (-)", () => {
expect(jsc.checkForall(jsc.array(jsc.nestring), noDashesProperty)).toBe(true);
});
it("should make sure the resulting concatenation is shorter than 200 chars", () => {
expect(jsc.checkForall(jsc.array(largeString), limitedStringLengthProperty)).toBe(true);
});
it("should make sure the resulting concatenation is shorter than 200 chars", () => {
expect(jsc.checkForall(jsc.array(jsc.nestring), lowerCaseProperty)).toBe(true);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment