Skip to content

Commit 7bb2eee

Browse files
committed
test: add tests
1 parent 21a0591 commit 7bb2eee

11 files changed

+589
-1
lines changed

.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
'generator-star-spacing': 0,
1313
// allow debugger during development
1414
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0,
15-
'no-multi-spaces': ['error', { ignoreEOLComments: true }]
15+
'no-multi-spaces': ['error', { ignoreEOLComments: true }],
16+
'standard/no-callback-literal': 0
1617
}
1718
}

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test
2+
dist

.stylelintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test
2+
dist

package-lock.json

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "Less plugin for Baidu DLS.",
55
"main": "dist/index.js",
66
"scripts": {
7+
"test": "node -r esm test/run.js",
78
"build": "rollup -c"
89
},
910
"keywords": [
@@ -16,19 +17,22 @@
1617
"author": "Justineo ([email protected])",
1718
"license": "MIT",
1819
"devDependencies": {
20+
"diff": "^4.0.1",
1921
"eslint": "^5.16.0",
2022
"eslint-config-prettier": "^4.3.0",
2123
"eslint-config-standard": "^12.0.0",
2224
"eslint-plugin-import": "^2.17.3",
2325
"eslint-plugin-node": "^9.1.0",
2426
"eslint-plugin-promise": "^4.1.1",
2527
"eslint-plugin-standard": "^4.0.0",
28+
"esm": "^3.2.25",
2629
"kolor": "^1.1.9",
2730
"less": "^3.9.0",
2831
"prettier": "^1.17.1",
2932
"rollup": "^1.12.4",
3033
"rollup-plugin-commonjs": "^10.0.0",
3134
"rollup-plugin-node-resolve": "^5.0.0",
35+
"strip-css-comments": "^4.0.0",
3236
"stylelint": "^10.0.1",
3337
"stylelint-config-prettier": "^5.2.0",
3438
"stylelint-config-standard": "^18.3.0"

src/components/button.less

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@import "../../global.less";
2+
13
/* Font sizes */
24
@dls-button-font-size-xs: @dls-font-size-s;
35
@dls-button-font-size-s: @dls-font-size-s;

test/run.js

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
import chalk from 'chalk'
4+
import less from 'less'
5+
import strip from 'strip-css-comments'
6+
const diff = require('diff')
7+
8+
const INCLUDE_PATH = path.resolve(__dirname, '..')
9+
const SPEC_DIR = path.resolve(__dirname, 'specs')
10+
11+
function logDiff (left, right) {
12+
diff.diffLines(left, right).forEach(item => {
13+
if (item.added || item.removed) {
14+
let text = item.value
15+
.replace('\n', '\u00b6\n')
16+
.replace('\ufeff', '[[BOM]]')
17+
log(chalk[item.added ? 'green' : 'red'](text))
18+
} else {
19+
let value = item.value.replace('\ufeff', '[[BOM]]')
20+
let lines = value.split('\n')
21+
22+
// max line count for each item
23+
let keepLines = 6
24+
// lines to be omitted
25+
let omitLines = lines.length - keepLines
26+
if (lines.length > keepLines) {
27+
lines.splice(
28+
Math.floor(keepLines / 2),
29+
omitLines,
30+
chalk.gray('(...' + omitLines + ' lines omitted...)')
31+
)
32+
}
33+
log(lines.join('\n'))
34+
}
35+
})
36+
log('\n')
37+
}
38+
39+
function extractName (file, ext) {
40+
let pattern = new RegExp('([^\\/\\\\]+)\\.' + ext + '$', 'i')
41+
let match = file.match(pattern)
42+
return match ? match[1] : null
43+
}
44+
45+
function log (msg) {
46+
process.stdout.write(msg)
47+
}
48+
49+
function logLine (msg) {
50+
log(msg + '\n')
51+
}
52+
53+
function getTests (specDir) {
54+
/**
55+
* Get all modules
56+
*/
57+
let srcDir = path.resolve(__dirname, '../src/components')
58+
let modules = [
59+
'global',
60+
...fs
61+
.readdirSync(srcDir)
62+
.map(moduleFile => extractName(moduleFile, 'less'))
63+
.filter(m => m)
64+
]
65+
66+
/**
67+
* Get test suites
68+
*/
69+
let suites = []
70+
let noTests = []
71+
72+
modules.forEach(module => {
73+
let moduleDir = path.join(specDir, module)
74+
if (!fs.existsSync(moduleDir)) {
75+
noTests.push(module)
76+
return
77+
}
78+
if (fs.statSync(moduleDir).isDirectory()) {
79+
let files = fs.readdirSync(moduleDir)
80+
if (files.length === 0) {
81+
noTests.push(module)
82+
}
83+
files.forEach(partFile => {
84+
let part = extractName(partFile, 'less')
85+
if (!part) {
86+
// .css files
87+
return
88+
}
89+
let src = fs.readFileSync(path.resolve(moduleDir, partFile), {
90+
encoding: 'utf8'
91+
})
92+
93+
let expected = ''
94+
if (fs.existsSync(path.resolve(moduleDir, part + '.css'))) {
95+
expected = fs.readFileSync(path.resolve(moduleDir, part) + '.css', {
96+
encoding: 'utf8'
97+
})
98+
}
99+
100+
suites.push({
101+
part: chalk.bold(module) + chalk.white('.') + part,
102+
src,
103+
expected
104+
})
105+
})
106+
}
107+
})
108+
if (noTests.length) {
109+
logLine(
110+
'\u2731 No test specs found for the following module' +
111+
(noTests.length > 1 ? 's' : '') +
112+
':\n' +
113+
noTests.join('\n') +
114+
'\n'
115+
)
116+
} else {
117+
logLine('\u2731 Great. Each module has got test specs.\n')
118+
}
119+
120+
/**
121+
* Prepare tests
122+
*/
123+
return suites.map(suite => {
124+
return done => {
125+
less
126+
.render(suite.src, {
127+
paths: [INCLUDE_PATH],
128+
javascriptEnabled: true
129+
})
130+
.then(
131+
result => {
132+
let passed = true
133+
let actual = strip(result.css, { preserve: false })
134+
.replace(/\n+/g, '\n')
135+
.replace(/^\n/, '')
136+
let expected = suite.expected
137+
if (actual !== expected) {
138+
logLine(chalk.red('\u2718 ' + suite.part))
139+
logDiff(actual, expected)
140+
passed = false
141+
} else {
142+
logLine(chalk.green('\u2714 ' + suite.part))
143+
}
144+
done(passed)
145+
},
146+
err => {
147+
logLine(chalk.red('\u2718 ' + suite.part))
148+
logLine('Less compile error:')
149+
logLine(err)
150+
done(false)
151+
}
152+
)
153+
}
154+
})
155+
}
156+
157+
class TestRunner {
158+
constructor (tests, endCallback) {
159+
this.tests = tests
160+
this.total = tests.length
161+
this.failed = 0
162+
this.endCallback = endCallback
163+
}
164+
165+
next () {
166+
let runner = this.tests.shift()
167+
if (runner) {
168+
runner(passed => {
169+
if (!passed) {
170+
this.failed++
171+
}
172+
this.next()
173+
})
174+
return
175+
}
176+
this.end()
177+
}
178+
179+
end () {
180+
logLine('\n--------\n')
181+
if (!this.failed) {
182+
logLine(
183+
'All ' + this.total + ' spec' + (this.total > 1 ? 's' : '') + ' passed.'
184+
)
185+
} else {
186+
let passed = this.total - this.failed
187+
logLine(
188+
passed +
189+
' spec' +
190+
(passed > 1 ? 's' : '') +
191+
' passed, ' +
192+
this.failed +
193+
' spec' +
194+
(this.failed > 1 ? 's' : '') +
195+
' failed.'
196+
)
197+
198+
// exit with code 1
199+
process.on('exit', () => {
200+
process.reallyExit(1)
201+
})
202+
}
203+
204+
this.endCallback && this.endCallback()
205+
}
206+
207+
start () {
208+
this.next()
209+
}
210+
}
211+
212+
new TestRunner(getTests(SPEC_DIR)).start()

test/specs/button/button.css

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
div {
2+
-dls-button-font-size-xs: 12px;
3+
-dls-button-font-size-s: 12px;
4+
-dls-button-font-size-m: 14px;
5+
-dls-button-font-size-l: 16px;
6+
-dls-button-font-size-xl: 16px;
7+
-dls-button-height-xs: 24px;
8+
-dls-button-height-s: 28px;
9+
-dls-button-height-m: 32px;
10+
-dls-button-height-l: 36px;
11+
-dls-button-height-xl: 40px;
12+
-dls-button-padding-xs: 8px;
13+
-dls-button-padding-s: 12px;
14+
-dls-button-padding-m: 16px;
15+
-dls-button-padding-l: 20px;
16+
-dls-button-padding-xl: 24px;
17+
-dls-button-border-radius-xs: 2px;
18+
-dls-button-border-radius-s: 2px;
19+
-dls-button-border-radius-m: 3px;
20+
-dls-button-border-radius-l: 4px;
21+
-dls-button-border-radius-xl: 4px;
22+
-dls-button-content-spacing: 4px;
23+
-dls-button-font-color-normal: #333;
24+
-dls-button-font-color-normal-disabled: #ccc;
25+
-dls-button-border-color-normal: #e0e0e0;
26+
-dls-button-border-color-normal-disabled: #eee;
27+
-dls-button-background-color-normal: #fff;
28+
-dls-button-background-color-normal-hover: #fafafa;
29+
-dls-button-background-color-normal-active: #f5f5f5;
30+
-dls-button-background-color-normal-disabled: #ccc;
31+
-dls-button-font-color-strong: #3d88f2;
32+
-dls-button-font-color-strong-disabled: #8cb8ff;
33+
-dls-button-border-color-strong: #66a3ff;
34+
-dls-button-border-color-strong-disabled: #d9e6ff;
35+
-dls-button-background-color-strong: #fff;
36+
-dls-button-background-color-strong-hover: #f2f7ff;
37+
-dls-button-background-color-strong-active: #d9e6ff;
38+
-dls-button-background-color-strong-disabled: #f2f7ff;
39+
-dls-button-font-color-primary: #fff;
40+
-dls-button-font-color-primary-disabled: #fff;
41+
-dls-button-border-color-primary: transparent;
42+
-dls-button-border-color-primary-disabled: transparent;
43+
-dls-button-background-color-primary: #3d88f2;
44+
-dls-button-background-color-primary-hover: #66a3ff;
45+
-dls-button-background-color-primary-active: #2970cc;
46+
-dls-button-background-color-primary-disabled: #d9e6ff;
47+
-dls-button-font-color-translucent: #fff;
48+
-dls-button-font-color-translucent-disabled: #999;
49+
-dls-button-border-color-translucent: transparent;
50+
-dls-button-border-color-translucent-disabled: transparent;
51+
-dls-button-background-color-translucent: rgba(255, 255, 255, 0.6);
52+
-dls-button-background-color-translucent-hover: rgba(255, 255, 255, 0.5);
53+
-dls-button-background-color-translucent-active: rgba(255, 255, 255, 0.7);
54+
-dls-button-background-color-translucent-disabled: rgba(255, 255, 255, 0.6);
55+
}

0 commit comments

Comments
 (0)