1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2025-04-14 17:32:55 -04:00

mruby: add tests for KernAux.sprintf

This commit is contained in:
Alex Kotov 2022-05-24 17:31:21 +03:00
parent 356978f358
commit 5fb00a8225
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
5 changed files with 57 additions and 2 deletions

View file

@ -25,6 +25,10 @@ Metrics/BlockLength:
- 'Rakefile'
- 'test/**/*.rb'
Security/YAMLLoad:
Exclude:
- 'test/**/*.rb'
Style/AndOr:
EnforcedStyle: conditionals

View file

@ -11,7 +11,9 @@ MRuby::Gem::Specification.new 'mruby-kernaux' do |spec|
Binding to libkernaux - auxiliary library for kernel development.
DESCRIPTION
spec.add_test_dependency 'mruby-io'
spec.add_test_dependency 'mruby-random'
spec.add_test_dependency 'mruby-yaml'
spec.linker.libraries << 'kernaux'
end

View file

@ -1,8 +1,32 @@
##
# Binding to [libkernaux](https://github.com/tailix/libkernaux) - auxiliary
# library for kernel development.
#
module KernAux
DEFAULT_ASSERT_CB = @assert_cb = lambda { |file, line, msg|
raise AssertError, "#{file}:#{line}:#{msg}"
}
SPRINTF1_BUFFER_SIZE = 10_000
def self.sprintf(*args)
args.map do |arg|
if arg.is_a? Array
sprintf1(*arg)
else
arg
end
end.join.freeze
end
def self.sprintf1(format, *args)
snprintf1(SPRINTF1_BUFFER_SIZE, format, *args).first
end
def self.snprintf1(_buffer_size, _format, ...)
['', 0]
end
class Error < RuntimeError; end
class AssertError < Error; end
end

View file

@ -0,0 +1,25 @@
# TODO: implement this
# rubocop:disable Style/BlockComments
=begin
assert 'KernAux.sprintf' do
[
['', 'using regular tests'],
['_orig', 'using original tests'],
].each do |(suffix, description)|
assert description do
printf_yml =
File.expand_path("../../../../tests/printf#{suffix}.yml", __FILE__)
YAML.load(File.read(printf_yml)).each do |test|
expected = test['result']
args = test['args']
assert "transforms #{args.inspect} to #{expected.inspect}" do
assert_equal expected, KernAux.sprintf(*args)
end
end
end
end
end
=end
# rubocop:enable Style/BlockComments

View file

@ -65,7 +65,7 @@ module KernAux
##
# Typical `printf`.
#
# @param args [Array<String, Array<(String, Object)>>]
# @param args [Array<String, Array<(String, Object)>, Array<(String, Integer, Object)>>]
# @return [String] formatted output
#
# @example
@ -107,7 +107,7 @@ module KernAux
# @param buffer_size [Integer] buffer size (including terminating null
# character)
# @param format [String] formatting string
# @return [String] formatted output
# @return [Array<(String, Integer)>] formatted output and it's size
#
# @see .sprintf1 Automatic buffer size
##