annotate src/libsndfile-1.0.27/programs/test-sndfile-metadata-set.py @ 40:1df64224f5ac

Current libsndfile source
author Chris Cannam
date Tue, 18 Oct 2016 13:22:47 +0100
parents
children
rev   line source
Chris@40 1 #!/usr/bin/python
Chris@40 2
Chris@40 3 # Copyright (C) 2008-2016 Erik de Castro Lopo <erikd@mega-nerd.com>
Chris@40 4 #
Chris@40 5 # All rights reserved.
Chris@40 6 #
Chris@40 7 # Redistribution and use in source and binary forms, with or without
Chris@40 8 # modification, are permitted provided that the following conditions are
Chris@40 9 # met:
Chris@40 10 #
Chris@40 11 # * Redistributions of source code must retain the above copyright
Chris@40 12 # notice, this list of conditions and the following disclaimer.
Chris@40 13 # * Redistributions in binary form must reproduce the above copyright
Chris@40 14 # notice, this list of conditions and the following disclaimer in
Chris@40 15 # the documentation and/or other materials provided with the
Chris@40 16 # distribution.
Chris@40 17 # * Neither the author nor the names of any contributors may be used
Chris@40 18 # to endorse or promote products derived from this software without
Chris@40 19 # specific prior written permission.
Chris@40 20 #
Chris@40 21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@40 22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
Chris@40 23 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Chris@40 24 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
Chris@40 25 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Chris@40 26 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Chris@40 27 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
Chris@40 28 # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Chris@40 29 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
Chris@40 30 # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Chris@40 31 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@40 32
Chris@40 33 # Simple test script for the sndfile-metadata-set program.
Chris@40 34
Chris@40 35 import commands, os, sys
Chris@40 36 import time, datetime
Chris@40 37
Chris@40 38 class Programs:
Chris@40 39 def __init__ (self, needs_exe):
Chris@40 40 if needs_exe:
Chris@40 41 extension = ".exe"
Chris@40 42 else:
Chris@40 43 extension = ""
Chris@40 44 self.meta_set_prog = "./sndfile-metadata-set" + extension
Chris@40 45 self.meta_get_prog = "./sndfile-metadata-get" + extension
Chris@40 46 self.make_sine_prog = "../examples/make_sine" + extension
Chris@40 47
Chris@40 48 def _run_command (self, should_fail, cmd):
Chris@40 49 status, output = commands.getstatusoutput (cmd)
Chris@40 50 if should_fail and not status:
Chris@40 51 print "\n\nError : command '%s' should have failed." % cmd
Chris@40 52 print output
Chris@40 53 print ""
Chris@40 54 sys.exit (1)
Chris@40 55 if not should_fail and status:
Chris@40 56 print "\n\nError : command '%s' should not have failed." % cmd
Chris@40 57 print output
Chris@40 58 print ""
Chris@40 59 sys.exit (1)
Chris@40 60 return output
Chris@40 61
Chris@40 62 def meta_set (self, should_fail, args):
Chris@40 63 return self._run_command (should_fail, self.meta_set_prog + " " + args)
Chris@40 64
Chris@40 65 def meta_get (self, should_fail, args):
Chris@40 66 return self._run_command (should_fail, self.meta_get_prog + " " + args)
Chris@40 67
Chris@40 68 def make_sine (self):
Chris@40 69 return os.system (self.make_sine_prog)
Chris@40 70
Chris@40 71 def check_executables (self):
Chris@40 72 for name in [ self.meta_set_prog, self.meta_get_prog, self.make_sine_prog ]:
Chris@40 73 if not (os.path.isfile (name)):
Chris@40 74 print "\n\nError : Can't find executable '%s'. Have you run make?" % name
Chris@40 75 sys.exit (1)
Chris@40 76
Chris@40 77
Chris@40 78 def print_test_name (name):
Chris@40 79 print " %-30s :" % name,
Chris@40 80
Chris@40 81 def assert_info (programs, filename, arg, value):
Chris@40 82 output = programs.meta_get (False, "%s %s" % (arg, filename))
Chris@40 83 if output.find (value) < 0:
Chris@40 84 print "\n\nError : not able to find '%s'." % value
Chris@40 85 print output
Chris@40 86 sys.exit (1)
Chris@40 87 return
Chris@40 88
Chris@40 89
Chris@40 90 def test_empty_fail (programs):
Chris@40 91 print_test_name ("Empty fail test")
Chris@40 92 output = programs.meta_set (True, "--bext-description Alpha sine.wav")
Chris@40 93 print "ok"
Chris@40 94
Chris@40 95 def test_copy (programs):
Chris@40 96 print_test_name ("Copy test")
Chris@40 97 output = programs.meta_set (False, "--bext-description \"First Try\" sine.wav output.wav")
Chris@40 98 assert_info (programs, "output.wav", "--bext-description", "First Try")
Chris@40 99 print "ok"
Chris@40 100
Chris@40 101 def test_update (programs, tests):
Chris@40 102 print_test_name ("Update test")
Chris@40 103 for arg, value in tests:
Chris@40 104 output = programs.meta_set (False, "%s \"%s\" output.wav" % (arg, value))
Chris@40 105 assert_info (programs, "output.wav", arg, value)
Chris@40 106 print "ok"
Chris@40 107
Chris@40 108 def test_post_mod (programs, tests):
Chris@40 109 print_test_name ("Post mod test")
Chris@40 110 for arg, value in tests:
Chris@40 111 assert_info (programs, "output.wav", arg, value)
Chris@40 112 print "ok"
Chris@40 113
Chris@40 114 def test_auto_date (programs):
Chris@40 115 print_test_name ("Auto date test")
Chris@40 116 output = programs.meta_set (False, "--bext-auto-time-date sine.wav date-time.wav")
Chris@40 117 target = datetime.date.today ().__str__ ()
Chris@40 118 assert_info (programs, "date-time.wav", "--bext-orig-date", target)
Chris@40 119 print "ok"
Chris@40 120
Chris@40 121
Chris@40 122 #-------------------------------------------------------------------------------
Chris@40 123
Chris@40 124 def test_coding_history (programs):
Chris@40 125 print_test_name ("Coding history test")
Chris@40 126 output = programs.meta_set (False, "--bext-coding-hist \"alpha beta\" output.wav")
Chris@40 127 output = programs.meta_get (False, "--bext-coding-hist output.wav")
Chris@40 128 print "ok"
Chris@40 129
Chris@40 130 #-------------------------------------------------------------------------------
Chris@40 131
Chris@40 132 def test_rewrite (programs):
Chris@40 133 print_test_name ("Rewrite test")
Chris@40 134 output = programs.meta_set (False, "--bext-originator \"Really, really long string\" output.wav")
Chris@40 135 output = programs.meta_set (False, "--bext-originator \"Short\" output.wav")
Chris@40 136 output = programs.meta_get (False, "--bext-originator output.wav")
Chris@40 137 if output.find ("really long") > 0:
Chris@40 138 print "\n\nError : output '%s' should not contain 'really long'." % output
Chris@40 139 sys.exit (1)
Chris@40 140 print "ok"
Chris@40 141
Chris@40 142 #===============================================================================
Chris@40 143
Chris@40 144 test_dir = "programs"
Chris@40 145
Chris@40 146 print "\nTesting WAV metadata manipulation:"
Chris@40 147
Chris@40 148 if os.path.isdir (test_dir):
Chris@40 149 os.chdir (test_dir)
Chris@40 150
Chris@40 151 if len (sys.argv) >= 1 and sys.argv [1].endswith ("mingw32"):
Chris@40 152 needs_exe = True
Chris@40 153 else:
Chris@40 154 needs_exe = False
Chris@40 155
Chris@40 156 programs = Programs (needs_exe)
Chris@40 157
Chris@40 158 programs.check_executables ()
Chris@40 159
Chris@40 160 programs.make_sine ()
Chris@40 161 if not os.path.isfile ("sine.wav"):
Chris@40 162 print "\n\nError : Can't file file 'sine.wav'."
Chris@40 163 sys.exit (1)
Chris@40 164
Chris@40 165 test_empty_fail (programs)
Chris@40 166 test_copy (programs)
Chris@40 167
Chris@40 168 tests = [
Chris@40 169 ("--bext-description", "Alpha"), ("--bext-originator", "Beta"), ("--bext-orig-ref", "Charlie"),
Chris@40 170 ("--bext-umid", "Delta"), ("--bext-orig-date", "2001-10-01"), ("--bext-orig-time", "01:02:03"),
Chris@40 171 ("--str-title", "Echo"), ("--str-artist", "Fox trot")
Chris@40 172 ]
Chris@40 173
Chris@40 174 test_auto_date (programs)
Chris@40 175 test_update (programs, tests)
Chris@40 176 test_post_mod (programs, tests)
Chris@40 177
Chris@40 178 test_update (programs, [ ("--str-artist", "Fox") ])
Chris@40 179
Chris@40 180 # This never worked.
Chris@40 181 # test_coding_history ()
Chris@40 182
Chris@40 183 test_rewrite (programs)
Chris@40 184
Chris@40 185
Chris@40 186 print ""
Chris@40 187
Chris@40 188 sys.exit (0)
Chris@40 189