Commit 872e8133 by Jun Aruga

Change testing framework from bacon to minitest.

parent 6a715831
require 'rake/testtask'
task :default => %w(test) task :default => %w(test)
desc 'Run tests with bacon' desc 'Run tests with minitest'
task :test => FileList['test/*_test.rb'] do |t| Rake::TestTask.new do |t|
sh "bacon -q -Ilib:test #{t.prerequisites.join(' ')}" t.libs << 'test'
t.pattern = 'test/*_test.rb'
end end
desc 'Generate mime tables' desc 'Generate mime tables'
......
...@@ -19,6 +19,6 @@ Gem::Specification.new do |s| ...@@ -19,6 +19,6 @@ Gem::Specification.new do |s|
s.homepage = 'https://github.com/minad/mimemagic' s.homepage = 'https://github.com/minad/mimemagic'
s.license = 'MIT' s.license = 'MIT'
s.add_development_dependency('bacon') s.add_development_dependency('minitest', '~> 5.11')
s.add_development_dependency('rake') s.add_development_dependency('rake')
end end
require 'bacon' require 'minitest/autorun'
require 'mimemagic' require 'mimemagic'
require 'stringio' require 'stringio'
require 'forwardable' require 'forwardable'
describe 'MimeMagic' do class TestMimeMagic < Minitest::Test
it 'should have type, mediatype and subtype' do # Do deep copy for constants of initial state.
MimeMagic.new('text/html').type.should.equal 'text/html' INIT_EXTENSIONS = Marshal.load(Marshal.dump(MimeMagic::EXTENSIONS))
MimeMagic.new('text/html').mediatype.should.equal 'text' INIT_TYPES = Marshal.load(Marshal.dump(MimeMagic::TYPES))
MimeMagic.new('text/html').subtype.should.equal 'html' INIT_MAGIC = Marshal.load(Marshal.dump(MimeMagic::MAGIC))
def setup
extentions = Marshal.load(Marshal.dump(INIT_EXTENSIONS))
types = Marshal.load(Marshal.dump(INIT_TYPES))
magic = Marshal.load(Marshal.dump(INIT_MAGIC))
MimeMagic.send(:remove_const, :EXTENSIONS) if MimeMagic.const_defined?(:EXTENSIONS)
MimeMagic.send(:remove_const, :TYPES) if MimeMagic.const_defined?(:TYPES)
MimeMagic.send(:remove_const, :MAGIC) if MimeMagic.const_defined?(:MAGIC)
MimeMagic.const_set('EXTENSIONS', extentions)
MimeMagic.const_set('TYPES', types)
MimeMagic.const_set('MAGIC', magic)
end end
it 'should have mediatype helpers' do def test_have_type_mediatype_and_subtype
MimeMagic.new('text/plain').should.be.text assert_equal 'text/html', MimeMagic.new('text/html').type
MimeMagic.new('text/html').should.be.text assert_equal 'text', MimeMagic.new('text/html').mediatype
MimeMagic.new('application/xhtml+xml').should.be.text assert_equal 'html', MimeMagic.new('text/html').subtype
MimeMagic.new('application/octet-stream').should.not.be.text
MimeMagic.new('image/png').should.not.be.text
MimeMagic.new('image/png').should.be.image
MimeMagic.new('video/ogg').should.be.video
MimeMagic.new('audio/mpeg').should.be.audio
end end
it 'should have hierarchy' do def test_have_mediatype_helpers
MimeMagic.new('text/html').should.be.child_of 'text/plain' assert MimeMagic.new('text/plain').text?
MimeMagic.new('text/x-java').should.be.child_of 'text/plain' assert MimeMagic.new('text/html').text?
assert MimeMagic.new('application/xhtml+xml').text?
refute MimeMagic.new('application/octet-stream').text?
refute MimeMagic.new('image/png').text?
assert MimeMagic.new('image/png').image?
assert MimeMagic.new('video/ogg').video?
assert MimeMagic.new('audio/mpeg').audio?
end end
it 'should have extensions' do def test_have_hierarchy
MimeMagic.new('text/html').extensions.should.equal %w(htm html) assert MimeMagic.new('text/html').child_of?('text/plain')
assert MimeMagic.new('text/x-java').child_of?('text/plain')
end end
it 'should have comment' do def test_have_extensions
MimeMagic.new('text/html').comment.should.equal 'HTML document' assert_equal %w(htm html), MimeMagic.new('text/html').extensions
end end
it 'should recognize extensions' do def test_have_comment
MimeMagic.by_extension('.html').should.equal 'text/html' assert_equal 'HTML document', MimeMagic.new('text/html').comment
MimeMagic.by_extension('html').should.equal 'text/html'
MimeMagic.by_extension(:html).should.equal 'text/html'
MimeMagic.by_extension('rb').should.equal 'application/x-ruby'
MimeMagic.by_extension('crazy').should.equal nil
MimeMagic.by_extension('').should.equal nil
end end
it 'should recognize by a path' do def test_recognize_extensions
MimeMagic.by_path('/adsjkfa/kajsdfkadsf/kajsdfjasdf.html').should.equal 'text/html' assert_equal 'text/html', MimeMagic.by_extension('.html').to_s
MimeMagic.by_path('something.html').should.equal 'text/html' assert_equal 'text/html', MimeMagic.by_extension('html').to_s
MimeMagic.by_path('wtf.rb').should.equal 'application/x-ruby' assert_equal 'text/html', MimeMagic.by_extension(:html).to_s
MimeMagic.by_path('where/am.html/crazy').should.equal nil assert_equal 'application/x-ruby', MimeMagic.by_extension('rb').to_s
MimeMagic.by_path('').should.equal nil assert_nil MimeMagic.by_extension('crazy')
assert_nil MimeMagic.by_extension('')
end end
it 'should recognize xlsx as zip without magic' do def test_recognize_by_a_path
assert_equal 'text/html', MimeMagic.by_path('/adsjkfa/kajsdfkadsf/kajsdfjasdf.html').to_s
assert_equal 'text/html', MimeMagic.by_path('something.html').to_s
assert_equal 'application/x-ruby', MimeMagic.by_path('wtf.rb').to_s
assert_nil MimeMagic.by_path('where/am.html/crazy')
assert_nil MimeMagic.by_path('')
end
def test_recognize_xlsx_as_zip_without_magic
file = "test/files/application.vnd.openxmlformats-officedocument.spreadsheetml.sheet" file = "test/files/application.vnd.openxmlformats-officedocument.spreadsheetml.sheet"
MimeMagic.by_magic(File.read(file)).should.equal "application/zip" assert_equal "application/zip", MimeMagic.by_magic(File.read(file)).to_s
MimeMagic.by_magic(File.open(file, 'rb')).should.equal "application/zip" assert_equal "application/zip", MimeMagic.by_magic(File.open(file, 'rb')).to_s
end end
it 'should recognize by magic' do def test_recognize_by_magic
require "mimemagic/overlay" load "mimemagic/overlay.rb"
Dir['test/files/*'].each do |file| Dir['test/files/*'].each do |file|
mime = file[11..-1].sub('.', '/') mime = file[11..-1].sub('.', '/')
MimeMagic.by_magic(File.read(file)).should.equal mime assert_equal mime, MimeMagic.by_magic(File.read(file)).to_s
MimeMagic.by_magic(File.open(file, 'rb')).should.equal mime assert_equal mime, MimeMagic.by_magic(File.open(file, 'rb')).to_s
end end
end end
it 'should recognize all by magic' do def test_recognize_all_by_magic
require 'mimemagic/overlay' load 'mimemagic/overlay.rb'
file = 'test/files/application.vnd.openxmlformats-officedocument.spreadsheetml.sheet' file = 'test/files/application.vnd.openxmlformats-officedocument.spreadsheetml.sheet'
mimes = %w[application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/zip] mimes = %w[application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/zip]
MimeMagic.all_by_magic(File.read(file)).map(&:type).should.equal mimes assert_equal mimes, MimeMagic.all_by_magic(File.read(file)).map(&:type)
end end
it 'should have add' do def test_have_add
MimeMagic.add('application/mimemagic-test', MimeMagic.add('application/mimemagic-test',
extensions: %w(ext1 ext2), extensions: %w(ext1 ext2),
parents: 'application/xml', parents: 'application/xml',
comment: 'Comment') comment: 'Comment')
MimeMagic.by_extension('ext1').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_extension('ext1').to_s
MimeMagic.by_extension('ext2').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_extension('ext2').to_s
MimeMagic.by_extension('ext2').comment.should.equal 'Comment' assert_equal 'Comment', MimeMagic.by_extension('ext2').comment
MimeMagic.new('application/mimemagic-test').extensions.should.equal %w(ext1 ext2) assert_equal %w(ext1 ext2), MimeMagic.new('application/mimemagic-test').extensions
MimeMagic.new('application/mimemagic-test').should.be.child_of 'text/plain' assert MimeMagic.new('application/mimemagic-test').child_of?('text/plain')
end end
it 'should process magic' do def test_process_magic
MimeMagic.add('application/mimemagic-test', MimeMagic.add('application/mimemagic-test',
magic: [[0, 'MAGICTEST'], # MAGICTEST at position 0 magic: [[0, 'MAGICTEST'], # MAGICTEST at position 0
[1, 'MAGICTEST'], # MAGICTEST at position 1 [1, 'MAGICTEST'], # MAGICTEST at position 1
[9..12, 'MAGICTEST'], # MAGICTEST starting at position 9 to 12 [9..12, 'MAGICTEST'], # MAGICTEST starting at position 9 to 12
[2, 'MAGICTEST', [[0, 'X'], [0, 'Y']]]]) # MAGICTEST at position 2 and (X at 0 or Y at 0) [2, 'MAGICTEST', [[0, 'X'], [0, 'Y']]]]) # MAGICTEST at position 2 and (X at 0 or Y at 0)
MimeMagic.by_magic('MAGICTEST').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_magic('MAGICTEST').to_s
MimeMagic.by_magic('XMAGICTEST').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_magic('XMAGICTEST').to_s
MimeMagic.by_magic(' MAGICTEST').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_magic(' MAGICTEST').to_s
MimeMagic.by_magic('123456789MAGICTEST').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_magic('123456789MAGICTEST').to_s
MimeMagic.by_magic('123456789ABMAGICTEST').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_magic('123456789ABMAGICTEST').to_s
MimeMagic.by_magic('123456789ABCMAGICTEST').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_magic('123456789ABCMAGICTEST').to_s
MimeMagic.by_magic('123456789ABCDMAGICTEST').should.equal nil assert_nil MimeMagic.by_magic('123456789ABCDMAGICTEST')
MimeMagic.by_magic('X MAGICTEST').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_magic('X MAGICTEST').to_s
MimeMagic.by_magic('Y MAGICTEST').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_magic('Y MAGICTEST').to_s
MimeMagic.by_magic('Z MAGICTEST').should.equal nil assert_nil MimeMagic.by_magic('Z MAGICTEST')
MimeMagic.by_magic(StringIO.new 'MAGICTEST').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_magic(StringIO.new 'MAGICTEST').to_s
MimeMagic.by_magic(StringIO.new 'XMAGICTEST').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_magic(StringIO.new 'XMAGICTEST').to_s
MimeMagic.by_magic(StringIO.new ' MAGICTEST').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_magic(StringIO.new ' MAGICTEST').to_s
MimeMagic.by_magic(StringIO.new '123456789MAGICTEST').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_magic(StringIO.new '123456789MAGICTEST').to_s
MimeMagic.by_magic(StringIO.new '123456789ABMAGICTEST').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_magic(StringIO.new '123456789ABMAGICTEST').to_s
MimeMagic.by_magic(StringIO.new '123456789ABCMAGICTEST').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_magic(StringIO.new '123456789ABCMAGICTEST').to_s
MimeMagic.by_magic(StringIO.new '123456789ABCDMAGICTEST').should.equal nil assert_nil MimeMagic.by_magic(StringIO.new '123456789ABCDMAGICTEST')
MimeMagic.by_magic(StringIO.new 'X MAGICTEST').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_magic(StringIO.new 'X MAGICTEST').to_s
MimeMagic.by_magic(StringIO.new 'Y MAGICTEST').should.equal 'application/mimemagic-test' assert_equal 'application/mimemagic-test', MimeMagic.by_magic(StringIO.new 'Y MAGICTEST').to_s
MimeMagic.by_magic(StringIO.new 'Z MAGICTEST').should.equal nil assert_nil MimeMagic.by_magic(StringIO.new 'Z MAGICTEST')
end end
it 'should handle different file objects' do
MimeMagic.add('application/mimemagic-test', magic: [[0, 'MAGICTEST']])
class IOObject
def initialize
@io = StringIO.new('MAGICTEST')
end
extend Forwardable class IOObject
delegate [:read, :size, :rewind, :eof?, :close] => :@io def initialize
@io = StringIO.new('MAGICTEST')
end end
MimeMagic.by_magic(IOObject.new).should.equal 'application/mimemagic-test'
class StringableObject extend Forwardable
def to_s delegate [:read, :size, :rewind, :eof?, :close] => :@io
'MAGICTEST' end
end
class StringableObject
def to_s
'MAGICTEST'
end end
MimeMagic.by_magic(StringableObject.new).should.equal 'application/mimemagic-test' end
def test_handle_different_file_objects
MimeMagic.add('application/mimemagic-test', magic: [[0, 'MAGICTEST']])
assert_equal 'application/mimemagic-test', MimeMagic.by_magic(IOObject.new).to_s
assert_equal 'application/mimemagic-test', MimeMagic.by_magic(StringableObject.new).to_s
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment