forked from SAP/less-openui5
/
test-diff.js
103 lines (90 loc) · 2.39 KB
/
test-diff.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
/* eslint-env mocha */
"use strict";
const assert = require("assert");
const css = require("@adobe/css-tools");;
const fs = require("fs");
const path = require("path");
// tested module
const diff = require("../lib/diff");
const options = {
encoding: "utf8"
};
const cssPath = "test/fixtures/diff/css";
describe("Diff algorithm", function() {
it("should create a diff with moved comment block in compare and additional rule in compare", function() {
const compareCSS = fs.readFileSync(path.join(cssPath, "library1/compare.css"), options);
const baseCSS = fs.readFileSync(path.join(cssPath, "library1/base.css"), options);
// Create diff object between embeddedCompare and embedded
const oBase = css.parse(baseCSS);
const oEmbedded = css.parse(compareCSS);
const oResult = diff(oBase, oEmbedded);
assert.deepStrictEqual(oResult.diff.stylesheet.rules.map(convertRuleToComparableString), [
{
"type": "rule",
"value": "a"
},
{
"type": "rule",
"value": "b"
},
{
"type": "comment",
"value": " mine "
},
{
"type": "rule",
"value": "b.test"
}
]);
assert.deepStrictEqual(oResult.stack.stylesheet.rules.map(convertRuleToComparableString), [
{
"type": "rule",
"value": "html"
},
{
"type": "comment",
"value": " mine2 "
},
{
"type": "comment",
"value": " single "
}
]);
});
it("should create a diff with more rules in base than in compare", function() {
const compareCSS = fs.readFileSync(path.join(cssPath, "library2/compare.css"), options);
const baseCSS = fs.readFileSync(path.join(cssPath, "library2/base.css"), options);
// Create diff object between embeddedCompare and embedded
const oBase = css.parse(baseCSS);
const oEmbedded = css.parse(compareCSS);
const oResult = diff(oBase, oEmbedded);
assert.deepStrictEqual(oResult.diff.stylesheet.rules.map(convertRuleToComparableString), [
{
"type": "rule",
"value": "a"
},
{
"type": "rule",
"value": "b"
}
]);
assert.deepStrictEqual(oResult.stack.stylesheet.rules.map(convertRuleToComparableString), []);
});
});
const convertRuleToComparableString = function(rule) {
if (rule.type === "comment") {
return {
type: rule.type,
value: rule.comment
};
} else if (rule.type === "rule") {
return {
type: rule.type,
value: rule.selectors.join(",")
};
}
return {
type: rule.type,
value: rule.toString()
};
};