|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -/* eslint-env mocha */ |
4 |
| - |
| 3 | +var fs = require('fs'); |
| 4 | +var path = require('path'); |
5 | 5 | var assert = require('assert');
|
| 6 | +var test = require('tape'); |
6 | 7 | var retext = require('retext');
|
7 | 8 | var english = require('retext-english');
|
8 | 9 | var emoticons = require('emoticon');
|
| 10 | +var negate = require('negate'); |
| 11 | +var hidden = require('is-hidden'); |
| 12 | +var toString = require('nlcst-to-string'); |
9 | 13 | var modifier = require('..');
|
10 | 14 |
|
11 |
| -var smile = require('./fixtures/smile'); |
12 |
| -var invalid = require('./fixtures/invalid'); |
13 |
| -var adjacent = require('./fixtures/adjacent'); |
14 |
| -var sentenceStart = require('./fixtures/sentence-start'); |
15 |
| - |
16 |
| -/* Processors. */ |
17 | 15 | var position = retext(english).use(plugin);
|
18 | 16 | var noPosition = retext(english).use(plugin).use(function (instance) {
|
19 | 17 | instance.Parser.prototype.position = false;
|
20 | 18 | });
|
21 | 19 |
|
22 |
| -/* |
23 |
| - * Tests. |
24 |
| - */ |
25 |
| - |
26 |
| -describe('nlcst-emoticon-modifier()', function () { |
27 |
| - it('should throw when not given a parent', function () { |
28 |
| - assert.throws( |
29 |
| - function () { |
30 |
| - modifier({}); |
31 |
| - }, |
32 |
| - /Missing children in `parent`/ |
33 |
| - ); |
34 |
| - }); |
35 |
| - |
36 |
| - it('should classify emoticons (`:)`)', function () { |
37 |
| - check('This makes me feel :).', smile); |
38 |
| - }); |
39 |
| - |
40 |
| - it('should NOT classify invalid emoticons (`):` and `: (`)', function () { |
41 |
| - check('Well: (This makes me feel):.', invalid); |
42 |
| - }); |
43 |
| - |
44 |
| - it('should classify adjacent emoticons (`:) :(`)', function () { |
45 |
| - check('This makes me feel :) :(.', adjacent); |
46 |
| - }); |
| 20 | +test('nlcst-emoticon-modifier()', function (t) { |
| 21 | + var root = path.join(__dirname, 'fixtures'); |
| 22 | + |
| 23 | + t.throws( |
| 24 | + function () { |
| 25 | + modifier({}); |
| 26 | + }, |
| 27 | + /Missing children in `parent`/, |
| 28 | + 'should throw when not given a parent' |
| 29 | + ); |
| 30 | + |
| 31 | + fs |
| 32 | + .readdirSync(root) |
| 33 | + .filter(negate(hidden)) |
| 34 | + .forEach(function (filename) { |
| 35 | + var tree = JSON.parse(fs.readFileSync(path.join(root, filename))); |
| 36 | + var fixture = toString(tree); |
| 37 | + var name = path.basename(filename, path.extname(filename)); |
| 38 | + |
| 39 | + t.deepEqual(position.parse(fixture), tree, name); |
| 40 | + t.deepEqual(noPosition.parse(fixture), clean(tree), name + ' (positionless)'); |
| 41 | + }); |
47 | 42 |
|
48 |
| - it('should classify emoticons at sentence start', function () { |
49 |
| - check('<3 I’m going to bed.', sentenceStart); |
50 |
| - }); |
| 43 | + t.end(); |
51 | 44 | });
|
52 | 45 |
|
53 |
| -/* |
54 |
| - * Check all emoticons. |
55 |
| - */ |
| 46 | +test('emoticons', function (t) { |
| 47 | + emoticons.forEach(function (emoticon) { |
| 48 | + emoticon.emoticons.forEach(function (value) { |
| 49 | + var fixture = 'Who doesn’t like ' + value + '?'; |
| 50 | + var node = position.run(position.parse(fixture)); |
| 51 | + var emoticon = node.children[0].children[0].children[6]; |
56 | 52 |
|
57 |
| -emoticons.forEach(function (emoticon) { |
58 |
| - emoticon.emoticons.forEach(function (value) { |
59 |
| - describe(value, function () { |
60 |
| - it( |
61 |
| - 'should classify `' + value + '` as an `EmoticonNode`', |
| 53 | + t.doesNotThrow( |
62 | 54 | function () {
|
63 |
| - var fixture = 'Who doesn’t like ' + value + '?'; |
64 |
| - var node = position.run(position.parse(fixture)); |
65 |
| - var emoticon = node.children[0].children[0].children[6]; |
66 |
| - |
67 | 55 | assert.strictEqual(emoticon.type, 'EmoticonNode');
|
68 | 56 | assert.strictEqual(emoticon.value, value);
|
69 |
| - } |
| 57 | + }, |
| 58 | + value |
70 | 59 | );
|
71 | 60 | });
|
72 | 61 | });
|
73 |
| -}); |
74 | 62 |
|
75 |
| -/* Short-cut to access the CST. */ |
76 |
| -function check(fixture, node) { |
77 |
| - assert.deepEqual(position.run(position.parse(fixture)), node); |
78 |
| - assert.deepEqual(noPosition.run(noPosition.parse(fixture, {position: false})), clean(node)); |
79 |
| -} |
| 63 | + t.end(); |
| 64 | +}); |
80 | 65 |
|
81 | 66 | /* Add modifier to processor. */
|
82 | 67 | function plugin(processor) {
|
|
0 commit comments