# -*- coding: utf-8 -*- """Tests for the project and solution file reader classes.""" from __future__ import unicode_literals import io import unittest from vstools import readers from vstools import resources from tests import test_lib class FileReaderTest(test_lib.BaseTestCase): """File reader tests.""" # pylint: disable=protected-access def testInitialize(self): """Tests the __init__ function.""" file_reader = readers.FileReader() self.assertIsNotNone(file_reader) @test_lib.skipUnlessHasTestFile(['2008.sln']) def testReadBinaryData(self): """Tests the _ReadBinaryData function.""" file_reader = readers.FileReader() path = self._GetTestFilePath(['2008.sln']) file_reader.Open(path) binary_data = file_reader._ReadBinaryData(5) self.assertEqual(binary_data, b'\xef\xbb\xbf\r\n') file_reader.Close() @test_lib.skipUnlessHasTestFile(['2008.vcproj']) def testReadLine(self): """Tests the _ReadLine function.""" file_reader = readers.FileReader() path = self._GetTestFilePath(['2008.vcproj']) file_reader.Open(path) file_reader._ReadLine() line_look_ahead = file_reader._ReadLine(look_ahead=True) line = file_reader._ReadLine() self.assertEqual(line, line_look_ahead) file_reader.Close() @test_lib.skipUnlessHasTestFile(['2008.vcproj']) def testOpenClose(self): """Tests the Open and Close functions.""" file_reader = readers.FileReader() path = self._GetTestFilePath(['2008.vcproj']) file_reader.Open(path) file_reader.Close() class VS2008ProjectFileReaderTest(test_lib.BaseTestCase): """Visual Studio 2008 project file reader tests.""" # pylint: disable=protected-access # TODO: add tests for _ParseConfigurationOption function. def testParseConfigurationOption(self): """Tests the _ParseConfigurationOption function.""" project_configuration = resources.VSProjectConfiguration() file_reader = readers.VS2008ProjectFileReader() line = 'Optimization="0"' file_reader._ParseConfigurationOption( project_configuration, 'Optimization', 'optimization', line) self.assertEqual(project_configuration.optimization, '0') def testParseConfigurationOptions(self): """Tests the _ParseConfigurationOptions function.""" project_configuration = resources.VSProjectConfiguration() file_reader = readers.VS2008ProjectFileReader() configuration_options = {'Optimization': 'optimization'} line = 'Optimization="0"' file_reader._ParseConfigurationOptions( project_configuration, configuration_options, line) self.assertEqual(project_configuration.optimization, '0') def testReadConfiguration(self): """Tests the _ReadConfiguration function.""" test_data = [ ' Name="Release|Win32"', (' OutputDirectory=' '"$(SolutionDir)$(ConfigurationName)"'), ' IntermediateDirectory="$(ConfigurationName)"', ' ConfigurationType="2"', ' CharacterSet="1"', ' >', ' '] file_reader = readers.VS2008ProjectFileReader() file_data = '\n'.join(test_data).encode('utf-8') file_reader._file = io.BytesIO(file_data) project_configuration = file_reader._ReadConfiguration('', ' ', ' ', ' ', ' ', ' '] project_information = resources.VSProjectInformation() file_reader = readers.VS2008ProjectFileReader() file_data = '\n'.join(test_data).encode('utf-8') file_reader._file = io.BytesIO(file_data) file_reader._ReadFiles(project_information) def testReadProjectInformation(self): """Tests the _ReadProjectInformation function.""" test_data = [ ' Name="libcerror"', ' ProjectGUID="{C42F5217-137D-4F10-9D6A-3C6D44E43453}"', ' RootNamespace="libcerror"', ' TargetFrameworkVersion="131072"', ' >'] project_information = resources.VSProjectInformation() file_reader = readers.VS2008ProjectFileReader() file_data = '\n'.join(test_data).encode('utf-8') file_reader._file = io.BytesIO(file_data) file_reader._ReadProjectInformation(project_information) def testReadHeader(self): """Tests the ReadHeader function.""" test_data = [ '', '