mirror of https://github.com/tailix/libkernaux.git
Ruby: Add method KernAux.sprintf1
This commit is contained in:
parent
cf789a21a3
commit
b30a98d9b3
|
@ -6,18 +6,28 @@ require_relative 'kernaux/version'
|
|||
# Auxiliary library for kernel development.
|
||||
#
|
||||
module KernAux
|
||||
# Default callback for assertions
|
||||
DEFAULT_ASSERT_CB = lambda { |file, line, str|
|
||||
# Default callback for assertions ({.assert_cb})
|
||||
DEFAULT_ASSERT_CB = @assert_cb = lambda { |file, line, str|
|
||||
raise AssertError, "#{file}:#{line}:#{str}"
|
||||
}
|
||||
|
||||
@assert_cb = DEFAULT_ASSERT_CB
|
||||
# Buffer size for {.sprintf1}
|
||||
# @todo make it dynamic
|
||||
SPRINTF1_BUFFER_SIZE = 10_000
|
||||
|
||||
def self.panic(msg)
|
||||
file, line = caller(1..1).first.split(':')[0..1]
|
||||
assert_do file, Integer(line), msg
|
||||
end
|
||||
|
||||
def self.sprintf1(format, arg = nil)
|
||||
if arg.nil?
|
||||
snprintf1(SPRINTF1_BUFFER_SIZE, format).first
|
||||
else
|
||||
snprintf1(SPRINTF1_BUFFER_SIZE, format, arg).first
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# Our base class for runtime errors.
|
||||
#
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
RSpec.describe KernAux, '.sprintf1' do
|
||||
context 'with 1 argument' do
|
||||
subject(:sprintf1) { described_class.sprintf1 format }
|
||||
|
||||
let(:format) { '%%' }
|
||||
|
||||
it { is_expected.to be_instance_of String }
|
||||
it { is_expected.to be_frozen }
|
||||
it { is_expected.to eq described_class.snprintf1(1000, format).first }
|
||||
end
|
||||
|
||||
context 'with 2 argument' do
|
||||
subject(:sprintf1) { described_class.sprintf1 format, arg }
|
||||
|
||||
let(:format) { '%s' }
|
||||
let(:arg) { 'Hello, World!' }
|
||||
|
||||
it { is_expected.to be_instance_of String }
|
||||
it { is_expected.to be_frozen }
|
||||
it { is_expected.to eq described_class.snprintf1(1000, format, arg).first }
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue