Created
May 7, 2018 14:55
-
-
Save ddanielbee/237af28b58d2a33ff361b0093fdba54f to your computer and use it in GitHub Desktop.
Property Based Testing String stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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