Sha256: 583ca5cda42f07e5acb776d604cb5ce1b70b5d4891776730ad16b40ceb6466aa

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

#!/usr/bin/env ruby

require 'test/unit'
require 'tempfile'
require 'spruz/xt/file_binary'

module Spruz
  class SpruzFileBinaryTest < Test::Unit::TestCase
    def test_ascii_buffer_size
      write_file do |file|
        file.write "A" * 10 + "\x00"
        assert_equal true,  file.ascii?(:buffer_size => 10)
        assert_equal true,  File.ascii?(file.path, :buffer_size => 10)
        assert_equal false, file.binary?(:buffer_size => 10)
        assert_equal false, File.binary?(file.path, :buffer_size => 10)
      end
    end

    def test_binary
      write_file do |file|
        file.write "A" * 69 + "\x01" * 31
        assert_equal true,  file.binary?
        assert_equal true,  File.binary?(file.path)
        assert_equal false, file.ascii?
        assert_equal false, File.ascii?(file.path)
      end
    end

    def test_ascii_offset
      write_file do |file|
        file.write "\x01" * 31 + "A" * 70
        assert_equal false, file.binary?(:offset => 1)
        assert_equal false, File.binary?(file.path, :offset => 1)
        assert_equal true,  file.ascii?(:offset => 1)
        assert_equal true,  File.ascii?(file.path, :offset => 1)
      end
    end

    def test_binary_zero
      write_file do |file|
        file.write "A" * 50 + "\0"  + "A" * 49
        assert_equal true,  file.binary?
        assert_equal true,  File.binary?(file.path)
        assert_equal false, file.ascii?
        assert_equal false, File.ascii?(file.path)
      end
    end

    def test_ascii
      write_file do |file|
        file.write "A" * 100
        assert_equal false, file.binary?
        assert_equal false, File.binary?(file.path)
        assert_equal true,  file.ascii?
        assert_equal true,  File.ascii?(file.path)
      end
    end

    private

    def write_file
      File.open(File.join(Dir.tmpdir, "temp.#$$"), 'wb+') do |file|
        yield file
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spruz-0.2.13 tests/spruz_file_binary_test.rb