require File.dirname(__FILE__) + '/test_helper.rb' require 'json' class TestJsont < Test::Unit::TestCase def setup end def test_expressions assert_equal(Jsont.new(JSON.parse('{ "self": "{pnt}
", "pnt": "{pnt.x}{pnt.y}" }')).transform(JSON.parse('{"pnt": { "x":2, "y":3 }}')), '
23
') assert_equal(Jsont.new(JSON.parse('{ "self": "{$}
","self[*]": "{$}" }')).transform(JSON.parse('[1,2]')), '
12
') assert_equal(Jsont.new(JSON.parse('{ "self": "\n{$}\n
", "self[*]": "{$}\n", "self[*][*]": "{$}" }')).transform(JSON.parse('[[1,2],[3,4]]')), '
12
34
') assert_equal(Jsont.new(JSON.parse('{"self": "
\n{p}\n
", "p": "{$}
\n", "p[*]": "{$.x}{$.y}"}')).transform(JSON.parse('{"a":"hello", "p":[{"x":1, "y":2},{"x":3, "y":4}]}')),'
1234
') assert_equal(Jsont.new(JSON.parse('{ "self": "{$.title}" }')).transform(JSON.parse('{ "uri":"http://somewhere.org", "title":"somewhere homepage" }')), 'somewhere homepage') assert_equal(Jsont.new({'self' => '
{$.test}
', 'test' => '

{$.x} {$.y}

'}).transform({'test' => {'x' => 1, 'y' => 2}}), '

1 2

') assert_equal(Jsont.new({ "self" => "
{$.mainNav}{$.subNav}
", "mainNav" => "", "mainNav.menus[*]" => "
  • {$.label}\n
  • ", "mainNav.menus[*].items[*]" => "
  • {$.label}
  • ", "subNav" => "", "subNav.menus[*]" => "
    {$.items}
    ", "subNav.menus[*].items[*]" => "
  • {$.label}
  • " }).transform({ "mainNav"=> { "menus"=> [ { "label"=> "Research", "url"=> "http://localhost/research/databases.php", "class"=> "research", "items"=> [ {"url"=> "http://localhost/research/databases.php", "label"=> "Databases"}, {"url"=> "http://localhost/research/homework-help.php", "label"=> "Homework Help"}, {"url"=> "http://localhost/research/government.php", "label"=> "Government"}, {"url"=> "http://localhost/research/local-history.php", "label"=> "Local/Family History"}, {"url"=> "http://localhost/research/teacher-resources.php", "label"=> "Teacher Resources"}, {"url"=> "http://localhost/research/business.php", "label"=> "Business"}, {"url"=> "http://localhost/research/investing.php", "label"=> "Investing"}, {"url"=> "http://localhost/research/careers.php", "label"=> "Careers"} ] } ] }, "subNav"=> { "menus"=> [ { "class"=> "research", "items"=> [ {"url"=> "http://localhost/research/databases.php", "label"=> "Databases"}, {"url"=> "http://localhost/research/homework-help.php", "label"=> "Homework Help"}, {"url"=> "http://localhost/research/government.php", "label"=> "Government"}, {"url"=> "http://localhost/research/local-history.php", "label"=> "Local/Family History"}, {"url"=> "http://localhost/research/teacher-resources.php", "label"=> "Teacher Resources"}, {"url"=> "http://localhost/research/business.php", "label"=> "Business"}, {"url"=> "http://localhost/research/investing.php", "label"=> "Investing"}, {"url"=> "http://localhost/research/careers.php", "label"=> "Careers"} ] } ] } }), '
    ') assert_equal( Jsont.new( {'self' => '
    {$.test}
    ', 'test' => Proc.new{|t, opts| t['x']+t['y']}} ).transform( {'test' => {'x' => 1, 'y' => 2}} ), '
    3
    ') end end