test/test_jsont.rb in jsont-0.1.0 vs test/test_jsont.rb in jsont-0.1.1

- old
+ new

@@ -1,30 +1,72 @@ require File.dirname(__FILE__) + '/test_helper.rb' - +require 'json' class TestJsont < Test::Unit::TestCase def setup end def test_expressions - assert_equal(Jsont.new('{ "self": "<table>{pnt}</table>", "pnt": "<tr><td>{pnt.x}</td><td>{pnt.y}</td></tr>" }').transform('{"pnt": { "x":2, "y":3 }}'), '<table><tr><td>2</td><td>3</td></tr></table>') + assert_equal(Jsont.new(JSON.parse('{ "self": "<table>{pnt}</table>", "pnt": "<tr><td>{pnt.x}</td><td>{pnt.y}</td></tr>" }')).transform(JSON.parse('{"pnt": { "x":2, "y":3 }}')), '<table><tr><td>2</td><td>3</td></tr></table>') - assert_equal(Jsont.new('{ "self": "<table><tr>{$}</tr></table>","self[*]": "<td>{$}</td>" }').transform('[1,2]'), '<table><tr><td>1</td><td>2</td></tr></table>') + assert_equal(Jsont.new(JSON.parse('{ "self": "<table><tr>{$}</tr></table>","self[*]": "<td>{$}</td>" }')).transform(JSON.parse('[1,2]')), '<table><tr><td>1</td><td>2</td></tr></table>') - assert_equal(Jsont.new('{ "self": "<table>\n{$}\n</table>", + assert_equal(Jsont.new(JSON.parse('{ "self": "<table>\n{$}\n</table>", "self[*]": "<tr>{$}</tr>\n", - "self[*][*]": "<td>{$}</td>" }').transform('[[1,2],[3,4]]'), '<table> + "self[*][*]": "<td>{$}</td>" }')).transform(JSON.parse('[[1,2],[3,4]]')), '<table> <tr><td>1</td><td>2</td></tr> <tr><td>3</td><td>4</td></tr> </table>') - assert_equal(Jsont.new('{"self": "<div>\n{p}\n</div>", "p": "<table><tr>{$}</tr></table>\n", "p[*]": "<td>{$.x}</td><td>{$.y}</td>"}').transform('{"a":"hello", "p":[{"x":1, "y":2},{"x":3, "y":4}]}'),'<div> + assert_equal(Jsont.new(JSON.parse('{"self": "<div>\n{p}\n</div>", "p": "<table><tr>{$}</tr></table>\n", "p[*]": "<td>{$.x}</td><td>{$.y}</td>"}')).transform(JSON.parse('{"a":"hello", "p":[{"x":1, "y":2},{"x":3, "y":4}]}')),'<div> <table><tr><td>1</td><td>2</td><td>3</td><td>4</td></tr></table> </div>') - assert_equal(Jsont.new('{ "self": "<a href=\"{uri}\" title=\'{title}\'>{$.title}</a>" }').transform('{ "uri":"http://somewhere.org", "title":"somewhere homepage" }'), '<a href="http://somewhere.org" title=\'somewhere homepage\'>somewhere homepage</a>') + assert_equal(Jsont.new(JSON.parse('{ "self": "<a href=\"{uri}\" title=\'{title}\'>{$.title}</a>" }')).transform(JSON.parse('{ "uri":"http://somewhere.org", "title":"somewhere homepage" }')), '<a href="http://somewhere.org" title=\'somewhere homepage\'>somewhere homepage</a>') assert_equal(Jsont.new({'self' => '<div>{$.test}</div>', 'test' => '<p>{$.x} {$.y}</p>'}).transform({'test' => {'x' => 1, 'y' => 2}}), '<div><p>1 2</p></div>') + + assert_equal(Jsont.new({ + "self" => "<div id=\"topNav\">{$.mainNav}{$.subNav}</div>", + "mainNav" => "<ul id=\"mainNav\">{$.menus}</ul>", + "mainNav.menus[*]" => "<li class=\"dropdown {$.class}\"><a class=\"navLabel\" href=\"{$.url}\">{$.label}</a>\n<ul class=\"clearfix\">{$.items}</ul></li>", + "mainNav.menus[*].items[*]" => "<li class=\"dropDownItem\"><a href=\"{$.url}\">{$.label}</a></li>", + "subNav" => "<ul id=\"subNav\">{$.menus}</ul>", + "subNav.menus[*]" => "<div class=\"{$.class}\">{$.items}</div>", + "subNav.menus[*].items[*]" => "<li><a class=\"{$.class}\" href=\"{$.url}\">{$.label}</a></li>" + }).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"} + ] + } ] + } + }), '<div id="topNav"><ul id="mainNav"><li class="dropdown research"><a class="navLabel" href="http://localhost/research/databases.php">Research</a> +<ul class="clearfix"><li class="dropDownItem"><a href="http://localhost/research/databases.php">Databases</a></li><li class="dropDownItem"><a href="http://localhost/research/homework-help.php">Homework Help</a></li><li class="dropDownItem"><a href="http://localhost/research/government.php">Government</a></li><li class="dropDownItem"><a href="http://localhost/research/local-history.php">Local/Family History</a></li><li class="dropDownItem"><a href="http://localhost/research/teacher-resources.php">Teacher Resources</a></li><li class="dropDownItem"><a href="http://localhost/research/business.php">Business</a></li><li class="dropDownItem"><a href="http://localhost/research/investing.php">Investing</a></li><li class="dropDownItem"><a href="http://localhost/research/careers.php">Careers</a></li></ul></li></ul><ul id="subNav"><div class="research"><li><a class="" href="http://localhost/research/databases.php">Databases</a></li><li><a class="" href="http://localhost/research/homework-help.php">Homework Help</a></li><li><a class="" href="http://localhost/research/government.php">Government</a></li><li><a class="" href="http://localhost/research/local-history.php">Local/Family History</a></li><li><a class="" href="http://localhost/research/teacher-resources.php">Teacher Resources</a></li><li><a class="" href="http://localhost/research/business.php">Business</a></li><li><a class="" href="http://localhost/research/investing.php">Investing</a></li><li><a class="" href="http://localhost/research/careers.php">Careers</a></li></div></ul></div>') + + + end end