You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2010/10/27 01:43:00 UTC
[lucy-commits] svn commit: r1027773 - in /incubator/lucy/trunk:
core/KinoSearch/Test/Util/TestJson.c core/KinoSearch/Util/Json.cfh
perl/lib/KinoSearch.pm perl/xs/KinoSearch/Util/Json.c
Author: marvin
Date: Tue Oct 26 23:43:00 2010
New Revision: 1027773
URL: http://svn.apache.org/viewvc?rev=1027773&view=rev
Log:
Add tests for encoding IntNum and FloatNum types as JSON. Slight refactoring
of test-only internals for JSON.
Modified:
incubator/lucy/trunk/core/KinoSearch/Test/Util/TestJson.c
incubator/lucy/trunk/core/KinoSearch/Util/Json.cfh
incubator/lucy/trunk/perl/lib/KinoSearch.pm
incubator/lucy/trunk/perl/xs/KinoSearch/Util/Json.c
Modified: incubator/lucy/trunk/core/KinoSearch/Test/Util/TestJson.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/core/KinoSearch/Test/Util/TestJson.c?rev=1027773&r1=1027772&r2=1027773&view=diff
==============================================================================
--- incubator/lucy/trunk/core/KinoSearch/Test/Util/TestJson.c (original)
+++ incubator/lucy/trunk/core/KinoSearch/Test/Util/TestJson.c Tue Oct 26 23:43:00 2010
@@ -104,8 +104,8 @@ test_escapes(TestBatch *batch)
CB_Truncate(string, 0);
CB_Cat_Char(string, i);
char *escaped = control_escapes[i];
- CharBuf *json = Json_encode_string(string);
- CharBuf *decoded = Json_decode_string(json);
+ CharBuf *json = Json_to_json((Obj*)string);
+ CharBuf *decoded = (CharBuf*)Json_from_json(json);
CB_setf(json_wanted, "\"%s\"", escaped);
CB_Trim(json);
@@ -123,8 +123,8 @@ test_escapes(TestBatch *batch)
char *source = quote_escapes_source[i];
char *escaped = quote_escapes_json[i];
CB_setf(string, source, strlen(source));
- CharBuf *json = Json_encode_string(string);
- CharBuf *decoded = Json_decode_string(json);
+ CharBuf *json = Json_to_json((Obj*)string);
+ CharBuf *decoded = (CharBuf*)Json_from_json(json);
CB_setf(json_wanted, "\"%s\"", escaped);
CB_Trim(json);
@@ -143,6 +143,25 @@ test_escapes(TestBatch *batch)
}
static void
+test_numbers(TestBatch *batch)
+{
+ Integer64 *i64 = Int64_new(33);
+ CharBuf *json = Json_to_json((Obj*)i64);
+ CB_Trim(json);
+ TEST_TRUE(batch, json && CB_Equals_Str(json, "33", 2), "Integer");
+ DECREF(json);
+
+ Float64 *f64 = Float64_new(33.33);
+ json = Json_to_json((Obj*)f64);
+ CB_Trim(json);
+ TEST_TRUE(batch, json && CB_Equals_Str(json, "33.33", 5), "Float");
+ DECREF(json);
+
+ DECREF(i64);
+ DECREF(f64);
+}
+
+static void
test_to_and_from(TestBatch *batch)
{
Obj *dump = S_make_dump();
@@ -208,11 +227,15 @@ test_spew_and_slurp(TestBatch *batch)
void
TestJson_run_tests()
{
- TestBatch *batch = TestBatch_new(90);
+ TestBatch *batch = TestBatch_new(92);
+
+ // Liberalize for testing.
+ Json_set_tolerant(true);
TestBatch_Plan(batch);
test_to_and_from(batch);
test_escapes(batch);
+ test_numbers(batch);
test_spew_and_slurp(batch);
DECREF(batch);
Modified: incubator/lucy/trunk/core/KinoSearch/Util/Json.cfh
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/core/KinoSearch/Util/Json.cfh?rev=1027773&r1=1027772&r2=1027773&view=diff
==============================================================================
--- incubator/lucy/trunk/core/KinoSearch/Util/Json.cfh (original)
+++ incubator/lucy/trunk/core/KinoSearch/Util/Json.cfh Tue Oct 26 23:43:00 2010
@@ -48,15 +48,11 @@ class KinoSearch::Util::Json inherits Ki
inert incremented nullable Obj*
slurp_json(Folder *folder, const CharBuf *path);
- /** Testing only.
+ /** Allow the encoder to output strings, etc, instead of throwing an error
+ * on anything other than a hash or an array. Testing only.
*/
- inert incremented CharBuf*
- encode_string(CharBuf *string);
-
- /** Testing only.
- */
- inert incremented CharBuf*
- decode_string(CharBuf *json);
+ inert void
+ set_tolerant(bool_t tolerant);
}
Modified: incubator/lucy/trunk/perl/lib/KinoSearch.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/perl/lib/KinoSearch.pm?rev=1027773&r1=1027772&r2=1027773&view=diff
==============================================================================
--- incubator/lucy/trunk/perl/lib/KinoSearch.pm (original)
+++ incubator/lucy/trunk/perl/lib/KinoSearch.pm Tue Oct 26 23:43:00 2010
@@ -635,16 +635,7 @@ sub error {$KinoSearch::Object::Err::err
return to_kino( $json_encoder->decode( $_[1] ) );
}
- my $loose_encoder = JSON::XS->new->pretty(1)->canonical(1)->allow_nonref(1);
-
- sub encode_string {
- my ( undef, $string ) = @_;
- return $loose_encoder->encode($string);
- }
-
- sub decode_string {
- return to_kino( $loose_encoder->decode( $_[1] ) );
- }
+ sub set_tolerant { $json_encoder->allow_nonref( $_[1] ) }
}
{
Modified: incubator/lucy/trunk/perl/xs/KinoSearch/Util/Json.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/perl/xs/KinoSearch/Util/Json.c?rev=1027773&r1=1027772&r2=1027773&view=diff
==============================================================================
--- incubator/lucy/trunk/perl/xs/KinoSearch/Util/Json.c (original)
+++ incubator/lucy/trunk/perl/xs/KinoSearch/Util/Json.c Tue Oct 26 23:43:00 2010
@@ -53,17 +53,10 @@ Json_from_json(CharBuf *json)
ARG_STR("json", json));
}
-CharBuf*
-Json_encode_string(CharBuf *string)
+void
+Json_set_tolerant(bool_t tolerant)
{
- return Host_callback_str(JSON, "encode_string", 1,
- ARG_STR("string", string));
-}
-
-CharBuf*
-Json_decode_string(CharBuf *json)
-{
- return (CharBuf*)Host_callback_obj(JSON, "decode_string", 1,
- ARG_STR("json", json));
+ Host_callback(JSON, "set_tolerant", 1,
+ ARG_I32("tolerant", tolerant));
}