Created
January 12, 2013 17:03
-
-
Save prystupa/4519294 to your computer and use it in GitHub Desktop.
OrderBookSteps with support for tracking rejected orders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrderBookSteps extends ShouldMatchers { | |
val orderTypes = OrderType.all(buyBook, sellBook) | |
val buyBook: OrderBook = new OrderBook(Buy, orderTypes) | |
val sellBook: OrderBook = new OrderBook(Sell, orderTypes) | |
var actualRejected = Vector.empty[Order] | |
events { | |
case RejectedOrder(order) => actualRejected = actualRejected :+ order | |
} | |
// the rest of the [unmodified] code is omitted | |
// ... | |
@Then("^the following \"([^\"]*)\" orders are rejected:$") | |
def the_following_orders_are_rejected(sideString: String, orderTable: DataTable) { | |
val (side, _) = getBook(sideString) | |
val expected = parseOrders(orderTable, side) | |
actualRejected should equal(expected) | |
actualRejected = Vector.empty | |
} | |
private def parseOrders(orderTable: DataTable, side: Side): Vector[Order] = { | |
orderTable.asList[OrderRow](classOf[OrderRow]).toVector.map( | |
r => r.price match { | |
case "MO" => MarketOrder(r.broker, side, r.qty) | |
case "Peg" => PegOrder(r.broker, side, r.qty) | |
case _ => LimitOrder(r.broker, side, r.qty, r.price.toDouble) | |
}) | |
} | |
private def events(handler: PartialFunction[OrderBookEvent, Unit]) { | |
List(buyBook, sellBook).foreach(book => book.subscribe(new book.Sub { | |
def notify(pub: book.Pub, event: OrderBookEvent) { | |
handler(event) | |
} | |
})) | |
} | |
private case class OrderRow(broker: String, qty: Double, price: String) | |
private case class BookRow(broker: String, qty: Double, price: String) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment