// Cut the file at one character shorter until it is zero length. // Each time attempt to parse using all four option permutations. // This will yield numerous syntax errors but should not crash // the parser. static bool TestIterativeEof(string yaml) { try { YamlReaderOptions options = new YamlReaderOptions(); while (yaml.Length > 0) { for (int i = 0; i < 4; ++i) { options.IgnoreTextOutsideDocumentMarkers = (i & 1) != 0; options.MergeDocuments = (i & 2) != 0; using (var reader = new MicroYamlReader(new StringReader(yaml), options)) { while (reader.MoveNextDocument()) { while (reader.MoveNext()) { // Do nothing //Trace("(\"{0}\", \"{1}\")", EscapeString(reader.Current.Key), EscapeString(reader.Current.Value)); } } } } // Sorten by one character yaml = yaml.Substring(0, yaml.Length - 1); } } catch (Exception err) { ReportError(err.ToString()); return(false); } return(true); }
static bool CompareDocWithExpected(string yaml, IEnumerable <KeyValuePair <string, string> > expected, bool mergeDocs, bool ignoreTextOutside = false) { bool success = true; Yaml.MicroYamlReader reader = null; IEnumerator <KeyValuePair <string, string> > comp = null; try { var options = new YamlReaderOptions(); options.MergeDocuments = mergeDocs; options.IgnoreTextOutsideDocumentMarkers = ignoreTextOutside; reader = new MicroYamlReader(new StringReader(yaml), options); comp = expected.GetEnumerator(); bool eofComp = false; for (;;) { bool docHasValue = reader.MoveNext(); bool compHasValue = false; if (!eofComp) { compHasValue = comp.MoveNext(); if (mergeDocs) { while (compHasValue && comp.Current.Key == null) { compHasValue = comp.MoveNext(); } } eofComp = !compHasValue; } if (!docHasValue) { if (!reader.MoveNextDocument()) { break; } Trace("--- Document Break ---"); if (compHasValue && comp.Current.Key != null) { Trace(" Unexpected document break."); success = false; } continue; } if (eofComp) { ReportError("Expected result at EOF but YAML input remains."); success = false; } Trace("(\"{0}\", \"{1}\")", EscapeString(reader.Current.Key), EscapeString(reader.Current.Value)); if (reader.ImmediateError != null) { ReportError(reader.ImmediateError); success = false; } if (!eofComp) { if (comp.Current.Key == null) { Trace(" Expected document break."); } else { if (!string.Equals(reader.Current.Key, comp.Current.Key, StringComparison.Ordinal)) { Trace(" Keys don't match:\r\n \"{0}\"\r\n \"{1}\"", EscapeString(reader.Current.Key), EscapeString(comp.Current.Key)); success = false; } if (!string.Equals(reader.Current.Value, comp.Current.Value)) { Trace(" Values don't match:\r\n \"{0}\"\r\n \"{1}\"", EscapeString(reader.Current.Value), EscapeString(comp.Current.Value)); success = false; } } } } if (comp.MoveNext()) { ReportError("YAML at EOF but still values in expected result."); } } catch (Exception err) { ReportError(err.ToString()); success = false; } finally { if (comp != null) { comp.Dispose(); comp = null; } if (reader != null) { reader.Dispose(); reader = null; } } return(success); }