Beispiel #1
0
        public static void RunTestExample <T>(string filePartName)
            where T : IXLExample, new()
        {
            // Make sure tests run on a deterministic culture
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            var example = new T();

            string[] pathParts = filePartName.Split(new char[] { '\\' });
            string   filePath1 = Path.Combine(new List <string>()
            {
                TestsExampleOutputDirectory
            }.Concat(pathParts).ToArray());

            var extension = Path.GetExtension(filePath1);
            var directory = Path.GetDirectoryName(filePath1);

            var fileName = Path.GetFileNameWithoutExtension(filePath1);

            fileName += ActualTestResultPostFix;
            fileName  = Path.ChangeExtension(fileName, extension);

            filePath1 = Path.Combine(directory, "z" + fileName);
            var filePath2 = Path.Combine(directory, fileName);

            //Run test
            example.Create(filePath1);
            new XLWorkbook(filePath1).SaveAs(filePath2, true);
            bool success = true;

#pragma warning disable 162
            try
            {
                //Compare
                // ReSharper disable ConditionIsAlwaysTrueOrFalse
                if (CompareWithResources)
                // ReSharper restore ConditionIsAlwaysTrueOrFalse

                {
                    string resourcePath = filePartName.Replace('\\', '.').TrimStart('.');
                    using (var streamExpected = _extractor.ReadFileFromResToStream(resourcePath))
                        using (var streamActual = File.OpenRead(filePath2))
                        {
                            string message;
                            success = ExcelDocsComparer.Compare(streamActual, streamExpected, TestHelper.IsRunningOnUnix, out message);
                            var formattedMessage =
                                String.Format(
                                    "Actual file '{0}' is different than the expected file '{1}'. The difference is: '{2}'",
                                    filePath2, resourcePath, message);

                            Assert.IsTrue(success, formattedMessage);
                        }
                }
            }
            finally
            {
                //if (success && File.Exists(filePath)) File.Delete(filePath);
            }
#pragma warning restore 162
        }
Beispiel #2
0
        public static void RunTestExample <T>(string filePartName, bool evaluateFormulae = false)
            where T : IXLExample, new()
        {
            // Make sure tests run on a deterministic culture
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            var example = new T();

            string[] pathParts = filePartName.Split(new char[] { '\\' });
            string   filePath1 = Path.Combine(new List <string>()
            {
                ExampleTestsOutputDirectory
            }.Concat(pathParts).ToArray());

            var extension = Path.GetExtension(filePath1);
            var directory = Path.GetDirectoryName(filePath1);

            var fileName = Path.GetFileNameWithoutExtension(filePath1);

            fileName += ActualTestResultPostFix;
            fileName  = Path.ChangeExtension(fileName, extension);

            filePath1 = Path.Combine(directory, "z" + fileName);
            var filePath2 = Path.Combine(directory, fileName);

            //Run test
            example.Create(filePath1);
            using (var wb = new XLWorkbook(filePath1))
                wb.SaveAs(filePath2, true, evaluateFormulae);

            if (CompareWithResources)
            {
                string resourcePath = "Examples." + filePartName.Replace('\\', '.').TrimStart('.');
                using (var streamExpected = _extractor.ReadFileFromResToStream(resourcePath))
                    using (var streamActual = File.OpenRead(filePath2))
                    {
                        string message;
                        var    success          = ExcelDocsComparer.Compare(streamActual, streamExpected, out message);
                        var    formattedMessage =
                            String.Format(
                                "Actual file '{0}' is different than the expected file '{1}'. The difference is: '{2}'",
                                filePath2, resourcePath, message);

                        Assert.IsTrue(success, formattedMessage);
                    }
            }
        }
Beispiel #3
0
        public static void LoadFile(string filePartName)
        {
            var extractor = new ResourceFileExtractor(null, ".Resource.");

            string resourcePath = filePartName.Replace('\\', '.').TrimStart('.');

            using (var stream = extractor.ReadFileFromResToStream(resourcePath))
            {
                var wb = new XLWorkbook(stream);
                wb.Dispose();
            }
        }
Beispiel #4
0
        /// <summary>
        /// Read file in current assembly by specific file name
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        /// <exception cref="ApplicationException"><c>ApplicationException</c>.</exception>
        public Stream ReadFileFromResToStream(string fileName)
        {
            string _nameResFile = AssemblyName + m_resourceFilePath + fileName;
            Stream _stream      = Assembly.GetManifestResourceStream(_nameResFile);

            #region Not found
            if (ReferenceEquals(_stream, null))
            {
                #region Get from base extractor
                if (!ReferenceEquals(m_baseExtractor, null))
                {
                    return(m_baseExtractor.ReadFileFromResToStream(fileName));
                }
                #endregion
                throw new ApplicationException("Can't find resource file " + _nameResFile);
            }
            #endregion
            return(_stream);
        }
Beispiel #5
0
        public static Stream GetStreamFromResource(string resourcePath)
        {
            var extractor = new ResourceFileExtractor(null, ".Resource.");

            return(extractor.ReadFileFromResToStream(resourcePath));
        }