1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-30 10:29:48 +02:00

add raw serializer tests

This commit is contained in:
Ian Storm Taylor
2016-07-14 16:17:26 -07:00
parent 68e87ebfb8
commit 4f06f322cd
39 changed files with 654 additions and 0 deletions

View File

@@ -37,6 +37,18 @@ class Text extends new Record(DEFAULTS) {
return new Text(properties)
}
/**
* Create a list of `Texts` from an array.
*
* @param {Array} elements
* @return {List} map
*/
static createList(elements = []) {
if (List.isList(elements)) return elements
return new List(elements.map(Text.create))
}
/**
* Get the node's kind.
*

View File

@@ -0,0 +1,11 @@
nodes:
- kind: block
type: quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: one

View File

@@ -0,0 +1,17 @@
nodes:
- type: quote
isVoid: false
data: {}
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- characters:
- text: o
marks: []
- text: n
marks: []
- text: e
marks: []

View File

@@ -0,0 +1,10 @@
nodes:
- kind: block
type: paragraph
data:
key: value
nodes:
- kind: text
ranges:
- text: one

View File

@@ -0,0 +1,14 @@
nodes:
- type: paragraph
isVoid: false
data:
key: value
nodes:
- characters:
- text: o
marks: []
- text: n
marks: []
- text: e
marks: []

View File

@@ -0,0 +1,9 @@
nodes:
- kind: block
type: paragraph
isVoid: true
nodes:
- kind: text
ranges:
- text: one

View File

@@ -0,0 +1,7 @@
nodes:
- type: paragraph
isVoid: true
data: {}
nodes:
- characters: []

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: one

View File

@@ -0,0 +1,13 @@
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- characters:
- text: o
marks: []
- text: n
marks: []
- text: e
marks: []

View File

@@ -0,0 +1,14 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
nodes:
- kind: inline
type: hashtag
nodes:
- kind: text
ranges:
- text: one

View File

@@ -0,0 +1,21 @@
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- type: link
isVoid: false
data: {}
nodes:
- type: hashtag
isVoid: false
data: {}
nodes:
- characters:
- text: o
marks: []
- text: n
marks: []
- text: e
marks: []

View File

@@ -0,0 +1,13 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
data:
key: value
nodes:
- kind: text
ranges:
- text: one

View File

@@ -0,0 +1,18 @@
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- type: link
isVoid: false
data:
key: value
nodes:
- characters:
- text: o
marks: []
- text: n
marks: []
- text: e
marks: []

View File

@@ -0,0 +1,12 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
isVoid: true
nodes:
- kind: text
ranges:
- text: one

View File

@@ -0,0 +1,11 @@
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- type: link
isVoid: true
data: {}
nodes:
- characters: []

View File

@@ -0,0 +1,11 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
nodes:
- kind: text
ranges:
- text: one

View File

@@ -0,0 +1,17 @@
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- type: link
isVoid: false
data: {}
nodes:
- characters:
- text: o
marks: []
- text: n
marks: []
- text: e
marks: []

View File

@@ -0,0 +1,12 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: o
- text: n
marks:
- type: bold
- text: e

View File

@@ -0,0 +1,15 @@
nodes:
- type: paragraph
isVoid: false
data: {}
nodes:
- characters:
- text: o
marks: []
- text: n
marks:
- type: bold
data: {}
- text: e
marks: []

View 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: 'quote',
nodes: Block.createList([
{
type: 'paragraph',
nodes: Text.createList([
{
characters: Character.createList([
{ text: 'o' },
{ text: 'n' },
{ text: 'e' }
])
}
])
}
])
}
])
})
})

View File

@@ -0,0 +1,11 @@
nodes:
- kind: block
type: quote
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: one

View File

@@ -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' }
])
}
])
}
])
})
})

View File

@@ -0,0 +1,10 @@
nodes:
- kind: block
type: paragraph
data:
key: value
nodes:
- kind: text
ranges:
- text: one

View File

@@ -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' }
])
}
])
}
])
})
})

View File

@@ -0,0 +1,9 @@
nodes:
- kind: block
type: paragraph
isVoid: true
nodes:
- kind: text
ranges:
- text: ""

View 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' }
])
}
])
}
])
})
})

View File

@@ -0,0 +1,8 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: one

View File

@@ -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'
}
])
}
])
}
])
}
])
})
})

View File

@@ -0,0 +1,11 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: text
ranges:
- text: "on"
- text: e
marks:
- type: bold

View File

@@ -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' }
])
}
])
}
])
}
])
}
])
})
})

View File

@@ -0,0 +1,14 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
nodes:
- kind: inline
type: hashtag
nodes:
- kind: text
ranges:
- text: one

View File

@@ -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' }
])
}
])
}
])
}
])
})
})

View File

@@ -0,0 +1,13 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
data:
key: value
nodes:
- kind: text
ranges:
- text: one

View File

@@ -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' }
])
}
])
}
])
}
])
})
})

View File

@@ -0,0 +1,12 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
isVoid: true
nodes:
- kind: text
ranges:
- text: ""

View 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' }
])
}
])
}
])
}
])
})
})

View File

@@ -0,0 +1,11 @@
nodes:
- kind: block
type: paragraph
nodes:
- kind: inline
type: link
nodes:
- kind: text
ranges:
- text: one

79
test/serializers/index.js Normal file
View File

@@ -0,0 +1,79 @@
import assert from 'assert'
import fs from 'fs'
import readMetadata from 'read-metadata'
import { Html, Plain, Raw } from '../..'
import { equal, strictEqual } from '../helpers/assert-json'
import { resolve } from 'path'
/**
* Serializers.
*/
const SERIALIZERS = {
html: Html,
plain: Plain,
raw: Raw
}
/**
* Tests.
*/
describe('serializers', () => {
const serializers = fs.readdirSync(resolve(__dirname, './fixtures'))
for (const serializer of serializers) {
describe(serializer, () => {
describe('deserialize()', () => {
const dir = resolve(__dirname, './fixtures', serializer, 'deserialize')
const tests = fs.readdirSync(dir)
for (const test of tests) {
it(test, () => {
const innerDir = resolve(dir, test)
const input = readMetadata.sync(resolve(innerDir, 'input.yaml'))
const expected = readMetadata.sync(resolve(innerDir, 'output.yaml'))
const Serializer = SERIALIZERS[serializer]
const state = Serializer.deserialize(input)
const json = state.document.toJS()
strictEqual(clean(json), expected)
})
}
})
describe('serialize()', () => {
const dir = resolve(__dirname, './fixtures', serializer, 'serialize')
const tests = fs.readdirSync(dir)
for (const test of tests) {
it(test, () => {
const innerDir = resolve(dir, test)
const input = require(resolve(innerDir, 'input.js')).default
const expected = readMetadata.sync(resolve(innerDir, 'output.yaml'))
const Serializer = SERIALIZERS[serializer]
const serialized = Serializer.serialize(input)
strictEqual(serialized, expected)
})
}
})
})
}
})
/**
* Clean a `json` object of dynamic `key` properties.
*
* @param {Object} json
* @return {Object}
*/
function clean(json) {
const { key, cache, decorations, ...props } = json
if (props.nodes) {
props.nodes = props.nodes.map(clean)
}
return props
}

View File

@@ -1,3 +1,4 @@
import './serializers'
import './rendering'
import './transforms'