private void exportPRGToolStripMenuItem_Click(object sender, EventArgs e) { using var x = new PrgStartAddressDialog(); x.StartAddress = 832; if (x.ShowDialog(this) != DialogResult.OK) { return; } var location = x.StartAddress; var memory = new Memory(); var range = new RangeLocation((Address)location, 504); foreach (var sprite in Sprites) { memory.SetBytes(range, sprite.GetBytes()); location += 63; } try { using var s = new SaveFileDialog(); s.Title = @"Export PRG"; s.Filter = @"PRG files (*.prg)|*.prg|All files (*.*)|*.*"; if (s.ShowDialog(this) != DialogResult.OK) { return; } memory.SaveRange(s.FileName, range); } catch (Exception exception) { MessageDisplayer.Fail(exception.Message, "Export failed"); } }
//--------------------------------------------------------------------------- // Create range of specific text unit size //--------------------------------------------------------------------------- internal void CreateTextUnitRange(out TextPatternRange range, TextUnit unit, int expectedCount, TargetRangeType targetRangeType, RangeLocation rangeLocation, CheckType checkType) { int actualCount = 0; TextPatternRange documentRange = Pattern_DocumentRange(CheckType.Verification); if (IsEmptyRange(documentRange, checkType)) ThrowMe(checkType, ParseType(targetRangeType) + " requires non-empty range"); // Create empty range CreateEmptyRange(out range, rangeLocation, checkType); // Expand range to match Range_MoveEndpointByUnit(range, TextPatternRangeEndpoint.End, unit, expectedCount, ref actualCount, null, checkType); // If we did a move endPoint, did expected count = actual count? if (expectedCount != actualCount) { ThrowMe(checkType, "MoveEndpointByUnit(" + ParseType(unit) + "," + expectedCount + ") returned " + actualCount + ", unable to create range of type " + Parse(targetRangeType)); } else { Comment("Created range size " + expectedCount + " " + Parse(unit) + " units"); } }
//--------------------------------------------------------------------------- // Create a TextPatternRange that is a subset of the existing TextPattern.DocumentRange //--------------------------------------------------------------------------- internal void CreateSubRange(out TextPatternRange subRange, RangeLocation rangeLocation, out int startOffset, out int endOffset, CheckType checkType) { TextPatternRange documentRange = Pattern_DocumentRange(CheckType.Verification); int trailingCount = TextLibrary.CountTrailingCRLF(m_le, Pattern_DocumentRange(checkType)); int stringLength = Range_GetText(documentRange).Length - trailingCount; int subRangeLength = (stringLength / 3) - (stringLength/12) + 1; // should 1 less than 1/4. Note that +1 is very important, -1 causes overlaping sub-ranges!!! Comment("Creating sub-range at " + Parse(rangeLocation)); // sanity check: we must have a string length >= 8 if (stringLength < 8) ThrowMe(checkType, "CreateSubRange(" + ParseType(rangeLocation) + ") expected TextPattern.DocumentRange of length 8 or greater, actual length = " + stringLength); // Is it impossible to create sub-range??? while( (subRangeLength > 0) && ((subRangeLength*4)>stringLength) ) subRangeLength--; // this is very small string lengths. // sanity check: If our sub range is too small, we're hosed. Technically, we shouldn't have gotten this far because of earlier checks if( subRangeLength == 0) ThrowMe( checkType, "Unable to create subRange, length of document is likely too small. Should be >= 6, actual length = " + stringLength ); Comment("String length = " + stringLength); Comment("Maximum sub-range size = " + subRangeLength); // initialize vars startOffset = 0; endOffset = 0; // Find upper / lower bounds switch (rangeLocation) { case RangeLocation.Start: // Range 0...{1/8 to 1/4} of document // range from 0 to {7/12...9/12} startOffset = 0; endOffset -= (subRangeLength * 3 ) + (int)Helpers.RandomValue(0, subRangeLength/2); // e.g. -1 * (3/4 + Random(0...1/8)) break; case RangeLocation.Middle: // Range = ~1/4...~3/4 startOffset = subRangeLength + (int) Helpers.RandomValue(0, (subRangeLength)); // e.g. 1/4 + Random(0..1/4) endOffset -= subRangeLength + (int) Helpers.RandomValue(0, (subRangeLength)); // e.g. -1 * (1/4 + Random(0..1/4)) break; case RangeLocation.End: // Range = ~3/4...7/8 of document // Range from {7/12...9/12} to stringLength startOffset = (subRangeLength * 3) + (int) Helpers.RandomValue(0, (subRangeLength/2)); // e.g. 3/4 + Random(0..1/8) endOffset = 0; break; default: break; } // Sanity check... can't be greater than length. // Can't be equal to length (we'd have an empty range then--not what we wanted) if ((startOffset + (-1 * endOffset)) >= stringLength) { Comment("StartOffset = " + startOffset); Comment("EndOffset = " + endOffset); ThrowMe(checkType, "startOffset + (-1*endOffset) cannot be greater than length of document"); } // Another sanity check, end can't be equal to greater than start (must before start if( startOffset >= (stringLength + endOffset)) { Comment("StartOffset = " + startOffset); Comment("EndOffset = " + endOffset); ThrowMe(checkType, "Test case problem: startoffset (" + startOffset + ") cannot be <= [stringLength + endOffset(" + (stringLength + endOffset) + ")], must have at non-zero length range(!)"); } // create sub-range subRange = null; Range_Clone(documentRange, ref subRange, null, checkType); if( startOffset != 0 ) Range_MoveEndpointByUnit(subRange, TextPatternRangeEndpoint.Start, TextUnit.Character, startOffset, ref startOffset, null, checkType); if( endOffset != 0 ) Range_MoveEndpointByUnit(subRange, TextPatternRangeEndpoint.End, TextUnit.Character, endOffset, ref endOffset, null, checkType); // Give results string s = Range_GetText(subRange); Comment("Created sub-range from " + startOffset + " to " + (_actualText.Length + endOffset) + " (characters)" ); Comment("Sub-range length = " + s.Length); Comment("Sub-range = '" + TrimText(s, 512) + "'"); }
//--------------------------------------------------------------------------- // Overload for CreateSubRange //--------------------------------------------------------------------------- internal void CreateSubRange(out TextPatternRange subRange, RangeLocation rangeLocation, CheckType checkType) { int startOffset = 0; int endOffset = 0; CreateSubRange(out subRange, rangeLocation, out startOffset, out endOffset, checkType); }
internal void CreateEmptyRandomRange(out TextPatternRange range, RangeLocation rangeLoc, TargetRangeType targetRangeType, CheckType checkType) { int actualCount = 0; TextPatternRange documentRange = Pattern_DocumentRange(CheckType.Verification); // Requires non-empty range if (IsEmptyRange(documentRange, checkType)) ThrowMe(checkType, ParseType(targetRangeType) + " requires non-empty range"); // Get range and text in the range range = null; range = Pattern_DocumentRange(checkType); // calculate position of random range int length = Range_GetText(range).Length; int offset = 1 + Helpers.RandomValue(1, length / 4); // Create empty range by collapsing endpoint to start CreateEmptyRange(out range, RangeLocation.Start, checkType); // Now, create the degenerate range switch (rangeLoc) { case RangeLocation.Start: // Move start (and end) endPoint to location in document // This works because Start EndPoint can never roll past end endPoint (it just rolls with start) Range_MoveEndpointByUnit(range, TextPatternRangeEndpoint.Start, TextUnit.Character, offset, ref actualCount, null, checkType); break; case RangeLocation.End: offset = 0 - offset; // was length - offset. This seems to be correct // Move start (and end) endPoint to location in document // This works because Start EndPoint can never roll past end endPoint (it just rolls with start) Range_MoveEndpointByUnit(range, TextPatternRangeEndpoint.Start, TextUnit.Character, offset, ref actualCount, null, checkType); break; default: throw new ArgumentException("TS_CreateEmptyRandomRange() has no support for " + ParseType(rangeLoc)); } // Final check if (IsEmptyRange(range, checkType)) Comment("Created degenerate (empty) range near " + rangeLoc + " of the document"); else { ThrowMe(checkType, "TS_CreateEmptyRange() failed to create empty range, text = '" + TrimText(Range_GetText(range), 512) + "'"); } }
internal void CreateEmptyRange(out TextPatternRange range, RangeLocation rangeLoc, CheckType checkType) { string text = ""; int expectedCount = 0; int actualCount = 0; // Get range and text in the range range = null; range = Pattern_DocumentRange(checkType); if (IsEmptyRange(range, checkType)) { Comment("Document already has an empty range, proceeding with next test step"); return; } else { Range_GetText(range, ref text, -1, null, checkType); } // Now, create the degenerate range switch (rangeLoc) { case RangeLocation.Start: // Move end point to start of document expectedCount = -1; actualCount = expectedCount; ExpandRange(range, TextPatternRangeEndpoint.End, TextUnit.Document, expectedCount, out actualCount, true, checkType); break; case RangeLocation.Middle: // Move start point to middle of doc expectedCount = text.Length / 2; actualCount = expectedCount; ExpandRange(range, TextPatternRangeEndpoint.Start, TextUnit.Character, expectedCount, out actualCount, true, checkType); // Move End point to middle of doc expectedCount = -1 * (text.Length - expectedCount); actualCount = expectedCount; ExpandRange(range, TextPatternRangeEndpoint.End, TextUnit.Character, expectedCount, out actualCount, true, checkType); break; case RangeLocation.End: // Move end point to start of document expectedCount = 1; actualCount = expectedCount; ExpandRange(range, TextPatternRangeEndpoint.Start, TextUnit.Document, expectedCount, out actualCount, true, checkType); //Bug1134056: Resolve RichEdit control inconsistencies / unexpected behavior TextLibrary.TrimRangeCRLF(m_le, range); break; default: throw new ArgumentException("TS_CreateEmptyRange() has no support for " + ParseType(rangeLoc)); } // Final check if (IsEmptyRange(range, checkType)) Comment("Created degenerate (empty) range at " + rangeLoc + " of the document"); else { ThrowMe(checkType, "TS_CreateEmptyRange() failed to create empty range, text = '" + TrimText(Range_GetText(range), 512) + "'"); } }
//--------------------------------------------------------------------------- // Create a degenerate range within pattern, requires non-zero length pattern //--------------------------------------------------------------------------- internal void TS_CreateEmptyRange(out TextPatternRange range, RangeLocation rangeLoc, CheckType checkType) { CreateEmptyRange(out range, rangeLoc, checkType); m_TestStep++; }
//--------------------------------------------------------------------------- // Helper for RangeFromPoint() test cases //--------------------------------------------------------------------------- internal void RangeFromPointHelper(SampleText sampleText, int expandBy, TextPatternRangeEndpoint endPoint, RangeLocation rangeLocation, BoundingRectangleLocation pointLocation, RangeCompare rangeCompare, Type expectedException) { Point screenLocation; Rect[] boundRects = new Rect[0]; string actualText = ""; TextPatternRange callingRange = null; TextPatternRange rangeFromPt = null; // Pre-Condition Verify text is expected value <<sampleText>> TS_SetText(sampleText, out actualText, CheckType.IncorrectElementConfiguration); // Pre-Condition Create empty calling range @ <<RangeLocation>> of TextPattern.DocumentRange TS_CreateEmptyRange(out callingRange, rangeLocation, CheckType.Verification); // Pre-Condition Expand <<endPoint>> endPoint by <<expandby>> character(s) TS_ExpandRange(ref callingRange, endPoint, TextUnit.Character, expandBy, true, CheckType.Verification); // Pre-Condition Scroll range into view // Bug 1134054: TextPatternRange.ScrollIntoView fails to scroll horizontally on edit control // We need to make sure we scroll off the view so we can scroll back on it. // Otherwise the second scrollIntoView call may be a no-op... Range_ScrollIntoView(callingRange, false, null, CheckType.Verification); TS_ScrollIntoView(callingRange, true, null, CheckType.Verification); // Pre-Condition Call GetBoundingRectangles() on that range TS_GetBoundingRectangles(callingRange, ref boundRects, false, null, CheckType.Verification); // Pre-Condition Create a point <<pointLocation>> of bounding rect TS_CreatePoint(ref callingRange, boundRects, out screenLocation, pointLocation, CheckType.Verification); // Call RangeFromPoint on point TS_RangeFromPoint(ref rangeFromPt, screenLocation, expectedException, CheckType.Verification); // Verify that range returned is <<rangeCompare>> TS_CompareRanges(callingRange, rangeFromPt, rangeCompare, expectedException, CheckType.Verification); }
/// --------------------------------------------------------------------------- /// <summary>Parses values for enum</summary> /// --------------------------------------------------------------------------- static public string ParseType(RangeLocation value) { return ParseType(value.GetType().ToString(), value.ToString()); }
static public string Parse(RangeLocation value) { switch (value) { case RangeLocation.Start: return "Start"; case RangeLocation.Middle: return "Middle"; case RangeLocation.End: return "End"; default: throw new ArgumentException("Parse() has no support for " + ParseType(value)); } }
public void TestParse() { RangeLocation expect = new RangeLocation(30, 600); Assert.AreEqual(expect, RangeLocation.Parse("30-600")); Assert.AreEqual(expect, RangeLocation.Parse("30.5, 600.1")); }