Permalink
Browse files

Add spec tests for #1313

  • Loading branch information...
1 parent 3809805 commit aa3c4906acaa62398ae8e8e1fa7d166feefd6b4c @ferrous26 ferrous26 committed Jun 10, 2011
Showing with 64 additions and 0 deletions.
  1. +64 −0 spec/macruby/library/json/objc_types_spec.rb
@@ -0,0 +1,64 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+module JSONSpecHelper
+ # run once with json and then again with json/pure
+ def serialize input, expected
+ ['json','json/pure'].each do |lib|
+ ret = ruby_exe("p (#{input}).to_json == '#{expected}'", options: "-r #{lib}")
+ ret.chomp.should == 'true'
+ end
+ end
+end
+
+
+describe 'JSON serialization using the C extension' do
+ extend JSONSpecHelper
+
+ it 'works with NSString and NSMutableString' do
+ serialize('NSString.stringWithString("test")','"test"')
+ serialize('NSMutableString.stringWithString("test")','"test"')
+ # TODO test a string with a different encoding
+ end
+
+ it 'works with NSDictionary and NSMutableDictionary' do
+ serialize('NSDictionary.dictionaryWithObject(2, forKey:1)','{"1":2}')
+ serialize('NSMutableDictionary.dictionaryWithObject(2, forKey:1)','{"1":2}')
+ end
+
+ it 'works with NSArray and NSMutableArray' do
+ serialize('NSArray.arrayWithArray(["test",1,[]])','["test",1,[]]')
+ serialize('NSMutableArray.arrayWithArray(["test",1,[]])','["test",1,[]]')
+ end
+
+ it 'works with NSNumber' do
+ serialize('NSNumber.numberWithDouble(3.14159265)','3.14159265')
+ serialize('NSNumber.numberWithInt(1234)','1234')
+ serialize('NSNumber.numberWithChar("a")','97')
+ end
+
+ it 'works with other Objective-C objects' do
+ serialize('NSDate.distantPast','"0000-12-28 19:00:00 -0500"')
+ end
+
+ # ticket #1313
+ it 'does not segfault when an Objective-C object is nested in a ruby Array' do
+ serialize('[NSString.stringWithString("")]','[""]')
+ serialize('[NSArray.arrayWithArray([])]','[[]]')
+ end
+
+ # basic tests to make sure objective-c adjustments didn't break regular
+ # ruby stuff, to be thorough you should run the MRI test suite
+ it 'still works with ruby objects' do
+ serialize('nil','null')
+ serialize('false','false')
+ serialize('true','true')
+ serialize('{a: 2}','{"a":2}')
+ serialize('["test",1,[]]','["test",1,[]]')
+ serialize('"hello"','"hello"')
+ serialize('42','42')
+ serialize('1000000000000000000000','1000000000000000000000')
+ serialize('3.14159265','3.14159265')
+ serialize('Class','"Class"')
+ end
+
+end

0 comments on commit aa3c490

Please sign in to comment.