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