private static string GetCustomProperty(GeneratorWrapper wrapper, string customProperty) { var propertyBuilder = new StringBuilder(); if (string.IsNullOrEmpty(customProperty)) { return(propertyBuilder.ToString()); } var propertyDynamic = DynamicJson.Parse(customProperty); foreach (var property in wrapper.Properties) { if (!propertyDynamic.IsDefined(property.Name) && string.IsNullOrEmpty(property.Default)) { throw new Exception( $"属性{(string.IsNullOrEmpty(property.Description) ? property.Name : property.Description)}未赋值"); } propertyBuilder.AppendLine(string.Format(" var {0} = {2}{1}{2};", property.Name, propertyDynamic[property.Name], property.Type.ToLower() == "string" ? "\"" : "")); } return(propertyBuilder.ToString()); }
public void GetSourceValueThrowsExceptionWithNullContextTest() { var target = new GeneratorWrapper(PropertyExpression.FirstName, PropertyExpression.LastName); Action action = () => target.ReadSourceValue(null); action.ShouldThrow<ArgumentNullException>(); }
public void GetSourceValueReturnsNullWhenPropertyNotFoundTest() { var context = new SlimModel(); var target = new GeneratorWrapper(PropertyExpression.FirstName, PropertyExpression.LastName); var actual = target.ReadSourceValue(context); actual.Should().BeNull(); }
public void GetSourceValueThrowsExceptionWhenNoSourceExpressionProvidedTest() { var context = new Person(); var target = new GeneratorWrapper(PropertyExpression.FirstName, (Regex)null, (Type)null); Action action = () => target.ReadSourceValue(context); action.ShouldThrow<InvalidOperationException>(); }
public void GetSourceValueReturnsNullFromSourcePropertyTest() { var context = new Person(); var target = new GeneratorWrapper(PropertyExpression.FirstName, PropertyExpression.LastName); var actual = target.ReadSourceValue(context); actual.Should().BeNull(); }
public void GetSourceValueReturnsValueFromSourceStringPropertyTest() { var context = new Person { LastName = Guid.NewGuid().ToString() }; var target = new GeneratorWrapper(PropertyExpression.FirstName, PropertyExpression.LastName); var actual = target.ReadSourceValue(context); actual.Should().Be(context.LastName); }
public void GetSourceValueReturnsValueFromSourcePropertyTest(Gender gender, string expected) { var context = new Person { Gender = gender }; var target = new GeneratorWrapper(PropertyExpression.FirstName, PropertyExpression.Gender); var actual = target.ReadSourceValue(context); actual.Should().Be(expected); }
public void IsSupportedReturnsTrueWhenTargetExpressionMatchesReferenceNameTest() { var context = new SlimModel(); var buildChain = new LinkedList<object>(); buildChain.AddFirst(context); var target = new GeneratorWrapper(PropertyExpression.FirstName, typeof(string)); var actual = target.IsSupported(typeof(string), "FirstName", buildChain); actual.Should().BeTrue(); }
public void IsSupportedReturnsFalseWithNullBuildChainTest() { var context = new SlimModel(); var buildChain = new LinkedList<object>(); buildChain.AddFirst(context); var target = new GeneratorWrapper(PropertyExpression.FirstName, typeof(string)); var actual = target.IsSupported(typeof(string), "FirstName", null); actual.Should().BeFalse(); }
public void IsSupportedReturnsFalseForUnsupportedScenariosTest( Type type, string referenceName, Type contextType, bool expected) { var buildChain = new LinkedList<object>(); if (contextType != null) { var context = Activator.CreateInstance(contextType); buildChain.AddFirst(context); } var target = new GeneratorWrapper(PropertyExpression.FirstName, PropertyExpression.Gender); var actual = target.IsSupported(type, referenceName, buildChain); actual.Should().Be(expected); }
public void GetValueThrowsExceptionWithNullExpressionTest() { var context = new Person(); var target = new GeneratorWrapper(PropertyExpression.FirstName, PropertyExpression.FirstName, (Type)null); Action action = () => target.ReadValue(null, context); action.ShouldThrow<ArgumentNullException>(); }
public List <GeneratorWrapper> GetGeneratorWrappers() { var generatorWrappers = new List <GeneratorWrapper>(); var templates = GetTemplates(); foreach (var template in templates) { var generatorWrapper = new GeneratorWrapper(); var fileName = Path.GetFileNameWithoutExtension(template); using (var sr = new StreamReader(template)) { string tempLine = string.Empty; string line; while ((line = sr.ReadLine()) != null) { if (!string.IsNullOrEmpty(tempLine) || Regex.Matches(line, "<%").Count != Regex.Matches(line, "%>").Count) { tempLine += line; if (Regex.Matches(tempLine, "<%").Count == Regex.Matches(tempLine, "%>").Count) { line = tempLine; tempLine = string.Empty; } else { continue; } } if (!IsMatchCodeSetting(line, generatorWrapper, fileName)) { if (Regex.IsMatch(line, "<%.*%>")) { if (!line.Replace(" ", "").Contains("<%=") && (line.StartsWith("<%") || line.EndsWith("%>"))) { generatorWrapper.CodeBuilder.AppendLine(" " + line.Replace("<%", "").Replace("%>", "").TrimStart()); } else { int number = 0; string argumentStr = string.Empty; var list = new List <string>(); if (line.Contains("{") && line.Contains("}")) { line = line.Replace("{", "$0$").Replace("}", "{1}").Replace("$0$", "{0}"); number = 2; argumentStr = ", \"{\", \"}\""; } list.AddRange(GetCodeSegment(line)); foreach (var segment in list.Distinct()) { line = line.Replace(segment, "{" + number + "}"); argumentStr += ", " + segment.Replace("<%=", "").Replace("%>", ""); number++; } generatorWrapper.CodeBuilder.AppendLine( $" builder.AppendLine(string.Format(\"{line.Replace("\"", "\\\"")}\"{argumentStr}));"); } } else { generatorWrapper.CodeBuilder.AppendLine( $" builder.AppendLine(\"{line.Replace("\"", "\\\"")}\");"); } } } } generatorWrapper.TemplateName = fileName; generatorWrappers.Add(generatorWrapper); } return(generatorWrappers); }
private bool IsMatchCodeSetting(string text, GeneratorWrapper wrapper, string templateFileName) { bool isMatch = false; if (Regex.IsMatch(text, CodeTemplateMatch)) { var codeTemplate = GetModel <CodeTemplate>(text); if (string.IsNullOrEmpty(codeTemplate.Language)) { codeTemplate.Language = "text"; } if (string.IsNullOrEmpty(codeTemplate.FileName)) { codeTemplate.FileName = templateFileName; } wrapper.CodeTemplate = codeTemplate; isMatch = true; } else if (Regex.IsMatch(text, AssemblyMatch)) { var assembly = GetModel <Assembly>(text); if (string.IsNullOrEmpty(assembly.Extension)) { assembly.Extension = ".dll"; } if (!wrapper.Assemblies.Select(i => i.Name).Contains(assembly.Name)) { wrapper.Assemblies.Add(assembly); } isMatch = true; } else if (Regex.IsMatch(text, ImportMatch)) { var import = GetModel <Import>(text); if (!wrapper.Imports.Select(i => i.Namespace).Contains(import.Namespace)) { wrapper.Imports.Add(import); } isMatch = true; } else if (Regex.IsMatch(text, PropertyMatch)) { var property = GetModel <Property>(text); if (string.IsNullOrEmpty(property.Type)) { property.Type = "String"; } if (!string.IsNullOrEmpty(property.Name) && !wrapper.Properties.Select(i => i.Name).Contains(property.Name)) { wrapper.Properties.Add(property); } isMatch = true; } return(isMatch); }