-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtxt.test.ts
141 lines (122 loc) · 3.85 KB
/
txt.test.ts
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
import stencila from '@stencila/schema'
import { TxtCodec } from '.'
import { fixture } from '../../__tests__/helpers'
const txt = new TxtCodec()
describe('decode', () => {
const d = async (content: string) => txt.load(content)
it('decodes string representations of primitive nodes', async () => {
expect(await d('null')).toBe(null)
expect(await d('true')).toBe(true)
expect(await d('false')).toBe(false)
expect(await d('1')).toBe(1)
expect(await d('3.14')).toBe(3.14)
expect(await d(' 3.14 ')).toBe(' 3.14 ')
expect(await d('string')).toBe('string')
})
it('can decode a plain ASCII text file', async () => {
expect(await txt.read(fixture('ascii.txt'))).toBe(
'Just some plain ASCII text.\n',
)
})
it('can decode a UTF8 text file', async () => {
expect(await txt.read(fixture('utf8.txt'))).toBe(
'ეს არის ქართულ ენაზე დაწერილი უნიკოდში.\n',
)
})
})
describe('encode', () => {
const e = async (node: stencila.Node) => txt.dump(node)
it('encodes primitives as strings', async () => {
expect(await e(null)).toBe('null')
expect(await e(true)).toBe('true')
expect(await e(false)).toBe('false')
expect(await e(1)).toBe('1')
expect(await e(3.14)).toBe('3.14')
expect(await e('string')).toBe('string')
})
it('encodes arrays as space separated strings', async () => {
expect(await e([])).toBe('')
expect(await e([1])).toBe('1')
expect(await e([null, true, 42, 'str', 3.14])).toBe('null true 42 str 3.14')
})
it('encodes objects as space separated key value pairs', async () => {
expect(await e({})).toBe('')
expect(await e({ a: 1 })).toBe('1')
expect(await e({ a: null, b: true, c: 42, d: 'str', e: 3.14 })).toBe(
'null true 42 str 3.14',
)
})
it('encodes nested objects / arrays', async () => {
expect(await e(article)).toBe(articleString)
})
})
describe('stringify', () => {
const s = (node: stencila.Node) => TxtCodec.stringify(node)
test('it stringifies primitives', () => {
expect(s([null, true, false, NaN, 2, 'string'])).toEqual(
'null true false NaN 2 string',
)
})
test('it stringifies object values', () => {
expect(s({ a: 1, b: 'two', c: null })).toEqual('1 two null')
})
test('it stringifies inline content', () => {
expect(
s([
stencila.emphasis({ content: ['emphasis'] }),
stencila.strong({ content: ['strong'] }),
stencila.subscript({ content: ['sub'] }),
stencila.superscript({ content: ['sup'] }),
]),
).toEqual('emphasis strong sub sup')
})
test('it stringifies block content', () => {
expect(
s(
stencila.paragraph({
content: [
'A paragraph with ',
stencila.strong({ content: ['strong'] }),
' and ',
stencila.strong({
content: [stencila.superscript({ content: ['super strong'] })],
}),
],
}),
),
).toEqual('A paragraph with strong and super strong')
})
})
describe('invertibility', () => {
it('is able to invert primitive nodes', async () => {
expect(txt).toInvert(null)
expect(txt).toInvert(true)
expect(txt).toInvert(false)
expect(txt).toInvert(3.14)
expect(txt).toInvert(-100)
expect(txt).toInvert('3.14string')
expect(txt).toInvert('a string')
})
it('is not able to invert objects or arrays', async () => {
expect(txt).not.toInvert([1, 2, 3])
expect(txt).not.toInvert({ a: 1, b: 2 })
})
})
const article: stencila.Article = {
type: 'Article',
title: 'My simple article',
authors: [
{
type: 'Person',
givenNames: ['Peter'],
familyNames: ['Pan'],
},
],
content: [
{
type: 'Paragraph',
content: ['My first paragraph.'],
},
],
}
const articleString = 'My first paragraph.'