From 55f8652c64110fdb1d2bbfe2e5f2b14081518604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20K=C3=A4chele?= Date: Sat, 28 Oct 2023 23:22:14 +0200 Subject: [PATCH] Increase branch test coverage --- lib/rex/matcher.rb | 2 +- spec/order_book_spec.rb | 28 ++++++++++++++++++++++++++++ spec/spec_helper.rb | 4 +++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/rex/matcher.rb b/lib/rex/matcher.rb index 1dc39ff..9fe34f2 100644 --- a/lib/rex/matcher.rb +++ b/lib/rex/matcher.rb @@ -6,7 +6,7 @@ module Rex lowest_sell_order = order_book.lowest_sell_order return trades if highest_buy_order.nil? || lowest_sell_order.nil? - while highest_buy_order&.price&.>= lowest_sell_order&.price + while highest_buy_order.price >= lowest_sell_order.price max_amount = [highest_buy_order.remaining_amount, lowest_sell_order.remaining_amount].min highest_buy_order.remaining_amount -= max_amount lowest_sell_order.remaining_amount -= max_amount diff --git a/spec/order_book_spec.rb b/spec/order_book_spec.rb index a8c4a47..36e976b 100644 --- a/spec/order_book_spec.rb +++ b/spec/order_book_spec.rb @@ -85,6 +85,14 @@ RSpec.describe Rex::OrderBook do end end + describe "#best_sell_price" do + context "when the order book is empty" do + it "returns nil" do + expect(instance.best_sell_price).to eq(nil) + end + end + end + describe "#lowest_sell_Order" do context "when there is nothing in the book" do it "returns nil " do @@ -101,6 +109,12 @@ RSpec.describe Rex::OrderBook do instance.add_order(buy_order) end + context "when the order id is unknown" do + it "returns nil" do + expect(instance.cancel_order(-1)).to eq(nil) + end + end + context "when it is the last order for the given price" do it "removes the order from the side" do instance.cancel_order(buy_order.id) @@ -109,6 +123,20 @@ RSpec.describe Rex::OrderBook do end end + context "when it is not the last order for the given price" do + let(:another_buy_order) { build(:order, is_buy: true, price: 100) } + + before do + instance.add_order(another_buy_order) + end + + it "removes the order from the side" do + instance.cancel_order(buy_order.id) + + expect(instance.best_buy_price).to eq(100) + end + end + context "an order on the sell side is removed" do before do instance.add_order(buy_order) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ccec118..02d6e3e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,7 +2,9 @@ require "simplecov" -SimpleCov.start +SimpleCov.start do + enable_coverage :branch +end require "rex" Dir[