From: Joe Wreschnig Date: Mon, 19 May 2014 12:04:05 +0000 (+0200) Subject: Share common lerp requirement checks. X-Git-Tag: 1.0.0~5 X-Git-Url: https://git.yukkurigames.com/?a=commitdiff_plain;h=4c3e2d57468012099df78961480fc9634cca9829;p=string-lerp.git Share common lerp requirement checks. --- diff --git a/tests/string-lerp.js b/tests/string-lerp.js index f2fb88d..04fc444 100644 --- a/tests/string-lerp.js +++ b/tests/string-lerp.js @@ -1,14 +1,12 @@ var JS = this.JS || require('jstest'); -var m = require('../string-lerp'); +var stringLerp = require('../string-lerp'); var HALF_POOS = /\uD83D\uD83D|\uDCA9\uDCA9|\uD83D$/; var UNUSUAL_Q = 'q\u0307\u0323'; +var A = "Do you like green eggs and ham?"; +var B = "I do not like them, Sam-I-am."; -JS.Test.describe('fast lerp', function () { with (this) { - var lerp = m.fastLerp; - var A = "Do you like green eggs and ham?"; - var B = "I do not like them, Sam-I-am."; - +function itHandlesBaseCases (parent, lerp) { with (parent) { it("handles empty strings", function () { with (this) { assertEqual("", lerp("", "", -1)); assertEqual("", lerp("", "", 0)); @@ -23,25 +21,9 @@ JS.Test.describe('fast lerp', function () { with (this) { assertEqual(B, lerp(B, B, i)); } }}); +}} - it("handles lows", function () { with (this) { - assertEqual(A, lerp(A, B, -Infinity)); - assertEqual(A, lerp(A, B, -1)); - assertEqual(A, lerp(A, B, 0)); - }}); - - it("handles highs", function () { with (this) { - assertEqual(B, lerp(A, B, 1)); - assertEqual(B, lerp(A, B, 2)); - assertEqual(B, lerp(A, B, Infinity)); - }}); - - it("crams strings together", function () { with (this) { - assertEqual("I do not ke green eggs and ham?", lerp(A, B, 0.3)); - assertEqual("I do not like tn eggs and ham?", lerp(A, B, 0.5)); - assertEqual("I do not like them, Sam-I-am?", lerp(A, B, 0.98)); - }}); - +function itMakesValidStrings (parent, lerp) { with (parent) { it("doesn't make half a poo", function () { with (this) { var poos = "\uD83D\uDCA9\uD83D\uDCA9\uD83D\uDCA9\uD83D\uDCA9"; assertEqual("\uD83D\uDCA9\uD83D\uDCA9", lerp("", poos, 0.5)); @@ -57,28 +39,9 @@ JS.Test.describe('fast lerp', function () { with (this) { assert(r === "a" || r === UNUSUAL_Q); } }}); -}}); - -JS.Test.describe('diff lerp', function () { with (this) { - var lerp = m.diffLerp; - var A = "Do you like green eggs and ham?"; - var B = "I do not like them, Sam-I-am."; - - it("handles empty strings", function () { with (this) { - assertEqual("", lerp("", "", -1)); - assertEqual("", lerp("", "", 0)); - assertEqual("", lerp("", "", 0.5)); - assertEqual("", lerp("", "", 1)); - assertEqual("", lerp("", "", 2)); - }}); - - it("maintains identity", function () { with (this) { - for (var i = -1; i < 2; i += 1/1024) { - assertEqual(A, lerp(A, A, i)); - assertEqual(B, lerp(B, B, i)); - } - }}); +}} +function itDoesntOvershoot (parent, lerp) { with (parent) { it("handles lows", function () { with (this) { assertEqual(A, lerp(A, B, -Infinity)); assertEqual(A, lerp(A, B, -1)); @@ -90,7 +53,24 @@ JS.Test.describe('diff lerp', function () { with (this) { assertEqual(B, lerp(A, B, 2)); assertEqual(B, lerp(A, B, Infinity)); }}); +}} + +JS.Test.describe('fast lerp', function () { with (this) { + var lerp = stringLerp.fastLerp; + + itHandlesBaseCases(this, lerp); + itMakesValidStrings(this, lerp); + itDoesntOvershoot(this, lerp); + + it("crams strings together", function () { with (this) { + assertEqual("I do not ke green eggs and ham?", lerp(A, B, 0.3)); + assertEqual("I do not like tn eggs and ham?", lerp(A, B, 0.5)); + assertEqual("I do not like them, Sam-I-am?", lerp(A, B, 0.98)); + }}); + +}}); +function itEditsHumanly(parent, lerp) { with (parent) { it("edits strings", function () { with (this) { assertEqual(A, lerp(A, B, 0.01)); assertEqual("I do not like treen eggs and ham?", lerp(A, B, 0.3)); @@ -99,22 +79,6 @@ JS.Test.describe('diff lerp', function () { with (this) { assertEqual(B, lerp(A, B, 0.99)); }}); - it("doesn't make half a poo", function () { with (this) { - var poos = "\uD83D\uDCA9\uD83D\uDCA9\uD83D\uDCA9\uD83D\uDCA9"; - assertEqual("\uD83D\uDCA9\uD83D\uDCA9", lerp("", poos, 0.5)); - assertEqual("\uD83D\uDCA9", lerp("", poos, 0.35)); - - for (var i = 0; i <= 1; i += 1/256) - assertNot(lerp("", poos, i).match(HALF_POOS)); - }}); - - it("doesn't misplace combining marks", function () { with (this) { - for (var i = 0; i <= 1; i += 1/256) { - var r = lerp("a", UNUSUAL_Q, i); - assert(r === "a" || r === UNUSUAL_Q); - } - }}); - it("prefers ins/del to sub/sub", function () { with (this) { // When the cost is uniform this string can be transformed by // rewriting the whole thing for the same cost as deleting the @@ -130,19 +94,18 @@ JS.Test.describe('diff lerp', function () { with (this) { // costs. assertEqual("core", lerp("apple core", "core dump", 0.51)); }}); -}}); +}} -JS.Test.describe('numeric lerp', function () { with (this) { - var lerp = m.numericLerp; +JS.Test.describe('diff lerp', function () { with (this) { + var lerp = stringLerp.diffLerp; - it("handles empty strings", function () { with (this) { - assertEqual("", lerp("", "", -1)); - assertEqual("", lerp("", "", 0)); - assertEqual("", lerp("", "", 0.5)); - assertEqual("", lerp("", "", 1)); - assertEqual("", lerp("", "", 2)); - }}); + itHandlesBaseCases(this, lerp); + itMakesValidStrings(this, lerp); + itDoesntOvershoot(this, lerp); + itEditsHumanly(this, lerp); +}}); +function itTreatsNumeralsAsNumbers(parent, lerp) { with (parent) { it("handles single numbers", function () { with (this) { assertEqual("0", lerp("0", "100", 0)); assertEqual("50", lerp("0", "100", 0.5)); @@ -167,43 +130,27 @@ JS.Test.describe('numeric lerp', function () { with (this) { }}); it("handles multiple numbers", function () { with (this) { - var A = "Chapter 0. The sky was rgb(0, 0, 0)." - var B = "Chapter 10. The sky was rgb(0, 0, 255)." + var A = "Chapter 0. The sky was rgb(0, 0, 0)."; + var B = "Chapter 10. The sky was rgb(0, 0, 255)."; assertEqual(A, lerp(A, B, 0)); assertEqual(B, lerp(A, B, 1)); assertEqual("Chapter 5. The sky was rgb(0, 0, 128).", lerp(A, B, 0.5)); }}); -}}); - -JS.Test.describe('fast lerp', function () { with (this) { - var lerp = m.lerp; - var A = "Do you like green eggs and ham?"; - var B = "I do not like them, Sam-I-am."; +}} - it("handles empty strings", function () { with (this) { - assertEqual("", lerp("", "", -1)); - assertEqual("", lerp("", "", 0)); - assertEqual("", lerp("", "", 0.5)); - assertEqual("", lerp("", "", 1)); - assertEqual("", lerp("", "", 2)); - }}); +JS.Test.describe('numeric lerp', function () { with (this) { + var lerp = stringLerp.numericLerp; - it("maintains identity", function () { with (this) { - for (var i = -1; i < 2; i += 1/1024) { - assertEqual(A, lerp(A, A, i)); - assertEqual(B, lerp(B, B, i)); - } - }}); + itHandlesBaseCases(this, lerp); + itTreatsNumeralsAsNumbers(this, lerp); +}}); - it("handles lows", function () { with (this) { - assertEqual(A, lerp(A, B, -Infinity)); - assertEqual(A, lerp(A, B, -1)); - assertEqual(A, lerp(A, B, 0)); - }}); +JS.Test.describe('auto lerp', function () { with (this) { + var lerp = stringLerp.lerp; - it("handles highs", function () { with (this) { - assertEqual(B, lerp(A, B, 1)); - assertEqual(B, lerp(A, B, 2)); - assertEqual(B, lerp(A, B, Infinity)); - }}); + itHandlesBaseCases(this, lerp); + itMakesValidStrings(this, lerp); + itDoesntOvershoot(this, lerp); + itTreatsNumeralsAsNumbers(this, lerp); + itEditsHumanly(this, lerp); }});