#!/usr/bin/env ruby require 'test/unit' require 'mahoro' class MahoroTestCase < Test::Unit::TestCase def setup @m = Mahoro.new end def test_file @m.flags = Mahoro::NONE assert_equal('ASCII C program text', @m.file('mahoro.c')) end def test_mime_file @m.flags = Mahoro::MIME assert({ 'text/x-c; charset=us-ascii' => true, 'text/x-c charset=us-ascii' => true }.include?(@m.file('mahoro.c'))) end def test_null_byte_in_path assert_raises(ArgumentError) { @m.file("mahoro.c\0foo") } end def test_buffer @m.flags = Mahoro::NONE assert_equal('ASCII C program text', @m.buffer(File.read('mahoro.c'))) end def test_buffer_string_convert tmp = File.read('mahoro.c') buf = Struct.new(:to_str).new(tmp) assert_equal('ASCII C program text', @m.buffer(buf)) end def test_buffer_invalid @m.flags = Mahoro::NONE assert_raises(TypeError) { @m.buffer @m } end def test_mime_buffer @m.flags = Mahoro::MIME assert({ 'text/x-c; charset=us-ascii' => true, 'text/x-c charset=us-ascii' => true }.include?(@m.buffer(File.read('mahoro.c')))) end def test_valid assert(@m.valid?, 'Default database was not valid.') end def test_valid_with_null assert_raises(ArgumentError) { @m.valid? "mahoro.c\0" } end def test_compile File.open(__FILE__) do |fp| fp.flock File::LOCK_EX assert Mahoro.compile("magic.sample") assert_nothing_raised do File.unlink("magic.sample.mgc") end end end def test_compile_bad assert_raises(ArgumentError) do Mahoro.compile "magic.sample\0" end end end # arch-tag: test