]> Lady’s Gitweb - Pisces/blobdiff - string.test.js
Add toIntegralNumber to numeric.js
[Pisces] / string.test.js
index f70faf6be0b569a7e23ab22c483a6c34f7a44400..6407d9415b2aa469006e517db5fc66adbd2c93e7 100644 (file)
 import {
   assert,
   assertEquals,
+  assertSpyCall,
+  assertSpyCalls,
   assertStrictEquals,
   assertThrows,
   describe,
   it,
+  spy,
 } from "./dev-deps.js";
 import {
   asciiLowercase,
@@ -29,6 +32,7 @@ import {
   splitOnCommas,
   stripAndCollapseASCIIWhitespace,
   stripLeadingAndTrailingASCIIWhitespace,
+  toString,
 } from "./string.js";
 
 describe("Matcher", () => {
@@ -59,6 +63,10 @@ describe("Matcher", () => {
     assert(new Matcher("") instanceof RegExp);
   });
 
+  it("[[Construct]] throws when provided with a noncallable, non·null third argument", () => {
+    assertThrows(() => new Matcher("", undefined, "failure"));
+  });
+
   describe("::dotAll", () => {
     it("[[Get]] returns true when the dotAll flag is present", () => {
       assertStrictEquals(new Matcher(/(?:)/su).dotAll, true);
@@ -75,10 +83,47 @@ describe("Matcher", () => {
         [...new Matcher(/.(?<wow>(?:.(?=.))*)(.)?/u).exec("success")],
         ["success", "ucces", "s"],
       );
+      assertEquals(
+        [...new Matcher(
+          /.(?<wow>(?:.(?=.))*)(.)?/u,
+          undefined,
+          ($) => $ === "success",
+        ).exec("success")],
+        ["success", "ucces", "s"],
+      );
+    });
+
+    it("[[Call]] calls the constraint if the match succeeds", () => {
+      const constraint = spy((_) => true);
+      const matcher = new Matcher("(.).*", undefined, constraint);
+      const result = matcher.exec({
+        toString() {
+          return "etaoin";
+        },
+      });
+      assertSpyCalls(constraint, 1);
+      assertSpyCall(constraint, 0, {
+        args: ["etaoin", result, matcher],
+        self: undefined,
+      });
+    });
+
+    it("[[Call]] does not call the constraint if the match fails", () => {
+      const constraint = spy((_) => true);
+      const matcher = new Matcher("", undefined, constraint);
+      matcher.exec("failure");
+      assertSpyCalls(constraint, 0);
     });
 
     it("[[Call]] returns null given a partial match", () => {
-      assertEquals(new Matcher("").exec("failure"), null);
+      assertStrictEquals(new Matcher("").exec("failure"), null);
+    });
+
+    it("[[Call]] returns null if the constraint fails", () => {
+      assertStrictEquals(
+        new Matcher(".*", undefined, () => false).exec(""),
+        null,
+      );
     });
   });
 
@@ -149,12 +194,43 @@ describe("Matcher", () => {
     it("[[Call]] returns true for a complete match", () => {
       assertStrictEquals(new Matcher("")(""), true);
       assertStrictEquals(new Matcher(/.*/su)("success\nyay"), true);
+      assertStrictEquals(
+        new Matcher(/.*/su, undefined, ($) => $ === "success")(
+          "success",
+        ),
+        true,
+      );
+    });
+
+    it("[[Call]] calls the constraint if the match succeeds", () => {
+      const constraint = spy((_) => true);
+      const matcher = new Matcher("(.).*", undefined, constraint);
+      matcher("etaoin");
+      assertSpyCalls(constraint, 1);
+      assertEquals(constraint.calls[0].args[0], "etaoin");
+      assertEquals([...constraint.calls[0].args[1]], ["etaoin", "e"]);
+      assertEquals(constraint.calls[0].args[2], matcher);
+      assertEquals(constraint.calls[0].self, undefined);
+    });
+
+    it("[[Call]] does not call the constraint if the match fails", () => {
+      const constraint = spy((_) => true);
+      const matcher = new Matcher("", undefined, constraint);
+      matcher("failure");
+      assertSpyCalls(constraint, 0);
     });
 
     it("[[Call]] returns false for a partial match", () => {
       assertStrictEquals(new Matcher("")("failure"), false);
       assertStrictEquals(new Matcher(/.*/u)("failure\nno"), false);
     });
+
+    it("[[Call]] returns false if the constraint fails", () => {
+      assertStrictEquals(
+        new Matcher(".*", undefined, () => false)(""),
+        false,
+      );
+    });
   });
 
   describe("~lastIndex", () => {
@@ -529,3 +605,20 @@ describe("stripLeadingAndTrailingASCIIWhitespace", () => {
     );
   });
 });
+
+describe("toString", () => {
+  it("[[Call]] converts to a string", () => {
+    assertStrictEquals(
+      toString({
+        toString() {
+          return "success";
+        },
+      }),
+      "success",
+    );
+  });
+
+  it("[[Call]] throws when provided a symbol", () => {
+    assertThrows(() => toString(Symbol()));
+  });
+});
This page took 0.022218 seconds and 4 git commands to generate.