test_lorca.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # -*- coding: utf-8 -*-
  2. # -*- frozen_string_literal: true -*-
  3. require_relative "./../_config/minitest"
  4. require "lorca"
  5. module FakeExpansion
  6. module LorcaPlugin
  7. def fake
  8. end
  9. end
  10. module LorcaExtension
  11. def warn msg="extension deprecation"
  12. super msg
  13. end
  14. end
  15. end
  16. module NoPluginExpansion
  17. LorcaExtension = Module.new
  18. end
  19. module NoExtensionExpansion
  20. LorcaPlugin = Module.new
  21. end
  22. module LoadsDependenciesExpansion
  23. def self.load_dependencies mod, **_
  24. mod.send(:include, Module.new {
  25. def whatever
  26. true
  27. end
  28. }
  29. )
  30. end
  31. end
  32. module ConfigurableExpansion
  33. def self.configure mod, **stns#, &block
  34. mod.settings[:something] = stns[:something]
  35. end
  36. end
  37. class TestLorcaCoreExtension < Minitest::Test
  38. parallelize_me!
  39. def test_lorca_can_add_expansions
  40. Lorca.add FakeExpansion
  41. lorca = Lorca.new
  42. assert_respond_to lorca, :fake
  43. end
  44. def test_extensions_can_override_warn_for_custom_warnings
  45. Lorca.add FakeExpansion
  46. _, warning = capture_io { Lorca.warn }
  47. assert_match %r/extension deprecation/, warning
  48. end
  49. def test_add_expansion_with_no_plugin
  50. assert Lorca.add(NoPluginExpansion)
  51. end
  52. def test_add_expansion_with_no_extension
  53. assert Lorca.add(NoExtensionExpansion)
  54. end
  55. def test_add_expansions_in_succession
  56. assert Lorca.add(Module.new).add(Module.new)
  57. end
  58. def test_expansion_can_load_dependencies_when_needed
  59. Lorca.add LoadsDependenciesExpansion
  60. assert Lorca.new.whatever
  61. end
  62. def test_can_configure_expansion_when_added
  63. Lorca.add ConfigurableExpansion, something: "configured"
  64. assert_equal "configured", Lorca.settings[:something]
  65. end
  66. end
  67. class TestLorcaCorePlugin < Minitest::Test
  68. parallelize_me!
  69. def setup
  70. @lorca = Lorca.new
  71. end
  72. def test_has_immutable_settings
  73. assert_frozen @lorca.settings
  74. end
  75. def test_rolls_dice
  76. assert_includes 1..6, @lorca.roll_dice
  77. end
  78. def test_rolls_a_word_id_given_n_dice
  79. n_dice = 5
  80. assert_match %r/[1-6]{#{n_dice}}/, @lorca.roll_word_id(dice: n_dice)
  81. end
  82. def test_fails_to_roll_word_id_given_invalid_number_of_dice
  83. [0, -1, nil, Object.new].map do |invalid_dice|
  84. e = assert_raises(Lorca::LorcaDiceError) {
  85. @lorca.roll_word_id dice: invalid_dice
  86. }
  87. assert_match %r"should be a positive integer"i, e.message
  88. end
  89. end
  90. def test_validates_counter_is_within_range
  91. num = 8
  92. assert_equal num, @lorca.validate_counter(num, range: 1..10)
  93. end
  94. def test_fails_to_validate_counter_out_of_range
  95. err_min = assert_raises(Lorca::LorcaCounterError) do
  96. @lorca.validate_counter 3, range: 4..7
  97. end
  98. err_max = assert_raises(Lorca::LorcaCounterError) do
  99. @lorca.validate_counter 8, range: 4..7
  100. end
  101. message = %r"\Acounter out of range\z"i
  102. assert_match message, err_min.message
  103. assert_match message, err_max.message
  104. end
  105. def test_validates_a_range
  106. range = 3..6
  107. assert_equal range, @lorca.validate_range(range)
  108. end
  109. def test_fails_to_validate_an_invalid_range
  110. [3..2, 4..0, -1..4, "a".."z"].map do |invalid_range|
  111. e = assert_raises(Lorca::LorcaRangeError) {
  112. @lorca.validate_range invalid_range
  113. }
  114. assert_match %r"\Ashould be an end-inclusive range of positive integers\z"i,
  115. e.message
  116. end
  117. end
  118. def test_validates_positive_integer
  119. num = 8
  120. assert_equal num, @lorca.validate_positive_integer(num)
  121. end
  122. def test_fails_to_validate_positive_integer_given_invalid_input
  123. [-1, 0, 2.1, Object.new, nil].map do |invalid_counter|
  124. e = assert_raises(Lorca::LorcaCounterError) {
  125. @lorca.validate_positive_integer invalid_counter
  126. }
  127. assert_match %r"\Ashould be a positive integer\z"i, e.message
  128. end
  129. end
  130. end