Sha256: 9587393cb85748a921d6888baeba7ec9f2815d82f86714cf3f34b056d0b23adf

Contents?: true

Size: 1.95 KB

Versions: 4

Compression:

Stored size: 1.95 KB

Contents

require File.dirname(__FILE__) + '/../../spec_helper'

describe "OEmbed::Formatter::JSON::Backends::JSONGem" do
  include OEmbedSpecHelper

  before(:all) do
    expect {
      OEmbed::Formatter::JSON.backend = 'JSONGem'
    }.to raise_error(LoadError)

    require 'json'

    expect {
      OEmbed::Formatter::JSON.backend = 'JSONGem'
    }.to_not raise_error
  end

  it "should support JSON" do
    expect {
      OEmbed::Formatter.supported?(:json)
    }.to_not raise_error
  end

  it "should be using the JSONGem backend" do
    expect(OEmbed::Formatter::JSON.backend).to eq(OEmbed::Formatter::JSON::Backends::JSONGem)
  end

  it "should decode a JSON String" do
    decoded = OEmbed::Formatter.decode(:json, valid_response(:json))
    # We need to compare keys & values separately because we don't expect all
    # non-string values to be recognized correctly.
    expect(decoded.keys).to eq(valid_response(:object).keys)
    expect(decoded.values.map{|v|v.to_s}).to eq(valid_response(:object).values.map{|v|v.to_s})
  end

  it "should raise an OEmbed::ParseError when decoding an invalid JSON String" do
    expect {
      decode = OEmbed::Formatter.decode(:json, invalid_response('unclosed_container', :json))
    }.to raise_error(OEmbed::ParseError)
    expect {
      decode = OEmbed::Formatter.decode(:json, invalid_response('unclosed_tag', :json))
    }.to raise_error(OEmbed::ParseError)
    expect {
      decode = OEmbed::Formatter.decode(:json, invalid_response('invalid_syntax', :json))
    }.to raise_error(OEmbed::ParseError)
  end

  it "should raise an OEmbed::ParseError when decoding fails with an unexpected error" do
    error_to_raise = ArgumentError
    expect(OEmbed::Formatter::JSON.backend.parse_error).to_not be_kind_of(error_to_raise)

    expect(::JSON).to receive(:parse).
      and_throw(error_to_raise.new("unknown error"))

    expect {
      decode = OEmbed::Formatter.decode(:json, valid_response(:json))
    }.to raise_error(OEmbed::ParseError)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby-oembed-0.9.0 spec/formatter/json/jsongem_backend_spec.rb
ruby-oembed-0.8.14 spec/formatter/json/jsongem_backend_spec.rb
ruby-oembed-0.8.13 spec/formatter/json/jsongem_backend_spec.rb
ruby-oembed-0.8.12 spec/formatter/json/jsongem_backend_spec.rb