Class: PhusionPassenger::Utils::ParserTest

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

Constant Summary collapse

PARSED =
JSON.parse DATA.read

Instance Method Summary collapse

Instance Method Details

#parse_string(str) ⇒ Object



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

def parse_string(str) JSON.parse(%(["#{str}"]).gsub('\\\\', '\\')).first end

#parsedObject



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

def parsed() PARSED end

#test_arrayObject



227
228
229
# File 'lib/phusion_passenger/utils/json.rb', line 227

def test_array
  assert_equal ["4438f", {"a" => "b"}], parsed['head']['sha']
end

#test_boolObject



220
221
222
223
# File 'lib/phusion_passenger/utils/json.rb', line 220

def test_bool
  assert_equal true, parsed['head']['repository']['fork']
  assert_equal false, parsed['head']['repository']['private']
end

#test_hashObject



214
215
216
# File 'lib/phusion_passenger/utils/json.rb', line 214

def test_hash
  assert_equal %w[label ref repository sha user], parsed['head'].keys.sort
end

#test_invalidObject



230
231
232
233
234
235
236
237
238
# File 'lib/phusion_passenger/utils/json.rb', line 230

def test_invalid
  assert_raises(RuntimeError) { JSON.parse %({) }
  assert_raises(RuntimeError) { JSON.parse %({ "foo": }) }
  assert_raises(RuntimeError) { JSON.parse %([ "foo": "bar" ]) }
  assert_raises(RuntimeError) { JSON.parse %([ ~"foo" ]) }
  assert_raises(RuntimeError) { JSON.parse %([ "foo ]) }
  assert_raises(RuntimeError) { JSON.parse %([ "foo\\" ]) }
  assert_raises(RuntimeError) { JSON.parse %([ "foo\\uabGd" ]) }
end

#test_nilObject



224
225
226
# File 'lib/phusion_passenger/utils/json.rb', line 224

def test_nil
  assert_nil parsed['head']['user']['company']
end

#test_numberObject



217
218
219
# File 'lib/phusion_passenger/utils/json.rb', line 217

def test_number
  assert_equal 124.3e2, parsed['head']['repository']['size']
end

#test_stringObject



202
203
204
205
# File 'lib/phusion_passenger/utils/json.rb', line 202

def test_string
  assert_equal "Pagination library for \"Rails 3\", Sinatra, Merb, DataMapper, and more",
    parsed['head']['repository']['description']
end

#test_string_specialsObject



206
207
208
209
210
211
212
213
# File 'lib/phusion_passenger/utils/json.rb', line 206

def test_string_specials
  assert_equal "\r\n\t\f\b", parse_string('\r\n\t\f\b')
  assert_equal "aA", parse_string('\u0061\u0041')
  assert_equal "\e", parse_string('\u001B')
  assert_equal "xyz", parse_string('\x\y\z')
  assert_equal '"\\/', parse_string('\"\\\\\\/')
  assert_equal 'no #{interpolation}', parse_string('no #{interpolation}')
end