1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-03-06 05:49:47 +01:00
slate/test-migration.js
Ian Storm Taylor 9fca2d3b9b start removing raw serializer (#1098)
* start removing raw serializer

* convert first tests to use jsx

* simplify jsx tests for raw serializer

* allow for options in raw serializer tests

* add more preserve option tests

* convert plain serializer tests

* convert html serialize

* start converting html deserialize

* add hyperscript util

* remove slate-sugar copy-pasted

* finish converting html deserialize

* convert plugins tests

* update rendering tests

* convert schemas tests

* convert core plugin tests

* update hyperscript utl

* refactor changes test directory structure

* fix changes before migration

* add migrated changes test files

* remove <x- prefixes from migration

* get addMark at-current-range tests passing

* get delete at-current-range tests passing

* remove old tests

* convert deleteForward and deleteBackward

* convert insertBlock, insertInline, insertFragment, insertText

* convert removeMark, setBlock, setInline, splitBlock, splitInline

* add unstaged tests

* convert toggleMark, unwrapBlock, unwrapInline, wrapBlock, wrapInline, wrapText

* convert call, setData

* convert on-selection tests

* remove old on-selection tests

* convert history tests

* convert insertFragmentByKey, insertNodeByKey, insertTextByKey, mergeNodeByKey, moveNodeByKey

* convert removeNodeByKey, removeTextByKey, setMarkByKey, setNodeByKey

* convert splitDescendantsByKey, splitNodeByKey, unwrapBlockByKey, unwrapInlineByKey, unwrapNodeByKey, wrapBlockByKey

* fix tests

* port missing at-range tests to at-current-range

* remove at-range tests

* fix raw serializer tests

* fix linter

* fix to prefer toJSON as the canonical method name

* fix todos

* remove internal references to Raw

* add deprecation helper to Text.fromJSON

* convert examples to not use Raw, and not rely on terse

* remove focus-blur example, rename large document example

* refactor Raw serialize to deprecate, not remove, terse

* deprecate defaultBlockType, toRaw, cleanup serializers
2017-09-10 14:56:03 -07:00

133 lines
3.8 KiB
JavaScript

/* eslint-disable prefer-template,no-console */
const React = require('react')
const fs = require('fs')
const parse5 = require('parse5')
// const pretty = require('pretty')
// const prettifyXml = require('prettify-xml')
const { pd } = require('pretty-data')
// const readMetadata = require('read-metadata')
const yaml = require('yaml-js')
const { resolve } = require('path')
const { Html, State } = require('..')
const html = new Html({
parseHtml: parse5.parseFragment,
rules: [
{
serialize(obj, children) {
switch (obj.kind) {
case 'block':
case 'inline': {
return React.createElement(`x-${obj.type}`, obj.data.toJSON(), children)
}
case 'document':
case 'state': {
return React.createElement(`x-${obj.kind}`, null, children)
}
case 'mark': {
return React.createElement(`x-${obj.type[0]}`, obj.data.toJSON(), children)
}
}
}
}
]
})
const failures = []
const dir = resolve(__dirname, '../test/changes')
const categories = fs.readdirSync(dir).filter(c => c[0] != '.' && c != 'index.js')
for (const category of categories) {
// if (category != 'at-current-range') continue
const categoryDir = resolve(dir, category)
const methods = fs.readdirSync(categoryDir).filter(c => c[0] != '.')
for (const method of methods) {
// if (method != 'add-mark') continue
const methodDir = resolve(categoryDir, method)
const tests = fs.readdirSync(methodDir).filter(t => t[0] != '.' && !~t.indexOf('.js'))
for (const test of tests) {
// if (test != 'across-blocks' && test != 'across-inlines') continue
try {
const testDir = resolve(methodDir, test)
const indexFile = resolve(testDir, 'index.js')
let index = fs.readFileSync(indexFile, 'utf8')
index = index.replace(/^import assert.*$/m, '')
index = index.replace(/function \(state\)/m, 'function (change)')
index = index.replace(/ *const { document.*\n/m, '')
index = index.replace(/const next = state\n\s*\.change\(\)/m, 'change')
index = index.replace(/\n\s*\.state\n/m, '\n')
index = index.replace(/\n\s*return next\n\s*}/m, '\n}')
index = index.trim()
const inputFile = resolve(testDir, 'input.yaml')
const inputString = fs.readFileSync(inputFile, 'utf8')
const inputJson = yaml.load(inputString)
const inputState = State.fromJSON({ document: inputJson })
const inputJsx = html.serialize(inputState)
let input = pd.xml(inputJsx)
input = input.replace(/\n/mg, '\n ').trim()
const outputFile = category == 'on-selection'
? inputFile
: resolve(testDir, 'output.yaml')
const outputString = fs.readFileSync(outputFile, 'utf8')
const outputJson = yaml.load(outputString)
const outputState = State.fromJSON({ document: outputJson })
const outputJsx = html.serialize(outputState)
let output = pd.xml(outputJsx)
output = output.replace(/\n/mg, '\n ').trim()
const contents = `/** @jsx h */
import h from '../../../helpers/h'
${index}
export const input = (
<state>
<document>
${input}
</document>
</state>
)
export const output = (
<state>
<document>
${output}
</document>
</state>
)
`
const newFile = `${testDir}.js`
fs.writeFileSync(newFile, contents)
}
catch (e) {
const name = `${category} ${method} ${test}`
failures.push(name)
console.error()
console.error(`Failure: ${name}`)
console.error(e)
console.error()
}
}
}
}
if (failures.length) {
console.error()
console.error(`!!! ${failures.length} Failures !!!`)
console.error(failures.join('\n'))
console.error()
}
process.exit(0)