| 1 |
"use strict"; |
| 2 |
|
| 3 |
var fs = require("fs"); |
| 4 |
var path = require("path"); |
| 5 |
var postcss = require("postcss"); |
| 6 |
var processor = require("../src"); |
| 7 |
|
| 8 |
function generateInvalidCSS(css) { |
| 9 |
css.walkDecls(function (decl) { |
| 10 |
decl.value = decl.value.replace(/_colon_/g, ":"); // because using a : in the tests would make it invalid CSS. |
| 11 |
}); |
| 12 |
} |
| 13 |
|
| 14 |
function normalize(str) { |
| 15 |
return str.replace(/\r\n?/g, "\n").replace(/\n$/, ""); |
| 16 |
} |
| 17 |
|
| 18 |
var generateScopedName = processor.generateScopedName; |
| 19 |
|
| 20 |
describe("test-cases", function () { |
| 21 |
var testDir = path.join(__dirname, "test-cases"); |
| 22 |
fs.readdirSync(testDir).forEach(function (testCase) { |
| 23 |
if (fs.existsSync(path.join(testDir, testCase, "source.css"))) { |
| 24 |
it("should " + testCase.replace(/-/g, " "), () => { |
| 25 |
var input = normalize( |
| 26 |
fs.readFileSync(path.join(testDir, testCase, "source.css"), "utf-8") |
| 27 |
); |
| 28 |
|
| 29 |
var expected, expectedError; |
| 30 |
|
| 31 |
if (fs.existsSync(path.join(testDir, testCase, "expected.error.txt"))) { |
| 32 |
expectedError = normalize( |
| 33 |
fs.readFileSync( |
| 34 |
path.join(testDir, testCase, "expected.error.txt"), |
| 35 |
"utf-8" |
| 36 |
) |
| 37 |
).split("\n")[0]; |
| 38 |
} else { |
| 39 |
expected = normalize( |
| 40 |
fs.readFileSync( |
| 41 |
path.join(testDir, testCase, "expected.css"), |
| 42 |
"utf-8" |
| 43 |
) |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
var config = { from: "/input" }; |
| 48 |
var options = { |
| 49 |
generateScopedName: function (exportedName, inputPath) { |
| 50 |
var normalizedPath = inputPath.replace(/^[A-Z]:/, ""); |
| 51 |
return generateScopedName(exportedName, normalizedPath); |
| 52 |
}, |
| 53 |
}; |
| 54 |
|
| 55 |
if (fs.existsSync(path.join(testDir, testCase, "config.json"))) { |
| 56 |
config = JSON.parse( |
| 57 |
fs.readFileSync( |
| 58 |
path.join(testDir, testCase, "config.json"), |
| 59 |
"utf-8" |
| 60 |
) |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
if (fs.existsSync(path.join(testDir, testCase, "options.js"))) { |
| 65 |
options = require(path.join(testDir, testCase, "options.js")); |
| 66 |
} |
| 67 |
|
| 68 |
var pipeline = postcss([generateInvalidCSS, processor(options)]); |
| 69 |
|
| 70 |
if (expectedError) { |
| 71 |
expect(() => { |
| 72 |
pipeline.process(input, config).css; |
| 73 |
}).toThrow(new RegExp(expectedError)); |
| 74 |
} else { |
| 75 |
expect(expected).toBe(pipeline.process(input, config).css); |
| 76 |
} |
| 77 |
}); |
| 78 |
} |
| 79 |
}); |
| 80 |
}); |