Refactor test helpers (#1336)

* Refactor test helpers

* Combine common codes from `parse` and `build`, and dispatch using
`Mode`
* Remove `toNotBuild` and `toNotParse` and use `expect.not`
* Remove `Warning` and instead mock `console.warn`
* Add `expect.toHavePassed` and check whether parse/build succeeded
lazily
* Improve failed test `message`:
  - Use color
  - Print stack traces(excluding internals)
  - Print diff
  - Follow jest matcher output style

* Update helpers.js

* Remove toBeTruthy checks

getParsed throws an error if parsing fails.

* Used tagged literals

* Use .toHaveLength

* Use tagged literals

* Use to{Build,Parse}Like where possible

* Remove compareParseTree

* Use snapshot where possible

* Join into one line where <88 chars

* Revert console.warn() to throw an error

Merge `expectToWarn` to `expectKaTeX`, like
`expect.toFailWithParseError`

* Remove call to console.warn from stack traces

* Fix merge errors

* Remove `getTree`

* Move `_getBuilt` into `getBuilt`
* Move default settings and tagging literal support in to `getParsed` 
and `getBuilt`
* Remove stack traces clean-up
* Remove `toHavePassed` matcher

* Extract `expected` string construction into `printExpectedResult`
This commit is contained in:
ylemkimon
2018-07-22 11:06:07 +09:00
committed by Kevin Barabash
parent c9947220b6
commit 71035c7111
7 changed files with 1054 additions and 1200 deletions

View File

@@ -8,92 +8,92 @@ import {strictSettings, nonstrictSettings} from "./helpers";
describe("unicode", function() {
it("should parse Latin-1 inside \\text{}", function() {
expect('\\text{ÀÁÂÃÄÅÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåèéêëìíîïñòóôõöùúûüýÿ' +
'ÆÇÐØÞßæçðøþ}').toParse();
expect`\text{ÀÁÂÃÄÅÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåèéêëìíîïñòóôõöùúûüýÿÆÇÐØÞßæçðøþ}`
.toParse();
});
it("should not parse Latin-1 outside \\text{} with strict", function() {
const chars = 'ÀÁÂÃÄÅÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåèéêëìíîïñòóôõöùúûüýÿÇÐÞçþ';
for (const ch of chars) {
expect(ch).toNotParse(strictSettings);
expect(ch).not.toParse(strictSettings);
}
});
it("should parse Latin-1 outside \\text{}", function() {
expect('ÀÁÂÃÄÅÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåèéêëìíîïñòóôõöùúûüýÿ' +
'ÇÐÞçðþ').toParse(nonstrictSettings);
expect`ÀÁÂÃÄÅÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåèéêëìíîïñòóôõöùúûüýÿÇÐÞçðþ`
.toParse(nonstrictSettings);
});
it("should parse all lower case Greek letters", function() {
expect("αβγδεϵζηθϑικλμνξοπϖρϱςστυφϕχψω").toParse();
expect`αβγδεϵζηθϑικλμνξοπϖρϱςστυφϕχψω`.toParse();
});
it("should parse math upper case Greek letters", function() {
expect("ΓΔΘΛΞΠΣΥΦΨΩ").toParse();
expect`ΓΔΘΛΞΠΣΥΦΨΩ`.toParse();
});
it("should parse Cyrillic inside \\text{}", function() {
expect('\\text{БГДЖЗЙЛФЦШЫЮЯ}').toParse();
expect`\text{БГДЖЗЙЛФЦШЫЮЯ}`.toParse();
});
it("should not parse Cyrillic outside \\text{} with strict", function() {
expect('БГДЖЗЙЛФЦШЫЮЯ').toNotParse(strictSettings);
expect`БГДЖЗЙЛФЦШЫЮЯ`.not.toParse(strictSettings);
});
it("should parse CJK inside \\text{}", function() {
expect('\\text{私はバナナです}').toParse();
expect('\\text{여보세요}').toParse();
expect`\text{私はバナナです}`.toParse();
expect`\text{여보세요}`.toParse();
});
it("should not parse CJK outside \\text{} with strict", function() {
expect('私はバナナです。').toNotParse(strictSettings);
expect('여보세요').toNotParse(strictSettings);
expect`私はバナナです。`.not.toParse(strictSettings);
expect`여보세요`.not.toParse(strictSettings);
});
it("should parse Devangari inside \\text{}", function() {
expect('\\text{नमस्ते}').toParse();
expect`\text{नमस्ते}`.toParse();
});
it("should not parse Devangari outside \\text{} with strict", function() {
expect('नमस्ते').toNotParse(strictSettings);
expect`नमस्ते`.not.toParse(strictSettings);
});
it("should parse Georgian inside \\text{}", function() {
expect('\\text{გამარჯობა}').toParse();
expect`\text{გამარჯობა}`.toParse();
});
it("should not parse Georgian outside \\text{} with strict", function() {
expect('გამარჯობა').toNotParse(strictSettings);
expect`გამარჯობა`.not.toParse(strictSettings);
});
it("should parse extended Latin characters inside \\text{}", function() {
expect('\\text{ěščřžůřťďňőİı}').toParse();
expect`\text{ěščřžůřťďňőİı}`.toParse();
});
it("should not parse extended Latin outside \\text{} with strict", function() {
expect('ěščřžůřťďňőİı').toNotParse(strictSettings);
expect`ěščřžůřťďňőİı`.not.toParse(strictSettings);
});
it("should not allow emoji in strict mode", function() {
expect('✌').toNotParse(strictSettings);
expect('\\text{✌}').toNotParse(strictSettings);
expect``.not.toParse(strictSettings);
expect`\text{✌}`.not.toParse(strictSettings);
const settings = new Settings({
strict: (errorCode) =>
(errorCode === "unknownSymbol" ? "error" : "ignore"),
});
expect('✌').toNotParse(settings);
expect('\\text{✌}').toNotParse(settings);
expect``.not.toParse(settings);
expect`\text{✌}`.not.toParse(settings);
});
it("should allow emoji outside strict mode", function() {
expect('✌').toWarn();
expect('\\text{✌}').toWarn();
expect``.toWarn();
expect`\text{✌}`.toWarn();
const settings = new Settings({
strict: (errorCode) =>
(errorCode === "unknownSymbol" ? "ignore" : "error"),
});
expect('✌').toParse(settings);
expect('\\text{✌}').toParse(settings);
expect``.toParse(settings);
expect`\text{✌}`.toParse(settings);
});
});