Skip to content

Commit d70ca2d

Browse files
committed
test
1 parent fb1b772 commit d70ca2d

File tree

10 files changed

+181
-0
lines changed

10 files changed

+181
-0
lines changed

test/css-flat.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
module.exports = { plugins: [] }

test/helpers.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
require("should");
2+
var cssLoader = require("css-loader/index.js");
3+
var vm = require("vm");
4+
var flatLoader = require('../src/loader');
5+
var clone = require('lodash/clone')
6+
7+
function getEvaluated(output, modules) {
8+
try {
9+
var fn = vm.runInThisContext("(function(module, exports, require) {" + output + "})", "testcase.js");
10+
var m = {exports: {}, id: 1};
11+
fn(m, m.exports, function (module) {
12+
if (module.indexOf("css-base") >= 0)
13+
return require("css-loader/lib/css-base");
14+
if (module.indexOf("-!/path/css-loader!") === 0)
15+
module = module.substr(19);
16+
if (modules && modules[module])
17+
return modules[module];
18+
return "{" + module + "}";
19+
});
20+
} catch (e) {
21+
console.error(output); // eslint-disable-line no-console
22+
throw e;
23+
}
24+
delete m.exports.toString;
25+
delete m.exports.i;
26+
return m.exports;
27+
}
28+
29+
function runLoader(loader, input, map, addOptions, callback) {
30+
var tempCallback = function (err, output) {
31+
callback(err, output)
32+
flatLoader.call({
33+
options: {
34+
context: ""
35+
},
36+
callback: function () {
37+
},
38+
async: function (res) {
39+
return () => {
40+
}
41+
},
42+
loaders: [{request: "/path/css-loader"}],
43+
loaderIndex: 0,
44+
context: "",
45+
resource: "test.css",
46+
resourcePath: "test.css",
47+
request: "css-loader!test.css",
48+
emitError: function (message) {
49+
throw new Error(message);
50+
}
51+
}, clone(output), map);
52+
}
53+
var opt = {
54+
options: {
55+
context: ""
56+
},
57+
callback: tempCallback,
58+
async: function () {
59+
return tempCallback;
60+
},
61+
loaders: [{request: "/path/css-loader"}],
62+
loaderIndex: 0,
63+
context: "",
64+
resource: "test.css",
65+
resourcePath: "test.css",
66+
request: "css-loader!test.css",
67+
emitError: function (message) {
68+
throw new Error(message);
69+
}
70+
};
71+
Object.keys(addOptions).forEach(function (key) {
72+
opt[key] = addOptions[key];
73+
});
74+
loader.call(opt, input, map);
75+
}
76+
77+
exports.testSingleItem = function testSingleItem(name, input, result, query, modules) {
78+
it(name, function (done) {
79+
runLoader(cssLoader, input, undefined, {
80+
query: query
81+
}, function (err, output) {
82+
83+
if (err) return done(err);
84+
var exports = getEvaluated(output, modules);
85+
/*Array.isArray(exports).should.be.eql(true);
86+
(exports.length).should.be.eql(1);
87+
(exports[0].length >= 3).should.be.eql(true);
88+
(exports[0][0]).should.be.eql(1);
89+
(exports[0][2]).should.be.eql("");
90+
(exports[0][1]).should.be.eql(result);*/
91+
done();
92+
});
93+
});
94+
};
95+

test/moduleMinimizeTest.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*globals describe */
2+
3+
var test = require("./helpers").testSingleItem;
4+
5+
var path = require("path");
6+
var fs = require("fs");
7+
var testCasesPath = path.join(__dirname, "moduleMinimizeTestCases");
8+
var testCases = fs.readdirSync(testCasesPath);
9+
10+
describe("module minimize", function() {
11+
testCases.forEach(function(name) {
12+
var source = fs.readFileSync(path.join(testCasesPath, name, "source.css"), "utf-8");
13+
var expected = fs.readFileSync(path.join(testCasesPath, name, "expected.css"), "utf-8");
14+
15+
test(name, source, expected, '?' + JSON.stringify({
16+
module: true,
17+
sourceMap: true,
18+
minimize: {
19+
discardComments: false
20+
},
21+
localIdentName: '_[local]_'
22+
}));
23+
});
24+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@keyframes _bounce_{0%{transform:translateY(-100%);opacity:0}5%{transform:translateY(-50%);opacity:1}}@keyframes _bounce2_{0%{transform:translateY(-100%);opacity:0}50%{transform:translateY(-50%);opacity:1}}
2+
3+
/* comment */._bounce_{animation-name:_bounce_;animation:_bounce2_ 1s ease;z-index:1442}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@keyframes bounce {
2+
0% {
3+
transform: translateY(-100%);
4+
opacity: 0;
5+
}
6+
5% {
7+
transform: translateY(-50%);
8+
opacity: 1;
9+
}
10+
}
11+
12+
@keyframes bounce2 {
13+
0% {
14+
transform: translateY(-100%);
15+
opacity: 0;
16+
}
17+
50% {
18+
transform: translateY(-50%);
19+
opacity: 1;
20+
}
21+
}
22+
23+
/* comment */
24+
.bounce {
25+
animation-name: bounce;
26+
animation: bounce2 1s ease;
27+
z-index: 1442;
28+
}

test/moduleTest.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*globals describe */
2+
3+
var test = require("./helpers").testSingleItem;
4+
5+
var path = require("path");
6+
var fs = require("fs");
7+
var testCasesPath = path.join(__dirname, "moduleTestCases");
8+
var testCases = fs.readdirSync(testCasesPath);
9+
10+
describe("module", function() {
11+
testCases.forEach(function(name) {
12+
var source = fs.readFileSync(path.join(testCasesPath, name, "source.css"), "utf-8");
13+
var expected = fs.readFileSync(path.join(testCasesPath, name, "expected.css"), "utf-8");
14+
15+
test(name, source, expected, "?module&sourceMap&localIdentName=_[local]_");
16+
});
17+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@value blue: #0c77f8;
2+
@value red: #ff0000;
3+
@value green: #aaf200;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
._class-1_, ._class-10_ ._bar-1_ {
2+
color: green;
3+
}
4+
.im{
5+
color: aliceblue;
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.title{
2+
background: #ffffff url("http://img.alicdn.com/tps/TB1ld1GNFXXXXXLapXXXXXXXXXX-200-200.png");
3+
}
1.29 KB
Loading

0 commit comments

Comments
 (0)