Class: PhusionPassenger::Utils::GeneratorTest
- Inherits:
-
Test::Unit::TestCase
- Object
- Test::Unit::TestCase
- PhusionPassenger::Utils::GeneratorTest
- Defined in:
- lib/phusion_passenger/utils/json.rb
Instance Method Summary collapse
- #generate(obj) ⇒ Object
- #test_array ⇒ Object
- #test_bool ⇒ Object
- #test_date ⇒ Object
- #test_hash ⇒ Object
- #test_invalid_json ⇒ Object
- #test_invalid_object ⇒ Object
- #test_nested_structure ⇒ Object
- #test_null ⇒ Object
- #test_string ⇒ Object
- #test_string_unicode ⇒ Object
- #test_symbol ⇒ Object
- #test_time ⇒ Object
Instance Method Details
#generate(obj) ⇒ Object
242 |
# File 'lib/phusion_passenger/utils/json.rb', line 242 def generate(obj) JSON.generate(obj) end |
#test_array ⇒ Object
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_bool ⇒ Object
246 247 248 |
# File 'lib/phusion_passenger/utils/json.rb', line 246 def test_bool assert_equal %([true, false]), generate([true, false]) end |
#test_date ⇒ Object
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_hash ⇒ Object
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_json ⇒ Object
280 281 282 |
# File 'lib/phusion_passenger/utils/json.rb', line 280 def test_invalid_json assert_raises(ArgumentError) { generate("abc") } end |
#test_invalid_object ⇒ Object
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. end |
#test_nested_structure ⇒ Object
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_null ⇒ Object
249 250 251 |
# File 'lib/phusion_passenger/utils/json.rb', line 249 def test_null assert_equal %([null]), generate([nil]) end |
#test_string ⇒ Object
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_unicode ⇒ Object
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_symbol ⇒ Object
266 267 268 |
# File 'lib/phusion_passenger/utils/json.rb', line 266 def test_symbol assert_equal %(["abc"]), generate([:abc]) end |
#test_time ⇒ Object
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 |