-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from yokenzan/yokenzan/fix-kakkotsuki-mark
fix(format): カッコ付き数字の置換処理で常に⑴を返してしまう不具合を修正
- Loading branch information
Showing
9 changed files
with
173 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module Receiptisan | ||
module Util | ||
module Formatter | ||
module KakkotsukiFormatter | ||
Formatter = ::Receiptisan::Util::Formatter | ||
|
||
KAKKO_ICHI_CODEPOINT = 0x2474 # ⑴のコードポイント | ||
TARGET_RANGE = 1..20 | ||
|
||
# カッコ付数字の文字が含まれていたら置換する | ||
# @return [String] | ||
def format(string) | ||
string.gsub(/(([1-9][0-9]*))/) do | ||
matched_number = ::Regexp.last_match(1) | ||
number = Formatter.to_hankaku(matched_number).to_i | ||
convertable?(number) ? convert(number - 1) : matched_number | ||
end | ||
end | ||
|
||
# カッコ付数字の文字を生成する | ||
# @param zero_based_index [Integer] | ||
# @return [String] | ||
def convert(zero_based_index) | ||
# 用意されている文字はカッコ20まで | ||
unless convertable?(zero_based_index + 1) | ||
min = TARGET_RANGE.begin - 1 | ||
max = TARGET_RANGE.end - 1 | ||
|
||
raise ArgumentError, "given index is out of range (#{min}~#{max}): '#{zero_based_index}'" | ||
end | ||
|
||
(KAKKO_ICHI_CODEPOINT + zero_based_index).chr('UTF-8') | ||
end | ||
|
||
def convertable?(number) | ||
number.between?(TARGET_RANGE.begin, TARGET_RANGE.end) | ||
end | ||
|
||
module_function :format, :convert, :convertable? | ||
end | ||
end | ||
end | ||
end |
67 changes: 67 additions & 0 deletions
67
spec/lib/receiptisan/util/formatter/kakkotsuki_formatter_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'receiptisan' | ||
|
||
RSpec.describe Receiptisan::Util::Formatter::KakkotsukiFormatter do | ||
describe '#convert' do | ||
context '0を渡したとき' do | ||
specify '⑴を返す' do | ||
expect(described_class.convert(0)).to eq '⑴' | ||
end | ||
end | ||
|
||
context '1を渡したとき' do | ||
specify '⑵を返す' do | ||
expect(described_class.convert(1)).to eq '⑵' | ||
end | ||
end | ||
|
||
context '3を渡したとき' do | ||
specify '⑶を返す' do | ||
expect(described_class.convert(2)).to eq '⑶' | ||
end | ||
end | ||
|
||
context '19を渡したとき' do | ||
specify '⒇を返す' do | ||
expect(described_class.convert(19)).to eq '⒇' | ||
end | ||
end | ||
|
||
context '20を渡したとき' do | ||
specify '(21)は表示不可能なので範囲外である旨例外を投げる' do | ||
expect { described_class.convert(20) }.to raise_error(ArgumentError, "given index is out of range (0~19): '20'") | ||
end | ||
end | ||
end | ||
|
||
describe '#format' do | ||
context 'カッコ数字を含む文字列を渡したとき' do | ||
context '「生化学的検査(1)判断料」を渡したとき' do | ||
specify '「生化学的検査⑴判断料」を返す' do | ||
expect(described_class.format('生化学的検査(1)判断料')).to eq '生化学的検査⑴判断料' | ||
end | ||
end | ||
|
||
context '「生化学的検査(2)判断料」を渡したとき' do | ||
specify '「生化学的検査⑵判断料」を返す' do | ||
expect(described_class.format('生化学的検査(2)判断料')).to eq '生化学的検査⑵判断料' | ||
end | ||
end | ||
end | ||
|
||
context('カッコ数字を含まない文字列を渡したとき') do | ||
context '「処方箋料(リフィル以外・その他)」を渡したとき' do | ||
specify '何も変更せず「処方箋料(リフィル以外・その他)」を返す' do | ||
expect(described_class.format('処方箋料(リフィル以外・その他)')).to eq '処方箋料(リフィル以外・その他)' | ||
end | ||
end | ||
|
||
context '「一般名処方加算2(処方箋料)」を渡したとき' do | ||
specify '何も変更せず「一般名処方加算2(処方箋料)」を返す' do | ||
expect(described_class.format('一般名処方加算2(処方箋料)')).to eq '一般名処方加算2(処方箋料)' | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'receiptisan' | ||
|
||
RSpec.describe Receiptisan::Util::Formatter do | ||
# TODO: Integer以外のパターン | ||
|
||
describe '#to_currency' do | ||
context '正の整数' do | ||
context '4桁のとき' do | ||
specify '千の位と百の位の間にカンマを付した文字列を返すこと' do | ||
expect(described_class.to_currency(1234)).to eq '1,234' | ||
end | ||
end | ||
|
||
context '6桁のとき' do | ||
specify '千の位と百の位の間にカンマを付した文字列を返すこと' do | ||
expect(described_class.to_currency(123_456)).to eq '123,456' | ||
end | ||
end | ||
|
||
context '7桁のとき' do | ||
specify '百万の位と十万の位、千の位と百の位の間にそれぞれカンマを付した文字列を返すこと' do | ||
expect(described_class.to_currency(1_234_567)).to eq '1,234,567' | ||
end | ||
end | ||
|
||
context '3桁のとき' do | ||
specify 'カンマを付さず3桁の数値を文字列として返すこと' do | ||
expect(described_class.to_currency(123)).to eq '123' | ||
end | ||
end | ||
end | ||
|
||
context '負の整数' do | ||
context '4桁のとき' do | ||
specify 'マイナス符号つきの、千の位と百の位の間にカンマを付した文字列を返すこと' do | ||
expect(described_class.to_currency(-1234)).to eq '-1,234' | ||
end | ||
end | ||
|
||
context '3桁のとき' do | ||
specify 'カンマを付さず3桁の数値を文字列として返すこと' do | ||
expect(described_class.to_currency(-123)).to eq '-123' | ||
end | ||
end | ||
end | ||
end | ||
end |