-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmathml.test.ts
More file actions
129 lines (111 loc) · 3.54 KB
/
mathml.test.ts
File metadata and controls
129 lines (111 loc) · 3.54 KB
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
import schema from '@stencila/schema'
import { MathMLCodec, normalize } from '.'
import {
asciimathBlock,
asciimathFragment,
mathmlBlock,
mathmlBlockString,
mathmlFragment,
mathmlInlineString,
mathmlString,
texBlock,
texFragment,
texString,
} from '../../__fixtures__/math/kitchen-sink'
const mathml = new MathMLCodec()
describe('sniff', () => {
const sniff = mathml.sniff
test('success', async () => {
expect(await sniff('<math>')).toBe(true)
expect(await sniff(' <math')).toBe(true)
expect(await sniff('\n <math></math>')).toBe(true)
})
test('success', async () => {
expect(await sniff('foo <math')).toBe(false)
expect(await sniff(__dirname)).toBe(false)
expect(await sniff(__filename)).toBe(false)
})
})
describe('normalize', () => {
it('replaces deprecated constants with values', () => {
expect(
normalize(`
<mml:mspace width="veryverythinmathspace"/>
<mml:mspace width="verythinmathspace"/>
<mml:mspace width="thinmathspace"/>
<mml:mspace width="mediummathspace"/>
<mml:mspace width="thickmathspace"/>
<mml:mspace width="verythickmathspace"/>
<mml:mspace width="veryverythickmathspace"/>
<mml:mspace width="negativeveryverythinmathspace"/>
<mml:mspace width="negativeverythinmathspace"/>
<mml:mspace width="negativethinmathspace"/>
<mml:mspace width="negativemediummathspace"/>
<mml:mspace width="negativethickmathspace"/>
<mml:mspace width="negativeverythickmathspace"/>
<mml:mspace width="negativeveryverythickmathspace"/>
`),
).toBe(`
<mml:mspace width="0.0555em"/>
<mml:mspace width="0.1111em"/>
<mml:mspace width="0.1667em"/>
<mml:mspace width="0.2222em"/>
<mml:mspace width="0.2778em"/>
<mml:mspace width="0.3333em"/>
<mml:mspace width="0.3889em"/>
<mml:mspace width="-0.0555em"/>
<mml:mspace width="-0.1111em"/>
<mml:mspace width="-0.1667em"/>
<mml:mspace width="-0.2222em"/>
<mml:mspace width="-0.2778em"/>
<mml:mspace width="-0.3333em"/>
<mml:mspace width="-0.3889em]"/>
`)
})
})
describe('decode', () => {
const decode = (content: string) => mathml.load(content)
test('success', async () => {
expect(await decode(mathmlString)).toEqual(
schema.mathFragment({ mathLanguage: 'mathml', text: mathmlString }),
)
expect(await decode(mathmlInlineString)).toEqual(
schema.mathFragment({ mathLanguage: 'mathml', text: mathmlInlineString }),
)
expect(await decode(mathmlBlockString)).toEqual(
schema.mathBlock({ mathLanguage: 'mathml', text: mathmlBlockString }),
)
})
test('failure', async () => {
expect(await decode('')).toEqual(
schema.mathFragment({ mathLanguage: 'mathml', text: '' }),
)
})
})
describe('encode', () => {
const encode = (node: schema.Math) => mathml.dump(node)
test('success', async () => {
expect(await encode(mathmlFragment)).toEqual(mathmlString)
expect(await encode(mathmlBlock)).toEqual(mathmlString)
// The MathML generated by MathJax for these test fixtures
// should match this regex
const mml = /<mi>E<\/mi>/
expect(await encode(asciimathFragment)).toMatch(mml)
expect(await encode(asciimathBlock)).toMatch(mml)
expect(await encode(texFragment)).toMatch(mml)
expect(await encode(texBlock)).toMatch(mml)
// No `mathLanguage`, assumed to be `tex`
expect(await encode(schema.mathFragment({ text: texString }))).toMatch(mml)
})
test('failure', async () => {
expect(
await encode(
// @ts-ignore
schema.article(),
),
).toMatch('')
expect(
await encode(schema.mathFragment({ mathLanguage: 'foo', text: '' })),
).toMatch('')
})
})