Code coverage report for mdast/spec/mdast.spec.js

Statements: 100% (89 / 89)      Branches: 100% (51 / 51)      Functions: 100% (11 / 11)      Lines: 100% (89 / 89)      Ignored: 2 statements     

All files » mdast/spec/ » mdast.spec.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194    1   1 1 1   1 1 1       1   1   1 872     1 2008     2008   2008 780 780     2008 1180     2008           549 549   549     1459 4 4   4     1455 17 17 17 17   17     1438 246 246   246 9           246     1192 38   38     1154 69 69 69   69     1085 878 878   878     207         58 58   58     149 87 87       87 87   87     62 2       2       2 2   2     60 4   4 8     4   4 12               4   4           56 56 56     1 1 56 56     56 56   56 56     1           1          
'use strict';
 
var mdast, assert, fixtures, validateToken, validateTokens;
 
mdast = require('..');
assert = require('assert');
fixtures = require('./fixtures.js');
 
describe('mdast()', function () {
    it('should be of type `function`', function () {
        assert(typeof mdast === 'function');
    });
});
 
describe('msast.Parser()', function () {});
 
describe('msast.Lexer()', function () {});
 
validateTokens = function (children) {
    children.forEach(validateToken);
};
 
validateToken = function (context) {
    var keys = Object.keys(context),
        type = context.type;
 
    assert('type' in context);
 
    if ('children' in context) {
        assert(Array.isArray(context.children));
        validateTokens(context.children);
    }
 
    if ('value' in context) {
        assert(typeof context.value === 'string');
    }
 
    if (
        type === 'paragraph' ||
        type === 'blockquote' ||
        type === 'looseItem' ||
        type === 'listItem'
    ) {
        assert(keys.length === 2);
        assert('children' in context);
 
        return;
    }
 
    if (type === 'footnote') {
        assert(keys.length === 2);
        assert('id' in context);
 
        return;
    }
 
    if (type === 'heading') {
        assert(keys.length === 3);
        assert(context.depth > 0);
        assert(context.depth <= 6);
        assert('children' in context);
 
        return;
    }
 
    if (type === 'code') {
        assert(keys.length === 2 || keys.length === 3);
        assert('value' in context);
 
        if ('lang' in context) {
            assert(
                context.lang === null ||
                typeof context.lang === 'string'
            );
        }
 
        return;
    }
 
    if (type === 'horizontalRule' || type === 'break') {
        assert(keys.length === 1);
 
        return;
    }
 
    if (type === 'list') {
        assert('children' in context);
        assert(typeof context.ordered === 'boolean');
        assert(keys.length === 3);
 
        return;
    }
 
    if (type === 'text') {
        assert(keys.length === 2);
        assert('value' in context);
 
        return;
    }
 
    if (
        type === 'strong' ||
        type === 'emphasis' ||
        type === 'delete'
    ) {
        assert(keys.length === 2);
        assert('children' in context);
 
        return;
    }
 
    if (type === 'link') {
        assert('children' in context);
        assert(
            context.title === null ||
            typeof context.title === 'string'
        );
        assert(typeof context.href === 'string');
        assert(keys.length === 4);
 
        return;
    }
 
    if (type === 'image') {
        assert(
            context.title === null ||
            typeof context.title === 'string'
        );
        assert(
            context.alt === null ||
            typeof context.alt === 'string'
        );
        assert(typeof context.href === 'string');
        assert(keys.length === 4);
 
        return;
    }
 
    if (type === 'table') {
        context.header.forEach(validateTokens);
 
        context.cells.forEach(function (row) {
            row.forEach(validateTokens);
        });
 
        assert(Array.isArray(context.align));
 
        context.align.forEach(function (align) {
            assert(
                align === null ||
                align === 'left' ||
                align === 'right' ||
                align === 'center'
            );
        });
 
        assert(keys.length === 4);
 
        return;
    }
 
    /* This is the last possible type. If more types are added, they
     * should be added before this block, or the type:html tests should
     * be wrapped in an if statement. */
    assert(type === 'html');
    assert(keys.length === 2);
    assert('value' in context);
};
 
describe('fixtures', function () {
    fixtures.forEach(function (fixture) {
        it('should work on ' + fixture.name, function () {
            var results = mdast(fixture.input, fixture.options),
                baseline = fixture.output;
 
            validateTokens(results);
            baseline = JSON.parse(baseline);
 
            try {
                assert(JSON.stringify(results) === JSON.stringify(baseline));
            } catch (error) {
                /* istanbul ignore next: Shouldn't reach, helps debugging. */
                console.log(
                    JSON.stringify(results, null, 2),
                    JSON.stringify(baseline, null, 2)
                );
 
                /* istanbul ignore next: Shouldn't reach, helps debugging. */
                throw error;
            }
        });
    });
});