]> sigrok.org Git - sigrok-test.git/blobdiff - decoder/pdtest
avr_isp: add test for ATmega328/P
[sigrok-test.git] / decoder / pdtest
index 58b6eee19244353226938a35cf1c7cc27d24328e..a2a33fc9b66260f609f32a98db067656c61231e2 100755 (executable)
@@ -24,7 +24,7 @@ import re
 from getopt import getopt
 from tempfile import mkstemp
 from subprocess import Popen, PIPE
-from difflib import Differ
+from difflib import unified_diff
 from hashlib import md5
 from shutil import copy
 
@@ -62,7 +62,7 @@ def usage(msg=None):
   -l  List test(s)
   -s  Show test(s)
   -r  Run test(s)
-  -f  Fix failed test(s)
+  -f  Fix failed test(s) / create initial output for new test(s)
   -c  Report decoder code coverage
   -R <directory>  Save test reports to <directory>
   <test>  Protocol decoder name ("i2c") and optionally test name ("i2c/rtc")""")
@@ -113,6 +113,7 @@ def parse_testfile(path, pd, tc, op_type, op_class):
                     'name': f.pop(0),
                     'channels': [],
                     'options': [],
+                    'initial_pins': [],
                 }
                 while len(f):
                     if len(f) == 1:
@@ -131,6 +132,12 @@ def parse_testfile(path, pd, tc, op_type, op_class):
                         pd_spec['channels'].append([opt, val])
                     elif a == 'option':
                         pd_spec['options'].append([opt, val])
+                    elif a == 'initial_pin':
+                        try:
+                            val = int(val)
+                        except:
+                            raise E_syntax
+                        pd_spec['initial_pins'].append([opt, val])
                     else:
                         raise E_syntax
                 tclist[-1]['pdlist'].append(pd_spec)
@@ -139,9 +146,26 @@ def parse_testfile(path, pd, tc, op_type, op_class):
                     raise E_syntax
                 tclist[-1]['stack'] = f
             elif key == 'input':
-                if len(f) != 1:
+                if len(f) < 1:
                     raise E_syntax
-                tclist[-1]['input'] = f[0]
+                input_spec = {
+                    'name': f.pop(0),
+                    'format': None,
+                    'options': [],
+                }
+                while len(f):
+                    if len(f) < 2:
+                        # Always needs <key> <value>
+                        raise E_syntax
+                    a, b = f[:2]
+                    f = f[2:]
+                    if a == 'format':
+                        input_spec['format'] = b
+                    elif a == 'option':
+                        input_spec['options'].append(b)
+                    else:
+                        raise E_syntax
+                tclist[-1]['input'] = input_spec
             elif key == 'output':
                 op_spec = {
                     'pd': f.pop(0),
@@ -229,12 +253,9 @@ def get_tests(testnames):
 def diff_text(f1, f2):
     t1 = open(f1).readlines()
     t2 = open(f2).readlines()
-    diff = []
-    d = Differ()
-    for line in d.compare(t1, t2):
-        if line[:2] in ('- ', '+ '):
-            diff.append(line.strip())
-
+    diff = list(unified_diff(t1, t2))
+    diff = diff[2:] # Strip two from/to filename lines with "+++"/"---".
+    diff = [d.strip() for d in diff if d[0] in ('+', '-')]
     return diff
 
 
@@ -319,7 +340,17 @@ def run_tests(tests, fix=False):
                         args.extend(['-p', "%s=%d" % (label, channel)])
                     for option, value in spd['options']:
                         args.extend(['-o', "%s=%s" % (option, value)])
-                args.extend(['-i', os.path.join(dumps_dir, tc['input'])])
+                    for label, initial_pin in spd['initial_pins']:
+                        args.extend(['-N', "%s=%d" % (label, initial_pin)])
+                # Setup input spec for this test (optional format spec).
+                in_spec = tc['input']
+                infile = os.path.join(dumps_dir, in_spec['name'])
+                args.extend(['-i', infile])
+                if in_spec['format']:
+                    args.extend(['-I', in_spec['format']])
+                    for opt in in_spec['options']:
+                        args.extend(['-I', opt])
+                # Setup output spec for this test.
                 for op in tc['output']:
                     name = "%s/%s/%s" % (pd, tc['name'], op['type'])
                     opargs = ['-O', "%s:%s" % (op['pd'], op['type'])]
@@ -485,7 +516,9 @@ def show_tests(tests):
                     for label, channel in pd['channels']:
                         print("    Channel %s=%d" % (label, channel))
                     for option, value in pd['options']:
-                        print("    Option %s=%d" % (option, value))
+                        print("    Option %s=%s" % (option, value))
+                    for label, initial_pin in pd['initial_pins']:
+                        print("    Initial pin %s=%d" % (label, initial_pin))
                 if 'stack' in tc:
                     print("  Stack: %s" % ' '.join(tc['stack']))
                 print("  Input: %s" % tc['input'])
@@ -561,7 +594,7 @@ ret = 0
 try:
     if args:
         testlist = get_tests(args)
-    elif opt_all:
+    elif opt_all or opt_list:
         testlist = get_tests(os.listdir(tests_dir))
     else:
         usage("Specify either -a or tests.")
@@ -591,4 +624,3 @@ except Exception as e:
         raise
 
 sys.exit(ret)
-