Sha256: 671c3e83705e3045a9e722365ea1788ff214596fc9619baab4bb3b2a6a907de7

Contents?: true

Size: 1.45 KB

Versions: 7

Compression:

Stored size: 1.45 KB

Contents

require "strong_json"

describe StrongJSON::Type::Array, "#coerce" do
  it "returns empty" do
    type = StrongJSON::Type::Array.new(StrongJSON::Type::Base.new(:any))

    expect(type.coerce([])).to eq([])
  end

  it "test array of number" do
    type = StrongJSON::Type::Array.new(StrongJSON::Type::Base.new(:number))
    expect(type.coerce([1])).to eq([1])
  end

  it "test array of array of number" do
    a = StrongJSON::Type::Array.new(StrongJSON::Type::Base.new(:number))
    type = StrongJSON::Type::Array.new(a)
    expect(type.coerce([[1]])).to eq([[1]])
  end

  it "reject non array" do
    type = StrongJSON::Type::Array.new(StrongJSON::Type::Base.new(:number))

    expect { type.coerce({}) }.to raise_error(StrongJSON::Type::TypeError)
  end

  it "reject membership" do
    type = StrongJSON::Type::Array.new(StrongJSON::Type::Base.new(:number))

    expect { type.coerce(["a"]) }.to raise_error(StrongJSON::Type::TypeError)
  end

  it "rejects nil" do
    type = StrongJSON::Type::Array.new(StrongJSON::Type::Base.new(:number))

    expect { type.coerce(nil) }.to raise_error(StrongJSON::Type::TypeError)
  end

  describe "=~" do
    it "returns true for array" do
      type = StrongJSON::Type::Array.new(StrongJSON::Type::Base.new(:number))
      expect(type =~ []).to be_truthy
    end

    it "returns false for number" do
      type = StrongJSON::Type::Array.new(StrongJSON::Type::Base.new(:number))
      expect(type =~ 3.0).to be_falsey
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
strong_json-2.1.2 spec/array_spec.rb
strong_json-2.1.1 spec/array_spec.rb
strong_json-2.1.0 spec/array_spec.rb
strong_json-2.0.0 spec/array_spec.rb
strong_json-1.1.0 spec/array_spec.rb
strong_json-1.0.1 spec/array_spec.rb
strong_json-1.0.0 spec/array_spec.rb