public PreprocessTextTransformationTest() { _host = new MockTemplatingHost(); _transformer = new PreprocessTextTransformation("Test", "TestNs", null, _host); var renderAccessor = new PrivateObject(_transformer); _invokeRender = block => renderAccessor.Invoke("Render", new[] { typeof(Block) }, new object[] { block }) as string; }
// todo add to cli tool /// <summary> /// 将 T4 模板转换成 cs 代码 /// </summary> /// <param name="content">模板内容</param> /// <param name="className">生成的 cs 代码类名</param> /// <param name="classNamespace">生成的 cs 代码名称空间名</param> /// <returns>cs 代码内容及其引用的 Reference</returns> public PreprocessResult PreprocessT4Template(string content, string className, string classNamespace) { var result = new Parser(_host).Parse(content); var transformation = new PreprocessTextTransformation(className, classNamespace, result, _host); var preprocessedContent = transformation.TransformText(); var preprocessed = new PreprocessResult { References = result.References.Distinct().ToArray(), PreprocessedContent = preprocessedContent }; return(preprocessed); }
public void TransformSimpleTextTest() { // <# // foreach (XmlAttribute attr in attributes) // { // #> // Found another one! // <# // allAtributes.Add(attr.Name); // } // #> // <#+ // private void OutputFixedAttributeName(string name) // { // #> // Attribute: <#= CultureInfo.CurrentCulture.TextInfo.ToTitleCase(name) #> // <#+ // <<< Notice that this is also a class feature block. // } // #> var parseResult = new ParseResult(); parseResult.Imports.AddRange(_host.StandardImports); parseResult.ContentBlocks.AddRange(new List <Block> { new Block { BlockType = BlockType.StandardControlBlock, Content = @" foreach (XmlAttribute attr in attributes) { " }, new Block { BlockType = BlockType.TextBlock, Content = @" Found another one! " }, new Block { BlockType = BlockType.StandardControlBlock, Content = @" allAtributes.Add(attr.Name); }" } }); parseResult.FeatureBlocks.AddRange(new List <Block> { new Block { BlockType = BlockType.ClassFeatureControlBlock, Content = @" private void OutputFixedAttributeName(string name) {" }, new Block { BlockType = BlockType.TextBlock, Content = @" Attribute: " }, new Block { BlockType = BlockType.ExpressionControlBlock, Content = @"CultureInfo.CurrentCulture.TextInfo.ToTitleCase(name)" }, new Block { BlockType = BlockType.ClassFeatureControlBlock, Content = @" }" } }); var transformer = new PreprocessTextTransformation("SimpleText", "Test", parseResult, _host); var transformResult = transformer.TransformText(); transformResult.Should().NotBeEmpty(); var newLineRegex = new Regex("\r\n|\r|\n"); var expected = newLineRegex.Replace(@"using System; using TextTemplating; using TextTemplating.Infrastructure; using TextTemplating.T4.Parsing; using TextTemplating.T4.Preprocessing; namespace Test { public partial class SimpleText : TextTransformationBase { public override string TransformText() { foreach (XmlAttribute attr in attributes) { WriteLine(); Write(""Found another one!""); WriteLine(); allAtributes.Add(attr.Name); } return GenerationEnvironment.ToString(); } private void OutputFixedAttributeName(string name) { WriteLine(); Write(""Attribute: ""); Write((CultureInfo.CurrentCulture.TextInfo.ToTitleCase(name)).ToString()); } } } ", Environment.NewLine); newLineRegex.Replace(transformResult, Environment.NewLine).Should().Be(expected); }