diff options
Diffstat (limited to 'src/expand/test.cpp')
-rw-r--r-- | src/expand/test.cpp | 78 |
1 files changed, 76 insertions, 2 deletions
diff --git a/src/expand/test.cpp b/src/expand/test.cpp index fba6556f..01e566ff 100644 --- a/src/expand/test.cpp +++ b/src/expand/test.cpp @@ -7,6 +7,7 @@ */ #include <synext_decorator.hpp> #include <ast/ast.hpp> +#include <ast/crate.hpp> class CTestHandler: public ExpandDecorator @@ -18,10 +19,83 @@ class CTestHandler: ERROR(sp, E0000, "#[test] can only be put on functions - found on " << i.tag_str()); } - // TODO: Proper #[test] support, for now just remove them - i = AST::Item::make_None({}); + if( crate.m_test_harness ) + { + ::AST::TestDesc td; + for(const auto& node : path.nodes()) + { + td.name += "::"; + td.name += node.name(); + } + td.path = ::AST::Path(path); + + crate.m_tests.push_back( mv$(td) ); + } + else + { + i = AST::Item::make_None({}); + } + } +}; +class CTestHandler_SP: + public ExpandDecorator +{ + AttrStage stage() const override { return AttrStage::Pre; } + + void handle(const Span& sp, const AST::MetaItem& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const override { + if( ! i.is_Function() ) { + ERROR(sp, E0000, "#[should_panic] can only be put on functions - found on " << i.tag_str()); + } + + if( crate.m_test_harness ) + { + for(auto& td : crate.m_tests) + { + if( td.path != path ) + continue ; + + if( mi.has_sub_items() ) + { + td.panic_type = ::AST::TestDesc::ShouldPanic::YesWithMessage; + // TODO: Check that name is correct and that it is a string + td.expected_panic_message = mi.items().at(0).string(); + } + else + { + td.panic_type = ::AST::TestDesc::ShouldPanic::Yes; + } + return ; + } + //ERROR() + } + } +}; +class CTestHandler_Ignore: + public ExpandDecorator +{ + AttrStage stage() const override { return AttrStage::Pre; } + + void handle(const Span& sp, const AST::MetaItem& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const override { + if( ! i.is_Function() ) { + ERROR(sp, E0000, "#[should_panic] can only be put on functions - found on " << i.tag_str()); + } + + if( crate.m_test_harness ) + { + for(auto& td : crate.m_tests) + { + if( td.path != path ) + continue ; + + td.ignore = true; + return ; + } + //ERROR() + } } }; STATIC_DECORATOR("test", CTestHandler); +STATIC_DECORATOR("should_panic", CTestHandler_SP); +STATIC_DECORATOR("ignore", CTestHandler_Ignore); |