| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# Copyright (c) 2017, the cclib development team |
| 4 |
# |
| 5 |
# This file is part of cclib (http://cclib.github.io) and is distributed under |
| 6 |
# the terms of the BSD 3-Clause License. |
| 7 |
|
| 8 |
"""Unit tests for XYZ writer.""" |
| 9 |
|
| 10 |
import os |
| 11 |
import unittest |
| 12 |
|
| 13 |
import cclib |
| 14 |
|
| 15 |
|
| 16 |
__filedir__ = os.path.dirname(__file__) |
| 17 |
__filepath__ = os.path.realpath(__filedir__) |
| 18 |
__datadir__ = os.path.join(__filepath__, "..", "..") |
| 19 |
|
| 20 |
|
| 21 |
class XYZWriterTest(unittest.TestCase): |
| 22 |
|
| 23 |
def test_init(self): |
| 24 |
"""Does the class initialize correctly?""" |
| 25 |
fpath = os.path.join(__datadir__, "data/ADF/basicADF2007.01/dvb_gopt.adfout") |
| 26 |
data = cclib.io.ccread(fpath) |
| 27 |
xyz = cclib.io.xyzwriter.XYZ(data) |
| 28 |
|
| 29 |
# The object should keep the ccData instance passed to its constructor. |
| 30 |
self.assertEqual(xyz.ccdata, data) |
| 31 |
|
| 32 |
def test_roundtrip_one(self): |
| 33 |
"""Does a written XYZ file with a single structure match a reference |
| 34 |
output? |
| 35 |
|
| 36 |
Perform a roundtrip test, reading an XYZ file into a ccData |
| 37 |
instance then writing it out again. |
| 38 |
""" |
| 39 |
orig_fpath = os.path.join(__datadir__, "test/bridge/uracil.xyz") |
| 40 |
reader = cclib.io.xyzreader.XYZ(orig_fpath) |
| 41 |
data = reader.parse() |
| 42 |
orig_repr = reader.filecontents |
| 43 |
|
| 44 |
writer = cclib.io.xyzwriter.XYZ(data) |
| 45 |
new_repr = writer.generate_repr() |
| 46 |
|
| 47 |
ref_fpath = os.path.join(__filedir__, "data/uracil_one_ref.xyz") |
| 48 |
with open(ref_fpath) as ref: |
| 49 |
# Compare the contents of the reference file (left) with |
| 50 |
# the string representation generated by the writer |
| 51 |
# (right). |
| 52 |
assert ref.read() == new_repr |
| 53 |
|
| 54 |
def test_roundtrip_two(self): |
| 55 |
"""Does a written XYZ file with two structures match a reference |
| 56 |
output? |
| 57 |
|
| 58 |
Perform a roundtrip test, reading an XYZ file into a ccData |
| 59 |
instance then writing it out again. |
| 60 |
""" |
| 61 |
orig_fpath = os.path.join(__filedir__, "data/uracil_two.xyz") |
| 62 |
reader = cclib.io.xyzreader.XYZ(orig_fpath) |
| 63 |
data = reader.parse() |
| 64 |
orig_repr = reader.filecontents |
| 65 |
|
| 66 |
# If not `allgeom`, only the last structure present in a |
| 67 |
# `ccData` will be written out. |
| 68 |
writer = cclib.io.xyzwriter.XYZ(data, allgeom=True) |
| 69 |
new_repr = writer.generate_repr() |
| 70 |
|
| 71 |
ref_fpath = os.path.join(__filedir__, "data/uracil_two_ref.xyz") |
| 72 |
with open(ref_fpath) as ref: |
| 73 |
# Compare the contents of the reference file (left) with |
| 74 |
# the string representation generated by the writer |
| 75 |
# (right). |
| 76 |
assert ref.read() == new_repr |
| 77 |
|
| 78 |
|
| 79 |
if __name__ == "__main__": |
| 80 |
unittest.main() |