summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbinoam P. Marques Jr <abinoam@gmail.com>2023-01-01 21:57:34 -0300
committerAbinoam P. Marques Jr <abinoam@gmail.com>2023-01-02 18:04:23 -0300
commitce3481d8c2fb48e7ced5260ffdddbb30d024f11c (patch)
tree17de106dad9d2447ca4f3f8b1240fa1a5cdb63b4
parentb5543d973bbc638e83b9f835969b5fb7de96cd94 (diff)
downloadhighline-ce3481d8c2fb48e7ced5260ffdddbb30d024f11c.tar.gz
Add test for the new validation class feature
-rwxr-xr-xtest/test_highline.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/test/test_highline.rb b/test/test_highline.rb
index 13ff406..8de8551 100755
--- a/test/test_highline.rb
+++ b/test/test_highline.rb
@@ -1626,6 +1626,55 @@ class TestHighLine < Minitest::Test
assert_equal("Gray, James Edward", answer)
end
+ class ZeroToTwentyFourValidator
+ def self.valid?(answer)
+ (0..24).include? answer.to_i
+ end
+
+ def self.inspect
+ "(0..24) rule"
+ end
+ end
+
+ def test_validation_with_custom_validator_class
+ @input << "26\n25\n24\n"
+ @input.rewind
+
+ answer = @terminal.ask("What hour of the day is it?: ", Integer) do |q|
+ q.validate = ZeroToTwentyFourValidator
+ end
+
+ assert_equal(24, answer)
+ assert_equal("What hour of the day is it?: " \
+ "Your answer isn't valid (must match (0..24) rule).\n" \
+ "? Your answer isn't valid (must match (0..24) rule).\n" \
+ "? ", @output.string)
+ end
+
+ require 'dry/types'
+
+ module Types
+ include Dry.Types
+ end
+
+ def test_validation_with_dry_types
+ @input << "random string\nanother uncoercible string\n42\n"
+ @input.rewind
+
+ answer = @terminal.ask("Type a number: ", Integer) do |q|
+ q.validate = Types::Coercible::Integer
+ end
+
+ assert_equal(42, answer)
+ assert_match(Regexp.new(<<~REGEXP.chomp),
+ Type a number: Your answer isn't valid .must match .*Dry.*Types.*Integer.*..
+ \\\? Your answer isn't valid .must match .*Dry.*Types.*Integer.*..
+ \\\?
+ REGEXP
+ @output.string
+ )
+ end
+
def test_validation_with_overriding_static_message
@input << "Not valid answer\n" \
"42\n"