diff options
author | Florian Frank <flori@ping.de> | 2010-02-27 21:03:20 +0100 |
---|---|---|
committer | Florian Frank <flori@ping.de> | 2010-02-27 21:16:56 +0100 |
commit | 92b013ce8e0a5c3caefd15f450409ed69aa539f2 (patch) | |
tree | 0ee7a7f4b19c456c0d62fc91e0447c59775cd1b3 | |
parent | 07d7b7141eb6b11414b16521d67bdf9f316caf19 (diff) | |
download | json-92b013ce8e0a5c3caefd15f450409ed69aa539f2.tar.gz |
Improved some tests
First stab at Rubinius compatibility
-rw-r--r-- | CHANGES | 6 | ||||
-rw-r--r-- | ext/json/ext/extconf_generator.rb | 1 | ||||
-rw-r--r-- | ext/json/ext/extconf_parser.rb | 1 | ||||
-rw-r--r-- | ext/json/ext/generator.c | 5 | ||||
-rw-r--r-- | ext/json/ext/generator.h | 12 | ||||
-rw-r--r-- | ext/json/ext/parser.c | 13 | ||||
-rw-r--r-- | ext/json/ext/parser.h | 12 | ||||
-rw-r--r-- | ext/json/ext/parser.rl | 13 | ||||
-rwxr-xr-x | tests/test_json_addition.rb | 10 | ||||
-rwxr-xr-x | tests/test_json_rails.rb | 10 |
10 files changed, 33 insertions, 50 deletions
@@ -1,3 +1,9 @@ +2010-02-27 (1.2.2) + * Made some changes to make the building of the parser/generator compatible + to Rubinius. +2009-11-25 (1.2.1) + * Added :symbolize_names option to Parser, which returns symbols instead of + strings in object names/keys. 2009-10-01 (1.2.0) * fast_generate now raises an exeception for nan and infinite floats. * On Ruby 1.8 json supports parsing of UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, diff --git a/ext/json/ext/extconf_generator.rb b/ext/json/ext/extconf_generator.rb index be80319..a9e8562 100644 --- a/ext/json/ext/extconf_generator.rb +++ b/ext/json/ext/extconf_generator.rb @@ -14,7 +14,6 @@ if RUBY_VERSION >= '1.9' $CFLAGS << ' -DRUBY_19' end -have_header("ruby/st.h") || have_header("st.h") have_header("ruby/re.h") || have_header("re.h") have_header("ruby/encoding.h") create_makefile 'generator' diff --git a/ext/json/ext/extconf_parser.rb b/ext/json/ext/extconf_parser.rb index 7693cfc..4da1661 100644 --- a/ext/json/ext/extconf_parser.rb +++ b/ext/json/ext/extconf_parser.rb @@ -14,6 +14,5 @@ if RUBY_VERSION >= '1.9' $CFLAGS << ' -DRUBY_19' end -have_header("ruby/st.h") || have_header("st.h") have_header("re.h") create_makefile 'parser' diff --git a/ext/json/ext/generator.c b/ext/json/ext/generator.c index 8beca18..817e426 100644 --- a/ext/json/ext/generator.c +++ b/ext/json/ext/generator.c @@ -12,7 +12,7 @@ static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject, static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before, i_object_nl, i_array_nl, i_max_nesting, i_allow_nan, i_ascii_only, - i_pack, i_unpack, i_create_id, i_extend; + i_pack, i_unpack, i_create_id, i_extend, i_key_p; /* * Copyright 2001-2004 Unicode, Inc. @@ -675,7 +675,7 @@ static VALUE cState_configure(VALUE self, VALUE opts) } tmp = ID2SYM(i_max_nesting); state->max_nesting = 19; - if (st_lookup(RHASH_TBL(opts), tmp, 0)) { + if (option_given_p(opts, tmp)) { VALUE max_nesting = rb_hash_aref(opts, tmp); if (RTEST(max_nesting)) { Check_Type(max_nesting, T_FIXNUM); @@ -1334,6 +1334,7 @@ void Init_generator() i_unpack = rb_intern("unpack"); i_create_id = rb_intern("create_id"); i_extend = rb_intern("extend"); + i_key_p = rb_intern("key?"); #ifdef HAVE_RUBY_ENCODING_H CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8")); i_encoding = rb_intern("encoding"); diff --git a/ext/json/ext/generator.h b/ext/json/ext/generator.h index f4fb34b..baa02a6 100644 --- a/ext/json/ext/generator.h +++ b/ext/json/ext/generator.h @@ -7,14 +7,6 @@ #include "ruby.h" -#if HAVE_RUBY_ST_H -#include "ruby/st.h" -#endif - -#if HAVE_ST_H -#include "st.h" -#endif - #if HAVE_RUBY_RE_H #include "ruby/re.h" #endif @@ -30,9 +22,7 @@ #define FORCE_UTF8(obj) #endif -#ifndef RHASH_TBL -#define RHASH_TBL(hsh) (RHASH(hsh)->tbl) -#endif +#define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key)) #ifndef RHASH_SIZE #define RHASH_SIZE(hsh) (RHASH(hsh)->tbl->num_entries) diff --git a/ext/json/ext/parser.c b/ext/json/ext/parser.c index 0122571..72c39cc 100644 --- a/ext/json/ext/parser.c +++ b/ext/json/ext/parser.c @@ -79,7 +79,7 @@ static VALUE mJSON, mExt, cParser, eParserError, eNestingError; static VALUE CNaN, CInfinity, CMinusInfinity; static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions, - i_chr, i_max_nesting, i_allow_nan, i_object_class, i_array_class; + i_chr, i_max_nesting, i_allow_nan, i_object_class, i_array_class, i_key_p; #line 108 "parser.rl" @@ -1611,7 +1611,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) rb_raise(rb_eArgError, "opts needs to be like a hash"); } else { VALUE tmp = ID2SYM(i_max_nesting); - if (st_lookup(RHASH_TBL(opts), tmp, 0)) { + if (option_given_p(opts, tmp)) { VALUE max_nesting = rb_hash_aref(opts, tmp); if (RTEST(max_nesting)) { Check_Type(max_nesting, T_FIXNUM); @@ -1623,14 +1623,14 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) json->max_nesting = 19; } tmp = ID2SYM(i_allow_nan); - if (st_lookup(RHASH_TBL(opts), tmp, 0)) { + if (option_given_p(opts, tmp)) { VALUE allow_nan = rb_hash_aref(opts, tmp); json->allow_nan = RTEST(allow_nan) ? 1 : 0; } else { json->allow_nan = 0; } tmp = ID2SYM(i_create_additions); - if (st_lookup(RHASH_TBL(opts), tmp, 0)) { + if (option_given_p(opts, tmp)) { VALUE create_additions = rb_hash_aref(opts, tmp); if (RTEST(create_additions)) { json->create_id = rb_funcall(mJSON, i_create_id, 0); @@ -1641,13 +1641,13 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) json->create_id = rb_funcall(mJSON, i_create_id, 0); } tmp = ID2SYM(i_object_class); - if (st_lookup(RHASH_TBL(opts), tmp, 0)) { + if (option_given_p(opts, tmp)) { json->object_class = rb_hash_aref(opts, tmp); } else { json->object_class = Qnil; } tmp = ID2SYM(i_array_class); - if (st_lookup(RHASH_TBL(opts), tmp, 0)) { + if (option_given_p(opts, tmp)) { json->array_class = rb_hash_aref(opts, tmp); } else { json->array_class = Qnil; @@ -1897,6 +1897,7 @@ void Init_parser() i_allow_nan = rb_intern("allow_nan"); i_object_class = rb_intern("object_class"); i_array_class = rb_intern("array_class"); + i_key_p = rb_intern("key?"); #ifdef HAVE_RUBY_ENCODING_H CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8")); CEncoding_UTF_16BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16be")); diff --git a/ext/json/ext/parser.h b/ext/json/ext/parser.h index 5f2506d..3982af2 100644 --- a/ext/json/ext/parser.h +++ b/ext/json/ext/parser.h @@ -7,14 +7,6 @@ #include "re.h" #endif -#if HAVE_RUBY_ST_H -#include "ruby/st.h" -#endif - -#if HAVE_ST_H -#include "st.h" -#endif - #ifdef HAVE_RUBY_ENCODING_H #include "ruby/encoding.h" #define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding()) @@ -22,9 +14,7 @@ #define FORCE_UTF8(obj) #endif -#ifndef RHASH_TBL -#define RHASH_TBL(hsh) (RHASH(hsh)->tbl) -#endif +#define option_given_p(opts, key) RTEST(rb_funcall(opts, i_key_p, 1, key)) /* unicode */ diff --git a/ext/json/ext/parser.rl b/ext/json/ext/parser.rl index b91ac00..f09c66d 100644 --- a/ext/json/ext/parser.rl +++ b/ext/json/ext/parser.rl @@ -77,7 +77,7 @@ static VALUE mJSON, mExt, cParser, eParserError, eNestingError; static VALUE CNaN, CInfinity, CMinusInfinity; static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions, - i_chr, i_max_nesting, i_allow_nan, i_object_class, i_array_class; + i_chr, i_max_nesting, i_allow_nan, i_object_class, i_array_class, i_key_p; %%{ machine JSON_common; @@ -609,7 +609,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) rb_raise(rb_eArgError, "opts needs to be like a hash"); } else { VALUE tmp = ID2SYM(i_max_nesting); - if (st_lookup(RHASH_TBL(opts), tmp, 0)) { + if (option_given_p(opts, tmp)) { VALUE max_nesting = rb_hash_aref(opts, tmp); if (RTEST(max_nesting)) { Check_Type(max_nesting, T_FIXNUM); @@ -621,14 +621,14 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) json->max_nesting = 19; } tmp = ID2SYM(i_allow_nan); - if (st_lookup(RHASH_TBL(opts), tmp, 0)) { + if (option_given_p(opts, tmp)) { VALUE allow_nan = rb_hash_aref(opts, tmp); json->allow_nan = RTEST(allow_nan) ? 1 : 0; } else { json->allow_nan = 0; } tmp = ID2SYM(i_create_additions); - if (st_lookup(RHASH_TBL(opts), tmp, 0)) { + if (option_given_p(opts, tmp)) { VALUE create_additions = rb_hash_aref(opts, tmp); if (RTEST(create_additions)) { json->create_id = rb_funcall(mJSON, i_create_id, 0); @@ -639,13 +639,13 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) json->create_id = rb_funcall(mJSON, i_create_id, 0); } tmp = ID2SYM(i_object_class); - if (st_lookup(RHASH_TBL(opts), tmp, 0)) { + if (option_given_p(opts, tmp)) { json->object_class = rb_hash_aref(opts, tmp); } else { json->object_class = Qnil; } tmp = ID2SYM(i_array_class); - if (st_lookup(RHASH_TBL(opts), tmp, 0)) { + if (option_given_p(opts, tmp)) { json->array_class = rb_hash_aref(opts, tmp); } else { json->array_class = Qnil; @@ -754,6 +754,7 @@ void Init_parser() i_allow_nan = rb_intern("allow_nan"); i_object_class = rb_intern("object_class"); i_array_class = rb_intern("array_class"); + i_key_p = rb_intern("key?"); #ifdef HAVE_RUBY_ENCODING_H CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8")); CEncoding_UTF_16BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16be")); diff --git a/tests/test_json_addition.rb b/tests/test_json_addition.rb index 51e4a67..ec89c07 100755 --- a/tests/test_json_addition.rb +++ b/tests/test_json_addition.rb @@ -95,7 +95,7 @@ class TC_JSONAddition < Test::Unit::TestCase c = C.new assert !C.json_creatable? json = generate(c) - assert_raises(ArgumentError) { JSON.parse(json) } + assert_raises(ArgumentError, NameError) { JSON.parse(json) } end def test_raw_strings @@ -110,11 +110,9 @@ class TC_JSONAddition < Test::Unit::TestCase json_raw_object = raw.to_json_raw_object hash = { 'json_class' => 'String', 'raw'=> raw_array } assert_equal hash, json_raw_object - json_raw = <<EOT.chomp -{\"json_class\":\"String\",\"raw\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255]} -EOT -# " - assert_equal json_raw, json + assert_match /\A\{.*\}\Z/, json + assert_match /"json_class":"String"/, json + assert_match /"raw":\[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255\]/, json raw_again = JSON.parse(json) assert_equal raw, raw_again end diff --git a/tests/test_json_rails.rb b/tests/test_json_rails.rb index d33402d..6fd201b 100755 --- a/tests/test_json_rails.rb +++ b/tests/test_json_rails.rb @@ -116,7 +116,7 @@ class TC_JSONRails < Test::Unit::TestCase c = C.new # with rails addition all objects are theoretically creatable assert C.json_creatable? json = generate(c) - assert_raises(ArgumentError) { JSON.parse(json) } + assert_raises(ArgumentError, NameError) { JSON.parse(json) } end def test_raw_strings @@ -131,11 +131,9 @@ class TC_JSONRails < Test::Unit::TestCase json_raw_object = raw.to_json_raw_object hash = { 'json_class' => 'String', 'raw'=> raw_array } assert_equal hash, json_raw_object - json_raw = <<EOT.chomp -{\"json_class\":\"String\",\"raw\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255]} -EOT -# " - assert_equal json_raw, json + assert_match /\A\{.*\}\Z/, json + assert_match /"json_class":"String"/, json + assert_match /"raw":\[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255\]/, json raw_again = JSON.parse(json) assert_equal raw, raw_again end |