diff --git a/src/spectator/dsl.cr b/src/spectator/dsl.cr index 8695ccd..49b6d37 100644 --- a/src/spectator/dsl.cr +++ b/src/spectator/dsl.cr @@ -2,12 +2,46 @@ require "./example_group" module Spectator module DSL + + SPECIAL_CHAR_MAPPING = { + '~' => "Tilde", + '`' => "Tick", + '!' => "Bang", + '@' => "At", + '#' => "Hash", + '$' => "Dollar", + '%' => "Percent", + '^' => "Carret", + '&' => "And", + '*' => "Star", + '(' => "LParen", + ')' => "RParen", + '+' => "Plus", + '=' => "Eq", + '{' => "LBrace", + '}' => "RBrace", + '[' => "LBracket", + ']' => "RBracket", + ':' => "Colon", + ';' => "SColon", + '<' => "Lt", + '>' => "Gt", + ',' => "Comma", + '.' => "Dot", + '?' => "Question", + '/' => "Slash", + '"' => "DQuote", + '|' => "Or", + '\\' => "BSlash", + '\'' => "SQuote" + } + macro describe(what, type = "Describe", &block) context({{what}}, {{type}}) {{block}} end macro context(what, type = "Context", &block) - {% safe_name = what.id.stringify.gsub(/\W+/, "_") %} + {% safe_name = what.id.stringify.chars.map { |c| SPECIAL_CHAR_MAPPING[c] || c }.join("").gsub(/\W+/, "_") %} {% module_name = (type.id + safe_name.camelcase).id %} {% context_module = CONTEXT_MODULE %} {% parent_given_vars = GIVEN_VARIABLES %} @@ -37,7 +71,7 @@ module Spectator end macro it(description, &block) - {% safe_name = description.id.stringify.gsub(/\W+/, "_") %} + {% safe_name = description.id.stringify.chars.map { |c| SPECIAL_CHAR_MAPPING[c] || c }.join("").gsub(/\W+/, "_") %} {% class_name = (safe_name.camelcase + "Example").id %} {% given_vars = GIVEN_VARIABLES %} {% var_names = given_vars.map { |v| v[0] } %}