mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-01 19:22:35 +02:00
add plain text serializer tests
This commit is contained in:
@@ -12,19 +12,40 @@ import Text from '../models/text'
|
||||
*/
|
||||
|
||||
function deserialize(string) {
|
||||
const characters = string.split('').map(char => {
|
||||
return State.create({
|
||||
document: Document.create({
|
||||
nodes: string.split('\n').map(deserializeLine)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize a `line` of text.
|
||||
*
|
||||
* @param {String} line
|
||||
* @return {Block}
|
||||
*/
|
||||
|
||||
function deserializeLine(line) {
|
||||
return Block.create({
|
||||
type: 'line',
|
||||
nodes: [
|
||||
Text.create({
|
||||
characters: line.split('').map(deserializeCharacter)
|
||||
})
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize a `character`.
|
||||
*
|
||||
* @param {String} char
|
||||
* @return {Character}
|
||||
*/
|
||||
|
||||
function deserializeCharacter(char) {
|
||||
return { text: char }
|
||||
})
|
||||
|
||||
const text = Text.create({ characters })
|
||||
const block = Block.create({
|
||||
type: 'paragraph',
|
||||
nodes: [text]
|
||||
})
|
||||
|
||||
const document = Document.create({ nodes: [block] })
|
||||
const state = State.create({ document })
|
||||
return state
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,7 +56,7 @@ function deserialize(string) {
|
||||
*/
|
||||
|
||||
function serialize(state) {
|
||||
return state.blocks
|
||||
return state.document.nodes
|
||||
.map(block => block.text)
|
||||
.join('\n')
|
||||
}
|
||||
|
@@ -0,0 +1,2 @@
|
||||
one
|
||||
two
|
@@ -0,0 +1,24 @@
|
||||
|
||||
nodes:
|
||||
- type: line
|
||||
isVoid: false
|
||||
data: {}
|
||||
nodes:
|
||||
- characters:
|
||||
- text: o
|
||||
marks: []
|
||||
- text: n
|
||||
marks: []
|
||||
- text: e
|
||||
marks: []
|
||||
- type: line
|
||||
isVoid: false
|
||||
data: {}
|
||||
nodes:
|
||||
- characters:
|
||||
- text: t
|
||||
marks: []
|
||||
- text: w
|
||||
marks: []
|
||||
- text: o
|
||||
marks: []
|
@@ -0,0 +1 @@
|
||||
one
|
13
test/serializers/fixtures/plain/deserialize/line/output.yaml
Normal file
13
test/serializers/fixtures/plain/deserialize/line/output.yaml
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
nodes:
|
||||
- type: line
|
||||
isVoid: false
|
||||
data: {}
|
||||
nodes:
|
||||
- characters:
|
||||
- text: o
|
||||
marks: []
|
||||
- text: n
|
||||
marks: []
|
||||
- text: e
|
||||
marks: []
|
@@ -0,0 +1,26 @@
|
||||
|
||||
import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..'
|
||||
|
||||
export default State.create({
|
||||
document: Document.create({
|
||||
nodes: Block.createList([
|
||||
{
|
||||
type: 'quote',
|
||||
nodes: Block.createList([
|
||||
{
|
||||
type: 'paragraph',
|
||||
nodes: Text.createList([
|
||||
{
|
||||
characters: Character.createList([
|
||||
{ text: 'o' },
|
||||
{ text: 'n' },
|
||||
{ text: 'e' }
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
})
|
||||
})
|
@@ -0,0 +1 @@
|
||||
one
|
@@ -0,0 +1,22 @@
|
||||
|
||||
import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..'
|
||||
|
||||
export default State.create({
|
||||
document: Document.create({
|
||||
nodes: Block.createList([
|
||||
{
|
||||
type: 'paragraph',
|
||||
data: { key: 'value' },
|
||||
nodes: Text.createList([
|
||||
{
|
||||
characters: Character.createList([
|
||||
{ text: 'o' },
|
||||
{ text: 'n' },
|
||||
{ text: 'e' }
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
})
|
||||
})
|
@@ -0,0 +1 @@
|
||||
one
|
@@ -0,0 +1,22 @@
|
||||
|
||||
import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..'
|
||||
|
||||
export default State.create({
|
||||
document: Document.create({
|
||||
nodes: Block.createList([
|
||||
{
|
||||
type: 'paragraph',
|
||||
isVoid: true,
|
||||
nodes: Text.createList([
|
||||
{
|
||||
characters: Character.createList([
|
||||
{ text: 'o' },
|
||||
{ text: 'n' },
|
||||
{ text: 'e' }
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
})
|
||||
})
|
21
test/serializers/fixtures/plain/serialize/block/input.js
Normal file
21
test/serializers/fixtures/plain/serialize/block/input.js
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..'
|
||||
|
||||
export default State.create({
|
||||
document: Document.create({
|
||||
nodes: Block.createList([
|
||||
{
|
||||
type: 'paragraph',
|
||||
nodes: Text.createList([
|
||||
{
|
||||
characters: Character.createList([
|
||||
{ text: 'o' },
|
||||
{ text: 'n' },
|
||||
{ text: 'e' }
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
})
|
||||
})
|
@@ -0,0 +1 @@
|
||||
one
|
@@ -0,0 +1,28 @@
|
||||
|
||||
import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..'
|
||||
|
||||
export default State.create({
|
||||
document: Document.create({
|
||||
nodes: Block.createList([
|
||||
{
|
||||
type: 'paragraph',
|
||||
nodes: Text.createList([
|
||||
{
|
||||
characters: Character.createList([
|
||||
{ text: 'o' },
|
||||
{ text: 'n' },
|
||||
{
|
||||
text: 'e',
|
||||
marks: Mark.createSet([
|
||||
{
|
||||
type: 'bold'
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
})
|
||||
})
|
@@ -0,0 +1 @@
|
||||
one
|
@@ -0,0 +1,31 @@
|
||||
|
||||
import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..'
|
||||
|
||||
export default State.create({
|
||||
document: Document.create({
|
||||
nodes: Block.createList([
|
||||
{
|
||||
type: 'paragraph',
|
||||
nodes: Inline.createList([
|
||||
{
|
||||
type: 'link',
|
||||
nodes: Inline.createList([
|
||||
{
|
||||
type: 'hashtag',
|
||||
nodes: Text.createList([
|
||||
{
|
||||
characters: Character.createList([
|
||||
{ text: 'o' },
|
||||
{ text: 'n' },
|
||||
{ text: 'e' }
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
})
|
||||
})
|
@@ -0,0 +1 @@
|
||||
one
|
@@ -0,0 +1,27 @@
|
||||
|
||||
import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..'
|
||||
|
||||
export default State.create({
|
||||
document: Document.create({
|
||||
nodes: Block.createList([
|
||||
{
|
||||
type: 'paragraph',
|
||||
nodes: Inline.createList([
|
||||
{
|
||||
type: 'link',
|
||||
data: { key: 'value' },
|
||||
nodes: Text.createList([
|
||||
{
|
||||
characters: Character.createList([
|
||||
{ text: 'o' },
|
||||
{ text: 'n' },
|
||||
{ text: 'e' }
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
})
|
||||
})
|
@@ -0,0 +1 @@
|
||||
one
|
@@ -0,0 +1,27 @@
|
||||
|
||||
import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..'
|
||||
|
||||
export default State.create({
|
||||
document: Document.create({
|
||||
nodes: Block.createList([
|
||||
{
|
||||
type: 'paragraph',
|
||||
nodes: Inline.createList([
|
||||
{
|
||||
type: 'link',
|
||||
isVoid: true,
|
||||
nodes: Text.createList([
|
||||
{
|
||||
characters: Character.createList([
|
||||
{ text: 'o' },
|
||||
{ text: 'n' },
|
||||
{ text: 'e' }
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
})
|
||||
})
|
26
test/serializers/fixtures/plain/serialize/inline/input.js
Normal file
26
test/serializers/fixtures/plain/serialize/inline/input.js
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
import { Block, Character, Document, Inline, Mark, State, Text } from '../../../../../..'
|
||||
|
||||
export default State.create({
|
||||
document: Document.create({
|
||||
nodes: Block.createList([
|
||||
{
|
||||
type: 'paragraph',
|
||||
nodes: Inline.createList([
|
||||
{
|
||||
type: 'link',
|
||||
nodes: Text.createList([
|
||||
{
|
||||
characters: Character.createList([
|
||||
{ text: 'o' },
|
||||
{ text: 'n' },
|
||||
{ text: 'e' }
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
}
|
||||
])
|
||||
})
|
||||
})
|
@@ -0,0 +1 @@
|
||||
one
|
@@ -11,9 +11,32 @@ import { resolve } from 'path'
|
||||
*/
|
||||
|
||||
const SERIALIZERS = {
|
||||
html: Html,
|
||||
plain: Plain,
|
||||
raw: Raw
|
||||
|
||||
html: (dir) => {
|
||||
const module = require(dir)
|
||||
const html = new Html(module)
|
||||
return {
|
||||
extension: 'html',
|
||||
deserialize: html.deserialize,
|
||||
serialize: html.serialize,
|
||||
read: file => fs.readFileSync(file, 'utf8').trim()
|
||||
}
|
||||
},
|
||||
|
||||
plain: (dir) => ({
|
||||
extension: 'txt',
|
||||
deserialize: Plain.deserialize,
|
||||
serialize: Plain.serialize,
|
||||
read: file => fs.readFileSync(file, 'utf8').trim()
|
||||
}),
|
||||
|
||||
raw: (dir) => ({
|
||||
extension: 'yaml',
|
||||
deserialize: Raw.deserialize,
|
||||
serialize: Raw.serialize,
|
||||
read: file => readMetadata.sync(file)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,9 +55,10 @@ describe('serializers', () => {
|
||||
for (const test of tests) {
|
||||
it(test, () => {
|
||||
const innerDir = resolve(dir, test)
|
||||
const input = readMetadata.sync(resolve(innerDir, 'input.yaml'))
|
||||
const Serializer = SERIALIZERS[serializer](innerDir)
|
||||
|
||||
const expected = readMetadata.sync(resolve(innerDir, 'output.yaml'))
|
||||
const Serializer = SERIALIZERS[serializer]
|
||||
const input = Serializer.read(resolve(innerDir, `input.${Serializer.extension}`))
|
||||
const state = Serializer.deserialize(input)
|
||||
const json = state.document.toJS()
|
||||
strictEqual(clean(json), expected)
|
||||
@@ -49,10 +73,12 @@ describe('serializers', () => {
|
||||
for (const test of tests) {
|
||||
it(test, () => {
|
||||
const innerDir = resolve(dir, test)
|
||||
const Serializer = SERIALIZERS[serializer](innerDir)
|
||||
|
||||
const input = require(resolve(innerDir, 'input.js')).default
|
||||
const expected = readMetadata.sync(resolve(innerDir, 'output.yaml'))
|
||||
const Serializer = SERIALIZERS[serializer]
|
||||
const expected = Serializer.read(resolve(innerDir, `output.${Serializer.extension}`))
|
||||
const serialized = Serializer.serialize(input)
|
||||
debugger
|
||||
strictEqual(serialized, expected)
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user