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