/// <summary> /// Add a property to this JSObject with the provided name. The provided action will be invoked to populate the value of this property. /// </summary> /// <param name="propertyName">The name of the new property.</param> /// <param name="propertyValueAction">The action to invoke to add the property's value.</param> public void Property(string propertyName, Action <JSValue> propertyValueAction) { if (!string.IsNullOrEmpty(propertyBeingConstructed)) { throw new InvalidOperationException($"Cannot add a property to a JSObject while constructing its child property (\"{propertyBeingConstructed}\")."); } SetCurrentState(State.Property); if (PropertyNameNeedsToBeQuoted(propertyName)) { builder.QuotedString(propertyName); } else { builder.Text(propertyName); } builder.Text(": "); propertyBeingConstructed = propertyName; try { builder.Value(propertyValueAction); } finally { propertyBeingConstructed = null; } propertyNames.Add(propertyName); }
public void InsertBetweenExistingPositions() { JSBuilder builder = new JSBuilder(); builder.Text("ab"); BuilderPosition p1 = builder.CreatePosition(); builder.Text("cd"); BuilderPosition p2 = builder.CreatePosition(); builder.Text("ef"); Assert.AreEqual(2, p1.CharactersAfterPreviousPosition); Assert.AreEqual(2, p1.GetIndexInBuilder()); Assert.AreEqual(2, p2.CharactersAfterPreviousPosition); Assert.AreEqual(4, p2.GetIndexInBuilder()); builder.Insert(3, "Z"); Assert.AreEqual("abcZdef", builder.ToString()); Assert.AreEqual(2, p1.CharactersAfterPreviousPosition); Assert.AreEqual(2, p1.GetIndexInBuilder()); Assert.AreEqual(3, p2.CharactersAfterPreviousPosition); Assert.AreEqual(5, p2.GetIndexInBuilder()); }
public void TextWithNewLineTwice() { JSBuilder builder = new JSBuilder(); builder.Text("\n"); builder.Text("\n"); Assert.AreEqual("\n\n", builder.ToString()); Assert.IsFalse(builder.WriteNewLineBeforeNextText); }
public void HasChangedLinesSinceWithZeroIndexAndSingleLine() { JSBuilder builder = new JSBuilder(); builder.Text("abc"); Assert.IsFalse(builder.HasChangedLinesSince(0)); }
public void HasChangedLinesSinceWithNegativeIndexAndMultipleLines() { JSBuilder builder = new JSBuilder(); builder.Text("a\nb\nc"); Assert.ThrowsException <IndexOutOfRangeException>(() => builder.HasChangedLinesSince(-1)); }
public void HasChangedLinesSinceWithZeroIndexAndMultipleLines() { JSBuilder builder = new JSBuilder(); builder.Text("a\nb\nc"); Assert.IsTrue(builder.HasChangedLinesSince(0)); }
public void TextWithNull() { JSBuilder builder = new JSBuilder(); builder.Text(null); Assert.AreEqual("", builder.ToString()); Assert.IsFalse(builder.WriteNewLineBeforeNextText); }
public void InsertAfterAnExistingPosition() { JSBuilder builder = new JSBuilder(); builder.Text("abc"); BuilderPosition p = builder.CreatePosition(); builder.Text("df"); Assert.AreEqual(3, p.CharactersAfterPreviousPosition); Assert.AreEqual(3, p.GetIndexInBuilder()); builder.Insert(4, "e"); Assert.AreEqual(3, p.CharactersAfterPreviousPosition); Assert.AreEqual(3, p.GetIndexInBuilder()); }
/// <summary> /// Add a new parameter to this parameter list. /// </summary> /// <param name="parameterName">The name of the parameter.</param> /// <param name="parameterType">The type of the parameter.</param> /// <param name="optional">Whether or not the parameter is optional.</param> public void Parameter(string parameterName, string parameterType, bool optional = false) { if (hasParameters) { builder.Text(", "); } else { hasParameters = true; } builder.Text(parameterName); if (optional) { builder.Text("?"); } builder.Text($": {parameterType}"); }
public void CreatePositionWithNonEmptyBuilder() { JSBuilder builder = new JSBuilder(); builder.Text("abcd"); BuilderPosition p = builder.CreatePosition(); Assert.AreEqual(4, p.CharactersAfterPreviousPosition); Assert.AreEqual(4, p.GetIndexInBuilder()); Assert.AreSame(p, builder.CreatePosition()); }
public void CreatePositionWhenAnotherPositionAlreadyExists() { JSBuilder builder = new JSBuilder(); BuilderPosition p1 = builder.CreatePosition(); builder.Text("abcd"); BuilderPosition p2 = builder.CreatePosition(); Assert.AreEqual(0, p1.CharactersAfterPreviousPosition); Assert.AreEqual(0, p1.GetIndexInBuilder()); Assert.AreEqual(4, p2.CharactersAfterPreviousPosition); Assert.AreEqual(4, p2.GetIndexInBuilder()); }
/// <summary> /// Add text to this TSValue. /// </summary> /// <param name="text">The text to add to this TSValue.</param> public virtual void Text(string text) { builder.Text(text); }
public void Text(string text) { SetCurrentState(State.Statement); builder.Text(text); }