Sha256: 38c67b8af0b3ffe554a3defda83360885a71621f3c815c74ce555c7430b1d0b4

Contents?: true

Size: 1.72 KB

Versions: 3

Compression:

Stored size: 1.72 KB

Contents

require "strong_json"

describe StrongJSON::Type::Enum do
  describe "=~" do
    let (:type) {
      StrongJSON::Type::Enum.new([StrongJSON::Type::Literal.new(3),
                                  StrongJSON::Type::Literal.new(4)])
    }

    it "returns true for 3" do
      expect(type =~ 3).to be_truthy
    end

    it "returns true for 4" do
      expect(type =~ 4).to be_truthy
    end

    it "returns false for 5" do
      expect(type =~ 5).to be_falsey
    end
  end

  describe "#coerce" do
    let (:type) {
      StrongJSON::Type::Enum.new([
                                   StrongJSON::Type::Object.new(id: StrongJSON::Type::Literal.new("id1"),
                                                                value: StrongJSON::Type::Base.new(:string)),
                                   StrongJSON::Type::Object.new(id: StrongJSON::Type::Base.new(:string),
                                                                value: StrongJSON::Type::Base.new(:symbol)),
                                   StrongJSON::Type::Optional.new(StrongJSON::Type::Literal.new(3)),
                                   StrongJSON::Type::Literal.new(false),
                                 ])
    }

    it "returns object with string value" do
      expect(type.coerce({id: "id1", value: "foo" })).to eq({ id: "id1", value: "foo" })
    end

    it "returns object with symbol value" do
      expect(type.coerce({id: "id2", value: "foo" })).to eq({ id: "id2", value: :foo })
    end

    it "accepts false" do
      expect(type.coerce(false)).to eq(false)
    end

    it "accepts nil" do
      expect(type.coerce(nil)).to eq(nil)
    end

    it "raises error" do
      expect { type.coerce(3.14) }.to raise_error(StrongJSON::Type::Error)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
strong_json-0.9.0 spec/enum_spec.rb
strong_json-0.8.0 spec/enum_spec.rb
strong_json-0.7.1 spec/enum_spec.rb