Skip to content

Commit

Permalink
100% code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bfontaine committed Feb 2, 2014
1 parent 2eae1bb commit 4a756bc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/dz.rb
Expand Up @@ -8,7 +8,7 @@ def version
end

def dz2bin(s)
hxs = s.gsub(/^#.*?$/, ' ').split(/[^0-9A-Fa-f]/)
hxs = s.gsub(/#.*?$/, ' ').split(/[^0-9A-Fa-f]/m)
hxs.reject!(&:empty?)

hxs.map { |x| x.to_i(16) }.pack('C*')
Expand Down
55 changes: 54 additions & 1 deletion tests/dz2bin_tests.rb
Expand Up @@ -3,6 +3,59 @@

class DZ_dz2bin_test < Test::Unit::TestCase

# TODO
# to_bytes("AABBCC") -> "\xAA\xBB\xCC"
def to_bytes(b)
[b].pack('H*')
end

def test_empty_string
assert_equal('', DZ.dz2bin(''))
end

def test_only_a_comment
assert_equal('', DZ.dz2bin('#'))
assert_equal('', DZ.dz2bin('# foo'))
assert_equal('', DZ.dz2bin(' # foo'))
end

def test_empty_lines
assert_equal('', DZ.dz2bin("\n\n\n"))
end

def test_no_hexa
assert_equal('', DZ.dz2bin("lol\n# something\n | ---- $<%"))
end

def test_one_byte
assert_equal(to_bytes('00'), DZ.dz2bin('00'))
assert_equal(to_bytes('42'), DZ.dz2bin('42'))
assert_equal(to_bytes('AF'), DZ.dz2bin('AF'))
assert_equal(to_bytes('FF'), DZ.dz2bin('FF'))
end

def test_two_bytes
assert_equal(to_bytes('0000'), DZ.dz2bin('00 00'))
assert_equal(to_bytes('4237'), DZ.dz2bin('42 37'))
assert_equal(to_bytes('AF10'), DZ.dz2bin('AF 10'))
assert_equal(to_bytes('FFDC'), DZ.dz2bin('FF DC'))
end

def test_two_bytes_stuff_between
assert_equal(to_bytes('0000'), DZ.dz2bin('00 x 00'))
assert_equal(to_bytes('4237'), DZ.dz2bin('42 $$ 37'))
assert_equal(to_bytes('AF10'), DZ.dz2bin('AF **** 10'))
assert_equal(to_bytes('FFDC'), DZ.dz2bin('FF <!---> DC'))
end

def test_two_bytes_different_lines
assert_equal(to_bytes('4237'), DZ.dz2bin("42\n37"))
end

def test_multiple_bytes
assert_equal(to_bytes('4237AB'), DZ.dz2bin(%q[
42 # <-- here is 42
37 |-------| AB
]))
end

end
2 changes: 1 addition & 1 deletion tests/tests.rb
Expand Up @@ -12,7 +12,7 @@
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
SimpleCov.start { add_filter '/tests/' }

require 'dz'
require_relative '../lib/dz'

for t in Dir.glob( File.join( test_dir, '*_tests.rb' ) )
require t
Expand Down

0 comments on commit 4a756bc

Please sign in to comment.