test-bug fixed (missing x for DirectiveRegexp). Tests added.

This commit is contained in:
Denis Knauf 2020-04-29 23:14:50 +02:00
parent 90e4757ff3
commit 0956292c88
4 changed files with 68 additions and 3 deletions

34
Gemfile.lock Normal file
View file

@ -0,0 +1,34 @@
PATH
remote: .
specs:
iounpack (0.2.0)
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.3)
rake (12.3.3)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-core (3.9.1)
rspec-support (~> 3.9.1)
rspec-expectations (3.9.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.2)
PLATFORMS
ruby
DEPENDENCIES
iounpack!
rake (~> 12.0)
rspec (~> 3.2)
BUNDLED WITH
2.1.4

View file

@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["changelog_uri"] = spec.homepage
spec.add_development_dependency "rspec", "~> 3.2"
# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do

View file

@ -68,17 +68,18 @@ class IOUnpack
Directives[dir.directive.to_s] = dir
Regexp.quote dir.directive
end.join( '|')+')'
DirectiveRegexp = /\A\s* (?<directive> #{DirectivePattern} ) \s* (?<count> \d+)? \s*/m
DirectiveRegexp = /\A\s* (?<directive> #{DirectivePattern} ) \s* (?<count> \d+)? \s*/mx
attr_reader :len, :pattern
def initialize pattern
@order, @len, @pattern = [], 0, pattern
@len, @pattern = 0, pattern
loop do
case pattern
when /\A\s*\z/m then break
when DirectiveRegexp
pattern = $'
m = [Directives[$~[:directive]], $~[:count] ? $~[:count].to_i : 1]
@order.push m
@len += m.first * m.last
else
raise ArgumentError, "Unknown token: #{pattern[0]}"

28
spec/iounpack_spec.rb Normal file
View file

@ -0,0 +1,28 @@
require 'iounpack'
describe IOUnpack do
it 'has nothing to read, if empty directives' do
expect( IOUnpack.new('').len).to eql(0)
end
<<EOF.each_line do |l|
1 c
2 cc
2 c2
2 cc
2 c c
3 cc c
4 cc c2
4 c3c
14 c13c
14 c13 c
16 q>q<
19 q>2c3
19 q>2 c3
EOF
/\A(\d+) (.*)/
it "wants read #$2 bytes for '#$1'" do
expect( IOUnpack.new('cc').len).to eql(2)
end
end
end