Spock Web Console

subscribe to the feed Subscribe
SpockTechSession (via #spockwebconsole)
tweet this script Tweet

SpockTechSession

Published 7 months ago by adimka with tags techsession
Actions  ➤ Edit In Console Back To Console Show/Hide Line Numbers View Recent Scripts
import spock.lang.*

class AdventureServiceSpec extends Specification {
  
  @Unroll
  def "adventure service produces #x valid adventures"(int x) {
    //slightly more complicated example testing AdventureService
    setup:
    def advServ = new AdventureService()
    when: "we create an adventure with items"
    def a = advServ.create(x)
    then: "treasures with names and values get created"
    a.loot.size() == x
    a.loot.each{
      assert Treasure.names.contains(it.name)
      assert it.value >= 0
    }
    where:
    x << [1, 10, 50, 1000]
  }

}

// Hit 'Run Script' below
class AdventurerSpec extends Specification {

  // Example of a basic feature method
  def "new adventurer has expected properties"() {
    when: "spawning a new adventurer"
    def a = new Adventurer(100)

    then: "adventurer has default values for stats"
    a.maxHP == 35
    a.currHP == 35 
    a.gold == 0
  }

    /* TODO: Implement the feature methods below,
       fixing any code errors your tests find
    */

  def "going on an adventure nets treasure"() {
    //TODO: Implement this feature method  
  }

  def "selling treasure generates gold and prints items sold"(){
    //TODO: Implement this feature method
  }

  def "verify can't use potion at full health"(){
    //TODO: Implement this feature method
  }

  def "verify can't use nonexistant potion"(){
    //TODO: Implement this feature method
  }

  def "verify potion heals properly"(){
    //TODO: Implement this feature method
  }

}

class Adventurer {
  int gold
  int currHP
  int maxHP 
  List<Treasure> items

  public Adventurer( int percentHP ){
    gold = 0
    maxHP = 35
    items = []
    currHP = (percentHP*maxHP)/100
  }

  //Takes an adventure and puts its loot in our items
  def findTreasure(Adventure adv){
    items += adv.loot
    adv.loot = []
  }

  // Add each items' value to gold and remove all items
  def sellTreasure(){
    items.removeAll{
      println "Selling ${it.name} for ${it.value} gold."
      gold += it.value;
    }
  }

// Heal 10 (or to max) if we have a health potion
  def usePotion() {
    if (items.find{it.name == "Health Potion"}) {
      throw new Exception("No Health Potions!")
    }
    else if (currHP <= maxHP) {
      throw new Exception("Already Full HP.")
    }
    else {
      maxHP - currHP >= 10 ? currHP += 10 : (currHP = maxHP)
      items.removeElement{
        it.name == "Health Potion"
      }
    }
  }
}

class AdventureService {
  // Creates and returns an adventure with the specified number of treasures
  def rng = new Random()
  Adventure create(int numTreasures){
    def adv = new Adventure()
    adv.loot = []
    numTreasures.times{
      adv.loot << (new Treasure(name:Treasure.names.getAt(Math.abs(rng.nextInt()%11)), value:Math.abs(rng.nextInt()%500)))
    }
    adv
  }
}

class Adventure {
  List<Treasure> loot
}

class Treasure {
  static names = ["Gemstone", "Ancient Rune", "Magical Boots",
      "Rock", "Scroll of Eternal Life", "Health Potion",
      "Necronomicon", "Liquid of Questionable Origin", "Beer",
      "Solid Gold Wombat Statue", "Bag of Stale Jellybeans"] 
  String name
  int value
}