+/*global require, it, assert, assertEqual, assertNot */
+/*jshint -W085 */ // "Don't use 'with'."
+
var JS = this.JS || require('jstest');
-var m = require('../string-lerp');
+var stringLerp = require('../string-lerp');
-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.";
+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.";
+function itHandlesBaseCases (parent, lerp) { with (parent) {
it("handles empty strings", function () { with (this) {
assertEqual("", lerp("", "", -1));
assertEqual("", lerp("", "", 0));
assertEqual(B, lerp(B, B, i));
}
}});
+}}
+
+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));
+ 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);
+ }
+ }});
+}}
+function itDoesntOvershoot (parent, lerp) { with (parent) {
it("handles lows", function () { with (this) {
assertEqual(A, lerp(A, B, -Infinity));
assertEqual(A, lerp(A, B, -1));
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));
}});
-}});
-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));
+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));
+ assertEqual("I do not like them, eggs and ham?", lerp(A, B, 0.5));
+ assertEqual("I do not like them, Sam-Iham?", lerp(A, B, 0.9));
+ assertEqual(B, lerp(A, B, 0.99));
}});
- 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));
- }
+ 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
+ // front and adding to the back. But visually, we'd rather do
+ // the latter.
+ assertEqual("core", lerp("hard core", "core dump", 0.50));
}});
- 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("weights ins/del cheaper than sub", function () { with (this) {
+ // When the cost is uniform it is cheaper to rewrite the
+ // former into the latter. But we'd rather keep the "core" for
+ // visual reasons, so we need to make sure we have unequal
+ // costs.
+ assertEqual("core", lerp("apple core", "core dump", 0.51));
}});
+}}
- it("handles highs", function () { with (this) {
- assertEqual(B, lerp(A, B, 1));
- assertEqual(B, lerp(A, B, 2));
- assertEqual(B, lerp(A, B, Infinity));
- }});
+JS.Test.describe('diff lerp', function () { with (this) {
+ var lerp = stringLerp.diffLerp;
- it("edits strings", function () { with (this) {
- assertEqual("I do not like treen eggs and ham?", lerp(A, B, 0.3));
- assertEqual("I do not like them, Sggs and ham?", lerp(A, B, 0.5));
- assertEqual("I do not like them, Sam-Iham?", lerp(A, B, 0.9));
- }});
+ itHandlesBaseCases(this, lerp);
+ itMakesValidStrings(this, lerp);
+ itDoesntOvershoot(this, lerp);
+ itEditsHumanly(this, lerp);
}});
-JS.Test.describe('numeric lerp', function () { with (this) {
- var lerp = m.numericLerp;
-
- it("handles empty strings", function () { with (this) {
- assertEqual("", lerp("", "", -1));
- assertEqual("", lerp("", "", 0));
- assertEqual("", lerp("", "", 0.5));
- assertEqual("", lerp("", "", 1));
- assertEqual("", lerp("", "", 2));
- }});
-
+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));
it("rounds integers", function () { with (this) {
assertEqual("12", lerp("0", "100", 0.123));
- assertEqual("12.3", lerp("0", "100.0", 0.123));
+ assertEqual("12.3", lerp("0.0", "100.0", 0.123));
assertEqual("12.3", lerp("0.0", "100", 0.123));
assertEqual("12.3", lerp("0.0", "100.0", 0.123));
}});
+ it("thinks about precision", function () { with (this) {
+ assertEqual("12.30", lerp("0.00", "100.00", 0.123));
+ assertEqual("12.300", lerp("0.000", "100.000", 0.123));
+ }});
+
it("computes parameters outside [0, 1]", function () { with (this) {
assertEqual("Giving 110%", lerp("Giving 0%", "Giving 100%", 1.1));
}});
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('numeric lerp', function () { with (this) {
+ var lerp = stringLerp.numericLerp;
+
+ itHandlesBaseCases(this, lerp);
+ itTreatsNumeralsAsNumbers(this, lerp);
+}});
+
+JS.Test.describe('auto lerp', function () { with (this) {
+ var lerp = stringLerp.lerp;
+
+ itHandlesBaseCases(this, lerp);
+ itMakesValidStrings(this, lerp);
+ itDoesntOvershoot(this, lerp);
+ itTreatsNumeralsAsNumbers(this, lerp);
+ itEditsHumanly(this, lerp);
}});