Skip to content

Instantly share code, notes, and snippets.

@cespare
Created February 27, 2013 22:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cespare/5052442 to your computer and use it in GitHub Desktop.
Save cespare/5052442 to your computer and use it in GitHub Desktop.
source "https://rubygems.org"
gem "toml", :git => "git://github.com/jm/toml", :ref => "479353753fc71173e9a8f778a9c3c20a0f92d189"
gem "json"
$ toml-test ~/test/test.sh
Test: array-mixed-types-arrays-and-ints (invalid)
Expected an error, but no error was reported.
-------------------------------------------------------------------------------
Test: array-mixed-types-ints-and-floats (invalid)
Expected an error, but no error was reported.
-------------------------------------------------------------------------------
Test: array-mixed-types-strings-and-ints (invalid)
Expected an error, but no error was reported.
-------------------------------------------------------------------------------
Test: duplicate-key-keygroup (invalid)
Expected an error, but no error was reported.
-------------------------------------------------------------------------------
Test: duplicate-keygroups (invalid)
Expected an error, but no error was reported.
-------------------------------------------------------------------------------
Test: duplicate-keys (invalid)
Expected an error, but no error was reported.
-------------------------------------------------------------------------------
Test: overflow-int (invalid)
Expected an error, but no error was reported.
-------------------------------------------------------------------------------
Test: overflow-neg-int (invalid)
Expected an error, but no error was reported.
-------------------------------------------------------------------------------
Test: array-empty (valid)
/home/caleb/test/toml.rb:27:in `transform_toml_value': Unhandled type: Parslet::Slice (RuntimeError)
from /home/caleb/test/toml.rb:24:in `block in transform_toml_value'
from /home/caleb/test/toml.rb:24:in `map'
from /home/caleb/test/toml.rb:24:in `transform_toml_value'
from /home/caleb/test/toml.rb:13:in `block in transform_toml'
from /home/caleb/test/toml.rb:12:in `each'
from /home/caleb/test/toml.rb:12:in `reduce'
from /home/caleb/test/toml.rb:12:in `transform_toml'
from /home/caleb/test/toml.rb:32:in `<main>'
33 passed, 9 failed
require "toml"
require "json"
raw = ARGF.read
begin
decoded = TOML.load(raw)
rescue
exit 1
end
def transform_toml(t)
jsonified = t.reduce({}) do |j, (k, v)|
j.merge k => transform_toml_value(v)
end
end
def transform_toml_value(v)
type, value = case v
when String then ["string", v]
when Fixnum, Bignum then ["integer", v.to_s]
when Float then ["float", v.to_s]
when DateTime then ["datetime", v.strftime("%Y-%m-%dT%H:%M:%SZ")]
when TrueClass, FalseClass then ["bool", v.to_s]
when Array then ["array", v.map { |e| transform_toml_value(e) }]
when Hash
return transform_toml(v)
else raise "Unhandled type: #{v.class}"
end
{ "type" => type, "value" => value }
end
puts transform_toml(decoded).to_json
#!/bin/sh
# I ran tests according to the directions at https://github.com/BurntSushi/toml-test:
# $ toml-test ~/test/toml_test.sh
BUNDLE_GEMFILE=~/test/Gemfile bundle exec ruby ~/test/toml.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment