public void SwitchWriter() { SwitchTemplate template = new SwitchTemplate(Code.Local("value")); CaseTemplate caseTemplate = new CaseTemplate(Code.String("one")); caseTemplate.Code.AddLine(Code.Comment("Some code here")); template.Cases.Add(caseTemplate); template.Default.AddLine(Code.Comment("Some code here")); SwitchWriter writer = new SwitchWriter(); writer.Write(template, this.output); Assert.AreEqual("switch (value)\r\n{\r\n case \"one\":\r\n // Some code here\r\n break;\r\n default:\r\n // Some code here\r\n break;\r\n}", this.output.ToString()); }
public virtual void Write(ICodeFragment fragment, IOutputCache output) { SwitchTemplate template = (SwitchTemplate)fragment; output.Add("switch (") .Add(template.Expression) .Add(")") .StartBlock(); foreach (CaseTemplate caseTemplate in template.Cases) { output.Add(caseTemplate); } if (template.Default.Fragments.Count > 0) { output.Add("default:") .Indent() .Add(template.Default) .Add("break").CloseLine() .UnIndent(); } output.EndBlock(); }