diff --git a/string_algorithms.cc b/string_algorithms.cc index cc06728..094fd93 100644 --- a/string_algorithms.cc +++ b/string_algorithms.cc @@ -6,6 +6,8 @@ static_assert( __cplusplus > 2020'99 ); #include #include +#include + #include "error.h" #include "AutoRAII.h" @@ -39,22 +41,31 @@ namespace Alepha::Hydrogen ::detail:: string_algorithms_m void writeChar( const char ch ) override { + if( C::debugExpansion ) error() << "Entry to write and got `" << ch << '`' << std::endl; std::ostream current{ underlying }; if( mode == Normal and ch == sigil ) { + if( C::debugExpansion ) error() << "Normal and got `" << ch << '`' << std::endl; mode= Symbol; varName.str( "" ); return; } if( mode == Symbol and ch == sigil ) { + if( C::debugExpansion ) error() << "Closed symbol and got `" << ch << '`' << std::endl; mode= Normal; const std::string name( varName.view() ); + if( C::debugExpansion ) + { + error() << "Asked to expand `" << name << '`' << std::endl; + } if( not name.empty() ) { + if( C::debugExpansion ) error() << "Name wasn't empty" << std::endl; if( not substitutions.contains( name ) ) { + if( C::debugExpansion ) error() << "Throwing" << std::endl; throw std::runtime_error{ "No such variable: `" + name +"`" }; } if( C::debugExpansion ) error() << "Expanding variable with name `" << name << "`" << std::endl; @@ -64,6 +75,7 @@ namespace Alepha::Hydrogen ::detail:: string_algorithms_m return; } + if( C::debugExpansion ) error() << "Stashing `" << ch << '`' << std::endl; if( mode == Symbol ) current.rdbuf( &varName ); current << ch; } @@ -76,7 +88,7 @@ namespace Alepha::Hydrogen ::detail:: string_algorithms_m { if( C::debugIOStreamLifecycle ) error() << "Mode not being normal, we're throwing (" << ++throws << " times now)..." << std::endl; mode= Normal; - throw std::runtime_error{ "Unterminated variable `" + varName.str() + " in expansion." }; + throw std::runtime_error{ "Unterminated variable `" + varName.str() + "` in expansion." }; } } }; @@ -92,6 +104,7 @@ namespace Alepha::Hydrogen ::detail:: string_algorithms_m exports::expandVariables( const std::string &text, const VarMap &vars, const char sigil ) { std::ostringstream oss; + IOStreams::EnableExceptions exc{ oss }; oss << StartSubstitutions{ sigil, vars }; oss << text; diff --git a/string_algorithms.test/0.cc b/string_algorithms.test/0.cc index 3c3887e..4c908e8 100644 --- a/string_algorithms.test/0.cc +++ b/string_algorithms.test/0.cc @@ -38,7 +38,7 @@ static auto init= enroll <=[] }, }; - "An exception should be thrown when there is a trailing unenclosed variable."_test <=TableTest< Alepha::expandVariables >::ExceptionCases + "An exception should be thrown when there is a trailing unenclosed variable."_test <=TableTest< Alepha::expandVariables >::Cases { { "Complete var", { "$H$ $W$", { { "H", lambaste<="Hello" }, { "W", lambaste<="World" } }, '$' }, @@ -46,11 +46,15 @@ static auto init= enroll <=[] }, { "Complete var", { "$H$ $W$", { { "H", lambaste<="Hello" }, { "W", lambaste<="World" } }, '$' }, - std::type_identity< void >{} + "Hello World" }, { "Incomplete var", { "$H$ $W", { { "H", lambaste<="Hello" }, { "W", lambaste<="World" } }, '$' }, - std::type_identity< std::exception >{} + std::runtime_error{ "Unterminated variable `W` in expansion." } + }, + { "Missing var", + { "$H$ $W$", { { "W", lambaste<="World" } }, '$' }, + std::runtime_error{ "No such variable: `H`" } }, };