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:
parent
356978f358
commit
5fb00a8225
5 changed files with 57 additions and 2 deletions
|
@ -25,6 +25,10 @@ Metrics/BlockLength:
|
|||
- 'Rakefile'
|
||||
- 'test/**/*.rb'
|
||||
|
||||
Security/YAMLLoad:
|
||||
Exclude:
|
||||
- 'test/**/*.rb'
|
||||
|
||||
Style/AndOr:
|
||||
EnforcedStyle: conditionals
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
25
pkgs/mruby/test/sprintf.rb
Normal file
25
pkgs/mruby/test/sprintf.rb
Normal 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
|
|
@ -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
|
||||
##
|
||||
|
|
Loading…
Add table
Reference in a new issue