public void VersionedStringSource_ExplicitlyConvertsToString_WhenSourceIsString() { var data = "Hello, world!"; var source = new VersionedStringSource(data); TheResultingString((String)source).ShouldBe("Hello, world!"); }
public void VersionedStringSource_ProducesCorrectString_WhenToStringIsCalled_AndSourceIsStringBuilder() { var data = new VersionedStringBuilder("Hello, world!"); var source = new VersionedStringSource(data); TheResultingString(source.ToString()).ShouldBe("Hello, world!"); }
public void VersionedStringSource_ExplicitlyConvertsToStringBuilder_WhenSourceIsStringBuilder() { var data = new VersionedStringBuilder("Hello, world!"); var source = new VersionedStringSource(data); TheResultingObject((VersionedStringBuilder)source) .ShouldSatisfyTheCondition(x => x.ToString() == "Hello, world!"); }
public void VersionedStringSource_IsInvalid_WhenNoSourceIsSpecified() { var source = new VersionedStringSource(); TheResultingValue(source.IsValid).ShouldBe(false); TheResultingValue(source.IsSourcedFromString).ShouldBe(false); TheResultingValue(source.IsSourcedFromStringBuilder).ShouldBe(false); }
public void VersionedStringSource_CannotConvertToString_WhenSourceIsNotString() { var data = new VersionedStringBuilder("Hello, world!"); var source = new VersionedStringSource(data); Assert.That(() => TheResultingString((String)source).ShouldBe("Hello, world!"), Throws.TypeOf <InvalidCastException>()); }
public void VersionedStringSource_CannotConvertToString_WhenSourceIsNotString() { var data = new VersionedStringBuilder("Hello, world!"); var source = new VersionedStringSource(data); Assert.That(() => TheResultingString((String)source).ShouldBe("Hello, world!"), Throws.TypeOf<InvalidCastException>()); }
public void VersionedStringSource_IsValid_WhenStringBuilderSourceIsSpecified() { var data = new VersionedStringBuilder("Hello, world!"); var source = new VersionedStringSource(data); TheResultingValue(source.IsValid).ShouldBe(true); TheResultingValue(source.IsSourcedFromString).ShouldBe(false); TheResultingValue(source.IsSourcedFromStringBuilder).ShouldBe(true); }
public void VersionedStringSource_IsValid_WhenStringSourceIsSpecified() { var data = "Hello, world!"; var source = new VersionedStringSource(data); TheResultingValue(source.IsValid).ShouldBe(true); TheResultingValue(source.IsSourcedFromString).ShouldBe(true); TheResultingValue(source.IsSourcedFromStringBuilder).ShouldBe(false); }
public void VersionedStringBuilder_VersionIsIncremented_WhenAppendVersionedStringSourceIsCalled_WithValidStringBuilderSource() { var builder = new VersionedStringBuilder("Hello, world!"); var source = new VersionedStringBuilder(" Goodbye, world!"); var value = new VersionedStringSource(source); builder.Append(value); TheResultingValue(builder.Version).ShouldBe(1); TheResultingString(builder.ToString()).ShouldBe("Hello, world! Goodbye, world!"); }
public void VersionedStringBuilder_VersionIsNotIncremented_WhenInsertVersionedStringSourceIsCalled_WithInvalidSource() { var builder = new VersionedStringBuilder("Hello, world!"); var value = new VersionedStringSource(); builder.Insert(5, value); TheResultingValue(builder.Version).ShouldBe(0); TheResultingString(builder.ToString()).ShouldBe("Hello, world!"); }
public void VersionedStringSource_CannotConvertToStringBuilder_WhenSourceIsNotStringBuilder() { var data = "Hello, world!"; var source = new VersionedStringSource(data); Assert.That(() => { TheResultingObject((VersionedStringBuilder)source) .ShouldSatisfyTheCondition(x => x.ToString() == "Hello, world!"); }, Throws.TypeOf <InvalidCastException>()); }
public void VersionedStringBuilder_VersionIsIncremented_WhenInsertVersionedStringSourceIsCalled_WithValidStringBuilderSource() { var builder = new VersionedStringBuilder("Hello, world!"); var source = new VersionedStringBuilder(" and goodbye"); var value = new VersionedStringSource(source); builder.Insert(5, value); TheResultingValue(builder.Version).ShouldBe(1); TheResultingString(builder.ToString()).ShouldBe("Hello and goodbye, world!"); }
public void VersionedStringSource_CannotConvertToStringBuilder_WhenSourceIsNotStringBuilder() { var data = "Hello, world!"; var source = new VersionedStringSource(data); Assert.That(() => { TheResultingObject((VersionedStringBuilder)source) .ShouldSatisfyTheCondition(x => x.ToString() == "Hello, world!"); }, Throws.TypeOf<InvalidCastException>()); }
public void VersionedStringBuilder_VersionIsIncremented_WhenAppendVersionedStringSourceIsCalled_WithValidStringSource() { var builder = new VersionedStringBuilder("Hello, world!"); var source = " Goodbye, world!"; var value = new VersionedStringSource(source); builder.Append(value); TheResultingValue(builder.Version).ShouldBe(1); TheResultingString(builder.ToString()).ShouldBe("Hello, world! Goodbye, world!"); }
/// <summary> /// Occurs when the value of the Text dependency property changes. /// </summary> private static void HandleTextChanged(DependencyObject dobj, VersionedStringSource oldValue, VersionedStringSource newValue) { var raiseTextChanged = false; var textBox = (TextBox)dobj; if (textBox.TextEditor != null) { raiseTextChanged = !textBox.TextEditor.HandleTextChanged(newValue); } if (raiseTextChanged) { textBox.OnTextChanged(); } }
/// <summary> /// Appends the contents of a <see cref="VersionedStringSource"/> to the end of the string builder. /// </summary> /// <param name="this">The <see cref="StringBuilder"/> instance.</param> /// <param name="value">The <see cref="VersionedStringSource"/> to append to the string builder.</param> /// <returns>The current <see cref="StringBuilder"/> instance.</returns> public static StringBuilder AppendVersionedStringSource(this StringBuilder @this, VersionedStringSource value) { Contract.Require(@this, "this"); if (value.IsValid) { if (value.IsSourcedFromString) { var source = (String)value; @this.Append(source); } else { var source = (VersionedStringBuilder)value; for (int i = 0; i < source.Length; i++) { @this.Append(source[i]); } } } return @this; }
/// <summary> /// Inserts the contents of a <see cref="VersionedStringSource"/> into the string builder at the specified position. /// </summary> /// <param name="this">The <see cref="StringBuilder"/> instance.</param> /// <param name="index">The index at which to begin inserting the value.</param> /// <param name="value">The <see cref="VersionedStringSource"/> to insert into the string builder.</param> /// <returns>The current <see cref="StringBuilder"/> instance.</returns> public static StringBuilder InsertVersionedStringSource(this StringBuilder @this, Int32 index, VersionedStringSource value) { Contract.Require(@this, "this"); Contract.EnsureRange(index >= 0 && index <= @this.Length, "index"); if (value.IsValid) { if (value.IsSourcedFromString) { var source = (String)value; @this.Insert(index, source); } else { var source = (VersionedStringBuilder)value; for (int i = 0; i < source.Length; i++) { @this.Insert(index + i, source[i]); } } } return @this; }
/// <summary> /// Appends the specified string to the end of the internal text buffer. /// </summary> private void AppendToBuffer(VersionedStringSource str) { if (str.IsValid) { if (str.IsSourcedFromString) { InsertIntoBuffer(new StringSegment((String)str), bufferText.Length); } else { InsertIntoBuffer(new StringSegment((StringBuilder)(VersionedStringBuilder)str), bufferText.Length); } } }
/// <summary> /// Inserts the specified string into the internal text buffer at the specified position. /// </summary> private void InsertIntoBuffer(VersionedStringSource str, Int32 position) { if (str.IsValid) { if (str.IsSourcedFromString) { InsertIntoBuffer(new StringSegment((String)str), position); } else { InsertIntoBuffer(new StringSegment((StringBuilder)(VersionedStringBuilder)str), position); } } }
/// <summary> /// Occurs when the value of the Text dependency property changes. /// </summary> private static void HandleTextChanged(DependencyObject dobj, VersionedStringSource oldValue, VersionedStringSource newValue) { var label = (TextBlock)dobj; label.UpdateTextParserResult(); }
/// <summary> /// Called when the value of the <see cref="TextBox.TextProperty"/> dependency property changes. /// </summary> /// <param name="value">The new value of the dependency property.</param> /// <returns><see langword="true"/> if the editor replaced the text with its own source; otherwise, <see langword="false"/>.</returns> internal Boolean HandleTextChanged(VersionedStringSource value) { if (value.IsSourcedFromStringBuilder && (VersionedStringBuilder)value == bufferText) return false; var valueHasNotChanged = value.Length == bufferText.Length; if (valueHasNotChanged) { for (int i = 0; i < bufferText.Length; i++) { if (bufferText[i] != value[i]) { valueHasNotChanged = false; break; } } } if (valueHasNotChanged) return false; if (value.IsSourcedFromStringBuilder) { var vsb = (VersionedStringBuilder)value; if (vsb.Version == bufferText.Version) return false; } BeginTrackingSelectionChanges(); ClearBuffer(value.Length == 0); InsertIntoBuffer(value, bufferText.Length); if (caretPosition > bufferText.Length) { caretBlinkTimer = 0; caretPosition = bufferText.Length; pendingScrollToCaret = true; } UpdateTextStringSource(); UpdateTextParserStream(); EndTrackingSelectionChanges(); return true; }
public void VersionedStringBuilder_VersionIsIncremented_WhenInsertVersionedStringSourceIsCalled_WithValidStringSource() { var builder = new VersionedStringBuilder("Hello, world!"); var source = " and goodbye"; var value = new VersionedStringSource(source); builder.Insert(5, value); TheResultingValue(builder.Version).ShouldBe(1); TheResultingString(builder.ToString()).ShouldBe("Hello and goodbye, world!"); }
/// <summary> /// Inserts the contents of the specified string source at the specified index within the string builder. /// </summary> /// <param name="index">The index at which to insert the value.</param> /// <param name="value">The value to insert.</param> /// <returns>The current instance of <see cref="VersionedStringBuilder"/>.</returns> public VersionedStringBuilder Insert(Int32 index, VersionedStringSource value) { if (value.IsValid) { if (value.IsSourcedFromString) { Insert(index, (String)value); } else { Insert(index, (VersionedStringBuilder)value); } } else { if (index < 0 || index > stringBuilder.Length) throw new ArgumentOutOfRangeException("index"); } return this; }
/// <summary> /// Called when the value of the <see cref="TextBox.TextProperty"/> dependency property changes. /// </summary> /// <param name="value">The new value of the dependency property.</param> /// <returns><c>true</c> if the editor replaced the text with its own source; otherwise, <c>false</c>.</returns> internal Boolean HandleTextChanged(VersionedStringSource value) { if (value.IsSourcedFromStringBuilder) { var vsb = (VersionedStringBuilder)value; if (vsb.Version == bufferText.Version) return false; } BeginTrackingSelectionChanges(); var valueLength = 0; if (value.IsValid) { if (value.IsSourcedFromString) { valueLength = ((String)value).Length; } else { valueLength = ((VersionedStringBuilder)value).Length; } } ClearBuffer(valueLength == 0); InsertIntoBuffer(value, bufferText.Length); if (caretPosition > bufferText.Length) { caretBlinkTimer = 0; caretPosition = bufferText.Length; pendingScrollToCaret = true; } UpdateTextStringSource(); UpdateTextParserStream(); EndTrackingSelectionChanges(); return true; }
/// <summary> /// Occurs when the value of the Text dependency property changes. /// </summary> private static void HandleTextChanged(DependencyObject dobj, VersionedStringSource oldValue, VersionedStringSource newValue) { var raiseTextChanged = false; var textBox = (TextBox)dobj; if (textBox.TextEditor != null) raiseTextChanged = !textBox.TextEditor.HandleTextChanged(newValue); if (raiseTextChanged) textBox.OnTextChanged(); }
public void VersionedStringSource_ProducesCorrectString_WhenToStringIsCalled_AndSourceIsString() { var data = "Hello, world!"; var source = new VersionedStringSource(data); TheResultingString(source.ToString()).ShouldBe("Hello, world!"); }
public void VersionedStringSource_ProducesCorrectString_WhenToStringIsCalled_AndSourceIsInvalid() { var source = new VersionedStringSource(); TheResultingString(source.ToString()).ShouldBe(null); }
/// <summary> /// Appends the contents of the specified string source at the end of the string builder. /// </summary> /// <param name="value">The value to append.</param> /// <returns>The current instance of <see cref="VersionedStringBuilder"/>.</returns> public VersionedStringBuilder Append(VersionedStringSource value) { if (value.IsValid) { if (value.IsSourcedFromString) { Append((String)value); } else { Append((VersionedStringBuilder)value); } } return this; }