Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update toSplitInto to accept delimiters object #3444

Merged
merged 3 commits into from Aug 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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