@Test public void buyItemNotWorthAnything() throws NamingException { ring.getItemDefinition().setCopper(0); Shopkeeper karcas = new Shopkeeper(); karcas.setName("Karcas"); karcas.setRoom(room1); HashSet<Person> persons = new HashSet<>(); persons.add(karn); persons.add(karcas); setField(Room.class, "persons", room1, persons); HashSet<Item> items = new HashSet<>(); items.add(ring); setField(Person.class, "items", karcas, items); BuyCommand buyCommand = new BuyCommand("buy( (\\w|-)+){1,4} from (\\w)+"); buyCommand.setCallback(commandRunner); assertThat(buyCommand.getRegExpr(), equalTo("buy( (\\w|-)+){1,4} from (\\w)+")); new Expectations() // an "expectation block" { { commandRunner.getItemBean(); result = itemBean; } }; DisplayInterface display = buyCommand.run("buy ring from karcas", karn); assertThat(display, not(nullValue())); assertThat(display.getBody(), equalTo("You are in a small room.")); String log = karn.getLog(0); assertThat( log, equalTo( "Karcas says [to you] : That item is not worth anything.<br />\nYou did not buy anything.<br />\n")); }
@Test public void buyOk() throws NamingException { Shopkeeper karcas = new Shopkeeper(); karcas.setName("Karcas"); karcas.setRoom(room1); HashSet<Person> persons = new HashSet<>(); persons.add(karn); persons.add(karcas); setField(Room.class, "persons", room1, persons); setField(Person.class, "copper", karn, 1111); HashSet<Item> items = new HashSet<>(); items.add(ring); setField(Person.class, "items", karcas, items); BuyCommand buyCommand = new BuyCommand("buy( (\\w|-)+){1,4} from (\\w)+"); buyCommand.setCallback(commandRunner); assertThat(buyCommand.getRegExpr(), equalTo("buy( (\\w|-)+){1,4} from (\\w)+")); new Expectations() // an "expectation block" { { commandRunner.getItemBean(); result = itemBean; } }; DisplayInterface display = buyCommand.run("buy ring from karcas", karn); assertThat(display, not(nullValue())); assertThat(display.getBody(), equalTo("You are in a small room.")); String log = karn.getLog(0); assertThat( log, equalTo("You buy a nice, golden, friendship ring from Karcas for 5 copper coins.<br />\n")); }
@Test public void buyToUnknownShopkeeper() { HashSet<Item> items = new HashSet<>(); items.add(ring); BuyCommand buyCommand = new BuyCommand("buy( (\\w|-)+){1,4} from (\\w)+"); buyCommand.setCallback(commandRunner); assertThat(buyCommand.getRegExpr(), equalTo("buy( (\\w|-)+){1,4} from (\\w)+")); DisplayInterface display = buyCommand.run("buy ring from karcas", karn); assertThat(display, not(nullValue())); assertThat(display.getBody(), equalTo("You are in a small room.")); String log = karn.getLog(0); assertThat(log, equalTo("Unable to locate shopkeeper.<br />\n")); }
@Test public void buyIllegalAmount() { User karcas = new User(); karcas.setName("Karcas"); karcas.setRoom(room1); HashSet<Item> items = new HashSet<>(); items.add(ring); setField(Person.class, "items", karcas, items); BuyCommand buyCommand = new BuyCommand("buy( (\\w|-)+){1,4} from (\\w)+"); buyCommand.setCallback(commandRunner); assertThat(buyCommand.getRegExpr(), equalTo("buy( (\\w|-)+){1,4} from (\\w)+")); DisplayInterface display = buyCommand.run("buy -2 ring from karcas", karn); assertThat(display, not(nullValue())); assertThat(display.getBody(), equalTo("You are in a small room.")); String log = karn.getLog(0); assertThat(log, equalTo("That is an illegal amount.<br />\n")); }
@Test public void buyItemShopkeeperDoesNotHave() { Shopkeeper karcas = new Shopkeeper(); karcas.setName("Karcas"); karcas.setRoom(room1); HashSet<Person> persons = new HashSet<>(); persons.add(karn); persons.add(karcas); setField(Room.class, "persons", room1, persons); BuyCommand buyCommand = new BuyCommand("buy( (\\w|-)+){1,4} from (\\w)+"); buyCommand.setCallback(commandRunner); assertThat(buyCommand.getRegExpr(), equalTo("buy( (\\w|-)+){1,4} from (\\w)+")); DisplayInterface display = buyCommand.run("buy brush from karcas", karn); assertThat(display, not(nullValue())); assertThat(display.getBody(), equalTo("You are in a small room.")); String log = karn.getLog(0); assertThat(log, equalTo("The shopkeeper doesn't have that.<br />\n")); }