Guest User

Untitled

a guest
Oct 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. # ===========================================================
  2. # TESTING LIBRARY
  3. class Proc
  4. def t; call rescue nil; end
  5. def +@; puts t ? 'OK' : f; end
  6. def -@; puts t ? f : 'OK'; end
  7. def f; "FAIL @ #{source_location.join(':')}"; end
  8. end
  9.  
  10.  
  11. # ===========================================================
  12. # "DIE" - Simple die / dice simulator
  13. class Die
  14. def initialize(sides = 6); raise ArgumentError if sides < 2; @sides = sides; end
  15. def roll; rand(@sides) + 1; end
  16. end
  17.  
  18. # ===========================================================
  19. # EXAMPLE TEST "SUITE" FOR "DIE"
  20. #
  21. # USAGE
  22. # +-> { block that should fail }
  23. # --> { block that should succeed }
  24.  
  25. +-> { Die.new(2) }
  26. --> { Die.new(1) }
  27. +-> { (1..6) === Die.new(6).roll }
  28.  
  29. +-> { die = Die.new(6)
  30. 1000.times { raise unless (1..6) === die.roll } }
  31.  
  32. +-> { die = Die.new(6)
  33. 1000.times { raise if die.roll.zero? } }
  34.  
  35. # This test will deliberately fail
  36. +-> { raise }
Add Comment
Please, Sign In to add comment