From 33eb2a8c79da38f740615bd7abf4ee49d75ed775 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sat, 9 May 2026 15:37:02 +0200 Subject: [PATCH] Fix test passing blank input into required field --- test/system/units_test.rb | 11 ++++++---- test/test_helper.rb | 43 +++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/test/system/units_test.rb b/test/system/units_test.rb index f015a41..1b1ce23 100644 --- a/test/system/units_test.rb +++ b/test/system/units_test.rb @@ -44,10 +44,12 @@ class UnitsTest < ApplicationSystemTestCase within 'tbody > tr:has(input[type=text], textarea)' do assert_selector ':focus' - maxlength = all(:fillable_field).to_h { |f| [f[:name], f[:maxlength].to_i || 2**16] } + maxlength = all(:fillable_field).to_h do |field| + [field[:name], field[:maxlength].to_i || 2**16] + end values = { - symbol: random_string(rand([1..3, 4..maxlength['unit[symbol]']].sample), - except: units.map(&:symbol)), + symbol: random_string(deep_rand(1..3, 4..maxlength['unit[symbol]']), + except: units.map(&:symbol), allow_blank: false), description: random_string(rand(0..maxlength['unit[description]'])) }.with_indifferent_access within :field, 'unit[multiplier]' do |field| @@ -61,7 +63,8 @@ class UnitsTest < ApplicationSystemTestCase end end - assert_selector '.flash.notice', text: t('units.create.success', unit: Unit.last.symbol) + assert_selector '.flash.notice', + text: t('units.create.success', unit: Unit.last.symbol) within 'tbody' do assert_no_selector :fillable_field assert_selector 'tr', count: @user.units.count diff --git a/test/test_helper.rb b/test/test_helper.rb index 5362a8d..d480547 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -13,27 +13,36 @@ class ActiveSupport::TestCase include ActionView::Helpers::TranslationHelper # List of categorized Unicode characters: - # * http://www.unicode.org/Public/UNIDATA/UnicodeData.txt - # File format: http://www.unicode.org/L2/L1999/UnicodeData.html - # Select from graphic ranges: L, M, N, P, S, Zs - UNICODE_CHARS = { - 1 => [*"\u0020".."\u007E"], - 2 => [*"\u00A0".."\u00AC", - *"\u00AE".."\u05FF", - *"\u0606".."\u061B", - *"\u061D".."\u06DC", - *"\u06DE".."\u070E", - *"\u0710".."\u07FF"] - } - UNICODE_CHARS.default = UNICODE_CHARS[1] + UNICODE_CHARS[2] - def random_string(bytes = 10, except: []) + # * source: http://www.unicode.org/Public/UNIDATA/UnicodeData.txt + # * file format: http://www.unicode.org/L2/L1999/UnicodeData.html + # * select from graphic ranges: L, M, N, P, S, Zs + UNICODE_CHARS = [ + *"\u0020".."\u007E", + *"\u00A0".."\u00AC", + *"\u00AE".."\u05FF", + *"\u0606".."\u061B", + *"\u061D".."\u06DC", + *"\u06DE".."\u070E", + *"\u0710".."\u07FF" + ] + def random_string(length, except: [], allow_blank: true) begin - result = '' - result += UNICODE_CHARS[bytes - result.bytesize].sample while bytes > result.bytesize - end while except.include?(result) + result = UNICODE_CHARS.sample(length).join + end while except.include?(result) || (!allow_blank && result.blank?) result end + def deep_rand(*args) + case args + when Array + args = args.sample + when Range + args = rand(args) + else + return args + end while true + end + # Assumes: max >= step and step = 1e[-]N, both as strings def random_number(max, step) max.delete!('.')