Skip to content

Commit

Permalink
Merge pull request #22 from TakuyaNoguchi/fix_yes_confirmed_bug
Browse files Browse the repository at this point in the history
yes_confirmed? のオプション(fail_on_error)のバグを修正
  • Loading branch information
TakuyaNoguchi authored May 20, 2022
2 parents 36ef776 + b13b703 commit 82efc85
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
8 changes: 4 additions & 4 deletions bizside_test_app/test/lib/bizside/string_utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ def test_rand_string
end

def test_create_random_alpha_string_case_sensitive_true
s = StringUtils.create_random_alpha_string(10, true)
s = Bizside::StringUtils.create_random_alpha_string(10, true)
assert s =~ /\A[A-Za-z]{10}\z/
end

def test_create_random_alpha_string_case_sensitive_false
s = StringUtils.create_random_alpha_string(10, false)
s = Bizside::StringUtils.create_random_alpha_string(10, false)
assert s =~ /\A[a-z]{10}\z/
end

def test_create_random_alpha_string_more_than_26_chars
s = StringUtils.create_random_alpha_string(53, true)
s = Bizside::StringUtils.create_random_alpha_string(53, true)
assert s =~ /\A[A-Za-z]{53}\z/

s = StringUtils.create_random_alpha_string(27, false)
s = Bizside::StringUtils.create_random_alpha_string(27, false)
assert s =~ /\A[a-z]{27}\z/
end
end
47 changes: 47 additions & 0 deletions bizside_test_app/test/lib/bizside/task_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'test_helper'
require 'bizside/task_helper'

# メソッドは全てトップレベルに定義されるため、mainオブジェクトを参照する方法を用意
MAIN_OBJECT = self

class Bizside::TaskHelperTest < ActiveSupport::TestCase
def test_yes_confirmed_true
assert MAIN_OBJECT.yes_confirmed?('yes')
assert MAIN_OBJECT.yes_confirmed?('Yes')
assert MAIN_OBJECT.yes_confirmed?('YES')
assert MAIN_OBJECT.yes_confirmed?('y')
assert MAIN_OBJECT.yes_confirmed?('Y')
assert MAIN_OBJECT.yes_confirmed?('true')
assert MAIN_OBJECT.yes_confirmed?('TRUE')
assert MAIN_OBJECT.yes_confirmed?(true)
end

def test_yes_confirmed_false
assert_not MAIN_OBJECT.yes_confirmed?('no')
assert_not MAIN_OBJECT.yes_confirmed?('No')
assert_not MAIN_OBJECT.yes_confirmed?('NO')
assert_not MAIN_OBJECT.yes_confirmed?('n')
assert_not MAIN_OBJECT.yes_confirmed?('N')
assert_not MAIN_OBJECT.yes_confirmed?('false')
assert_not MAIN_OBJECT.yes_confirmed?('FALSE')
assert_not MAIN_OBJECT.yes_confirmed?(false)
end

def test_yes_confirmed_fali_on_error
options = { fail_on_error: true }
template = 'yes/no または true/false 形式で入力してください。answer=%<answer>s'

assert MAIN_OBJECT.yes_confirmed?('yes', options)

assert_not MAIN_OBJECT.yes_confirmed?('no', options)

assert_equal format(template, answer: 'ok'),
assert_raise(RuntimeError) { MAIN_OBJECT.yes_confirmed?('ok', options) }.message
assert_equal format(template, answer: 'OK'),
assert_raise(RuntimeError) { MAIN_OBJECT.yes_confirmed?('OK', options) }.message
assert_equal format(template, answer: 'ng'),
assert_raise(RuntimeError) { MAIN_OBJECT.yes_confirmed?('ng', options) }.message
assert_equal format(template, answer: 'NG'),
assert_raise(RuntimeError) { MAIN_OBJECT.yes_confirmed?('NG', options) }.message
end
end
2 changes: 1 addition & 1 deletion lib/bizside/task_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def self.yes_confirmed?(yes_value, options = {})
ret = Bizside::Yes.confirmed?(yes_value)
if ret.nil?
if options[:fail_on_error]
fail "yes/no または true/false 形式で入力してください。answer=#{answer}"
fail "yes/no または true/false 形式で入力してください。answer=#{yes_value}"
else
ret = false
end
Expand Down

0 comments on commit 82efc85

Please sign in to comment.