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, validate: true, evaluateFormulae);

            // Also load from template and save it again - but not necessary to test against reference file
            // We're just testing that it can save.
            using (var ms = new MemoryStream())
                using (var wb = XLWorkbook.OpenFromTemplate(filePath1))
                    wb.SaveAs(ms, validate: true, evaluateFormulae);

            if (CompareWithResources)
            {
                string resourcePath = "Examples." + filePartName.Replace('\\', '.').TrimStart('.');
                using (var streamExpected = _extractor.ReadFileFromResourceToStream(resourcePath))
                    using (var streamActual = File.OpenRead(filePath2))
                    {
                        var success          = ExcelDocsComparer.Compare(streamActual, streamExpected, out string 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);
                    }
            }
        }
Exemplo n.º 2
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 ReadFileFromResourceToStream(string fileName)
        {
            string _nameResFile = AssemblyName + ResourceFilePath + fileName;
            Stream _stream      = Assembly.GetManifestResourceStream(_nameResFile);

            #region Not found

            if (_stream is null)
            {
                #region Get from base extractor

                if (!(m_baseExtractor is null))
                {
                    return(m_baseExtractor.ReadFileFromResourceToStream(fileName));
                }

                #endregion Get from base extractor

                throw new ArgumentException("Can't find resource file " + _nameResFile, nameof(fileName));
            }

            #endregion Not found

            return(_stream);
        }