Skip to content
Browse files

Merge in updates to atom handling

  • Loading branch information...
2 parents 5d4592c + 0711496 commit 2185888df508c52532ec60d40ec723d4cb0e127f @dreverri committed Aug 17, 2012
Showing with 42 additions and 6 deletions.
  1. +24 −5 lib/erlang_template_helper.rb
  2. +18 −1 test/lib/erlang_template_helper/erlang_template_helper_test.rb
View
29 lib/erlang_template_helper.rb
@@ -67,20 +67,39 @@ def initialize(str)
def to_s
case @str
- when /^__atom_(.*)/
- "'#{$1}'"
when /^__binary_(.*)/
- "<<\"#{$1}\">>"
+ to_binary($1)
when /^__string_(.*)/
- "\"#{$1}\""
+ to_string($1)
+ when /^__atom_(.*)/
+ to_atom($1)
else
- @str
+ to_atom(@str)
end
end
def pp(level=0)
to_s
end
+
+ private
+
+ def to_binary(str)
+ "<<\"#{str}\">>"
+ end
+
+ def to_string(str)
+ "\"#{str}\""
+ end
+
+ def to_atom(str)
+ case str
+ when /^[a-z][\w@]*$/
+ str
+ else
+ "'#{str}'"
+ end
+ end
end
class Array
View
19 test/lib/erlang_template_helper/erlang_template_helper_test.rb
@@ -7,7 +7,7 @@
end
it "should produce an atom" do
- Eth::Config.new("__atom_key").to_s.must_equal "'key'."
+ Eth::Config.new("__atom_key").to_s.must_equal "key."
end
it "should produce a binary" do
@@ -112,3 +112,20 @@ class Array
actual.must_equal expected
end
end
+
+describe "single quote atoms" do
+ atoms_under_test = {
+ "containing periods" => ["riak@127.0.0.1"],
+ "beginning with capital letters" => ["Hello"],
+ "containing non-aplhanumeric characters" => ["hello:world"],
+ "containing dashses" => ["hello-world"]
+ }
+
+ atoms_under_test.each do |scenario, atoms|
+ it "should single quote atoms #{scenario}" do
+ atoms.each do |atom|
+ Eth::String.new(atom).to_s.must_equal "'#{atom}'"
+ end
+ end
+ end
+end

0 comments on commit 2185888

Please sign in to comment.
Something went wrong with that request. Please try again.