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

Statements: 100% (95 / 95)      Branches: 100% (54 / 54)      Functions: 100% (11 / 11)      Lines: 100% (95 / 95)      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 195 196 197 198 199 200 201 202 203 204 205 206    1   1 1 1   1 1 1       1   1   1 887     1 2092       2092   2092 846 846     2092 1196     2092 56   56 3 5       56     2036         555 555   555     1481 6 6   6     1475 17 17 17 17   17     1458 246 246   246 9           246     1212 38   38     1174 69 69 69   69     1105 894 894   894     211         60 60   60     151 89 89       89 89   89     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,
        key;
 
    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 === 'root') {
        assert('children' in context);
 
        if (context.footnotes) {
            for (key in context.footnotes) {
                validateTokens(context.footnotes[key]);
            }
        }
 
        return;
    }
 
    if (
        type === 'paragraph' ||
        type === 'blockquote' ||
        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 root = mdast(fixture.input, fixture.options),
                baseline = fixture.output;
 
            validateToken(root);
            baseline = JSON.parse(baseline);
 
            try {
                assert(JSON.stringify(root) === JSON.stringify(baseline));
            } catch (error) {
                /* istanbul ignore next: Shouldn't reach, helps debugging. */
                console.log(
                    JSON.stringify(root, null, 2),
                    JSON.stringify(baseline, null, 2)
                );
 
                /* istanbul ignore next: Shouldn't reach, helps debugging. */
                throw error;
            }
        });
    });
});