Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Extract doc tests defined in record fields #870

Merged
merged 1 commit into from
Aug 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use gluon::{
base::{
ast::{Expr, Pattern, SpannedExpr},
filename_to_module,
metadata::BaseMetadata,
symbol::Symbol,
types::{ArcType, Type},
},
Expand Down Expand Up @@ -263,6 +264,18 @@ fn gather_doc_tests(expr: &SpannedExpr<Symbol>) -> Vec<(String, String)> {
}

struct DocVisitor(Vec<(String, String)>);

impl DocVisitor {
fn make_test_from_metadata(&mut self, name: &str, metadata: &BaseMetadata<'_>) {
if let Some(comment) = &metadata.comment() {
let source = make_test(&comment.content);
if !source.is_empty() {
self.0.push((format!("{}", name), String::from(source)));
}
}
}
}

impl Visitor<'_, '_> for DocVisitor {
type Ident = Symbol;

Expand All @@ -282,19 +295,25 @@ fn gather_doc_tests(expr: &SpannedExpr<Symbol>) -> Vec<(String, String)> {
}
}
}

Expr::TypeBindings(binds, _) => {
for bind in &**binds {
if let Some(ref comment) = bind.metadata.comment() {
let source = make_test(&comment.content);
if !source.is_empty() {
self.0.push((
format!("{}", bind.name.value.declared_name()),
String::from(source),
));
}
}
self.make_test_from_metadata(
bind.name.value.declared_name(),
&bind.metadata,
);
}
}

Expr::Record { types, exprs, .. } => {
for field in &**types {
self.make_test_from_metadata(field.name.declared_name(), &field.metadata);
}
for field in &**exprs {
self.make_test_from_metadata(field.name.declared_name(), &field.metadata);
}
}

_ => (),
}
walk_expr(self, expr);
Expand Down