From 0956292c88c07b4efacf36e44831f58b3dd381db Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Wed, 29 Apr 2020 23:14:50 +0200 Subject: [PATCH] test-bug fixed (missing x for DirectiveRegexp). Tests added. --- Gemfile.lock | 34 ++++++++++++++++++++++++++++++++++ iounpack.gemspec | 2 ++ lib/iounpack.rb | 7 ++++--- spec/iounpack_spec.rb | 28 ++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 Gemfile.lock create mode 100644 spec/iounpack_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..37a1293 --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/iounpack.gemspec b/iounpack.gemspec index a629ad1..19b2cb0 100644 --- a/iounpack.gemspec +++ b/iounpack.gemspec @@ -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 diff --git a/lib/iounpack.rb b/lib/iounpack.rb index dfb5c80..300212a 100644 --- a/lib/iounpack.rb +++ b/lib/iounpack.rb @@ -68,17 +68,18 @@ class IOUnpack Directives[dir.directive.to_s] = dir Regexp.quote dir.directive end.join( '|')+')' - DirectiveRegexp = /\A\s* (? #{DirectivePattern} ) \s* (? \d+)? \s*/m + DirectiveRegexp = /\A\s* (? #{DirectivePattern} ) \s* (? \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]}" diff --git a/spec/iounpack_spec.rb b/spec/iounpack_spec.rb new file mode 100644 index 0000000..ebdfa2a --- /dev/null +++ b/spec/iounpack_spec.rb @@ -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 + + <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