Module nif_test
[hide private]
[frames] | no frames]

Source Code for Module nif_test

  1  """Automated tests for the blender nif scripts.""" 
  2   
  3  # ***** BEGIN LICENSE BLOCK ***** 
  4  #  
  5  # BSD License 
  6  #  
  7  # Copyright (c) 2005-2009, NIF File Format Library and Tools 
  8  # All rights reserved. 
  9  #  
 10  # Redistribution and use in source and binary forms, with or without 
 11  # modification, are permitted provided that the following conditions 
 12  # are met: 
 13  # 1. Redistributions of source code must retain the above copyright 
 14  #    notice, this list of conditions and the following disclaimer. 
 15  # 2. Redistributions in binary form must reproduce the above copyright 
 16  #    notice, this list of conditions and the following disclaimer in the 
 17  #    documentation and/or other materials provided with the distribution. 
 18  # 3. The name of the NIF File Format Library and Tools project may not be 
 19  #    used to endorse or promote products derived from this software 
 20  #    without specific prior written permission. 
 21  #  
 22  # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 
 23  # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 24  # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 25  # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 
 26  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
 27  # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 28  # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 29  # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 30  # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
 31  # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 32  # 
 33  # ***** END LICENCE BLOCK ***** 
 34   
 35  import logging 
 36   
 37  import Blender 
 38  from import_nif import NifImport 
 39  from export_nif import NifExport 
 40  from nif_common import NifConfig 
 41   
42 -class TestSuite:
43 """A test suite class. 44 45 @ivar scene: The Blender scene for this test suite. 46 @type scene: Blender.Scene.Scene 47 @ivar layer: The current Blender layer in the scene. 48 @type layer: C{int} 49 """
50 - def __init__(self, name):
51 """Initialize a new test suite with given name. 52 53 @param name: The name of the test (will be used as name of the scene 54 for this test). 55 @type name: C{str} 56 """ 57 self.logger = logging.getLogger("niftools.blender.test") 58 self.scene = Blender.Scene.New(name) # new scene 59 self.layer = 1 # current layer 60 61 # set active scene 62 self.scene.makeCurrent() 63 # set active layer 64 self.scene.setLayers([self.layer])
65
66 - def test(self, filename=None, config=None, selection=None, next_layer=None):
67 """Run given test, and increase layer after export. 68 69 @param filename: The name of the file to test. 70 @type filename: C{str} 71 @param config: Configuration options that differ from their default 72 values. If the dictionary has the key EXPORT_VERSION then the 73 selection is exported, otherwise a file is imported. Optional. 74 @type config: C{dict} 75 @param selection: List of names of objects to select before running 76 the test. Optional. 77 @type selection: C{list} of C{str} 78 @param next_layer: Whether or not to advance a layer after the test. 79 If C{None} (default) then a layer is advanced only after export. 80 @type next_layer: C{bool} or C{NoneType} 81 @return: The import or export object. 82 @rtype: L{NifImport} or L{NifExport} 83 """ 84 85 if filename is None: 86 raise ValueError("A test must specify a filename.") 87 if config is None: 88 config = {} 89 if selection is None: 90 selection = [] 91 92 # select objects 93 self.scene.objects.selected = [ 94 ob for ob in self.scene.objects if ob.name in selection] 95 96 # set script configuration 97 finalconfig = dict(**NifConfig.DEFAULTS) 98 logging.getLogger("niftools").setLevel(logging.DEBUG) 99 logging.getLogger("pyffi").setLevel(logging.WARNING) 100 101 for key, value in config.items(): 102 finalconfig[key] = value 103 104 # run test 105 if 'EXPORT_VERSION' in config: 106 # export the imported files 107 self.logger.info("Exporting %s" % filename) 108 109 finalconfig["EXPORT_FILE"] = filename 110 result = NifExport(**finalconfig) 111 112 # increment active layer for next import 113 # different tests are put into different blender layers, 114 # so the results can be easily visually inspected 115 if next_layer in (None, True): 116 self.layer += 1 117 self.scene.setLayers([self.layer]) 118 119 # return test result 120 return result 121 else: 122 self.logger.info("Importing %s" % filename) 123 124 # import file and return test result 125 finalconfig["IMPORT_FILE"] = filename 126 result = NifImport(**finalconfig) 127 128 if next_layer is True: 129 self.layer += 1 130 self.scene.setLayers([self.layer]) 131 132 return result
133
134 - def run(self):
135 """Run the test suite. Override.""" 136 raise NotImplementedError
137
138 -def runtest(directory, files):
139 """Test the specified files. 140 141 @deprecated: Derive your tests from the L{TestSuite} class instead! 142 @param directory: Folder where the test nif files reside. 143 144 @param files: A list of all files to test. Each entry in the list 145 is a tuple containing the filename, a dictionary of config options 146 , and a list of names of 147 objects to select before running the import. 148 """ 149 scene = Blender.Scene.GetCurrent() # current scene 150 layer = 1 151 152 for filename, filecfg, selection in files: 153 # select objects 154 scene.objects.selected = [ 155 ob for ob in scene.objects if ob.name in selection] 156 157 # set script configuration 158 config = dict(**NifConfig.DEFAULTS) 159 for key, value in filecfg.items(): 160 config[key] = value 161 logger = logging.getLogger("niftools.blender.test") 162 logging.getLogger("niftools").setLevel(logging.DEBUG) 163 logging.getLogger("pyffi").setLevel(logging.WARNING) 164 165 # run test 166 if 'EXPORT_VERSION' in filecfg: 167 # export the imported files 168 logger.info("Exporting %s" % filename) 169 170 config["EXPORT_FILE"] = "%s/%s" % (directory, filename) 171 NifExport(**config) 172 173 # increment active layer for next import 174 # different tests are put into different blender layers, 175 # so the results can be easily visually inspected 176 layer += 1 177 scene.setLayers([layer]) 178 else: 179 logger.info("Importing %s" % filename) 180 181 config["IMPORT_FILE"] = "%s/%s" % (directory, filename) 182 183 # import <filename> 184 NifImport(**config) 185 186 # deselect everything 187 scene.objects.selected = [] 188 # reset active layer 189 scene.setLayers([1])
190