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
tdiary-5.1.2 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/tests/file_binary_test.rb
tdiary-5.1.1 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/tests/file_binary_test.rb
tdiary-5.0.8 vendor/bundle/ruby/2.5.0/gems/tins-0.13.2/tests/file_binary_test.rb
tdiary-5.0.8 vendor/bundle/gems/tins-0.13.2/tests/file_binary_test.rb
logstash-filter-cache-redis-0.3.1 vendor/bundle/jruby/1.9/gems/tins-1.6.0/tests/file_binary_test.rb
logstash-filter-cache-redis-0.3.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/tests/file_binary_test.rb
logstash-filter-cache-redis-0.2.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/tests/file_binary_test.rb
logstash-filter-cache-redis-0.1.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/tests/file_binary_test.rb
suzuko-0.1.8 vendor/bundle/ruby/2.0.0/gems/tins-1.6.0/tests/file_binary_test.rb
suzuko-0.1.7 vendor/bundle/ruby/2.0.0/gems/tins-1.6.0/tests/file_binary_test.rb
mastermind_adeybee-0.1.4 vendor/bundle/ruby/2.2.0/gems/tins-1.6.0/tests/file_binary_test.rb
mastermind_adeybee-0.1.3 vendor/bundle/ruby/2.2.0/gems/tins-1.6.0/tests/file_binary_test.rb
mastermind_adeybee-0.1.2 vendor/bundle/ruby/2.2.0/gems/tins-1.6.0/tests/file_binary_test.rb
mastermind_adeybee-0.1.1 vendor/bundle/ruby/2.2.0/gems/tins-1.6.0/tests/file_binary_test.rb
vagrant-cloudstack-1.2.0 vendor/bundle/gems/tins-1.6.0/tests/file_binary_test.rb
tins-1.6.0 tests/file_binary_test.rb
tins-1.5.4 tests/file_binary_test.rb
tins-1.5.3 tests/file_binary_test.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/tins-1.5.1/tests/file_binary_test.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/tins-1.5.2/tests/file_binary_test.rb