1 /*
2  *  Created by Phil on 27/6/2014.
3  *  Copyright 2014 Two Blue Cubes Ltd. All rights reserved.
4  *
5  *  Distributed under the Boost Software License, Version 1.0. (See accompanying
6  *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  */
8 #ifndef TWOBLUECUBES_CATCH_TAG_ALIAS_REGISTRY_HPP_INCLUDED
9 #define TWOBLUECUBES_CATCH_TAG_ALIAS_REGISTRY_HPP_INCLUDED
10 
11 #include "catch_tag_alias_registry.h"
12 #include "catch_console_colour.hpp"
13 #include "catch_interfaces_registry_hub.h"
14 #include "catch_stream.h"
15 
16 namespace Catch {
17 
~TagAliasRegistry()18     TagAliasRegistry::~TagAliasRegistry() {}
19 
find(std::string const & alias) const20     Option<TagAlias> TagAliasRegistry::find( std::string const& alias ) const {
21         std::map<std::string, TagAlias>::const_iterator it = m_registry.find( alias );
22         if( it != m_registry.end() )
23             return it->second;
24         else
25             return Option<TagAlias>();
26     }
27 
expandAliases(std::string const & unexpandedTestSpec) const28     std::string TagAliasRegistry::expandAliases( std::string const& unexpandedTestSpec ) const {
29         std::string expandedTestSpec = unexpandedTestSpec;
30         for( std::map<std::string, TagAlias>::const_iterator it = m_registry.begin(), itEnd = m_registry.end();
31                 it != itEnd;
32                 ++it ) {
33             std::size_t pos = expandedTestSpec.find( it->first );
34             if( pos != std::string::npos ) {
35                 expandedTestSpec =  expandedTestSpec.substr( 0, pos ) +
36                                     it->second.tag +
37                                     expandedTestSpec.substr( pos + it->first.size() );
38             }
39         }
40         return expandedTestSpec;
41     }
42 
add(std::string const & alias,std::string const & tag,SourceLineInfo const & lineInfo)43     void TagAliasRegistry::add( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) {
44 
45         if( !startsWith( alias, "[@" ) || !endsWith( alias, ']' ) ) {
46             std::ostringstream oss;
47             oss << Colour( Colour::Red )
48                 << "error: tag alias, \"" << alias << "\" is not of the form [@alias name].\n"
49                 << Colour( Colour::FileName )
50                 << lineInfo << '\n';
51             throw std::domain_error( oss.str().c_str() );
52         }
53         if( !m_registry.insert( std::make_pair( alias, TagAlias( tag, lineInfo ) ) ).second ) {
54             std::ostringstream oss;
55             oss << Colour( Colour::Red )
56                 << "error: tag alias, \"" << alias << "\" already registered.\n"
57                 << "\tFirst seen at "
58                 << Colour( Colour::Red ) << find(alias)->lineInfo << '\n'
59                 << Colour( Colour::Red ) << "\tRedefined at "
60                 << Colour( Colour::FileName) << lineInfo << '\n';
61             throw std::domain_error( oss.str().c_str() );
62         }
63     }
64 
~ITagAliasRegistry()65     ITagAliasRegistry::~ITagAliasRegistry() {}
66 
get()67     ITagAliasRegistry const& ITagAliasRegistry::get() {
68         return getRegistryHub().getTagAliasRegistry();
69     }
70 
RegistrarForTagAliases(char const * alias,char const * tag,SourceLineInfo const & lineInfo)71     RegistrarForTagAliases::RegistrarForTagAliases( char const* alias, char const* tag, SourceLineInfo const& lineInfo ) {
72         getMutableRegistryHub().registerTagAlias( alias, tag, lineInfo );
73     }
74 
75 } // end namespace Catch
76 
77 #endif // TWOBLUECUBES_CATCH_TAG_ALIAS_REGISTRY_HPP_INCLUDED
78