# -*- tcl -*-
#
# Testing "fumagic" (FileUtil Magic). Mimetype recognizer.
#
# Sourcing this file into Tcl runs the tests and generates output for errors.
# No output means no errors were found.
#
# Copyright (c) 2005-2006 by Andreas Kupries <andreas_kupries@users.sourceforge.net>
# All rights reserved.
#
# RCS: @(#) $Id: mimetypes.test,v 1.10 2006/10/09 21:41:40 andreas_kupries Exp $

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.4
testsNeedTcltest 1.0

catch {namespace delete ::fileutil::magic}
support {
    useLocalFile fumagic.testsupport
    useLocal     rtcore.tcl fileutil::magic::rt
}
testing {
    useLocal mimetypes.tcl fileutil::magic::mimetype
}

# -------------------------------------------------------------------------
# Now the package specific tests....

set path [makeFile {} bogus]
removeFile bogus

test fumagic.mimetype-1.1 {test file non-existance} {
    set res [catch {fileutil::magic::mimetype $path} msg]
    list $res $msg
} [list 1 "file not found: \"$path\""]

test fumagic.mimetype-1.2 {test file directory} {
    set f [makeDirectory fileTypeTest]
    set res [catch {fileutil::magic::mimetype $f} msg]
    regsub {file[0-9]+} $msg {fileXXX} msg
    removeDirectory fileTypeTest
    list $res $msg
} {0 application/x-directory}

test fumagic.mimetype-1.3 {test file empty} {
    set f [makeEmptyFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removeEmptyFile
    list $res $msg
} {0 {}}

test fumagic.mimetype-1.4 {test simple binary} {
    set f [makeBinFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removeBinFile
    list $res $msg
} {0 {}}

test fumagic.mimetype-1.5 {test elf executable} {
    set f [makeElfFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removeElfFile
    list $res $msg
} {0 application/x-executable-file}

test fumagic.mimetype-1.6 {test simple text} {
    set f [makeTextFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removeTextFile
    list $res $msg
} {0 {}}

test fumagic.mimetype-1.7 {test script file} {
    set f [makeScriptFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removeScriptFile
    list $res $msg
} {0 text/script}

test fumagic.mimetype-1.8 {test html text} {
    set f [makeHtmlFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removeHtmlFile
    list $res $msg
} {0 text/html}

test fumagic.mimetype-1.9 {test xml text} {knownBug} {
    set f [makeXmlFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removeXmlFile
    list $res $msg
} {0 text/xml}

test fumagic.mimetype-1.10 {test xml with dtd text} {knownBug} {
    set f [makeXmlDTDFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removeXmlDTDFile
    list $res $msg
} {0 text/xml}

test fumagic.mimetype-1.11 {test PGP message} {knownBug} {
    set f [makePgpFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removePgpFile
    list $res $msg
} {0 {PGP armored data}} ; # Result is not a mime type.

test fumagic.mimetype-1.12 {test binary graphic jpeg} {
    set f [makeJpegFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removeJpegFile
    list $res $msg
} {0 image/jpeg}

test fumagic.mimetype-1.13 {test binary graphic gif} {
    set f [makeGifFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removeGifFile
    list $res $msg
} {0 image/gif}

test fumagic.mimetype-1.14 {test binary graphic png} {
    set f [makePngFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removePngFile
    list $res $msg
} {0 image/x-png}

test fumagic.mimetype-1.15 {test binary graphic tiff} {
    set f [makeTiffFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removeTiffFile
    list $res $msg
} {0 image/tiff}

test fumagic.mimetype-1.16 {test binary pdf} {knownBug} {
    set f [makePdfFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removePdfFile
    list $res $msg
} {0 {PDF document}} ; # Result is not a mime type

test fumagic.mimetype-1.17 {test text ps} {
    set f [makePSFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removePSFile
    list $res $msg
} {0 application/postscript}

test fumagic.mimetype-1.18 {test text eps} {
    set f [makeEPSFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removeEPSFile
    list $res $msg
} {0 application/postscript}

test fumagic.mimetype-1.19 {test binary gravity_wave_data_frame} {
    set f [makeIgwdFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removeIgwdFile
    list $res $msg
} {0 {}}

test fumagic.mimetype-1.20 {test binary compressed bzip} {
    set f [makeBzipFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removeBzipFile
    list $res $msg
} {0 {application/x-bzip2 application/x-bzip}}

test fumagic.mimetype-1.21 {test binary compressed gzip} {
    set f [makeGzipFile]
    set res [catch {fileutil::magic::mimetype $f} msg]
    removeGzipFile
    list $res $msg
} {0 application/x-gzip}


testsuiteCleanup
return
