This repository was archived by the owner on Mar 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmake-json-patch-tests
More file actions
executable file
·85 lines (72 loc) · 2.13 KB
/
make-json-patch-tests
File metadata and controls
executable file
·85 lines (72 loc) · 2.13 KB
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
#!/usr/bin/env node
'use strict'
var path = require('path')
var fs = require('fs')
var DIR = path.join(__dirname, '..', 'test/json-patch-tests')
var FILES = ['spec_tests', 'tests']
var HEADER = [
"/*",
" DO NOT EDIT",
" generated by scripts/make-json-patch-tests",
"*/\n",
"'use strict'\n",
"var assert = require('assert')",
"var patch = require('../lib/patch')",
"var apply = patch.apply",
"var revert = patch.revert",
"var clone = require('json8').clone\n",
"describe('json-patch/json-patch-tests', function() {\n",
].join('\n')
var FOOTER = '\n})\n'
var body = HEADER
var tests = []
FILES.forEach(function(name) {
require(path.join(DIR, name + '.json')).forEach(function(test) {
tests.push(test)
})
})
tests.forEach(function(test) {
if (test.disabled)
return
if (!test.error && !test.expected)
return
body += "describe(\"" + (test.comment ? test.comment : 'no description') + "\", function() {\n"
if (test.error) {
body += [
"it('throws an error', function() {",
" var test = " + JSON.stringify(test),
" assert.throws(function() {",
" apply(clone(test.doc), test.patch)",
" })",
"})",
"\n",
"it('reverts the document to its original state if an error was thrown', function() {",
" var test = " + JSON.stringify(test),
" var doc = clone(test.doc)",
" assert.throws(function() {",
" apply(doc, test.patch)",
" })",
" assert.deepEqual(doc, test.doc)",
"})"
].join('\n')
}
else {
body += [
"it('applies the patch', function() {",
" var test = " + JSON.stringify(test),
" var r = apply(test.doc, test.patch)",
" assert.deepEqual(r, test.expected)",
"})",
"\n",
"it('reverts the patch', function() {",
" var test = " + JSON.stringify(test),
" var doc = clone(test.doc)",
" var r = apply(doc, test.patch, {reversible: true})",
" assert.deepEqual(revert(r[0], r[1]), test.doc)",
"})"
].join('\n')
}
body += "\n})\n"
})
body += FOOTER
fs.writeFileSync(path.join(__dirname, '..', 'test', 'json-patch-tests.js'), body)