Add tests so the transition is less nerve-wracking.
Patch by fu.
diffs (206 lines):
diff -r f7b821ec9d41 -r 94f593a5f7a6 cgi-bin/DW/Controller/Dev.pm
--- a/cgi-bin/DW/Controller/Dev.pm Tue Mar 01 16:59:00 2011 +0800
+++ b/cgi-bin/DW/Controller/Dev.pm Tue Mar 01 17:42:03 2011 +0800
@@ -19,6 +19,8 @@ use strict;
use strict;
use warnings;
use DW::Routing;
+
+use JSON;
DW::Routing->register_static( '/dev/classes', 'dev/classes.tt', app => 1 );
@@ -90,4 +92,52 @@ sub tests_handler {
includes => \@includes,
} );
}
+
+
+# test helpers
+if ( $LJ::IS_DEV_SERVER ) {
+ DW::Routing->register_string( '/dev/testhelper/jsondump',
\&testhelper_json_handler, app => 1, format => "json" )
+}
+
+sub testhelper_json_handler {
+ my $r = DW::Request->get;
+
+ my $undef;
+
+ my $hash = {
+ string => "string",
+ num => 42,
+ array => [ "a", "b", 2 ],
+ hash => { a => "apple", b => "bazooka" },
+ nil => undef,
+ nilvar => $undef,
+ blank => "",
+ zero => 0,
+ symbols => qq{"',;:},
+ html => qq{<a href="#">blah</a>}
+ };
+
+ my $array = [ 7, "string", "123", { "foo" => "bar" }, undef, $undef, "",
0, qq{"',;:}, qq{<a href="#">blah</a>} ];
+
+ if ( $r->method eq "GET" ) {
+ my $args = $r->get_args;
+
+ my $ret;
+ if ( $args->{output} eq "hash" ) {
+ $ret = $hash;
+ } elsif ( $args->{output} eq "array" ) {
+ $ret = $array;
+ }
+
+ if ( $args->{function} eq "js_dumper" ) {
+ $r->print( LJ::js_dumper( $ret ) );
+ } elsif ( $args->{function} eq "json" ) {
+ $r->print( JSON::objToJson( $ret ) );
+ }
+
+ return $r->OK;
+ }
+ # FIXME: handle post as well
+}
1;
+
diff -r f7b821ec9d41 -r 94f593a5f7a6 views/dev/tests/json.js
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/views/dev/tests/json.js Tue Mar 01 17:42:03 2011 +0800
@@ -0,0 +1,137 @@
+/* INCLUDE:
+jquery: js/json2.js
+*/
+/* defined as
+ my $hash = {
+ string => "string",
+ num => 42,
+ array => [ "a", "b", 2 ],
+ hash => { a => "apple", b => "bazooka" },
+ nil => undef,
+ nilvar => $undef,
+ blank => "",
+ zero => 0,
+ symbols => qq{"',;:},
+ html => qq{<a href="#">blah</a>}
+ };
+
+ my $array = [ 7, "string", "123", { "foo" => "bar" }, undef, $undef, "",
0, qq{"',;:}, qq{<a href="#">blah</a>} ];
+*/
+
+var expected_results = {
+ setup: function() {
+ this.js_dumper = {
+ array: [ 7, "string", 123, { foo: "bar" }, "", "", "", 0,
"\"',;:", "<a href=\"#\">blah</a>" ],
+ hash: {
+ string: "string",
+ num : 42,
+ array : [ "a", "b", 2 ],
+ hash : { a: "apple", b: "bazooka" },
+ nil : "",
+ nilvar: "",
+ blank : "",
+ zero : 0,
+ symbols: "\"',;:",
+ html : "<a href=\"#\">blah</a>",
+ }
+ };
+
+ this.json = {
+ array: [ 7, "string", 123, { foo: "bar" }, null, null, "", 0,
"\"',;:", "<a href=\"#\">blah</a>" ],
+ hash: {
+ string: "string",
+ num : 42,
+ array : [ "a", "b", 2 ],
+ hash : { a: "apple", b: "bazooka" },
+ nil : null,
+ nilvar: null,
+ blank : "",
+ zero : 0,
+ symbols: "\"',;:",
+ html : "<a href=\"#\">blah</a>"
+ }
+ };
+ }
+};
+
+module( "old", expected_results );
+function old_getjson(url, expected) {
+ HTTPReq.getJSON({
+ url: url,
+ method: "GET",
+ onData: function (data) {
+ start();
+ deepEqual( data, expected );
+ },
+ onError: function (msg) {
+ start();
+ ok( false, "shouldn't error" );
+ }
+ });
+}
+
+asyncTest( "js_dumper - array", 1, function() {
+ old_getjson( "/dev/testhelper/jsondump?function=js_dumper&output=array",
this.js_dumper.array );
+});
+
+
+asyncTest( "js_dumper - hash", 1, function() {
+ old_getjson( "/dev/testhelper/jsondump?function=js_dumper&output=hash",
this.js_dumper.hash );
+});
+
+asyncTest( "json module - array", 1, function() {
+ old_getjson( "/dev/testhelper/jsondump?function=json&output=array",
this.json.array );
+});
+
+asyncTest( "json module - hash", 1, function() {
+ old_getjson( "/dev/testhelper/jsondump?function=json&output=hash",
this.json.hash );
+});
+
+
+module( "jquery", expected_results );
+function jquery_getjson_ok(url, expected) {
+ $.ajax({
+ url: url,
+ dataType: "json",
+ success: function(data) {
+ start();
+ deepEqual( data, expected );
+ },
+ error: function(jqxhr, status, error) {
+ start();
+ ok( false, "error getting " + url + ": " + error );
+ }
+ });
+}
+
+function jquery_getjson_fail( url ) {
+ $.ajax({
+ url: url,
+ dataType: "json",
+ success: function(data) {
+ start();
+ ok( false, "unexpected success. js dumper output not strict JSON,
doesn't actually work with jquery" );
+ },
+ error: function(jqxhr, status, error) {
+ start();
+ ok( error.indexOf("Invalid JSON") == 0, "expected fail. js_dumper
output not strict JSON, doesn't actually work with jquery" );
+ }
+ });
+}
+
+asyncTest( "js_dumper - array", 1, function() {
+
jquery_getjson_fail("/dev/testhelper/jsondump?function=js_dumper&output=array");
+});
+
+asyncTest( "js_dumper - hash", 1, function() {
+
jquery_getjson_fail("/dev/testhelper/jsondump?function=js_dumper&output=hash");
+});
+
+asyncTest( "json module - array", 1, function() {
+ jquery_getjson_ok( "/dev/testhelper/jsondump?function=json&output=array",
this.json.array );
+});
+
+asyncTest( "json module - hash", 1, function() {
+ jquery_getjson_ok( "/dev/testhelper/jsondump?function=json&output=hash",
this.json.hash );
+});
+
_______________________________________________
dw-changelog mailing list
[email protected]
http://lists.dwscoalition.org/mailman/listinfo/dw-changelog