cannam@125: #!/usr/bin/python cannam@125: cannam@125: # Copyright (C) 2008-2016 Erik de Castro Lopo cannam@125: # cannam@125: # All rights reserved. cannam@125: # cannam@125: # Redistribution and use in source and binary forms, with or without cannam@125: # modification, are permitted provided that the following conditions are cannam@125: # met: cannam@125: # cannam@125: # * Redistributions of source code must retain the above copyright cannam@125: # notice, this list of conditions and the following disclaimer. cannam@125: # * Redistributions in binary form must reproduce the above copyright cannam@125: # notice, this list of conditions and the following disclaimer in cannam@125: # the documentation and/or other materials provided with the cannam@125: # distribution. cannam@125: # * Neither the author nor the names of any contributors may be used cannam@125: # to endorse or promote products derived from this software without cannam@125: # specific prior written permission. cannam@125: # cannam@125: # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS cannam@125: # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED cannam@125: # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR cannam@125: # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR cannam@125: # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, cannam@125: # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, cannam@125: # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; cannam@125: # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, cannam@125: # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR cannam@125: # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF cannam@125: # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cannam@125: cannam@125: # Simple test script for the sndfile-metadata-set program. cannam@125: cannam@125: import commands, os, sys cannam@125: import time, datetime cannam@125: cannam@125: class Programs: cannam@125: def __init__ (self, needs_exe): cannam@125: if needs_exe: cannam@125: extension = ".exe" cannam@125: else: cannam@125: extension = "" cannam@125: self.meta_set_prog = "./sndfile-metadata-set" + extension cannam@125: self.meta_get_prog = "./sndfile-metadata-get" + extension cannam@125: self.make_sine_prog = "../examples/make_sine" + extension cannam@125: cannam@125: def _run_command (self, should_fail, cmd): cannam@125: status, output = commands.getstatusoutput (cmd) cannam@125: if should_fail and not status: cannam@125: print "\n\nError : command '%s' should have failed." % cmd cannam@125: print output cannam@125: print "" cannam@125: sys.exit (1) cannam@125: if not should_fail and status: cannam@125: print "\n\nError : command '%s' should not have failed." % cmd cannam@125: print output cannam@125: print "" cannam@125: sys.exit (1) cannam@125: return output cannam@125: cannam@125: def meta_set (self, should_fail, args): cannam@125: return self._run_command (should_fail, self.meta_set_prog + " " + args) cannam@125: cannam@125: def meta_get (self, should_fail, args): cannam@125: return self._run_command (should_fail, self.meta_get_prog + " " + args) cannam@125: cannam@125: def make_sine (self): cannam@125: return os.system (self.make_sine_prog) cannam@125: cannam@125: def check_executables (self): cannam@125: for name in [ self.meta_set_prog, self.meta_get_prog, self.make_sine_prog ]: cannam@125: if not (os.path.isfile (name)): cannam@125: print "\n\nError : Can't find executable '%s'. Have you run make?" % name cannam@125: sys.exit (1) cannam@125: cannam@125: cannam@125: def print_test_name (name): cannam@125: print " %-30s :" % name, cannam@125: cannam@125: def assert_info (programs, filename, arg, value): cannam@125: output = programs.meta_get (False, "%s %s" % (arg, filename)) cannam@125: if output.find (value) < 0: cannam@125: print "\n\nError : not able to find '%s'." % value cannam@125: print output cannam@125: sys.exit (1) cannam@125: return cannam@125: cannam@125: cannam@125: def test_empty_fail (programs): cannam@125: print_test_name ("Empty fail test") cannam@125: output = programs.meta_set (True, "--bext-description Alpha sine.wav") cannam@125: print "ok" cannam@125: cannam@125: def test_copy (programs): cannam@125: print_test_name ("Copy test") cannam@125: output = programs.meta_set (False, "--bext-description \"First Try\" sine.wav output.wav") cannam@125: assert_info (programs, "output.wav", "--bext-description", "First Try") cannam@125: print "ok" cannam@125: cannam@125: def test_update (programs, tests): cannam@125: print_test_name ("Update test") cannam@125: for arg, value in tests: cannam@125: output = programs.meta_set (False, "%s \"%s\" output.wav" % (arg, value)) cannam@125: assert_info (programs, "output.wav", arg, value) cannam@125: print "ok" cannam@125: cannam@125: def test_post_mod (programs, tests): cannam@125: print_test_name ("Post mod test") cannam@125: for arg, value in tests: cannam@125: assert_info (programs, "output.wav", arg, value) cannam@125: print "ok" cannam@125: cannam@125: def test_auto_date (programs): cannam@125: print_test_name ("Auto date test") cannam@125: output = programs.meta_set (False, "--bext-auto-time-date sine.wav date-time.wav") cannam@125: target = datetime.date.today ().__str__ () cannam@125: assert_info (programs, "date-time.wav", "--bext-orig-date", target) cannam@125: print "ok" cannam@125: cannam@125: cannam@125: #------------------------------------------------------------------------------- cannam@125: cannam@125: def test_coding_history (programs): cannam@125: print_test_name ("Coding history test") cannam@125: output = programs.meta_set (False, "--bext-coding-hist \"alpha beta\" output.wav") cannam@125: output = programs.meta_get (False, "--bext-coding-hist output.wav") cannam@125: print "ok" cannam@125: cannam@125: #------------------------------------------------------------------------------- cannam@125: cannam@125: def test_rewrite (programs): cannam@125: print_test_name ("Rewrite test") cannam@125: output = programs.meta_set (False, "--bext-originator \"Really, really long string\" output.wav") cannam@125: output = programs.meta_set (False, "--bext-originator \"Short\" output.wav") cannam@125: output = programs.meta_get (False, "--bext-originator output.wav") cannam@125: if output.find ("really long") > 0: cannam@125: print "\n\nError : output '%s' should not contain 'really long'." % output cannam@125: sys.exit (1) cannam@125: print "ok" cannam@125: cannam@125: #=============================================================================== cannam@125: cannam@125: test_dir = "programs" cannam@125: cannam@125: print "\nTesting WAV metadata manipulation:" cannam@125: cannam@125: if os.path.isdir (test_dir): cannam@125: os.chdir (test_dir) cannam@125: cannam@125: if len (sys.argv) >= 1 and sys.argv [1].endswith ("mingw32"): cannam@125: needs_exe = True cannam@125: else: cannam@125: needs_exe = False cannam@125: cannam@125: programs = Programs (needs_exe) cannam@125: cannam@125: programs.check_executables () cannam@125: cannam@125: programs.make_sine () cannam@125: if not os.path.isfile ("sine.wav"): cannam@125: print "\n\nError : Can't file file 'sine.wav'." cannam@125: sys.exit (1) cannam@125: cannam@125: test_empty_fail (programs) cannam@125: test_copy (programs) cannam@125: cannam@125: tests = [ cannam@125: ("--bext-description", "Alpha"), ("--bext-originator", "Beta"), ("--bext-orig-ref", "Charlie"), cannam@125: ("--bext-umid", "Delta"), ("--bext-orig-date", "2001-10-01"), ("--bext-orig-time", "01:02:03"), cannam@125: ("--str-title", "Echo"), ("--str-artist", "Fox trot") cannam@125: ] cannam@125: cannam@125: test_auto_date (programs) cannam@125: test_update (programs, tests) cannam@125: test_post_mod (programs, tests) cannam@125: cannam@125: test_update (programs, [ ("--str-artist", "Fox") ]) cannam@125: cannam@125: # This never worked. cannam@125: # test_coding_history () cannam@125: cannam@125: test_rewrite (programs) cannam@125: cannam@125: cannam@125: print "" cannam@125: cannam@125: sys.exit (0) cannam@125: