Sha256: 14a2d149a884d13fbd822169cb5372ef8262391c805dd0a4e284320d4108d10a

Contents?: true

Size: 1.83 KB

Versions: 75

Compression:

Stored size: 1.83 KB

Contents

require 'test_helper'
require 'tempfile'
require 'tins/xt'

module Tins
  class TinsFileBinaryTest < 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

75 entries across 73 versions & 7 rubygems

Version Path
tins-0.5.0 tests/file_binary_test.rb
tins-0.4.3 tests/file_binary_test.rb
tins-0.4.2 tests/file_binary_test.rb
tins-0.4.1 tests/file_binary_test.rb
tins-0.4.0 tests/file_binary_test.rb
tins-0.3.14 tests/file_binary_test.rb
tins-0.3.13 tests/file_binary_test.rb
tins-0.3.12 tests/file_binary_test.rb
tins-0.3.11 tests/file_binary_test.rb
tins-0.3.10 tests/file_binary_test.rb
tins-0.3.9 tests/file_binary_test.rb
tins-0.3.8 tests/file_binary_test.rb
tins-0.3.7 tests/file_binary_test.rb
tins-0.3.6 tests/file_binary_test.rb
tins-0.3.5 tests/file_binary_test.rb