Sha256: dad1c53c8f04ea2a16932f6c7ed0f1a20bb19c7926c715ac1f4f5c1ac42cfd13

Contents?: true

Size: 1.39 KB

Versions: 6

Compression:

Stored size: 1.39 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))
                                 ])
    }

    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 "raises error" do
      expect { type.coerce(3.14) }.to raise_error(StrongJSON::Type::Error)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
strong_json-0.7.0 spec/enum_spec.rb
strong_json-0.6.0 spec/enum_spec.rb
strong_json-0.5.0 spec/enum_spec.rb
strong_json-0.4.0 spec/enum_spec.rb
strong_json-0.3.0 spec/enum_spec.rb
strong_json-0.2.0 spec/enum_spec.rb