[cig-commits] r13148 - cs/cigma/trunk/tests/libcigma

luis at geodynamics.org luis at geodynamics.org
Wed Oct 29 15:10:48 PDT 2008


Author: luis
Date: 2008-10-29 15:10:48 -0700 (Wed, 29 Oct 2008)
New Revision: 13148

Added:
   cs/cigma/trunk/tests/libcigma/skel.py
Log:
Template script for generating test fixtures

This python script takes a single command line argument (the test
prefix) and generates the appropriate class definition for use
with cppunit (see SkelTest.{h,cpp} for the basic template).

Added: cs/cigma/trunk/tests/libcigma/skel.py
===================================================================
--- cs/cigma/trunk/tests/libcigma/skel.py	                        (rev 0)
+++ cs/cigma/trunk/tests/libcigma/skel.py	2008-10-29 22:10:48 UTC (rev 13148)
@@ -0,0 +1,120 @@
+#!/usr/bin/env python
+from __future__ import with_statement
+
+import os, sys, re, operator
+
+def camel_split(name, strict=True):
+    """
+    Split a CamelCase name into its components. Retains capitalization
+    information where it isn't used to separate words. Retains leading
+    and trailing underscores.
+
+    See http://aspn.activestate.com/ASPN/Mail/Message/python-list/1550106
+    """
+    leading_bars, mid, trailing_bars = re.match(r'(_+)?(.*?)(_+)?$', name).groups()
+
+    if not mid:
+        return [name] if name else []
+    elif mid.isupper():
+        s = re.split('(_)_*', mid)
+    elif '_' not in mid and name.islower():
+        s = [mid]
+    else:
+        r = r'''^[lo][lo\d]*(?=[UP]|$)
+                |[UP][UP\d]*(?=_[UP\d])
+                |[UP][lo][lo\d]*
+                |[UP][UP\d]*(?=[UP][lo]|$)
+                |_'''
+        r = r.replace('UP', 'A-Z')
+        r = r.replace('lo', 'a-z')
+        pat = re.compile(r, re.X)
+        if strict:
+            s = pat.findall(mid)
+        else:
+            s = reduce(operator.add, map(pat.findall, re.split('(_)_*', mid)))
+
+    def norm_case(t):
+        previous, x = t
+        if (re.search('r[A-Z]{2,}', x)):
+            return x
+        else:
+            return x.lower()
+    def xwindow(iter, n=2, s=1):
+        """
+        Move an `n'-item (default 2) windows `s' steps (default 1)
+        at a time over `iter'
+        """
+        assert (n >= s)
+        last = []
+        for elt in iter:
+            last.append(elt)
+            if len(last) == n:
+                yield tuple(last)
+                last = last[s:]
+
+    s[1:]  = map(norm_case, xwindow(s))
+    s[0]   = (leading_bars or '') + s[0]
+    s[-1] += (trailing_bars or '')
+
+    if reduce(operator.add, map(len, s), 0) != len(name):
+        raise ValueError("Illegal CamelCase name supplied: %s" % name)
+
+    # filter out lonely _'s
+    s = filter('_'.__ne__, s)
+
+    return s
+
+
+def macro(name):
+    ns = camel_split(name)
+    return '_'.join(n.upper() for n in ns)
+
+
+def skel_copy(src, dest, verbose=False):
+
+    if verbose:
+        print 'Copying %s -> %s' % (src, dest)
+
+    with open(src,'r') as fp:
+        contents = fp.read()
+        classname = os.path.splitext(dest)[0]
+        macroname = macro(classname.replace('_',''))
+        if verbose:
+            print '\tReplacing SkelTest with', classname
+        contents = contents.replace('SkelTest', classname)
+        if verbose:
+            print '\tReplacing SKEL_TEST with', macroname
+        contents = contents.replace('SKEL_TEST', macroname)
+
+    with open(dest, 'w') as fp:
+        fp.write(contents)
+
+    return
+    
+
+def main():
+    
+    prefix = None
+    if len(sys.argv) > 1:
+        prefix = sys.argv[1]
+    
+    if not prefix:
+        print "Usage: %s <TestClassName>" % sys.argv[0]
+        sys.exit(1)
+    
+    header = prefix + ".h"
+    if os.path.exists(header):
+        print "Error: Header file %s already exists!" % header
+        sys.exit(2)
+
+    source = prefix + ".cpp"
+    if os.path.exists(source):
+        print "Error: Source file %s already exists!" % source
+        sys.exit(3)
+
+    skel_copy('SkelTest.h', header, verbose=True)
+    skel_copy('SkelTest.cpp', source, verbose=True)
+
+
+if __name__ == '__main__':
+    main()


Property changes on: cs/cigma/trunk/tests/libcigma/skel.py
___________________________________________________________________
Name: svn:executable
   + *



More information about the CIG-COMMITS mailing list