Class: PhusionPassenger::Utils::GeneratorTest

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/phusion_passenger/utils/json.rb

Instance Method Summary collapse

Instance Method Details

#generate(obj) ⇒ Object



242
# File 'lib/phusion_passenger/utils/json.rb', line 242

def generate(obj) JSON.generate(obj) end

#test_arrayObject



243
244
245
# File 'lib/phusion_passenger/utils/json.rb', line 243

def test_array
  assert_equal %([1, 2, 3]), generate([1, 2, 3])
end

#test_boolObject



246
247
248
# File 'lib/phusion_passenger/utils/json.rb', line 246

def test_bool
  assert_equal %([true, false]), generate([true, false])
end

#test_dateObject



262
263
264
265
# File 'lib/phusion_passenger/utils/json.rb', line 262

def test_date
  time = Date.new(2012, 04, 19)
  assert_equal %(["2012-04-19"]), generate([time])
end

#test_hashObject



269
270
271
272
273
274
# File 'lib/phusion_passenger/utils/json.rb', line 269

def test_hash
  json = generate(:abc => 123, 123 => 'abc')
  assert_match /^\{/, json
  assert_match /\}$/, json
  assert_equal [%("123": "abc"), %("abc": 123)], json[1...-1].split(', ').sort
end

#test_invalid_jsonObject



280
281
282
# File 'lib/phusion_passenger/utils/json.rb', line 280

def test_invalid_json
  assert_raises(ArgumentError) { generate("abc") }
end

#test_invalid_objectObject



283
284
285
286
# File 'lib/phusion_passenger/utils/json.rb', line 283

def test_invalid_object
  err = assert_raises(ArgumentError) { generate("a" => Object.new) }
  assert_equal "can't serialize Object", err.message
end

#test_nested_structureObject



275
276
277
278
279
# File 'lib/phusion_passenger/utils/json.rb', line 275

def test_nested_structure
  json = generate(:hash => {1=>2}, :array => [1,2])
  assert json.include?(%("hash": {"1": 2}))
  assert json.include?(%("array": [1, 2]))
end

#test_nullObject



249
250
251
# File 'lib/phusion_passenger/utils/json.rb', line 249

def test_null
  assert_equal %([null]), generate([nil])
end

#test_stringObject



252
253
254
# File 'lib/phusion_passenger/utils/json.rb', line 252

def test_string
  assert_equal %(["abc\\n123"]), generate(["abc\n123"])
end

#test_string_unicodeObject



255
256
257
# File 'lib/phusion_passenger/utils/json.rb', line 255

def test_string_unicode
  assert_equal %(["ć\\"č\\nž\\tš\\\\đ"]), generate(["ć\"č\nž\tš\\đ"])
end

#test_symbolObject



266
267
268
# File 'lib/phusion_passenger/utils/json.rb', line 266

def test_symbol
  assert_equal %(["abc"]), generate([:abc])
end

#test_timeObject



258
259
260
261
# File 'lib/phusion_passenger/utils/json.rb', line 258

def test_time
  time = Time.utc(2012, 04, 19, 1, 2, 3)
  assert_equal %(["2012-04-19 01:02:03 UTC"]), generate([time])
end