summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/cli-table2/lib/print-example.js
blob: ae23609cb2d3a95fab556c2749a10cc5c5d37f39 (plain)
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
var chai = require('chai');
var expect = chai.expect;
var colors = require('colors/safe');
var _ = require('lodash');
var fs = require('fs');
var git = require('git-rev');

function logExample(fn){
  runPrintingExample(fn,
    function logName(name){
      console.log(colors.gray('=========  ') + name + colors.gray('  ================'));
    },
    console.log,  //logTable
    console.log,  //logCode
    console.log,  //logSeparator
    identity      //screenShot
  )
}

function mdExample(fn,file,cb){
  git.long(function(commitHash){
    var buffer = [];

    runPrintingExample(fn,
      function logName(name){
        buffer.push('##### ' + name);
      },
      function logTable(table){
        //md files won't render color strings properly.
        table = stripColors(table);

        // indent table so is displayed preformatted text
        table = '    ' + (table.split('\n').join('\n    '));

        buffer.push(table);
      },
      function logCode(code){
        buffer.push('```javascript');
        buffer.push(stripColors(code));
        buffer.push('```');
      },
      function logSeparator(sep){
        buffer.push(stripColors(sep));
      },
      function logScreenShot(image){
          buffer.push('![table image](https://cdn.rawgit.com/jamestalmage/cli-table2/'
                  + commitHash + '/examples/screenshots/' + image + '.png)');
      }
    );

    fs.writeFileSync(file,buffer.join('\n'));

    if(cb){cb();}
  });
}

/**
 * Actually runs the tests and verifies the functions create the expected output.
 * @param name of the test suite, used in the mocha.describe call.
 * @param fn a function which creates the test suite.
 */
function runTest(name,fn){
  function testExample(name,fn,expected){
    it(name,function(){
      expect(fn().toString()).to.equal(expected.join('\n'));
    });
  }

  describe(name,function () {
    fn(testExample,identity);
  });
}

/**
 * Common execution for runs that print output (either to console or to a Markdown file);
 * @param fn - a function containing the tests/demos.
 * @param logName - callback to print the name of this test to the console or file.
 * @param logTable - callback to print the output of the table to the console or file.
 * @param logCode - callback to print the output of the table to the console or file.
 * @param logSeparator - callback to print extra whitespace between demos.
 * @param logScreenShot - write out a link to the screenShot image.
 */
function runPrintingExample(fn,logName,logTable,logCode,logSeparator,logScreenShot){

  /**
   * Called by every test/demo
   * @param name - the name of this test.
   * @param makeTable - a function which builds the table under test. Also, The contents of this function will be printed.
   * @param expected -  The expected result.
   * @param screenshot - If present, there is an image containing a screenshot of the output
   */
  function printExample(name,makeTable,expected,screenshot){
    var code = makeTable.toString().split('\n').slice(1,-2).join('\n');

    logName(name);
    if(screenshot && logScreenShot){
      logScreenShot(screenshot);
    }
    else {
      logTable(makeTable().toString());
    }
    logCode(code);
    logSeparator('\n');
  }

  fn(printExample);
}

// removes all the color characters from a string
function stripColors(str){
  return str.split( /\u001b\[(?:\d*;){0,5}\d*m/g).join('');
}

// returns the first arg - used as snapshot function
function identity(str){
  return str;
}

module.exports = {
  mdExample:mdExample,
  logExample:logExample,
  runTest:runTest
};