示例#1
0
def test_inside_quote():
    s = '"ab"de"hi"kl\\"o\\\\"p'
    test = [insideQuote(s, i) for i in range(len(s))]
    #           "     a     b     "      d      e      "     h     i     "      k      l      \      "      o      \      \      "     p
    expected = [
        True,
        True,
        True,
        False,
        False,
        False,
        True,
        True,
        True,
        False,
        False,
        False,
        False,
        False,
        False,
        False,
        False,
        True,
        True,
    ]
    assert test == expected
示例#2
0
def test_import_export():
    for fileList, fileHandler in zip(fileLists, fileHandlers):
        for fileName in fileList:
            elementDictionary1, default1 = fileHandler.fileImporter(fileName)
            exportFileName = os.path.splitext(fileName)[0] + exportEnd
            fileHandler.exportToFile(exportFileName, elementDictionary1, default1)
            with open(exportFileName) as f:
                # Check that lines are not split inside quotes
                for line in f:
                    assert not insideQuote(line, len(line) - 1)

            elementDictionary2, default2 = fileHandler.fileImporter(exportFileName)

            if not elementDictionary1 == elementDictionary2:
                for name in elementDictionary1:
                    e1 = elementDictionary1[name]
                    e2 = elementDictionary2[name]
                    if not e1 == e2:
                        print fileName
                        print name
                        if not e1.isBeamline():
                            assert compareNormalizedElementData(e1, e2)
                        else:
                            for index in range(max(len(e1.data), len(e2.data))):
                                assert compareNormalizedElementData(e1.data[index], e2.data[index])
            assert default1 == default2

            # Test elegant simulation on exported file
            # Choose the beam line with the most elements for testing
            try:
                if os.path.basename(fileName) in excludedList:
                    continue

                longestLength = 0
                longest = None
                for beamline in [el for el in elementDictionary2.values() if el.isBeamline()]:
                    length = beamline.getNumberOfElements()
                    if length > longestLength:
                        longest = beamline
                        longestLength = length
                if fileList == particleFileList:
                    with open(elegantTestFile, 'w') as f:
                        f.write(elegantSimTemplate % (exportFileName, longest.name, sddsFileName))
                    assert subprocess.call(['elegant', elegantTestFile]) == 0
            finally:
                for fileName in glob.glob(os.path.splitext(elegantTestFile)[0] + '.*') \
                        + glob.glob('*.out') \
                        + [exportFileName]:
                    os.remove(fileName)