示例#1
0
        private byte[] GenerateData()
        {
            DataSet dataSet;

            using (FileStream stream = new FileStream(this.InputFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                dataSet = EPPlusHelper.ReadExcelToDataSet(stream);
            }
            ProjectItems collection = this.ProjectItem.Collection;

            // Generate code
            StringBuilder builder = new StringBuilder();

            builder.AppendLine(string.Format(System.Globalization.CultureInfo.InvariantCulture, Parameters.DO_NOT_MODIFY, this.GetType().Name));
            builder.AppendLine($"using System;");
            builder.AppendLine($"using System.Collections.Generic;");
            builder.AppendLine($"using System.Globalization;");
            builder.AppendLine($"using System.Reflection;");
            builder.AppendLine($"using Xunit.Sdk;");
            builder.AppendLine($"");
            builder.AppendLine($"namespace {this.CodeNamespace}");
            builder.AppendLine($"{{");
            builder.AppendLine($"    public class ExcelTestDataAttribute : DataAttribute");
            builder.AppendLine($"    {{");
            builder.AppendLine($"        public override IEnumerable<object[]> GetData(MethodInfo testMethod)");
            builder.AppendLine($"        {{");
            builder.AppendLine($"            ParameterInfo[] parameters = testMethod.GetParameters();");
            builder.AppendLine($"            switch ($\"{{testMethod.DeclaringType.Name}}\")");
            builder.AppendLine($"            {{");
            foreach (DataTable table in dataSet.Tables)
            {
                builder.AppendLine($"                case \"{table.TableName}\":");

                ProjectItem referenceItem = null;
                foreach (ProjectItem item in collection)
                {
                    if (item.Name.Equals(table.TableName + ".cs", StringComparison.OrdinalIgnoreCase))
                    {
                        referenceItem = item;
                        break;
                    }
                }
                if (referenceItem != null)
                {
                    FileCodeModel model = referenceItem.FileCodeModel;
                    builder.AppendLine($"                    switch ($\"{{testMethod.Name}}\")");
                    builder.AppendLine($"                    {{");
                    foreach (var group in table.Rows.Cast <DataRow>().GroupBy <DataRow, string>(dr => dr["Method"].ToString()))
                    {
                        string       method    = group.Key;
                        CodeFunction eFunction = this.FindMethod(model, table.TableName, method);
                        if (eFunction == null)
                        {
                            continue;
                        }
                        string[] types = eFunction.Parameters.Cast <CodeParameter>().Select(p => p.Type.AsFullName).ToArray();
                        if (!types.All(p => ConvertFuncs.ContainsKey(p)))
                        {
                            continue;
                        }

                        builder.AppendLine($"                        case \"{method}\":");
                        foreach (DataRow row in group)
                        {
                            builder.AppendLine($"                            yield return new object[]");
                            builder.AppendLine($"                            {{");
                            for (int i = 0; i < types.Length; i++)
                            {
                                string text = (string)row[i + 1];
                                builder.AppendLine($"                                {ConvertFuncs[types[i]](text)},");
                            }
                            builder.AppendLine($"                            }};");
                        }
                        builder.AppendLine($"                            yield break;");
                    }
                    builder.AppendLine($"                        default:");
                    builder.AppendLine($"                            yield break;");
                    builder.AppendLine($"                    }}");
                }
                else
                {
                    builder.AppendLine($"                    yield break;");
                }
            }
            builder.AppendLine($"                default:");
            builder.AppendLine($"                    yield break;");
            builder.AppendLine($"            }}");
            builder.AppendLine($"        }}");
            // End Method.

            builder.AppendLine($"    }}");
            builder.AppendLine($"}}");
            builder.AppendLine($"");

            string code = builder.ToString();

            return(Encoding.UTF8.GetBytes(code));
        }