From 1c8a1c02dce94e23fd14da4e1fa5e65c6fe0fdb0 Mon Sep 17 00:00:00 2001 From: Markus Westerlind Date: Sun, 16 Aug 2020 11:17:22 +0200 Subject: [PATCH] test: Extract doc tests defined in record fields cc #779 --- tests/main.rs | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/tests/main.rs b/tests/main.rs index 41f9a28b13..a2f1eb613f 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -17,6 +17,7 @@ use gluon::{ base::{ ast::{Expr, Pattern, SpannedExpr}, filename_to_module, + metadata::BaseMetadata, symbol::Symbol, types::{ArcType, Type}, }, @@ -263,6 +264,18 @@ fn gather_doc_tests(expr: &SpannedExpr) -> 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; @@ -282,19 +295,25 @@ fn gather_doc_tests(expr: &SpannedExpr) -> 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);