# Path: vasppy/outcar.py
# def final_energy_from_outcar( filename='OUTCAR' ):
#     """
#     Finds and returns the energy from a VASP OUTCAR file, by searching for the last `energy(sigma->0)` entry.
# 
#     Args:
#         filename (Str, optional): OUTCAR filename. Defaults to 'OUTCAR'.
# 
#     Returns:
#         (Float): The last energy read from the OUTCAR file.
#     """
#     with open( filename ) as f:
#         outcar = f.read()
#     energy_re = re.compile( "energy\(sigma->0\) =\s+([-\d\.]+)" )
#     energy = float( energy_re.findall( outcar )[-1] )
#     return energy
# 
# def potcar_eatom_list_from_outcar( filename='OUTCAR' ):
#     """
#     Returns a list of EATOM values for the pseudopotentials used.
# 
#     Args:
#         filename (Str, optional): OUTCAR filename. Defaults to 'OUTCAR'.
# 
#     Returns:
#         (List(Float)): A list of EATOM values, in the order they appear in the OUTCAR.
#     """
#     with open( filename ) as f:
#         outcar = f.read()
#     eatom_re = re.compile( "energy of atom\s+\d+\s+EATOM=\s*([-\d\.]+)" )
#     eatom = [ float( e ) for e in eatom_re.findall( outcar ) ]
#     return eatom


import unittest
import numpy as np
from unittest.mock import Mock, patch, mock_open, call
from vasppy.outcar import final_energy_from_outcar, potcar_eatom_list_from_outcar



class OutcarTestCase( unittest.TestCase ):

    def test_final_energy_from_outcar( self ):
        example_file = """energy without entropy =    -2997.63294724  energy(sigma->0) =    -2997.63294724\n
                       energy  without entropy=    -2997.63294724  energy(sigma->0) =    -2997.63294724\n
                       energy without entropy =    -2997.63289805  energy(sigma->0) =    -2997.63289805\n"""    
        with patch( 'builtins.open', mock_open( read_data=example_file ), create=True ) as m:
            self.assertEqual( final_energy_from_outcar(), -2997.63289805 )

    def test_final_energy_from_outcar_with_filename(self):
        example_file = """energy without entropy =    -2997.63294724  energy(sigma->0) =    -2997.63294724\n
                       energy  without entropy=    -2997.63294724  energy(sigma->0) =    -2997.63294724\n
                       energy without entropy =    -2997.63289805  energy(sigma->0) =    -2997.63289805\n"""    
        with patch('builtins.open', mock_open(read_data=example_file), create=True) as m:

# Path: vasppy/outcar.py
# def final_energy_from_outcar( filename='OUTCAR' ):
#     """
#     Finds and returns the energy from a VASP OUTCAR file, by searching for the last `energy(sigma->0)` entry.
# 
#     Args:
#         filename (Str, optional): OUTCAR filename. Defaults to 'OUTCAR'.
# 
#     Returns:
#         (Float): The last energy read from the OUTCAR file.
#     """
#     with open( filename ) as f:
#         outcar = f.read()
#     energy_re = re.compile( "energy\(sigma->0\) =\s+([-\d\.]+)" )
#     energy = float( energy_re.findall( outcar )[-1] )
#     return energy
# 
# def potcar_eatom_list_from_outcar( filename='OUTCAR' ):
#     """
#     Returns a list of EATOM values for the pseudopotentials used.
# 
#     Args:
#         filename (Str, optional): OUTCAR filename. Defaults to 'OUTCAR'.
# 
#     Returns:
#         (List(Float)): A list of EATOM values, in the order they appear in the OUTCAR.
#     """
#     with open( filename ) as f:
#         outcar = f.read()
#     eatom_re = re.compile( "energy of atom\s+\d+\s+EATOM=\s*([-\d\.]+)" )
#     eatom = [ float( e ) for e in eatom_re.findall( outcar ) ]
#     return eatom


# Path: tests/test_outcar.py
import unittest
import numpy as np
from unittest.mock import Mock, patch, mock_open, call
from vasppy.outcar import final_energy_from_outcar, potcar_eatom_list_from_outcar




class OutcarTestCase( unittest.TestCase ):

    def test_final_energy_from_outcar( self ):
        example_file = """energy without entropy =    -2997.63294724  energy(sigma->0) =    -2997.63294724\n
                       energy  without entropy=    -2997.63294724  energy(sigma->0) =    -2997.63294724\n
                       energy without entropy =    -2997.63289805  energy(sigma->0) =    -2997.63289805\n"""    
        with patch( 'builtins.open', mock_open( read_data=example_file ), create=True ) as m:
            self.assertEqual( final_energy_from_outcar(), -2997.63289805 )

    def test_final_energy_from_outcar_with_filename(self):
        example_file = """energy without entropy =    -2997.63294724  energy(sigma->0) =    -2997.63294724\n
                       energy  without entropy=    -2997.63294724  energy(sigma->0) =    -2997.63294724\n
                       energy without entropy =    -2997.63289805  energy(sigma->0) =    -2997.63289805\n"""    
        with patch('builtins.open', mock_open(read_data=example_file), create=True) as m:

            final_energy_from_outcar(filename='foo')