Esempio n. 1
0
		public void TestWriteServerDependenciesAvailable_Works() {
			CodeBuilder cb = new CodeBuilder();

			var tpl = new Template();
			tpl.ClassName = "TestClass";

			var m1 = mocks.StrictMock<IMember>();
			var m2 = mocks.StrictMock<IMember>();
			
			Expect.Call(() => m1.WriteCode(tpl, MemberCodePoint.ServerConstructor, cb)).Do((Action<ITemplate, MemberCodePoint, CodeBuilder>)((_, __, x) => x.AppendLine("[a]")));
			Expect.Call(() => m2.WriteCode(tpl, MemberCodePoint.ServerConstructor, cb)).Do((Action<ITemplate, MemberCodePoint, CodeBuilder>)((_, __, x) => x.AppendLine("[b]")));

			mocks.ReplayAll();
			
			string expected =  "public void DependenciesAvailable() {" + Environment.NewLine
			                +  "	[a]" + Environment.NewLine
			                +  "	[b]" + Environment.NewLine
			                +  "	Constructed();" + Environment.NewLine
			                +  "}" + Environment.NewLine;

			Template.WriteServerDependenciesAvailable(cb, tpl, new List<IMember>() { m1, m2 });
			Assert.AreEqual(expected, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			
			mocks.VerifyAll();
		}
		public void TestWriteClientDefinition_Works() {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new NamedElementMember("div", "ElementName").WriteCode(tpl, MemberCodePoint.ClientDefinition, cb);
			Assert.AreEqual("private DivElement ElementName { get { return (DivElement)Document.GetElementById(id + \"_ElementName\"); } }" + Environment.NewLine + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);

			cb = new CodeBuilder();
			new NamedElementMember("input", "ElementName").WriteCode(tpl, MemberCodePoint.ClientDefinition, cb);
			Assert.AreEqual("private InputElement ElementName { get { return (InputElement)Document.GetElementById(id + \"_ElementName\"); } }" + Environment.NewLine + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);

			cb = new CodeBuilder();
			new NamedElementMember("input/x", "ElementName").WriteCode(tpl, MemberCodePoint.ClientDefinition, cb);
			Assert.AreEqual("private InputElement ElementName { get { return (InputElement)Document.GetElementById(id + \"_ElementName\"); } }" + Environment.NewLine + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);

			cb = new CodeBuilder();
			new NamedElementMember("input/text", "ElementName").WriteCode(tpl, MemberCodePoint.ClientDefinition, cb);
			Assert.AreEqual("private TextElement ElementName { get { return (TextElement)Document.GetElementById(id + \"_ElementName\"); } }" + Environment.NewLine + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);

			cb = new CodeBuilder();
			new NamedElementMember("inputx", "ElementName").WriteCode(tpl, MemberCodePoint.ClientDefinition, cb);
			Assert.AreEqual("private Element ElementName { get { return (Element)Document.GetElementById(id + \"_ElementName\"); } }" + Environment.NewLine + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);

			mocks.VerifyAll();
		}
		public void TestWriteDefinition_CustomInstantiateWorks() {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			string expected = "private Namespace.TestType TestId {" + Environment.NewLine
			                + "	get { return (Namespace.TestType)controls[\"TestId\"]; }" + Environment.NewLine
			                + "	set {" + Environment.NewLine
			                + "		controls[\"TestId\"] = value;" + Environment.NewLine
			                + "		if (!string.IsNullOrEmpty(id))" + Environment.NewLine
			                + "			((IControl)controls[\"TestId\"]).Id = id + \"_TestId\";" + Environment.NewLine
			                + "	}" + Environment.NewLine
			                + "}" + Environment.NewLine + Environment.NewLine;

			var member = new InstantiatedControlMember("TestId", "Namespace.TestType", true, new Dictionary<string, TypedMarkupData>(), new IMember[0]);

			CodeBuilder cb = new CodeBuilder();
			member.WriteCode(tpl, MemberCodePoint.ServerDefinition, cb);
			Assert.AreEqual(expected, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);

			cb = new CodeBuilder();
			member.WriteCode(tpl, MemberCodePoint.ClientDefinition, cb);
			Assert.AreEqual(expected, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
		private void TestWriteCode_NoChildrenWorks(FragmentCodePoint point) {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new InstantiatedControlFragment("CtlId", false, 0).WriteCode(tpl, point, cb);
			Assert.AreEqual("sb.Append(((IControl)CtlId).Html);" + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
		public void TestWriteServerDefinition_Works() {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new NamedElementMember("div", "ElementName").WriteCode(tpl, MemberCodePoint.ServerDefinition, cb);
			Assert.AreEqual("", cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
		public void TestWriteClientIdChangedCode_Works() {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new NamedElementMember("div", "TestId").WriteCode(tpl, MemberCodePoint.ClientIdChanging, cb);
			Assert.AreEqual("this.TestId.ID = value + \"_TestId\";" + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
Esempio n. 7
0
		public void TestWriteTransferConstructorCode_NothingWrittenWhenNotClient() {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new FieldMember("TestId", "Namespace.ServerType", null).WriteCode(tpl, MemberCodePoint.TransferConstructor, cb);
			Assert.AreEqual("", cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
		private void TestWriteCode_CDataWorks(FragmentCodePoint point) {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new LiteralFragment(" Test  \"fragment\"  ", true).WriteCode(tpl, point, cb);
			Assert.AreEqual("sb.Append(@\"<![CDATA[ Test  \"\"fragment\"\"  ]]>\");" + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
Esempio n. 9
0
		public void TestWriteTransferConstructorCode_WorksWhenBothServerAndClient() {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new FieldMember("TestId", "Namespace.ServerType", "Namespace.ClientType").WriteCode(tpl, MemberCodePoint.TransferConstructor, cb);
			Assert.AreEqual("this.TestId = (Namespace.ClientType)__cfg[\"TestId\"];" + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
Esempio n. 10
0
		public void TestWriteClientDefinition_WorksWhenClientType() {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new FieldMember("TestId", "Namespace.ServerType", "Namespace.ClientType").WriteCode(tpl, MemberCodePoint.ClientDefinition, cb);
			Assert.AreEqual("private Namespace.ClientType TestId;" + Environment.NewLine + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
Esempio n. 11
0
		public void TestWriteServerDefinition_NothingWrittenWhenNotServer() {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new FieldMember("TestId", null, "Namespace.ClientType").WriteCode(tpl, MemberCodePoint.ServerDefinition, cb);
			Assert.AreEqual("", cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
Esempio n. 12
0
		private void TestWriteCode_Works(FragmentCodePoint point) {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new PositionFragment().WriteCode(tpl, point, cb);
			Assert.AreEqual("sb.Append(PositionHelper.CreateStyle(Position, -1, -1));" + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
		private void TestWriteCode_Works(FragmentCodePoint point) {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new CodeExpressionFragment("[Some code]").WriteCode(tpl, point, cb);
			Assert.AreEqual("sb.Append([Some code]);" + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
		private void TestWriteCode_OneChildWorks(FragmentCodePoint point) {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new InstantiatedControlFragment("CtlId", false, 1).WriteCode(tpl, point, cb);
			Assert.AreEqual("((IControlHost)CtlId).SetInnerFragments(new string[] { CtlId_inner1() });" + Environment.NewLine
			              + "sb.Append(((IControl)CtlId).Html);" + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
		public void TestWriteClientDefinition_NothingWrittenIfClientCreateNotEnabled() {
			ITemplate tpl = mocks.StrictMock<ITemplate>();
			Expect.Call(tpl.EnableClientCreate).Return(false);
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new RenderFunctionMember("Test", "").WriteCode(tpl, MemberCodePoint.ClientDefinition, cb);
			Assert.AreEqual("", cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
		private void TestWriteCode_CustomInstantiateWorks(FragmentCodePoint point) {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new InstantiatedControlFragment("CtlId", true, 0).WriteCode(tpl, point, cb);
			Assert.AreEqual("if (CtlId == null) throw new InvalidOperationException(\"The control instance CtlId must be assigned before the control can be rendered.\");" + Environment.NewLine
			              + "sb.Append(((IControl)CtlId).Html);" + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
		public void TestWriteCode_NothingWrittenWhenItShouldNot() {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			foreach (var cp in new[] { MemberCodePoint.AttachSelf, MemberCodePoint.ServerIdChanging, MemberCodePoint.ServerConstructor, MemberCodePoint.ClientConstructor, MemberCodePoint.TransferConstructor, MemberCodePoint.ConfigObjectInit, MemberCodePoint.Attach }) {
				var cb = new CodeBuilder();
				new NamedElementMember("div", "TestId").WriteCode(tpl, cp, cb);
				Assert.AreEqual("", cb.ToString());
				Assert.AreEqual(0, cb.IndentLevel);
			}
			mocks.VerifyAll();
		}
		public void TestWriteServerDefinition_WorksWithSetterOnly() {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new PropertyMember("TestId", "Namespace.ServerType", "Namespace.ClientType", AccessModifier._ProtectedInternal, "field", "Namespace.ServerType", "Namespace.ClientType", false, true, null, false).WriteCode(tpl, MemberCodePoint.ServerDefinition, cb);
			Assert.AreEqual("private Namespace.ServerType field;" + Environment.NewLine
			              + "protected internal Namespace.ServerType TestId {" + Environment.NewLine
			              + "\tset { field = value; }" + Environment.NewLine
			              + "}" + Environment.NewLine + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
		public void TestWriteServerDefinition_WorksWithValueChangedHook() {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new PropertyMember("TestId", "Namespace.ServerType", "Namespace.ClientType", AccessModifier._Public, "field", "Namespace.ServerType", "Namespace.ClientType", true, true, "TestValueChanged", false).WriteCode(tpl, MemberCodePoint.ServerDefinition, cb);
			Assert.AreEqual("private Namespace.ServerType field;" + Environment.NewLine
			              + "public Namespace.ServerType TestId {" + Environment.NewLine
			              + "\tget { return field; }" + Environment.NewLine
			              + "\tset { field = value; TestValueChanged(); }" + Environment.NewLine
			              + "}" + Environment.NewLine + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
		public void TestWriteServerDefinition_WorksWithGetterAndSetterDifferentTypes() {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new PropertyMember("TestId", "Namespace1.ServerPropertyType", "Namespace1.ClientPropertyType", AccessModifier._Public, "field", "Namespace2.ServerFieldType", "Namespace2.ClientFieldType", true, true, null, false).WriteCode(tpl, MemberCodePoint.ServerDefinition, cb);
			Assert.AreEqual("private Namespace2.ServerFieldType field;" + Environment.NewLine
			              + "public Namespace1.ServerPropertyType TestId {" + Environment.NewLine
			              + "\tget { return (Namespace1.ServerPropertyType)field; }" + Environment.NewLine
			              + "\tset { field = (Namespace2.ServerFieldType)value; }" + Environment.NewLine
			              + "}" + Environment.NewLine + Environment.NewLine, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
Esempio n. 21
0
		    private string GetTemplateCodeFileContents(ISaltarelleParser parser, XmlDocument doc, string className, string nmspace) {
			    ITemplate template = parser.ParseTemplate(doc);
			    template.ClassName = className;
			    template.Nmspace   = nmspace;

			    CodeBuilder cb = new CodeBuilder();
			    cb.AppendLine("#pragma warning disable 1591")
			      .AppendLine("#if SERVER");
			    template.WriteServerCode(cb);
			    cb.AppendLine("#endif")
			      .AppendLine("#if CLIENT");
			    template.WriteClientCode(cb);
			    cb.AppendLine("#endif");
			
			    return cb.ToString();
		    }
		public void TestWriteIdChangedCode_NothingWritten() {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			var member = new InstantiatedControlMember("TestId", "Namespace.TestType", false, new Dictionary<string, TypedMarkupData>(), new IMember[0]);

			CodeBuilder cb = new CodeBuilder();
			member.WriteCode(tpl, MemberCodePoint.ServerIdChanging, cb);
			Assert.AreEqual("", cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);

			cb = new CodeBuilder();
			member.WriteCode(tpl, MemberCodePoint.ClientIdChanging, cb);
			Assert.AreEqual("", cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
Esempio n. 23
0
		public void TestWriteClientConstructor_Works() {
			CodeBuilder cb = new CodeBuilder();

			var tpl = new Template();
			tpl.ClassName = "TestClass";

			mocks.ReplayAll();
			
			string expected =  "[Obsolete(@\"" + Template.DoNotCallConstructorMessage.Replace("\"", "\"\"") + "\")]" + Environment.NewLine
			                +  "public TestClass(object config) {" + Environment.NewLine
			                +  "	__cfg = (!Script.IsUndefined(config) ? JsDictionary.GetDictionary(config) : null);" + Environment.NewLine
			                +  "}" + Environment.NewLine;

			Template.WriteClientConstructor(cb, tpl);
			Assert.AreEqual(expected, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			
			mocks.VerifyAll();
		}
Esempio n. 24
0
		public void TestWriteServerConstructor_Works() {
			CodeBuilder cb = new CodeBuilder();

			var tpl = new Template();
			tpl.ClassName = "TestClass";

			var m1 = mocks.StrictMock<IMember>();
			var m2 = mocks.StrictMock<IMember>();
			
			mocks.ReplayAll();
			
			string expected =  "[Obsolete(@\"" + Template.DoNotCallConstructorMessage.Replace("\"", "\"\"") + "\")]" + Environment.NewLine
                            +  "public TestClass() {" + Environment.NewLine
			                +  "}" + Environment.NewLine;

			Template.WriteServerConstructor(cb, tpl);
			Assert.AreEqual(expected, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			
			mocks.VerifyAll();
		}
		private void TestWriteDefinition_Works(bool server) {
			ITemplate tpl = mocks.StrictMock<ITemplate>();
			if (!server)
				Expect.Call(tpl.EnableClientCreate).Return(true);
			mocks.ReplayAll();
		
			var cb = new CodeBuilder();

			var f1 = mocks.StrictMock<IFragment>();
			var f2 = mocks.StrictMock<IFragment>();
			
			using (mocks.Ordered()) {
				Expect.Call(f1.TryMergeWithNext(f2)).Return(null);
				Expect.Call(() => f1.WriteCode(tpl, server ? FragmentCodePoint.ServerRender : FragmentCodePoint.ClientRender, cb)).Do((Action<ITemplate, FragmentCodePoint, CodeBuilder>)((_, __, x) => x.AppendLine("[a]")));
				Expect.Call(() => f2.WriteCode(tpl, server ? FragmentCodePoint.ServerRender : FragmentCodePoint.ClientRender, cb)).Do((Action<ITemplate, FragmentCodePoint, CodeBuilder>)((_, __, x) => x.AppendLine("[b]")));
			}
		
			string expected = "private string Method(int p1, string param2) {" + Environment.NewLine
			                +  "	StringBuilder sb = new StringBuilder();" + Environment.NewLine
			                +  "	[a]" + Environment.NewLine
			                +  "	[b]" + Environment.NewLine
			                +  "	return sb.ToString();" + Environment.NewLine
			                +  "}" + Environment.NewLine + Environment.NewLine;
			                       
			mocks.ReplayAll();

			var member = new RenderFunctionMember("Method", "int p1, string param2");
			member.AddFragment(f1);
			member.AddFragment(f2);
			if (server)
				member.WriteCode(tpl, MemberCodePoint.ServerDefinition, cb);
			else
				member.WriteCode(tpl, MemberCodePoint.ClientDefinition, cb);
			Assert.AreEqual(expected, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			
			mocks.VerifyAll();
		}
Esempio n. 26
0
		private void TestWriteCode_Works(FragmentCodePoint point) {
			for (int i = -5; i <= 5; i++) {
				var tpl = mocks.StrictMock<ITemplate>();
				mocks.ReplayAll();
			
				CodeBuilder cb = new CodeBuilder();
				for (int x = 0; x < 5; x++)
					cb.Indent();
				new CodeFragment("code", i).WriteCode(tpl, point, cb);
				Assert.AreEqual("\t\t\t\t\tcode" + Environment.NewLine, cb.ToString());
				Assert.AreEqual(5 + i, cb.IndentLevel);
				mocks.VerifyAll();

				tpl = mocks.StrictMock<ITemplate>();
				mocks.ReplayAll();
				cb = new CodeBuilder();
				for (int x = 0; x < 5; x++)
					cb.Indent();
				new CodeFragment(null, i).WriteCode(tpl, point, cb);
				Assert.AreEqual("", cb.ToString());
				Assert.AreEqual(5 + i, cb.IndentLevel);
				mocks.VerifyAll();
			}
		}
Esempio n. 27
0
		public void WriteCode(ITemplate tpl, MemberCodePoint point, CodeBuilder cb) {
			switch (point) {
				case MemberCodePoint.ServerDefinition:
				case MemberCodePoint.ClientDefinition:
					cb.AppendLine("private int copyrightYear;")
					  .AppendLine("public int CopyrightYear {").Indent()
					  .AppendLine("get { return copyrightYear; }")
					  .AppendLine("set { copyrightYear = value; }").Outdent()
					  .AppendLine("}").AppendLine();
					break;
				case MemberCodePoint.ServerConstructor:
					cb.AppendLine("copyrightYear = DateTime.Now.Year;");
					break;
				case MemberCodePoint.ClientConstructor:
					cb.AppendLine("copyrightYear = (new DateTime()).GetFullYear();");
					break;
				case MemberCodePoint.TransferConstructor:
					cb.AppendLine("copyrightYear = (int)" + ParserUtils.ConfigObjectName + "[\"copyrightYear\"];");
					break;
				case MemberCodePoint.ConfigObjectInit:
					cb.AppendLine(ParserUtils.ConfigObjectName + "[\"copyrightYear\"] = this.copyrightYear;");
					break;
			}
		}
Esempio n. 28
0
 internal void WriteClientIdProperty(CodeBuilder cb, ITemplate tpl, IList <IMember> orderedMembers)
 {
     WriteIdProperty(cb, false, tpl, orderedMembers);
 }
Esempio n. 29
0
 internal void WriteServerIdProperty(CodeBuilder cb, ITemplate tpl, IList <IMember> orderedMembers)
 {
     WriteIdProperty(cb, true, tpl, orderedMembers);
 }
Esempio n. 30
0
 internal static void WriteServerConstructor(CodeBuilder cb, ITemplate tpl)
 {
     cb.AppendLine("[Obsolete(@\"" + DoNotCallConstructorMessage.Replace("\"", "\"\"") + "\")]")
     .AppendLine("public " + tpl.ClassName + "() {")
     .AppendLine("}");
 }
		public void TestInstantiate_Works() {
			var propValues = new Dictionary<string, TypedMarkupData>() {
			                     { "IntProperty",    new TypedMarkupData("", () => 42) },
			                     { "StringProperty", new TypedMarkupData("", () => "Test value") },
			                     { "ArrayProperty",  new TypedMarkupData("", () => new int[] { 20, 21 }) }
			                 };

			var tpl = mocks.StrictMock<ITemplate>();
			var ctl = mocks.StrictMock<IInstantiatedTemplateControl>();
            var container = mocks.StrictMock<IContainer>();

            Expect.Call(container.CreateObjectByTypeName("SaltarelleParser.Tests.TestControlClass")).Return(new TestControlClass());
			TestControlClass addedControl = null;
			Expect.Call(() => ctl.AddControl(null, null)).IgnoreArguments().Constraints(Is.Equal("CtlName"), Is.TypeOf<TestControlClass>()).Do((Action<string, IControl>)((_, c) => addedControl = (TestControlClass)c));
			mocks.ReplayAll();
			CodeBuilder cb = new CodeBuilder();
			new InstantiatedControlMember("CtlName", "SaltarelleParser.Tests.TestControlClass", false, propValues, new IMember[0]).Instantiate(tpl, ctl, container);
			mocks.VerifyAll();

			Assert.AreEqual(42, addedControl.IntProperty);
			Assert.AreEqual("Test value", addedControl.StringProperty);
			Assert.IsTrue(new int[] { 20, 21 }.SequenceEqual(addedControl.ArrayProperty));
		}
		public void TestWriteCode_NothingWrittenWhenItShouldNot() {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			foreach (var cp in new[] { MemberCodePoint.AttachSelf }) {
				var cb = new CodeBuilder();
				new InstantiatedControlMember("CtlName", "Namespace.Type", true, new Dictionary<string, TypedMarkupData>(), new IMember[0]).WriteCode(tpl, cp, cb);
				Assert.AreEqual("", cb.ToString());
				Assert.AreEqual(0, cb.IndentLevel);
			}
			mocks.VerifyAll();
		}
		public void TestWriteAttachCode_WorksForNonCustomInstantiate() {
			var tpl = mocks.StrictMock<ITemplate>();
			mocks.ReplayAll();
			var cb = new CodeBuilder();
			string expected = "this.CtlName.Attach();" + Environment.NewLine;
			new InstantiatedControlMember("CtlName", "Namespace.Type", false, new Dictionary<string, TypedMarkupData>() { { "Prop1", new TypedMarkupData("value1") }, { "Prop2", new TypedMarkupData("value2") } }, new IMember[0]).WriteCode(tpl, MemberCodePoint.Attach, cb);
			Assert.AreEqual(expected, cb.ToString());
			Assert.AreEqual(0, cb.IndentLevel);
			mocks.VerifyAll();
		}
Esempio n. 34
0
        public void WriteClientCode(CodeBuilder cb)
        {
            var orderedMembers = TopologicalSort(members);

            foreach (var us in clientUsings)
            {
                cb.AppendLine("using " + us.Key + ";");
            }
            cb.AppendLine();

            if (!string.IsNullOrEmpty(nmspace))
            {
                cb.AppendFormat("namespace {0} {{", nmspace).Indent().AppendLine();
            }

            cb.AppendFormat("public partial class " + className)
            .Append(" : ")
            .Append(ClientInheritanceList)
            .Append(" {").AppendLine().Indent()
            .AppendLine("partial void Constructed();")
            .AppendLine("partial void Attached();").AppendLine()
            .AppendLine("private Dictionary<string, IControl> controls = new Dictionary<string, IControl>();")
            .AppendLine("private JsDictionary " + ParserUtils.ConfigObjectName + ";")
            .AppendLine()
            .AppendLine("private Position position;")
            .AppendLine("public " + (generateImplementationsAsOverrides ? "override " : "") + "Position Position {").Indent()
            .AppendLine("get { return isAttached ? PositionHelper.GetPosition(GetElement()) : position; }")
            .AppendLine("set {").Indent()
            .AppendLine("position = value;")
            .AppendLine("if (isAttached)").Indent()
            .AppendLine("PositionHelper.ApplyPosition(GetElement(), value);").Outdent()
            .Outdent().AppendLine("}")
            .Outdent().AppendLine("}").AppendLine()
            .AppendLine("private bool isAttached = false;")
            .AppendLine("public " + (generateImplementationsAsOverrides ? "override " : "") + "Element GetElement() { return isAttached ? Document.GetElementById(id) : null; }").AppendLine();

            WriteIdProperty(cb, false, this, orderedMembers);
            cb.AppendLine();

            foreach (var m in orderedMembers)
            {
                m.WriteCode(this, MemberCodePoint.ClientDefinition, cb);
            }

            WriteAttachSelf(cb, this, orderedMembers);
            cb.AppendLine();

            if (enableClientCreate)
            {
                WriteAttach(cb, this, orderedMembers);

                cb.AppendLine()
                .AppendLine("public " + (generateImplementationsAsOverrides ? "override " : "") + "string Html {").Indent()
                .AppendLine("get {").Indent()
                .AppendLine("if (string.IsNullOrEmpty(id))").Indent()
                .AppendLine("throw new InvalidOperationException(\"Must assign Id before rendering.\");").Outdent()
                .AppendLine("return " + MainRenderFunctionName + "();").Outdent()
                .AppendLine("}").Outdent()
                .AppendLine("}").AppendLine()
                .AppendLine("[AlternateSignature]")
                .AppendLine("public " + className + "() {}");
            }
            WriteClientConstructor(cb, this);
            cb.AppendLine();
            WriteClientDependenciesAvailable(cb, this, orderedMembers);

            cb.Outdent().AppendLine("}");
            if (!string.IsNullOrEmpty(nmspace))
            {
                cb.Outdent().AppendLine("}");
            }
        }