Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Dec 1, 2023
1 parent 8fe223a commit 99d6f95
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
125 changes: 125 additions & 0 deletions tests/html_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use deno_ast::ModuleSpecifier;
use deno_doc::html::*;
use deno_doc::{DocNode, DocParser, DocParserOptions};
use deno_graph::source::{LoadFuture, LoadResponse, Loader};
use deno_graph::{
BuildOptions, CapturingModuleAnalyzer, GraphKind, ModuleGraph,
};
use futures::future;
use indexmap::IndexMap;
use std::fs::read_dir;
use std::fs::read_to_string;
use std::rc::Rc;

struct SourceFileLoader {}

impl Loader for SourceFileLoader {
fn load(
&mut self,
specifier: &ModuleSpecifier,
_is_dynamic: bool,
_cache_setting: deno_graph::source::CacheSetting,
) -> LoadFuture {
let result = if specifier.scheme() == "file" {
let path = specifier.to_file_path().unwrap();
read_to_string(path)
.map(|content| {
Some(LoadResponse::Module {
specifier: specifier.clone(),
maybe_headers: None,
content: content.into(),
})
})
.map_err(|err| err.into())
} else {
Ok(None)
};
Box::pin(future::ready(result))
}
}

async fn get_files() -> IndexMap<ModuleSpecifier, Vec<DocNode>> {
let files = read_dir(
std::env::current_dir()
.unwrap()
.join("tests")
.join("testdata"),
)
.unwrap();

let source_files: Vec<ModuleSpecifier> = files
.into_iter()
.map(|entry| {
let entry = entry.unwrap();
ModuleSpecifier::from_file_path(entry.path()).unwrap()
})
.collect();

let mut loader = SourceFileLoader {};
let analyzer = CapturingModuleAnalyzer::default();
let mut graph = ModuleGraph::new(GraphKind::TypesOnly);
graph
.build(
source_files.clone(),
&mut loader,
BuildOptions {
module_analyzer: Some(&analyzer),
..Default::default()
},
)
.await;

let parser = DocParser::new(
&graph,
analyzer.as_capturing_parser(),
DocParserOptions {
diagnostics: false,
private: false,
},
)
.unwrap();

let mut source_files = source_files.clone();
source_files.sort();
let mut doc_nodes_by_url = IndexMap::with_capacity(source_files.len());
for source_file in source_files {
let nodes = parser.parse_with_reexports(&source_file).unwrap();
doc_nodes_by_url.insert(source_file, nodes);
}

doc_nodes_by_url
}

#[tokio::test]
async fn html_doc_files() {
let files = generate(
GenerateOptions {
package_name: None,
main_entrypoint: None,
global_symbols: Default::default(),
global_symbol_href_resolver: Rc::new(|_, _| String::new()),
url_resolver: Rc::new(default_url_resolver),
},
&get_files().await,
)
.unwrap();

let mut file_names = files.keys().collect::<Vec<_>>();
file_names.sort();

assert_eq!(
file_names,
[
"./all_symbols.html",
"./index.html",
"./~/Bar.html",
"./~/Foo.html",
"./~/index.html",
"fuse.js",
"page.css",
"search.js",
"search_index.js",
"styles.css",
]
);
}
3 changes: 3 additions & 0 deletions tests/testdata/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Foo {}

export class Bar extends Foo {}

0 comments on commit 99d6f95

Please sign in to comment.