Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 5, 2019
1 parent bc353ab commit 6799e03
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 1,070 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"retext": "^6.0.0",
"tape": "^4.0.0",
"tinyify": "^2.0.0",
"unist-builder": "^1.0.4",
"xo": "^0.24.0"
},
"scripts": {
Expand All @@ -46,7 +47,7 @@
"build-mangle": "browserify . -s retextEmoji -p tinyify > retext-emoji.min.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test/index.js",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run format && npm run build && npm run test-coverage"
},
"nyc": {
Expand Down
214 changes: 214 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
'use strict'

var test = require('tape')
var retext = require('retext')
var visit = require('unist-util-visit')
var u = require('unist-builder')
var emoji = require('.')

test('emoji', function(t) {
var processor = retext().use(emoji)
var fixture = 'It’s raining 🐱s and :dog:s. Now :3.'

t.throws(
function() {
retext()
.use(emoji, {convert: false})
.freeze()
},
/Illegal invocation: `false` is not a valid value/,
'should throw when given invalid `convert`'
)

t.deepEqual(
processor.runSync(processor.parse('This makes me feel :).')),
u('RootNode', {position: p(1, 1, 0, 1, 23, 22)}, [
u('ParagraphNode', {position: p(1, 1, 0, 1, 23, 22)}, [
u('SentenceNode', {position: p(1, 1, 0, 1, 23, 22)}, [
u('WordNode', {position: p(1, 1, 0, 1, 5, 4)}, [
u('TextNode', {position: p(1, 1, 0, 1, 5, 4)}, 'This')
]),
u('WhiteSpaceNode', {position: p(1, 5, 4, 1, 6, 5)}, ' '),
u('WordNode', {position: p(1, 6, 5, 1, 11, 10)}, [
u('TextNode', {position: p(1, 6, 5, 1, 11, 10)}, 'makes')
]),
u('WhiteSpaceNode', {position: p(1, 11, 10, 1, 12, 11)}, ' '),
u('WordNode', {position: p(1, 12, 11, 1, 14, 13)}, [
u('TextNode', {position: p(1, 12, 11, 1, 14, 13)}, 'me')
]),
u('WhiteSpaceNode', {position: p(1, 14, 13, 1, 15, 14)}, ' '),
u('WordNode', {position: p(1, 15, 14, 1, 19, 18)}, [
u('TextNode', {position: p(1, 15, 14, 1, 19, 18)}, 'feel')
]),
u('WhiteSpaceNode', {position: p(1, 19, 18, 1, 20, 19)}, ' '),
u(
'EmoticonNode',
{
position: p(1, 20, 19, 1, 22, 21),
data: {
names: ['smiley'],
description: 'smiling face with open mouth',
tags: ['happy', 'joy', 'haha']
}
},
':)'
),
u('PunctuationNode', {position: p(1, 22, 21, 1, 23, 22)}, '.')
])
])
]),
'should classify emoticons'
)

t.deepEqual(
processor.runSync(processor.parse('This makes me feel :sob:.')),
u('RootNode', {position: p(1, 1, 0, 1, 26, 25)}, [
u('ParagraphNode', {position: p(1, 1, 0, 1, 26, 25)}, [
u('SentenceNode', {position: p(1, 1, 0, 1, 26, 25)}, [
u('WordNode', {position: p(1, 1, 0, 1, 5, 4)}, [
u('TextNode', {position: p(1, 1, 0, 1, 5, 4)}, 'This')
]),
u('WhiteSpaceNode', {position: p(1, 5, 4, 1, 6, 5)}, ' '),
u('WordNode', {position: p(1, 6, 5, 1, 11, 10)}, [
u('TextNode', {position: p(1, 6, 5, 1, 11, 10)}, 'makes')
]),
u('WhiteSpaceNode', {position: p(1, 11, 10, 1, 12, 11)}, ' '),
u('WordNode', {position: p(1, 12, 11, 1, 14, 13)}, [
u('TextNode', {position: p(1, 12, 11, 1, 14, 13)}, 'me')
]),
u('WhiteSpaceNode', {position: p(1, 14, 13, 1, 15, 14)}, ' '),
u('WordNode', {position: p(1, 15, 14, 1, 19, 18)}, [
u('TextNode', {position: p(1, 15, 14, 1, 19, 18)}, 'feel')
]),
u('WhiteSpaceNode', {position: p(1, 19, 18, 1, 20, 19)}, ' '),
u(
'EmoticonNode',
{
position: p(1, 20, 19, 1, 25, 24),
data: {
names: ['sob'],
description: 'loudly crying face',
tags: ['sad', 'cry', 'bawling']
}
},
':sob:'
),
u('PunctuationNode', {position: p(1, 25, 24, 1, 26, 25)}, '.')
])
])
]),
'should classify gemoji'
)

t.deepEqual(
processor.runSync(processor.parse('It’s raining 🐱s and 🐶s.')),
u('RootNode', {position: p(1, 1, 0, 1, 26, 25)}, [
u('ParagraphNode', {position: p(1, 1, 0, 1, 26, 25)}, [
u('SentenceNode', {position: p(1, 1, 0, 1, 26, 25)}, [
u('WordNode', {position: p(1, 1, 0, 1, 5, 4)}, [
u('TextNode', {position: p(1, 1, 0, 1, 3, 2)}, 'It'),
u('PunctuationNode', {position: p(1, 3, 2, 1, 4, 3)}, '’'),
u('TextNode', {position: p(1, 4, 3, 1, 5, 4)}, 's')
]),
u('WhiteSpaceNode', {position: p(1, 5, 4, 1, 6, 5)}, ' '),
u('WordNode', {position: p(1, 6, 5, 1, 13, 12)}, [
u('TextNode', {position: p(1, 6, 5, 1, 13, 12)}, 'raining')
]),
u('WhiteSpaceNode', {position: p(1, 13, 12, 1, 14, 13)}, ' '),
u(
'EmoticonNode',
{
position: p(1, 14, 13, 1, 16, 15),
data: {names: ['cat'], description: 'cat face', tags: ['pet']}
},
'🐱'
),
u('WordNode', {position: p(1, 16, 15, 1, 17, 16)}, [
u('TextNode', {position: p(1, 16, 15, 1, 17, 16)}, 's')
]),
u('WhiteSpaceNode', {position: p(1, 17, 16, 1, 18, 17)}, ' '),
u('WordNode', {position: p(1, 18, 17, 1, 21, 20)}, [
u('TextNode', {position: p(1, 18, 17, 1, 21, 20)}, 'and')
]),
u('WhiteSpaceNode', {position: p(1, 21, 20, 1, 22, 21)}, ' '),
u(
'EmoticonNode',
{
position: p(1, 22, 21, 1, 24, 23),
data: {names: ['dog'], description: 'dog face', tags: ['pet']}
},
'🐶'
),
u('WordNode', {position: p(1, 24, 23, 1, 26, 25)}, [
u('TextNode', {position: p(1, 24, 23, 1, 25, 24)}, 's'),
u('PunctuationNode', {position: p(1, 25, 24, 1, 26, 25)}, '.')
])
])
])
]),
'should classify emoji'
)

retext()
.use(emoji)
.process(fixture, function(err, file) {
t.deepEqual(
[err, String(file)],
[null, fixture],
'should not transform without `convert`'
)
})

retext()
.use(emoji, {convert: 'encode'})
.process(fixture, function(err, file) {
t.deepEqual(
[err, String(file)],
[null, 'It’s raining 🐱s and 🐶s. Now 👨.'],
'should encode'
)
})

retext()
.use(emoji, {convert: 'decode'})
.process(fixture, function(err, file) {
t.deepEqual(
[err, String(file)],
[null, 'It’s raining :cat:s and :dog:s. Now :man:.'],
'should decode'
)
})

retext()
.use(data)
.use(emoji)
.process(fixture, function(err, file) {
t.deepEqual(
[err, String(file)],
[null, fixture],
'should not overwrite existing data'
)
})

function data() {
return transformer
function transformer(node) {
visit(node, visitor)
}

function visitor(child) {
child.data = {}
}
}

t.end()
})

// eslint-disable-next-line max-params
function p(sl, sc, so, el, ec, eo) {
return {start: point(sl, sc, so), end: point(el, ec, eo)}
}

function point(l, c, o) {
return {line: l, column: c, offset: o}
}
Loading

0 comments on commit 6799e03

Please sign in to comment.