# encoding: utf-8 require "json" describe ServiceObjects::Message do subject { described_class.new type: :foo, text: :bar } it "is comparable" do expect(subject).to be_kind_of ::Comparable end describe ".new" do it "requires the type" do expect { described_class.new text: :bar }.to raise_error ArgumentError end it "requires the text" do expect { described_class.new type: :foo }.to raise_error ArgumentError end it "freezes the object" do expect(subject).to be_frozen end end # .new describe "#type" do it "is stringified" do expect(subject.type).to eq "foo" end it "is frozen" do expect(subject.type).to be_frozen end end # #type describe "#text" do it "is stringified" do expect(subject.text).to eq "bar" end it "is frozen" do expect(subject.text).to be_frozen end end # #text describe "#priority" do it "is float" do expect(subject.priority).to be_kind_of Float end it "is customizable" do subject = described_class.new type: "foo", text: "bar", priority: 1.5 expect(subject.priority).to eq 1.5 end it "is set by default to 0.0 for non-errors" do subject = described_class.new type: "foo", text: "bar" expect(subject.priority).to eq 0.0 end it "is set by default to -1.0 for errors" do subject = described_class.new type: "error", text: "bar" expect(subject.priority).to eq(-1.0) end end # #priority describe "#inspect" do it "returns text, type and priority of the message" do expect(subject.inspect) .to eq %W( # ).join(" ") end end # #inspect describe "#==" do it "treats messages with the same type, text and priority as equal" do a = described_class.new type: "foo", text: "bar" b = described_class.new type: "foo", text: "bar" expect(a == b).to be_truthy end it "treats messages with different types as different" do a = described_class.new type: "bar", text: "foo" b = described_class.new type: "baz", text: "foo" expect(a == b).to be_falsey end it "treats messages with different texts as different" do a = described_class.new type: "foo", text: "bar" b = described_class.new type: "foo", text: "baz" expect(a == b).to be_falsey end it "treats messages with different priorities as different" do a = described_class.new type: "foo", text: "bar", priority: 1 b = described_class.new type: "foo", text: "bar", priority: 1.1 expect(a == b).to be_falsey end it "treats message as different from non-message" do a = described_class.new type: "foo", text: "bar" b = Struct.new(:type, :text).new("foo", "bar") expect(a == b).to be_falsey end end # #== describe "#<=>" do it "orders messages by priorities" do a = described_class.new type: "foo", text: "bar", priority: 0 b = described_class.new type: "foo", text: "bar", priority: 0.1 expect(a <=> b).to eq(-1) expect(b <=> a).to eq(1) end it "orders messages by type" do a = described_class.new type: "bar", text: "foo" b = described_class.new type: "baz", text: "foo" expect(a <=> b).to eq(-1) expect(b <=> a).to eq(1) end it "orders messages by text" do a = described_class.new type: "foo", text: "bar" b = described_class.new type: "foo", text: "baz" expect(a <=> b).to eq(-1) expect(b <=> a).to eq(1) end it "orders messages at first by priorities" do a = described_class.new type: "bar", text: "bar", priority: 0 b = described_class.new type: "baz", text: "baz", priority: 1 expect(a <=> b).to eq(-1) expect(b <=> a).to eq(1) end it "orders messages at second by types" do a = described_class.new type: "bar", text: "bar" b = described_class.new type: "baz", text: "baz" expect(a <=> b).to eq(-1) expect(b <=> a).to eq(1) end it "treats messages with the same type, text and priority as equal" do a = described_class.new type: "foo", text: "bar" b = described_class.new type: "foo", text: "bar" expect(a <=> b).to eq 0 expect(b <=> a).to eq 0 end it "returns nil if other value is not a message" do expect(subject <=> 1).to be_nil end end # #<=> describe "#to_h" do it "serializes type and text" do hash = { type: subject.type, text: subject.text } expect(subject.to_h).to eq hash end end # #to_h describe "#to_json" do it "serializes type and text" do hash = { type: subject.type, text: subject.text } expect(subject.to_json).to eq hash.to_json end end # #to_json end # ServiceObjects::Message