forked from apidoc/apidoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apidoc_test.js
109 lines (85 loc) · 3.49 KB
/
apidoc_test.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
/*jshint unused:false, expr:true */
/**
* Test: apiDoc full parse
*/
// node modules
var apidoc = require('apidoc-core');
var exec = require('child_process').exec;
var fs = require('fs-extra');
var path = require('path');
var semver = require('semver');
var should = require('should');
var versions = require('apidoc-example').versions;
describe('apiDoc full example', function() {
// get latest example for the used apidoc-spec
var latestExampleVersion = semver.maxSatisfying(versions, '~' + apidoc.getSpecificationVersion()); // ~0.2.0 = >=0.2.0 <0.3.0
var exampleBasePath = 'node_modules/apidoc-example/' + latestExampleVersion;
var fixturePath = exampleBasePath + '/fixtures';
var fixtureFiles = [
'api_data.js',
'api_data.json',
'api_project.js',
'api_project.json',
'index.html'
];
before(function(done) {
fs.removeSync('./tmp/');
done();
});
after(function(done) {
done();
});
// version found
it('should find latest example version', function(done) {
should(latestExampleVersion).be.ok;
done();
});
// create
it('should create example in tmp/', function(done) {
var cmd = 'node ./bin/apidoc -i ' + exampleBasePath + '/src/ -o tmp/ -t test/template/ --silent';
exec(cmd, function(err, stdout, stderr) {
if (err)
throw err;
if (stderr)
throw stderr;
done();
});
});
// check
it('should find created files', function(done) {
fixtureFiles.forEach(function(name) {
fs.existsSync(fixturePath + '/' + name).should.eql(true);
});
done();
});
// compare
it('created files should equal to fixtures', function(done) {
var timeRegExp = /\"time\"\:\s\"(.*)\"/g;
var versionRegExp = /\"version\"\:\s\"(.*)\"/g;
var filenameRegExp = new RegExp('(?!"filename":\\s")(' + exampleBasePath + '/)', 'g');
fixtureFiles.forEach(function(name) {
var fixtureContent = fs.readFileSync(fixturePath + '/' + name, 'utf8');
var createdContent = fs.readFileSync('./tmp/' + name, 'utf8');
// creation time remove (never equal)
fixtureContent = fixtureContent.replace(timeRegExp, '');
createdContent = createdContent.replace(timeRegExp, '');
// creation time remove (or fixtures must be updated every time the version change)
fixtureContent = fixtureContent.replace(versionRegExp, '');
createdContent = createdContent.replace(versionRegExp, '');
// remove the base path
createdContent = createdContent.replace(filenameRegExp, '');
var fixtureLines = fixtureContent.split(/\n/);
var createdLines = createdContent.split(/\n/);
if (fixtureLines.length !== createdLines.length)
throw new Error('File ./tmp/' + name + ' not equals to ' + fixturePath + '/' + name);
for (var lineNumber = 0; lineNumber < fixtureLines.length; lineNumber += 1) {
if (fixtureLines[lineNumber] !== createdLines[lineNumber])
throw new Error('File ./tmp/' + name + ' not equals to ' + fixturePath + '/' + name + ' in line ' + (lineNumber + 1) +
'\nfixture: ' + fixtureLines[lineNumber] +
'\ncreated: ' + createdLines[lineNumber]
);
}
});
done();
});
});