Thursday, September 19, 2024 11:52:38 PM
> settings

Customize


Authenticate

> json_spec.rb
# frozen_string_literal: true

describe ESM::JSON do
  describe ".parse" do
    let!(:data) do
      {
        int: 1,
        float: 2.5,
        bool: true,
        array: [-1, 83_383.52_332, false, [], {}],
        hash: {foo: "bar"}
      }
    end

    it "parses the json as a Hash" do
      expect(described_class.parse(data.to_json)).to eq(data)
    end

    it "parses the JSON as an OpenStruct" do
      expectation = OpenStruct.new(
        int: 1,
        float: 2.5,
        bool: true,
        array: [-1, 83_383.52_332, false, [], OpenStruct.new],
        hash: OpenStruct.new(foo: "bar")
      )

      expect(described_class.parse(data.to_json, as_ostruct: true)).to eq(expectation)
    end

    it "fails to parse and returns nil" do
      expect(described_class.parse("noop")).to be_nil
      expect(described_class.parse("{\"foo_bar':")).to be_nil
    end
  end
end
All opinions represented herein are my own
- © 2024 itsthedevman
- build 3c15a1b