forked from KaTeX/KaTeX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkatex-spec.js
4036 lines (3336 loc) · 148 KB
/
katex-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint max-len:0 */
import buildMathML from "../src/buildMathML";
import buildTree from "../src/buildTree";
import katex from "../katex";
import parseTree from "../src/parseTree";
import Options from "../src/Options";
import Settings from "../src/Settings";
import Style from "../src/Style";
import {
strictSettings, nonstrictSettings, trustSettings, r,
getBuilt, getParsed, stripPositions,
} from "./helpers";
const defaultOptions = new Options({
style: Style.TEXT,
size: 5,
maxSize: Infinity,
});
describe("A parser", function() {
it("should not fail on an empty string", function() {
expect``.toParse(strictSettings);
});
it("should ignore whitespace", function() {
expect` x y `.toParseLike("xy", strictSettings);
});
it("should ignore whitespace in atom", function() {
expect` x ^ y `.toParseLike("x^y", strictSettings);
});
});
describe("An ord parser", function() {
const expression = "1234|/@.\"`abcdefgzABCDEFGZ";
it("should not fail", function() {
expect(expression).toParse();
});
it("should build a list of ords", function() {
const parse = getParsed(expression);
for (let i = 0; i < parse.length; i++) {
const group = parse[i];
expect(group.type).toMatch("ord");
}
});
it("should parse the right number of ords", function() {
const parse = getParsed(expression);
expect(parse).toHaveLength(expression.length);
});
});
describe("A bin parser", function() {
const expression = r`+-*\cdot\pm\div`;
it("should not fail", function() {
expect(expression).toParse();
});
it("should build a list of bins", function() {
const parse = getParsed(expression);
for (let i = 0; i < parse.length; i++) {
const group = parse[i];
expect(group.type).toEqual("atom");
expect(group.family).toEqual("bin");
}
});
});
describe("A rel parser", function() {
const expression = r`=<>\leq\geq\neq\nleq\ngeq\cong`;
const notExpression = r`\not=\not<\not>\not\leq\not\geq\not\in`;
it("should not fail", function() {
expect(expression).toParse();
expect(notExpression).toParse();
});
it("should build a list of rels", function() {
const parse = getParsed(expression);
for (let i = 0; i < parse.length; i++) {
let group = parse[i];
if (group.type === "htmlmathml") {
expect(group.html).toHaveLength(1);
group = group.html[0];
}
if (group.type === "mclass") {
expect(group.mclass).toEqual("mrel");
} else {
expect(group.type).toEqual("atom");
expect(group.family).toEqual("rel");
}
}
});
});
describe("A punct parser", function() {
const expression = ",;";
it("should not fail", function() {
expect(expression).toParse(strictSettings);
});
it("should build a list of puncts", function() {
const parse = getParsed(expression);
for (let i = 0; i < parse.length; i++) {
const group = parse[i];
expect(group.type).toEqual("atom");
expect(group.family).toEqual("punct");
}
});
});
describe("An open parser", function() {
const expression = "([";
it("should not fail", function() {
expect(expression).toParse();
});
it("should build a list of opens", function() {
const parse = getParsed(expression);
for (let i = 0; i < parse.length; i++) {
const group = parse[i];
expect(group.type).toEqual("atom");
expect(group.family).toEqual("open");
}
});
});
describe("A close parser", function() {
const expression = ")]?!";
it("should not fail", function() {
expect(expression).toParse();
});
it("should build a list of closes", function() {
const parse = getParsed(expression);
for (let i = 0; i < parse.length; i++) {
const group = parse[i];
expect(group.type).toEqual("atom");
expect(group.family).toEqual("close");
}
});
});
describe("A \\KaTeX parser", function() {
it("should not fail", function() {
expect`\KaTeX`.toParse();
});
});
describe("A subscript and superscript parser", function() {
it("should not fail on superscripts", function() {
expect`x^2`.toParse();
});
it("should not fail on subscripts", function() {
expect`x_3`.toParse();
});
it("should not fail on both subscripts and superscripts", function() {
expect`x^2_3`.toParse();
expect`x_2^3`.toParse();
});
it("should not fail when there is no nucleus", function() {
expect`^3`.toParse();
expect`^3+`.toParse();
expect`_2`.toParse();
expect`^3_2`.toParse();
expect`_2^3`.toParse();
});
it("should produce supsubs for superscript", function() {
const parse = getParsed`x^2`[0];
expect(parse.type).toBe("supsub");
expect(parse.base).toBeDefined();
expect(parse.sup).toBeDefined();
expect(parse.sub).toBeUndefined();
});
it("should produce supsubs for subscript", function() {
const parse = getParsed`x_3`[0];
expect(parse.type).toBe("supsub");
expect(parse.base).toBeDefined();
expect(parse.sub).toBeDefined();
expect(parse.sup).toBeUndefined();
});
it("should produce supsubs for ^_", function() {
const parse = getParsed`x^2_3`[0];
expect(parse.type).toBe("supsub");
expect(parse.base).toBeDefined();
expect(parse.sup).toBeDefined();
expect(parse.sub).toBeDefined();
});
it("should produce supsubs for _^", function() {
const parse = getParsed`x_3^2`[0];
expect(parse.type).toBe("supsub");
expect(parse.base).toBeDefined();
expect(parse.sup).toBeDefined();
expect(parse.sub).toBeDefined();
});
it("should produce the same thing regardless of order", function() {
expect`x^2_3`.toParseLike`x_3^2`;
});
it("should not parse double subscripts or superscripts", function() {
expect`x^x^x`.not.toParse();
expect`x_x_x`.not.toParse();
expect`x_x^x_x`.not.toParse();
expect`x_x^x^x`.not.toParse();
expect`x^x_x_x`.not.toParse();
expect`x^x_x^x`.not.toParse();
});
it("should work correctly with {}s", function() {
expect`x^{2+3}`.toParse();
expect`x_{3-2}`.toParse();
expect`x^{2+3}_3`.toParse();
expect`x^2_{3-2}`.toParse();
expect`x^{2+3}_{3-2}`.toParse();
expect`x_{3-2}^{2+3}`.toParse();
expect`x_3^{2+3}`.toParse();
expect`x_{3-2}^2`.toParse();
});
it("should work with nested super/subscripts", function() {
expect`x^{x^x}`.toParse();
expect`x^{x_x}`.toParse();
expect`x_{x^x}`.toParse();
expect`x_{x_x}`.toParse();
});
});
describe("A subscript and superscript tree-builder", function() {
it("should not fail when there is no nucleus", function() {
expect`^3`.toBuild();
expect`_2`.toBuild();
expect`^3_2`.toBuild();
expect`_2^3`.toBuild();
});
});
describe("A parser with limit controls", function() {
it("should fail when the limit control is not preceded by an op node", function() {
expect`3\nolimits_2^2`.not.toParse();
expect`\sqrt\limits_2^2`.not.toParse();
expect`45 +\nolimits 45`.not.toParse();
});
it("should parse when the limit control directly follows an op node", function() {
expect`\int\limits_2^2 3`.toParse();
expect`\sum\nolimits_3^4 4`.toParse();
});
it("should parse when the limit control is in the sup/sub area of an op node", function() {
expect`\int_2^2\limits`.toParse();
expect`\int^2\nolimits_2`.toParse();
expect`\int_2\limits^2`.toParse();
});
it("should allow multiple limit controls in the sup/sub area of an op node", function() {
expect`\int_2\nolimits^2\limits 3`.toParse();
expect`\int\nolimits\limits_2^2`.toParse();
expect`\int\limits\limits\limits_2^2`.toParse();
});
it("should have the rightmost limit control determine the limits property " +
"of the preceding op node", function() {
let parsedInput = getParsed`\int\nolimits\limits_2^2`;
expect(parsedInput[0].base.limits).toBe(true);
parsedInput = getParsed`\int\limits_2\nolimits^2`;
expect(parsedInput[0].base.limits).toBe(false);
});
});
describe("A group parser", function() {
it("should not fail", function() {
expect`{xy}`.toParse();
});
it("should produce a single ord", function() {
const parse = getParsed`{xy}`;
expect(parse).toHaveLength(1);
const ord = parse[0];
expect(ord.type).toMatch("ord");
expect(ord.body).toBeTruthy();
});
});
describe("A \\begingroup...\\endgroup parser", function() {
it("should not fail", function() {
expect`\begingroup xy \endgroup`.toParse();
});
it("should fail when it is mismatched", function() {
expect`\begingroup xy`.not.toParse();
expect`\begingroup xy }`.not.toParse();
});
it("should produce a semi-simple group", function() {
const parse = getParsed`\begingroup xy \endgroup`;
expect(parse).toHaveLength(1);
const ord = parse[0];
expect(ord.type).toMatch("ord");
expect(ord.body).toBeTruthy();
expect(ord.semisimple).toBeTruthy();
});
it("should not affect spacing in math mode", function() {
expect`\begingroup x+ \endgroup y`.toBuildLike`x+y`;
});
});
describe("An implicit group parser", function() {
it("should not fail", function() {
expect`\Large x`.toParse();
expect`abc {abc \Large xyz} abc`.toParse();
});
it("should produce a single object", function() {
const parse = getParsed`\Large abc`;
expect(parse).toHaveLength(1);
const sizing = parse[0];
expect(sizing.type).toEqual("sizing");
expect(sizing.body).toBeTruthy();
expect(sizing.size).toBeDefined();
});
it("should apply only after the function", function() {
const parse = getParsed`a \Large abc`;
expect(parse).toHaveLength(2);
const sizing = parse[1];
expect(sizing.type).toEqual("sizing");
expect(sizing.body).toHaveLength(3);
});
it("should stop at the ends of groups", function() {
const parse = getParsed`a { b \Large c } d`;
const group = parse[1];
const sizing = group.body[1];
expect(sizing.type).toEqual("sizing");
expect(sizing.body).toHaveLength(1);
});
describe("within optional groups", () => {
it("should work with sizing commands: \\sqrt[\\small 3]{x}", () => {
const tree = stripPositions(getParsed`\sqrt[\small 3]{x}`);
expect(tree).toMatchSnapshot();
});
it("should work with \\color: \\sqrt[\\color{red} 3]{x}", () => {
const tree = stripPositions(getParsed`\sqrt[\color{red} 3]{x}`);
expect(tree).toMatchSnapshot();
});
it("should work style commands \\sqrt[\\textstyle 3]{x}", () => {
const tree = stripPositions(getParsed`\sqrt[\textstyle 3]{x}`);
expect(tree).toMatchSnapshot();
});
it("should work with old font functions: \\sqrt[\\tt 3]{x}", () => {
const tree = stripPositions(getParsed`\sqrt[\tt 3]{x}`);
expect(tree).toMatchSnapshot();
});
});
});
describe("A function parser", function() {
it("should parse no argument functions", function() {
expect`\div`.toParse();
});
it("should parse 1 argument functions", function() {
expect`\blue x`.toParse();
});
it("should parse 2 argument functions", function() {
expect`\frac 1 2`.toParse();
});
it("should not parse 1 argument functions with no arguments", function() {
expect`\blue`.not.toParse();
});
it("should not parse 2 argument functions with 0 or 1 arguments", function() {
expect`\frac`.not.toParse();
expect`\frac 1`.not.toParse();
});
it("should not parse a function with text right after it", function() {
expect`\redx`.not.toParse();
});
it("should parse a function with a number right after it", function() {
expect`\frac12`.toParse();
});
it("should parse some functions with text right after it", function() {
expect`\;x`.toParse();
});
});
describe("A frac parser", function() {
const expression = r`\frac{x}{y}`;
const dfracExpression = r`\dfrac{x}{y}`;
const tfracExpression = r`\tfrac{x}{y}`;
const cfracExpression = r`\cfrac{x}{y}`;
const genfrac1 = r`\genfrac ( ] {0.06em}{0}{a}{b+c}`;
const genfrac2 = r`\genfrac ( ] {0.8pt}{}{a}{b+c}`;
it("should not fail", function() {
expect(expression).toParse();
});
it("should produce a frac", function() {
const parse = getParsed(expression)[0];
expect(parse.type).toEqual("genfrac");
expect(parse.numer).toBeDefined();
expect(parse.denom).toBeDefined();
});
it("should also parse cfrac, dfrac, tfrac, and genfrac", function() {
expect(cfracExpression).toParse();
expect(dfracExpression).toParse();
expect(tfracExpression).toParse();
expect(genfrac1).toParse();
expect(genfrac2).toParse();
});
it("should parse cfrac, dfrac, tfrac, and genfrac as fracs", function() {
const dfracParse = getParsed(dfracExpression)[0];
expect(dfracParse.type).toEqual("genfrac");
expect(dfracParse.numer).toBeDefined();
expect(dfracParse.denom).toBeDefined();
const tfracParse = getParsed(tfracExpression)[0];
expect(tfracParse.type).toEqual("genfrac");
expect(tfracParse.numer).toBeDefined();
expect(tfracParse.denom).toBeDefined();
const cfracParse = getParsed(cfracExpression)[0];
expect(cfracParse.type).toEqual("genfrac");
expect(cfracParse.numer).toBeDefined();
expect(cfracParse.denom).toBeDefined();
const genfracParse = getParsed(genfrac1)[0];
expect(genfracParse.type).toEqual("genfrac");
expect(genfracParse.numer).toBeDefined();
expect(genfracParse.denom).toBeDefined();
expect(genfracParse.leftDelim).toBeDefined();
expect(genfracParse.rightDelim).toBeDefined();
});
it("should fail, given math as a line thickness to genfrac", function() {
const badGenFrac = "\\genfrac ( ] {b+c}{0}{a}{b+c}";
expect(badGenFrac).not.toParse();
});
it("should fail if genfrac is given less than 6 arguments", function() {
const badGenFrac = "\\genfrac ( ] {0.06em}{0}{a}";
expect(badGenFrac).not.toParse();
});
it("should parse atop", function() {
const parse = getParsed`x \atop y`[0];
expect(parse.type).toEqual("genfrac");
expect(parse.numer).toBeDefined();
expect(parse.denom).toBeDefined();
expect(parse.hasBarLine).toEqual(false);
});
});
describe("An over/brace/brack parser", function() {
const simpleOver = r`1 \over x`;
const complexOver = r`1+2i \over 3+4i`;
const braceFrac = r`a+b \brace c+d`;
const brackFrac = r`a+b \brack c+d`;
it("should not fail", function() {
expect(simpleOver).toParse();
expect(complexOver).toParse();
expect(braceFrac).toParse();
expect(brackFrac).toParse();
});
it("should produce a frac", function() {
let parse;
parse = getParsed(simpleOver)[0];
expect(parse.type).toEqual("genfrac");
expect(parse.numer).toBeDefined();
expect(parse.denom).toBeDefined();
parse = getParsed(complexOver)[0];
expect(parse.type).toEqual("genfrac");
expect(parse.numer).toBeDefined();
expect(parse.denom).toBeDefined();
const parseBraceFrac = getParsed(braceFrac)[0];
expect(parseBraceFrac.type).toEqual("genfrac");
expect(parseBraceFrac.numer).toBeDefined();
expect(parseBraceFrac.denom).toBeDefined();
expect(parseBraceFrac.leftDelim).toBeDefined();
expect(parseBraceFrac.rightDelim).toBeDefined();
const parseBrackFrac = getParsed(brackFrac)[0];
expect(parseBrackFrac.type).toEqual("genfrac");
expect(parseBrackFrac.numer).toBeDefined();
expect(parseBrackFrac.denom).toBeDefined();
expect(parseBrackFrac.leftDelim).toBeDefined();
expect(parseBrackFrac.rightDelim).toBeDefined();
});
it("should create a numerator from the atoms before \\over", function() {
const parse = getParsed(complexOver)[0];
const numer = parse.numer;
expect(numer.body).toHaveLength(4);
});
it("should create a denominator from the atoms after \\over", function() {
const parse = getParsed(complexOver)[0];
const denom = parse.denom;
expect(denom.body).toHaveLength(4);
});
it("should handle empty numerators", function() {
const emptyNumerator = r`\over x`;
const parse = getParsed(emptyNumerator)[0];
expect(parse.type).toEqual("genfrac");
expect(parse.numer).toBeDefined();
expect(parse.denom).toBeDefined();
});
it("should handle empty denominators", function() {
const emptyDenominator = r`1 \over`;
const parse = getParsed(emptyDenominator)[0];
expect(parse.type).toEqual("genfrac");
expect(parse.numer).toBeDefined();
expect(parse.denom).toBeDefined();
});
it("should handle \\displaystyle correctly", function() {
const displaystyleExpression = r`\displaystyle 1 \over 2`;
const parse = getParsed(displaystyleExpression)[0];
expect(parse.type).toEqual("genfrac");
expect(parse.numer.body[0].type).toEqual("styling");
expect(parse.denom).toBeDefined();
});
it("should handle \\textstyle correctly", function() {
expect`\textstyle 1 \over 2`.toParseLike`\frac{\textstyle 1}{2}`;
expect`{\textstyle 1} \over 2`.toParseLike`\frac{\textstyle 1}{2}`;
});
it("should handle nested factions", function() {
const nestedOverExpression = r`{1 \over 2} \over 3`;
const parse = getParsed(nestedOverExpression)[0];
expect(parse.type).toEqual("genfrac");
expect(parse.numer.body[0].type).toEqual("genfrac");
expect(parse.numer.body[0].numer.body[0].text).toEqual("1");
expect(parse.numer.body[0].denom.body[0].text).toEqual("2");
expect(parse.denom).toBeDefined();
expect(parse.denom.body[0].text).toEqual("3");
});
it("should fail with multiple overs in the same group", function() {
const badMultipleOvers = r`1 \over 2 + 3 \over 4`;
expect(badMultipleOvers).not.toParse();
const badOverChoose = r`1 \over 2 \choose 3`;
expect(badOverChoose).not.toParse();
});
});
describe("A genfrac builder", function() {
it("should not fail", function() {
expect("\\frac{x}{y}").toBuild();
expect("\\dfrac{x}{y}").toBuild();
expect("\\tfrac{x}{y}").toBuild();
expect("\\cfrac{x}{y}").toBuild();
expect("\\genfrac ( ] {0.06em}{0}{a}{b+c}").toBuild();
expect("\\genfrac ( ] {0.8pt}{}{a}{b+c}").toBuild();
expect("\\genfrac {} {} {0.8pt}{}{a}{b+c}").toBuild();
expect("\\genfrac [ {} {0.8pt}{}{a}{b+c}").toBuild();
});
});
describe("A infix builder", function() {
it("should not fail", function() {
expect("a \\over b").toBuild();
expect("a \\atop b").toBuild();
expect("a \\choose b").toBuild();
expect("a \\brace b").toBuild();
expect("a \\brack b").toBuild();
});
});
describe("A sizing parser", function() {
const sizeExpression = r`\Huge{x}\small{x}`;
it("should not fail", function() {
expect(sizeExpression).toParse();
});
it("should produce a sizing node", function() {
const parse = getParsed(sizeExpression)[0];
expect(parse.type).toEqual("sizing");
expect(parse.size).toBeDefined();
expect(parse.body).toBeDefined();
});
});
describe("A text parser", function() {
const textExpression = r`\text{a b}`;
const noBraceTextExpression = r`\text x`;
const nestedTextExpression =
r`\text{a {b} \blue{c} \textcolor{#fff}{x} \llap{x}}`;
const spaceTextExpression = r`\text{ a \ }`;
const leadingSpaceTextExpression = r`\text {moo}`;
const badTextExpression = r`\text{a b%}`;
const badFunctionExpression = r`\text{\sqrt{x}}`;
const mathTokenAfterText = r`\text{sin}^2`;
it("should not fail", function() {
expect(textExpression).toParse();
});
it("should produce a text", function() {
const parse = getParsed(textExpression)[0];
expect(parse.type).toEqual("text");
expect(parse.body).toBeDefined();
});
it("should produce textords instead of mathords", function() {
const parse = getParsed(textExpression)[0];
const group = parse.body;
expect(group[0].type).toEqual("textord");
});
it("should not parse bad text", function() {
expect(badTextExpression).not.toParse();
});
it("should not parse bad functions inside text", function() {
expect(badFunctionExpression).not.toParse();
});
it("should parse text with no braces around it", function() {
expect(noBraceTextExpression).toParse();
});
it("should parse nested expressions", function() {
expect(nestedTextExpression).toParse();
});
it("should contract spaces", function() {
const parse = getParsed(spaceTextExpression)[0];
const group = parse.body;
expect(group.length).toEqual(4);
expect(group[0].type).toEqual("spacing");
expect(group[1].type).toEqual("textord");
expect(group[2].type).toEqual("spacing");
expect(group[3].type).toEqual("spacing");
});
it("should handle backslash followed by newline", () => {
expect("\\text{\\ \t\r \n \t\r }").toParseLike("\\text{\\ }");
});
it("should accept math mode tokens after its argument", function() {
expect(mathTokenAfterText).toParse();
});
it("should ignore a space before the text group", function() {
const parse = getParsed(leadingSpaceTextExpression)[0];
// [m, o, o]
expect(parse.body).toHaveLength(3);
expect(parse.body.map(n => n.text).join("")).toBe("moo");
});
it("should parse math within text group", function() {
expect`\text{graph: $y = mx + b$}`.toParse(strictSettings);
expect`\text{graph: \(y = mx + b\)}`.toParse(strictSettings);
});
it("should parse math within text within math within text", function() {
expect`\text{hello $x + \text{world $y$} + z$}`.toParse(strictSettings);
expect`\text{hello \(x + \text{world $y$} + z\)}`.toParse(strictSettings);
expect`\text{hello $x + \text{world \(y\)} + z$}`.toParse(strictSettings);
expect`\text{hello \(x + \text{world \(y\)} + z\)}`.toParse(strictSettings);
});
it("should forbid \\( within math mode", function() {
expect`\(`.not.toParse();
expect`\text{$\(x\)$}`.not.toParse();
});
it("should forbid $ within math mode", function() {
expect`$x$`.not.toParse();
expect`\text{\($x$\)}`.not.toParse();
});
it("should detect unbalanced \\)", function() {
expect`\)`.not.toParse();
expect`\text{\)}`.not.toParse();
});
it("should detect unbalanced $", function() {
expect`$`.not.toParse();
expect`\text{$}`.not.toParse();
});
it("should not mix $ and \\(..\\)", function() {
expect`\text{$x\)}`.not.toParse();
expect`\text{\(x$}`.not.toParse();
});
it("should parse spacing functions", function() {
expect`a b\, \; \! \: \> ~ \thinspace \medspace \quad \ `.toBuild();
expect`\enspace \thickspace \qquad \space \nobreakspace`.toBuild();
});
it("should omit spaces after commands", function() {
expect`\text{\textellipsis !}`.toParseLike`\text{\textellipsis!}`;
});
});
describe("A texvc builder", function() {
it("should not fail", function() {
expect("\\lang\\N\\darr\\R\\dArr\\Z\\Darr\\alef\\rang").toBuild();
expect("\\alefsym\\uarr\\Alpha\\uArr\\Beta\\Uarr\\Chi").toBuild();
expect("\\clubs\\diamonds\\hearts\\spades\\cnums\\Complex").toBuild();
expect("\\Dagger\\empty\\harr\\Epsilon\\hArr\\Eta\\Harr\\exist").toBuild();
expect("\\image\\larr\\infin\\lArr\\Iota\\Larr\\isin\\Kappa").toBuild();
expect("\\Mu\\lrarr\\natnums\\lrArr\\Nu\\Lrarr\\Omicron").toBuild();
expect("\\real\\rarr\\plusmn\\rArr\\reals\\Rarr\\Reals\\Rho").toBuild();
expect("\\text{\\sect}\\sdot\\sub\\sube\\supe").toBuild();
expect("\\Tau\\thetasym\\weierp\\Zeta").toBuild();
});
});
describe("A color parser", function() {
const colorExpression = r`\blue{x}`;
const newColorExpression = r`\redA{x}`;
const customColorExpression1 = r`\textcolor{#fA6}{x}`;
const customColorExpression2 = r`\textcolor{#fA6fA6}{x}`;
const customColorExpression3 = r`\textcolor{fA6fA6}{x}`;
const badCustomColorExpression1 = r`\textcolor{bad-color}{x}`;
const badCustomColorExpression2 = r`\textcolor{#fA6f}{x}`;
const badCustomColorExpression3 = r`\textcolor{#gA6}{x}`;
const oldColorExpression = r`\color{#fA6}xy`;
it("should not fail", function() {
expect(colorExpression).toParse();
});
it("should build a color node", function() {
const parse = getParsed(colorExpression)[0];
expect(parse.type).toEqual("color");
expect(parse.color).toBeDefined();
expect(parse.body).toBeDefined();
});
it("should parse a custom color", function() {
expect(customColorExpression1).toParse();
expect(customColorExpression2).toParse();
expect(customColorExpression3).toParse();
});
it("should correctly extract the custom color", function() {
const parse1 = getParsed(customColorExpression1)[0];
const parse2 = getParsed(customColorExpression2)[0];
const parse3 = getParsed(customColorExpression3)[0];
expect(parse1.color).toEqual("#fA6");
expect(parse2.color).toEqual("#fA6fA6");
expect(parse3.color).toEqual("#fA6fA6");
});
it("should not parse a bad custom color", function() {
expect(badCustomColorExpression1).not.toParse();
expect(badCustomColorExpression2).not.toParse();
expect(badCustomColorExpression3).not.toParse();
});
it("should parse new colors from the branding guide", function() {
expect(newColorExpression).toParse();
});
it("should use one-argument \\color by default", function() {
expect(oldColorExpression).toParseLike`\textcolor{#fA6}{xy}`;
});
it("should use one-argument \\color if requested", function() {
expect(oldColorExpression).toParseLike(r`\textcolor{#fA6}{xy}`, {
colorIsTextColor: false,
});
});
it("should use two-argument \\color if requested", function() {
expect(oldColorExpression).toParseLike(r`\textcolor{#fA6}{x}y`, {
colorIsTextColor: true,
});
});
it("should not define \\color in global context", function() {
const macros = {};
expect(oldColorExpression).toParseLike(r`\textcolor{#fA6}{x}y`, {
colorIsTextColor: true,
globalGroup: true,
macros: macros,
});
expect(macros).toEqual({});
});
});
describe("A tie parser", function() {
const mathTie = "a~b";
const textTie = r`\text{a~ b}`;
it("should parse ties in math mode", function() {
expect(mathTie).toParse();
});
it("should parse ties in text mode", function() {
expect(textTie).toParse();
});
it("should produce spacing in math mode", function() {
const parse = getParsed(mathTie);
expect(parse[1].type).toEqual("spacing");
});
it("should produce spacing in text mode", function() {
const text = getParsed(textTie)[0];
const parse = text.body;
expect(parse[1].type).toEqual("spacing");
});
it("should not contract with spaces in text mode", function() {
const text = getParsed(textTie)[0];
const parse = text.body;
expect(parse[2].type).toEqual("spacing");
});
});
describe("A delimiter sizing parser", function() {
const normalDelim = r`\bigl |`;
const notDelim = r`\bigl x`;
const bigDelim = r`\Biggr \langle`;
it("should parse normal delimiters", function() {
expect(normalDelim).toParse();
expect(bigDelim).toParse();
});
it("should not parse not-delimiters", function() {
expect(notDelim).not.toParse();
});
it("should produce a delimsizing", function() {
const parse = getParsed(normalDelim)[0];
expect(parse.type).toEqual("delimsizing");
});
it("should produce the correct direction delimiter", function() {
const leftParse = getParsed(normalDelim)[0];
const rightParse = getParsed(bigDelim)[0];
expect(leftParse.mclass).toEqual("mopen");
expect(rightParse.mclass).toEqual("mclose");
});
it("should parse the correct size delimiter", function() {
const smallParse = getParsed(normalDelim)[0];
const bigParse = getParsed(bigDelim)[0];
expect(smallParse.size).toEqual(1);
expect(bigParse.size).toEqual(4);
});
});
describe("An overline parser", function() {
const overline = r`\overline{x}`;
it("should not fail", function() {
expect(overline).toParse();
});
it("should produce an overline", function() {
const parse = getParsed(overline)[0];
expect(parse.type).toEqual("overline");
});
});
describe("An lap parser", function() {
it("should not fail on a text argument", function() {
expect`\rlap{\,/}{=}`.toParse();
expect`\mathrlap{\,/}{=}`.toParse();
expect`{=}\llap{/\,}`.toParse();
expect`{=}\mathllap{/\,}`.toParse();
expect`\sum_{\clap{ABCDEFG}}`.toParse();
expect`\sum_{\mathclap{ABCDEFG}}`.toParse();
});
it("should not fail if math version is used", function() {
expect`\mathrlap{\frac{a}{b}}{=}`.toParse();
expect`{=}\mathllap{\frac{a}{b}}`.toParse();
expect`\sum_{\mathclap{\frac{a}{b}}}`.toParse();
});
it("should fail on math if AMS version is used", function() {
expect`\rlap{\frac{a}{b}}{=}`.not.toParse();
expect`{=}\llap{\frac{a}{b}}`.not.toParse();
expect`\sum_{\clap{\frac{a}{b}}}`.not.toParse();
});
it("should produce a lap", function() {
const parse = getParsed`\mathrlap{\,/}`[0];
expect(parse.type).toEqual("lap");
});
});
describe("A rule parser", function() {
const emRule = r`\rule{1em}{2em}`;
const exRule = r`\rule{1ex}{2em}`;
const badUnitRule = r`\rule{1au}{2em}`;