2022-01-21 14:52:46 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2022-06-13 19:55:05 +00:00
|
|
|
KernAux::Version.with_printf? and RSpec.describe KernAux, '.sprintf' do
|
2022-06-07 19:41:05 +00:00
|
|
|
subject :sprintf do
|
|
|
|
described_class.sprintf 'Hello, ', ['%s', 'World'], '!'
|
|
|
|
end
|
2022-01-21 14:52:46 +00:00
|
|
|
|
2022-06-07 19:41:05 +00:00
|
|
|
it { is_expected.to be_instance_of String }
|
|
|
|
it { is_expected.to be_frozen }
|
|
|
|
it { is_expected.to eq 'Hello, World!' }
|
2022-01-22 01:13:38 +00:00
|
|
|
|
2022-06-07 19:41:05 +00:00
|
|
|
context 'for empty string value' do
|
|
|
|
subject(:sprintf) { described_class.sprintf ['Hello testing%s'] }
|
2022-05-24 14:52:38 +00:00
|
|
|
|
2022-06-07 19:41:05 +00:00
|
|
|
it { is_expected.to eq 'Hello testing' }
|
|
|
|
end
|
2022-05-24 14:52:38 +00:00
|
|
|
|
2022-06-07 19:41:05 +00:00
|
|
|
[
|
|
|
|
['', 'using regular tests'],
|
|
|
|
['_orig', 'using original tests'],
|
|
|
|
].each do |(suffix, description)|
|
|
|
|
context description do
|
|
|
|
printf_yml = File.expand_path(
|
|
|
|
"../../../../../../common/printf#{suffix}.yml",
|
|
|
|
__dir__,
|
|
|
|
)
|
|
|
|
|
|
|
|
YAML.safe_load_file(printf_yml).each do |test|
|
|
|
|
expected = test['result']
|
|
|
|
|
|
|
|
args = test['args'].map do |arg|
|
|
|
|
if arg.is_a? String
|
|
|
|
arg
|
|
|
|
else
|
|
|
|
arg.map do |item|
|
|
|
|
if item.is_a? Array
|
2022-06-21 18:18:28 +00:00
|
|
|
if item.length == 1
|
|
|
|
item[0]
|
|
|
|
elsif item[0] == 'long long'
|
|
|
|
item[1]
|
|
|
|
else
|
|
|
|
raise "Unknown format: #{args.inspect}"
|
|
|
|
end
|
2022-06-07 19:41:05 +00:00
|
|
|
else
|
|
|
|
item
|
2022-05-24 11:14:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-06-07 19:41:05 +00:00
|
|
|
end
|
2022-05-23 19:09:56 +00:00
|
|
|
|
2022-06-07 19:41:05 +00:00
|
|
|
it "transforms #{args.inspect} to #{expected.inspect}" do
|
|
|
|
expect(described_class.sprintf(*args)).to eq expected
|
2022-01-23 21:52:42 +00:00
|
|
|
end
|
2022-01-22 01:13:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-01-21 14:52:46 +00:00
|
|
|
end
|