<commit_before># ***************************************************************************
# *   Copyright (c) 2020 <NAME> <<EMAIL>>              *
# *                                                                         *
# *   This file is part of the FreeCAD CAx development system.              *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   FreeCAD is distributed in the hope that it will be useful,            *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with FreeCAD; if not, write to the Free Software        *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************

__title__ = "Open files FEM unit tests"
__author__ = "<NAME>"
__url__ = "http://www.freecadweb.org"

import unittest
import tempfile
from os.path import join

import FreeCAD

from . import support_utils as testtools
from .support_utils import fcc_print


"""
# TODO: separate unit test:
# std document name of object ==  obj type
ATM:
for elmer equation obj: name != proxy type
material solid and material fluid obj: name != proxy type

# in addition for FeaturePythons
# std document name of object ==  the class name (for for femsolver obj)
# all femsolver objects class name is Proxy
"""


class TestObjectOpen(unittest.TestCase):
    fcc_print("import TestObjectOpen")

    # ********************************************************************************************
    def setUp(
        self
    ):
        # setUp is executed before every test
        doc_name = self.__class__.__name__
        self.document = FreeCAD.newDocument(doc_name)

        self.test_file_dir = join(
            testtools.get_fem_test_home_dir(),
            "open"
        )

    def test_00print(
        self
    ):
        fcc_print("\n{0}\n{1} run FEM TestObjectOpen tests {2}\n{0}".format(
            100 * "*",
            10 * "*",
            60 * "*"
        ))

    # ********************************************************************************************
    def test_femobjects_open_head(
        self
    ):
        # FreeCAD --run-test "femtest.app.test_object.TestObjectCreate.test_femobjects_make"
        fcc_print("load master head document objects")
        from .test_object import create_all_fem_objects_doc
        doc = create_all_fem_objects_doc(self.document)

        # todo save and load the document
        file_path = join(tempfile.gettempdir(), "all_objects_head.FCStd")
        doc.saveAs(file_path)
        self.document = FreeCAD.open(file_path)

        from femtools.femutils import type_of_obj

        # C++ objects
        self.assertEqual(
            "Fem::FemAnalysis",
            type_of_obj(doc.Analysis)
        )
        # TODO other C++ objects and view provider
        # Is just checking the type sufficient?
        # If there is a type there is at least a object with correct type ;-)

        # FeaturePythons
        # objects
        self.compare_feature_pythons_class_app(doc)
        # view provider
        self.compare_feature_pythons_class_gui(doc)

    # ********************************************************************************************
    def compare_feature_pythons_class_app(
        self,
        doc
    ):
        # see comments at file end, the code was created by some python code
        """
        # see code lines after comment block for the smarter version
        # but this makes it easy to understand what is happening
        self.assertEqual(
            "<class 'femobjects.constraint_bodyheatsource.ConstraintBodyHeatSource'>",
            str(doc.ConstraintBodyHeatSource.Proxy.__class__)
        )
        """
        from femobjects._FemConstraintBodyHeatSource import Proxy
        self.assertEqual(
            Proxy,
            doc.ConstraintBodyHeatSource.Proxy.__class__
        )

        from femobjects._FemConstraintElectrostaticPotential import Proxy
        self.assertEqual(
            Proxy,<commit_msg>FEM: unit test open doc obj, small improvements<commit_after># ***************************************************************************
# *   Copyright (c) 2020 <NAME> <<EMAIL>>              *
# *                                                                         *
# *   This file is part of the FreeCAD CAx development system.              *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   FreeCAD is distributed in the hope that it will be useful,            *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with FreeCAD; if not, write to the Free Software        *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************

__title__ = "Open files FEM unit tests"
__author__ = "<NAME>"
__url__ = "http://www.freecadweb.org"

import unittest
import tempfile
from os.path import join

import FreeCAD

from . import support_utils as testtools
from .support_utils import fcc_print


"""
# TODO: separate unit test:
# std document name of object ==  obj type
ATM:
for elmer equation obj: name != proxy type
material solid and material fluid obj: name != proxy type

# in addition for FeaturePythons
# std document name of object ==  the class name (for for femsolver obj)
# all femsolver objects class name is Proxy
"""


class TestObjectOpen(unittest.TestCase):
    fcc_print("import TestObjectOpen")

    # ********************************************************************************************
    def setUp(
        self
    ):
        # setUp is executed before every test
        doc_name = self.__class__.__name__
        self.document = FreeCAD.newDocument(doc_name)

        self.test_file_dir = join(
            testtools.get_fem_test_home_dir(),
            "open"
        )

    def test_00print(
        self
    ):
        fcc_print("\n{0}\n{1} run FEM TestObjectOpen tests {2}\n{0}".format(
            100 * "*",
            10 * "*",
            60 * "*"
        ))

    # ********************************************************************************************
    def test_femobjects_open_head(
        self
    ):
        fcc_print("load master head document objects")
        
        # get a document with all FEM objects
        from .test_object import create_all_fem_objects_doc
        doc = create_all_fem_objects_doc(self.document)

        # save and load the document
        file_path = join(tempfile.gettempdir(), "all_objects_head.FCStd")
        doc.saveAs(file_path)
        self.document = FreeCAD.open(file_path)

        # C++ objects
        self.compare_cpp_objs(doc)
        # FeaturePythons objects
        self.compare_feature_pythons_class_app(doc)
        # FeaturePythons view provider
        self.compare_feature_pythons_class_gui(doc)

    # ********************************************************************************************
    def compare_cpp_objs(
        self,
        doc
    ):
        from femtools.femutils import type_of_obj

        self.assertEqual(
            "Fem::FemAnalysis",
            type_of_obj(doc.Analysis)
        )
        # TODO other C++ objects and view provider
        # Is just checking the type sufficient?
        # If there is a type there is at least a object with correct type ;-)

    # ********************************************************************************************
    def compare_feature_pythons_class_app(
        self,
        doc
    ):
        # see comments at file end, the code was created by some python code
        """
        # see code lines after comment block for the smarter version
        # but this makes it easy to understand what is happening
        self.assertEqual(
            "<class 'femobjects.constraint_bodyheatsource.ConstraintBodyHeatSource'>",
            str(doc.ConstraintBodyHeatSource.Proxy.__class__)
        )
        """
        from femobjects._FemConstraintBodyHeatSource import Proxy
        self.assertEqual(
            Proxy,
            doc.ConstraintBodyHeatSource.Proxy.__class__
        )

        from femobjects._FemConstraintElectrostaticPotential import Proxy
        self.assertEqual(
            Proxy,
{'fraction_non_alphanumeric': 0.1562530556370392, 'fraction_numerical': 0.006257944656301946, 'mean_word_length': 1.9357060849598162, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5984479', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2381, 'n_words': 767}