Increase branch test coverage

This commit is contained in:
Tim Kächele 2023-10-28 23:22:14 +02:00
parent 7191df2a90
commit 55f8652c64
3 changed files with 32 additions and 2 deletions

View File

@ -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

View File

@ -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)

View File

@ -2,7 +2,9 @@
require "simplecov"
SimpleCov.start
SimpleCov.start do
enable_coverage :branch
end
require "rex"
Dir[