Feat crate-doc testings (#227)

This commit is contained in:
Folyd 2022-12-10 20:46:47 +08:00 committed by GitHub
parent a5e5998501
commit 1a51ae5d23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 39 deletions

View file

@ -40,8 +40,8 @@ class CrateDocManager {
// 2. https://docs.rs/md-5/0.10.5/md5/ // 2. https://docs.rs/md-5/0.10.5/md5/
// //
// Here is the rule: https://docs.rs/{crateName}/{crateVersion}/{libName} // Here is the rule: https://docs.rs/{crateName}/{crateVersion}/{libName}
static async addCrate(libName, version, searchIndex, crateName) { static async addCrate({ libName, version, searchIndex, crateName }) {
if (libName in searchIndex) { if (searchIndex && libName in searchIndex) {
await storage.setItem(`@${libName}`, searchIndex); await storage.setItem(`@${libName}`, searchIndex);
let doc = searchIndex[libName]["doc"]; let doc = searchIndex[libName]["doc"];
let crates = await CrateDocManager.getCrates(); let crates = await CrateDocManager.getCrates();

View file

@ -259,7 +259,7 @@ window.addEventListener("message", async function (event) {
event.data && event.data &&
event.data.direction === "rust-search-extension:docs.rs") { event.data.direction === "rust-search-extension:docs.rs") {
let message = event.data.message; let message = event.data.message;
await CrateDocManager.addCrate(message.libName, message.crateVersion, message.searchIndex, message.crateName); await CrateDocManager.addCrate(message);
insertAddToExtensionElement(getState(message.crateVersion)); insertAddToExtensionElement(getState(message.crateVersion));
console.log("Congrats! This crate has been installed successfully!"); console.log("Congrats! This crate has been installed successfully!");
} }

View file

@ -29,7 +29,6 @@
<script src="storage.js"></script> <script src="storage.js"></script>
<script src="deminifier.spec.js"></script> <script src="deminifier.spec.js"></script>
<script src="crate-doc.spec.js"></script> <script src="crate-doc.spec.js"></script>
<script src="script-lib.spec.js"></script>
<script> <script>
mocha.run(); mocha.run();
</script> </script>

View file

@ -1,3 +1,44 @@
function testCrateDocSearchManager({ crateName, crateVersion, searchIndex, libName }) {
localStorage.clear();
describe(`crate: ${crateName}, CrateDocSearchManager`, function () {
after(async function () {
await CrateDocManager.removeCrate(crateName);
});
describe("crates", function () {
it("getCrates()", async function () {
(await CrateDocManager.getCrates()).should.deep.equal({});
});
it("addCrate()", async function () {
await CrateDocManager.addCrate({ libName, crateVersion, searchIndex, crateName });
let crates = await CrateDocManager.getCrates();
Object.keys(crates).should.contains(libName);
});
it("getCrateByName()", async function () {
let a = await CrateDocManager.getCrateByName(crateName);
let b = await CrateDocManager.getCrateByName(libName);
a.should.deep.equal(b);
});
it("getSearchIndex()", async function () {
searchIndex.should.deep.equal(await CrateDocManager.getCrateSearchIndex(crateName));
searchIndex.should.deep.equal(await CrateDocManager.getCrateSearchIndex(libName));
});
it("removeCrate()", async function () {
await CrateDocManager.removeCrate(libName);
(await CrateDocManager.getCrates()).should.deep.equal({});
});
});
});
}
testCrateDocSearchManager({
crateName: "md-5",
crateVersion: "0.10.5",
libName: "md5",
searchIndex: JSON.parse('{"md5":{"doc":"An implementation of the MD5 cryptographic hash algorithm.","t":[8,6,3,11,11,10,11,11,11,2,10,10,11,10,10,10,11,11,11,10,10,10,10,11,11,11,11,11,10,11,11],"n":["Digest","Md5","Md5Core","borrow","borrow_mut","chain_update","clone","clone_into","default","digest","digest","finalize","finalize_fixed_core","finalize_into","finalize_into_reset","finalize_reset","fmt","from","into","new","new_with_prefix","output_size","reset","reset","to_owned","try_from","try_into","type_id","update","update_blocks","write_alg_name"],"q":["md5","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""],"d":["Convinience wrapper trait covering functionality of …","MD5 hasher state.","Core MD5 hasher state.","","","Process input data in a chained manner.","","","","","Compute hash of <code>data</code>.","Retrieve result and consume hasher instance.","","Write result into provided array and consume the hasher …","Write result into provided array and reset the hasher …","Retrieve result and reset hasher instance.","","Returns the argument unchanged.","Calls <code>U::from(self)</code>.","Create new hasher instance.","Create new hasher instance which has processed the …","Get output size of the hasher","Reset hasher instance to its initial state.","","","","","","Process data, updating the internal state.","",""],"i":[0,0,0,2,2,12,2,2,2,0,12,12,2,12,12,12,2,2,2,12,12,12,12,2,2,2,2,2,12,2,2],"f":[0,0,0,[[]],[[]],[1],[2,2],[[]],[[],2],0,[1,[[4,[3]]]],[[],[[4,[3]]]],[[2,5,6]],[4],[4],[[],[[4,[3]]]],[[2,7],8],[[]],[[]],[[]],[1],[[],9],[[]],[2],[[]],[[],10],[[],10],[[],11],[1],[2],[7,8]],"p":[{"ty":8,"name":"AsRef"},{"ty":3,"name":"Md5Core"},{"ty":15,"name":"u8"},{"ty":3,"name":"GenericArray"},{"ty":6,"name":"Buffer"},{"ty":6,"name":"Output"},{"ty":3,"name":"Formatter"},{"ty":6,"name":"Result"},{"ty":15,"name":"usize"},{"ty":4,"name":"Result"},{"ty":3,"name":"TypeId"},{"ty":8,"name":"Digest"}]}}'),
});
let crateName = "matches"; let crateName = "matches";
let crateVersion = "0.1.8"; let crateVersion = "0.1.8";
let libName = "matches"; let libName = "matches";
@ -13,37 +54,16 @@ let searchIndex = {
} }
}; };
describe("CrateDocSearchManager", function() { testCrateDocSearchManager({
after(async function() { crateName, crateVersion, libName, searchIndex
await CrateDocManager.removeCrate(crateName);
});
describe("crates", function() {
it("getCrates()", async function() {
(await CrateDocManager.getCrates()).should.deep.equal({});
});
it("addCrate()", async function() {
await CrateDocManager.addCrate(libName, crateVersion, searchIndex, crateName);
let crates = await CrateDocManager.getCrates();
Object.keys(crates).should.contains(crateName);
});
it("getSearchIndex()", async function() {
let searchIndex = await CrateDocManager.getCrateSearchIndex(crateName);
searchIndex.should.deep.equal(searchIndex);
});
it("removeCrate()", async function() {
await CrateDocManager.removeCrate(crateName);
(await CrateDocManager.getCrates()).should.deep.equal({});
});
});
}); });
describe("CrateDocSearch", function() { describe("CrateDocSearch", function () {
after(async function() { after(async function () {
await CrateDocManager.removeCrate(crateName); await CrateDocManager.removeCrate(crateName);
}); });
describe("search", function() { describe("search", function () {
let searcher = new CrateDocSearch(); let searcher = new CrateDocSearch();
[ [
["@match", 2], ["@match", 2],
@ -51,16 +71,16 @@ describe("CrateDocSearch", function() {
["@matches m", 5], ["@matches m", 5],
["@matches z", 1] ["@matches z", 1]
] ]
.forEach(function([keyword, len]) { .forEach(function ([keyword, len]) {
it(`"${keyword}" search()`, async function() { it(`"${keyword}" search()`, async function () {
await CrateDocManager.addCrate(libName, crateVersion, searchIndex, crateName); await CrateDocManager.addCrate({ libName, crateVersion, searchIndex, crateName });
let result = await searcher.search(keyword); let result = await searcher.search(keyword);
result.should.have.lengthOf(len); result.should.have.lengthOf(len);
});
}); });
});
}); });
describe("parseCrateDocsSearchKeyword", function() { describe("parseCrateDocsSearchKeyword", function () {
[ [
["@tokio", ["tokio", ""]], ["@tokio", ["tokio", ""]],
["@tokio spawn", ["tokio", "spawn"]], ["@tokio spawn", ["tokio", "spawn"]],
@ -71,8 +91,8 @@ describe("CrateDocSearch", function() {
["@tokio task::spawn", ["tokio", "task::spawn"]], ["@tokio task::spawn", ["tokio", "task::spawn"]],
["@tokio::task::spawn", ["tokio", "task::spawn"]], ["@tokio::task::spawn", ["tokio", "task::spawn"]],
["@tokio time::sleep::poll", ["tokio", "time::sleep::poll"]], ["@tokio time::sleep::poll", ["tokio", "time::sleep::poll"]],
].forEach(function([keyword, expected]) { ].forEach(function ([keyword, expected]) {
it(`parseCrateDocsSearchKeyword("${keyword}")`, function() { it(`parseCrateDocsSearchKeyword("${keyword}")`, function () {
let result = CrateDocSearch.parseCrateDocsSearchKeyword(keyword); let result = CrateDocSearch.parseCrateDocsSearchKeyword(keyword);
result.should.deep.equal(expected); result.should.deep.equal(expected);
}); });