Skip to content

Commit

Permalink
refactor: update toSplitInto to accept delimiter object (#3444)
Browse files Browse the repository at this point in the history
Update toSplitInto matcher to accept the same delimiter object type that's used in renderMathInElement

Co-authored-by: ylemkimon <y@ylem.kim>
  • Loading branch information
kkreine and ylemkimon committed Aug 29, 2022
1 parent f04c645 commit 99be728
Showing 1 changed file with 49 additions and 17 deletions.
66 changes: 49 additions & 17 deletions contrib/auto-render/test/auto-render-spec.js
Expand Up @@ -6,15 +6,14 @@ import renderMathInElement from "../auto-render";

beforeEach(function() {
expect.extend({
toSplitInto: function(actual, left, right, result) {
toSplitInto: function(actual, result, delimiters) {
const message = {
pass: true,
message: () => "'" + actual + "' split correctly",
};

const split =
splitAtDelimiters(actual,
[{left: left, right: right, display: false}]);
splitAtDelimiters(actual, delimiters);

if (split.length !== result.length) {
message.pass = false;
Expand Down Expand Up @@ -60,60 +59,76 @@ beforeEach(function() {

describe("A delimiter splitter", function() {
it("doesn't split when there are no delimiters", function() {
expect("hello").toSplitInto("(", ")", [{type: "text", data: "hello"}]);
expect("hello").toSplitInto(
[
{type: "text", data: "hello"},
],
[
{left: "(", right: ")", display: false},
]);
});

it("doesn't create a math node with only one left delimiter", function() {
expect("hello ( world").toSplitInto(
"(", ")",
[
{type: "text", data: "hello "},
{type: "text", data: "( world"},
],
[
{left: "(", right: ")", display: false},
]);
});

it("doesn't split when there's only a right delimiter", function() {
expect("hello ) world").toSplitInto(
"(", ")",
[
{type: "text", data: "hello ) world"},
],
[
{left: "(", right: ")", display: false},
]);
});

it("splits when there are both delimiters", function() {
expect("hello ( world ) boo").toSplitInto(
"(", ")",
[
{type: "text", data: "hello "},
{type: "math", data: " world ",
rawData: "( world )", display: false},
{type: "text", data: " boo"},
],
[
{left: "(", right: ")", display: false},
]);
});

it("splits on multi-character delimiters", function() {
expect("hello [[ world ]] boo").toSplitInto(
"[[", "]]",
[
{type: "text", data: "hello "},
{type: "math", data: " world ",
rawData: "[[ world ]]", display: false},
{type: "text", data: " boo"},
],
[
{left: "[[", right: "]]", display: false},
]);
expect("hello \\begin{equation} world \\end{equation} boo").toSplitInto(
"\\begin{equation}", "\\end{equation}",
[
{type: "text", data: "hello "},
{type: "math", data: "\\begin{equation} world \\end{equation}",
rawData: "\\begin{equation} world \\end{equation}",
display: false},
{type: "text", data: " boo"},
],
[
{left: "\\begin{equation}", right: "\\end{equation}",
display: false},
]);
});

it("splits mutliple times", function() {
expect("hello ( world ) boo ( more ) stuff").toSplitInto(
"(", ")",
[
{type: "text", data: "hello "},
{type: "math", data: " world ",
Expand All @@ -122,62 +137,75 @@ describe("A delimiter splitter", function() {
{type: "math", data: " more ",
rawData: "( more )", display: false},
{type: "text", data: " stuff"},
],
[
{left: "(", right: ")", display: false},
]);
});

it("leaves the ending when there's only a left delimiter", function() {
expect("hello ( world ) boo ( left").toSplitInto(
"(", ")",
[
{type: "text", data: "hello "},
{type: "math", data: " world ",
rawData: "( world )", display: false},
{type: "text", data: " boo "},
{type: "text", data: "( left"},
],
[
{left: "(", right: ")", display: false},
]);
});

it("doesn't split when close delimiters are in {}s", function() {
expect("hello ( world { ) } ) boo").toSplitInto(
"(", ")",
[
{type: "text", data: "hello "},
{type: "math", data: " world { ) } ",
rawData: "( world { ) } )", display: false},
{type: "text", data: " boo"},
],
[
{left: "(", right: ")", display: false},
]);

expect("hello ( world { { } ) } ) boo").toSplitInto(
"(", ")",
[
{type: "text", data: "hello "},
{type: "math", data: " world { { } ) } ",
rawData: "( world { { } ) } )", display: false},
{type: "text", data: " boo"},
],
[
{left: "(", right: ")", display: false},
]);
});

it("correctly processes sequences of $..$", function() {
expect("$hello$$world$$boo$").toSplitInto(
"$", "$",
[
{type: "math", data: "hello",
rawData: "$hello$", display: false},
{type: "math", data: "world",
rawData: "$world$", display: false},
{type: "math", data: "boo",
rawData: "$boo$", display: false},
],
[
{left: "$", right: "$", display: false},
]);
});

it("doesn't split at escaped delimiters", function() {
expect("hello ( world \\) ) boo").toSplitInto(
"(", ")",
[
{type: "text", data: "hello "},
{type: "math", data: " world \\) ",
rawData: "( world \\) )", display: false},
{type: "text", data: " boo"},
],
[
{left: "(", right: ")", display: false},
]);

/* TODO(emily): make this work maybe?
Expand All @@ -194,21 +222,25 @@ describe("A delimiter splitter", function() {

it("splits when the right and left delimiters are the same", function() {
expect("hello $ world $ boo").toSplitInto(
"$", "$",
[
{type: "text", data: "hello "},
{type: "math", data: " world ",
rawData: "$ world $", display: false},
{type: "text", data: " boo"},
],
[
{left: "$", right: "$", display: false},
]);
});

it("ignores \\$", function() {
expect("$x = \\$5$").toSplitInto(
"$", "$",
[
{type: "math", data: "x = \\$5",
rawData: "$x = \\$5$", display: false},
],
[
{left: "$", right: "$", display: false},
]);
});

Expand Down

0 comments on commit 99be728

Please sign in to comment.