AddEntry (ref _properties, String.Format ("\"{0}\"", name), script);
}
-
+
void AddEntry (ref IDictionary<string, string> dictionary, string key, string value) {
if (dictionary == null)
dictionary = new SortedDictionary<string, string> ();
dictionary [key] = value;
}
- protected internal override string GetScript () {
- if (String.IsNullOrEmpty (FormID)) {
- if (String.IsNullOrEmpty (ElementIDInternal))
- return String.Format ("$create({0}, {1}, {2}, {3});", Type, GetSerializedProperties (), GetSerializedEvents (), GetSerializedReferences ());
- else
- return String.Format ("$create({0}, {1}, {2}, {3}, $get(\"{4}\"));", Type, GetSerializedProperties (), GetSerializedEvents (), GetSerializedReferences (), ElementIDInternal);
- }
- else {
- if (String.IsNullOrEmpty (ElementIDInternal))
- return String.Format ("$create($get(\"{0}\"), {1}, {2}, {3}, {4});", FormID, Type, GetSerializedProperties (), GetSerializedEvents (), GetSerializedReferences ());
- else
- return String.Format ("$create($get(\"{0}\"), {1}, {2}, {3}, {4}, $get(\"{5}\"));", FormID, Type, GetSerializedProperties (), GetSerializedEvents (), GetSerializedReferences (), ElementIDInternal);
- }
+ protected internal override string GetScript ()
+ {
+ string id = ID;
+ if (id != String.Empty)
+ AddProperty ("id", id);
+
+ bool haveFormID = String.IsNullOrEmpty (FormID) == false;
+ bool haveElementID = String.IsNullOrEmpty (ElementIDInternal) == false;
+ var sb = new StringBuilder ("$create(");
+
+ if (haveFormID)
+ sb.Append ("$get(\"");
+ sb.Append (Type);
+ if (haveFormID)
+ sb.Append ("\")");
+
+ WriteSerializedProperties (sb);
+ WriteSerializedEvents (sb);
+ WriteSerializedReferences (sb);
+
+ if (haveElementID)
+ sb.AppendFormat (", $get(\"{0}\")", ElementIDInternal);
+
+ sb.Append (");");
+
+ return sb.ToString ();
}
- internal static string SerializeDictionary (IDictionary<string, string> dictionary) {
+ internal static string SerializeDictionary (IDictionary<string, string> dictionary)
+ {
if (dictionary == null || dictionary.Count == 0)
return "null";
StringBuilder sb = new StringBuilder ("{");
- foreach (string key in dictionary.Keys) {
+ foreach (string key in dictionary.Keys)
sb.AppendFormat ("{0}:{1},", key, dictionary [key]);
- }
sb.Length--;
sb.Append ("}");
return sb.ToString ();
}
- string GetSerializedProperties () {
- return SerializeDictionary (_properties);
+ void WriteSerializedProperties (StringBuilder sb)
+ {
+ sb.Append (", ");
+ sb.Append (SerializeDictionary (_properties));
}
- string GetSerializedEvents () {
- return SerializeDictionary (_events);
+ void WriteSerializedEvents (StringBuilder sb)
+ {
+ sb.Append (", ");
+ sb.Append (SerializeDictionary (_events));
}
- string GetSerializedReferences () {
- return SerializeDictionary (_references);
+ void WriteSerializedReferences (StringBuilder sb)
+ {
+ sb.Append (", ");
+ sb.Append (SerializeDictionary (_references));
}
}
}
\ No newline at end of file
}
[Test]
- public void ScriptBehaviorDescriptor_Defaults () {
+ public void ScriptBehaviorDescriptor_Defaults ()
+ {
PokerScriptBehaviorDescriptor scd = new PokerScriptBehaviorDescriptor ("My.Type", "Element1");
Assert.AreEqual ("My.Type", scd.Type, "Type");
Assert.AreEqual ("Element1", scd.ElementID, "ElementID");
string script = scd.DoGetScript ();
- Assert.AreEqual ("$create(My.Type, null, null, null, $get(\"Element1\"));", script);
+ Assert.AreEqual ("$create(My.Type, null, null, null, $get(\"Element1\"));", script, "#A1");
+
+ scd.ID = "SomeID";
+ script = scd.DoGetScript ();
+ Assert.AreEqual ("$create(My.Type, {\"id\":\"SomeID\"}, null, null, $get(\"Element1\"));", script, "#A2");
+
+ scd.Name = "SomeName";
+ script = scd.DoGetScript ();
+ Assert.AreEqual ("$create(My.Type, {\"id\":\"SomeID\",\"name\":\"SomeName\"}, null, null, $get(\"Element1\"));", script, "#A3");
}
[Test]
}
[Test]
- public void ScriptComponentDescriptor_Defaults () {
+ public void ScriptComponentDescriptor_Defaults ()
+ {
PokerScriptComponentDescriptor scd = new PokerScriptComponentDescriptor ("My.Type");
Assert.AreEqual ("My.Type", scd.Type, "Type");
Assert.AreEqual (String.Empty, scd.ClientID, "ClientID");
string script = scd.DoGetScript ();
- Assert.AreEqual ("$create(My.Type, null, null, null);", script);
+ Assert.AreEqual ("$create(My.Type, null, null, null);", script, "#A1");
+
+ scd.ID = "SomeID";
+ script = scd.DoGetScript ();
+ Assert.AreEqual ("$create(My.Type, {\"id\":\"SomeID\"}, null, null);", script, "#A2");
}
[Test]