static bool TestLegacyFormatId(string strGrade, string strId) { try { // Parse the grade and ID and check for errors var grade = ContentSpecId.ParseGrade(strGrade); var id = ContentSpecId.TryParse(strId, grade); if (id.ParseErrorSeverity != ErrorSeverity.NoError) { // Report a parse error WriteLine($"{strGrade} {strId}"); WriteLine(id.ParseErrorSeverity == ErrorSeverity.Corrected ? ConsoleColor.Green : ConsoleColor.Red, $" {id.ParseErrorDescription}"); return(false); } // Check for format-ability if (id.ValidateFor(id.ParseFormat) != ErrorSeverity.NoError) { // Report a validation error WriteLine(ConsoleColor.Red, $" {id.ValidationErrorDescription}"); return(false); } // Check for round-trip match string roundTrip = id.ToString(); string compId = RemedyMissingGrade(strId, grade); if (!string.Equals(compId, roundTrip, StringComparison.OrdinalIgnoreCase)) { WriteLine($"{strGrade} {strId}"); WriteLine(ConsoleColor.Red, $" ID doesn't match: {roundTrip}"); return(false); } // See if can be reformatted to enhanced if (id.ValidateFor(ContentSpecIdFormat.Enhanced) != ErrorSeverity.NoError) { WriteLine($"{strGrade} {strId}"); WriteLine(ConsoleColor.Red, $" Cannot convert to enhanced format: {id.ValidationErrorDescription}"); return(false); } // Round-trip through Enhanced ID format var enhancedIdStr = id.ToString(ContentSpecIdFormat.Enhanced); var enhancedId = ContentSpecId.TryParse(enhancedIdStr); if (!enhancedId.ParseSucceeded) { WriteLine($"{strGrade} {strId}"); WriteLine(ConsoleColor.Red, $" {enhancedIdStr}"); WriteLine(ConsoleColor.Red, $" Failed to parse enhanced format: {enhancedId.ParseErrorDescription}"); return(false); } if (!id.Equals(enhancedId)) { WriteLine($"{strGrade} {strId}"); WriteLine(ConsoleColor.Red, $" {enhancedIdStr}"); WriteLine(ConsoleColor.Red, $" Enhanced ID conversion is not equal."); return(false); } if (enhancedId.ValidateFor(id.ParseFormat) != ErrorSeverity.NoError) { WriteLine($"{strGrade} {strId}"); WriteLine(ConsoleColor.Red, $" {enhancedIdStr}"); WriteLine(ConsoleColor.Red, $" Cannot format enhanced to original format: ${enhancedId.ValidationErrorDescription}"); return(false); } roundTrip = enhancedId.ToString(id.ParseFormat); if (!string.Equals(compId, roundTrip, StringComparison.OrdinalIgnoreCase)) { WriteLine($"{strGrade} {strId}"); WriteLine(ConsoleColor.Red, $" {enhancedIdStr}"); WriteLine(ConsoleColor.Red, $" ID doesn't match when round-tripped through enahcned ID: {roundTrip}"); return(false); } } catch (Exception) { WriteLine(ConsoleColor.Red, $"{strGrade} {strId}"); throw; } return(true); }
static bool TestIdList(string path) { Console.WriteLine($"Testing IDs in '{path}'."); int idCount = 0; int errorCount = 0; int legacyFormatCount = 0; int enhancedFormatCount = 0; using (var idFile = new StreamReader(path)) { for (; ;) { // Read one input line string line = idFile.ReadLine(); if (line == null) { break; } line = line.Trim(); if (line.Length == 0) { continue; } // Separate the grade field if present var grade = ContentSpecGrade.Unspecified; int space = line.IndexOf(' '); if (space > 0) { grade = ContentSpecId.ParseGrade(line.Substring(0, space)); line = line.Substring(space + 1); } ++idCount; switch (TestId(line, grade)) { case ContentSpecIdFormat.Enhanced: ++enhancedFormatCount; break; case ContentSpecIdFormat.ElaV1: case ContentSpecIdFormat.MathV4: case ContentSpecIdFormat.MathV5: case ContentSpecIdFormat.MathV6: ++legacyFormatCount; break; default: ++errorCount; break; } } } // Report results Console.WriteLine($"{idCount,4} Ids Tested"); Console.WriteLine($"{enhancedFormatCount,4} Enhanced Ids"); Console.WriteLine($"{legacyFormatCount,4} Legacy Ids"); Console.WriteLine($"{errorCount,4} Errors Reported"); Console.WriteLine(); return(errorCount == 0); }
static bool TestEnhancedFormatId(string idStr) { try { // Parse the ID and check for errors var id = ContentSpecId.TryParse(idStr); if (id.ParseErrorSeverity != ErrorSeverity.NoError) { // Report a parse error WriteLine(idStr); WriteLine(id.ParseErrorSeverity == ErrorSeverity.Corrected ? ConsoleColor.Green : ConsoleColor.Red, $" {id.ParseErrorDescription}"); return(false); } if (id.ValidateFor(id.ParseFormat) != ErrorSeverity.NoError) { // Report a validation error WriteLine(idStr); WriteLine(ConsoleColor.Red, $" {id.ValidationErrorDescription}"); return(false); } // Check for round-trip match string roundTrip = id.ToString(); if (!string.Equals(idStr, roundTrip, StringComparison.OrdinalIgnoreCase)) { WriteLine(idStr); WriteLine(ConsoleColor.Red, $" ID doesn't match: {roundTrip}"); return(false); } // If claim or target is not specfied then it cannot be converted to // legacy format. Do not perform the test. if (id.Claim == ContentSpecClaim.Unspecified || string.IsNullOrEmpty(id.Target)) { return(true); } // Pick legacy format according to subject var legacyFormat = id.Subject == ContentSpecSubject.Math ? ContentSpecIdFormat.MathV4 : ContentSpecIdFormat.ElaV1; // See if can be reformatted to enhanced if (id.ValidateFor(legacyFormat) != ErrorSeverity.NoError) { WriteLine(idStr); WriteLine(ConsoleColor.Red, $" Cannot convert to legacy format: {id.ValidationErrorDescription}"); return(false); } // Round-trip through Legacy format var legacyIdStr = id.ToString(legacyFormat); var legacyId = ContentSpecId.TryParse(legacyIdStr); if (!legacyId.ParseSucceeded) { WriteLine(idStr); WriteLine(ConsoleColor.Red, $" {legacyIdStr}"); WriteLine(ConsoleColor.Red, $" Failed to parse enhanced format: {legacyId.ParseErrorDescription}"); return(false); } if (!id.Equals(legacyId)) { WriteLine(idStr); WriteLine(ConsoleColor.Red, $" {legacyIdStr}"); WriteLine(ConsoleColor.Red, $" Enhanced ID conversion is not equal."); return(false); } if (legacyId.ValidateFor(id.ParseFormat) != ErrorSeverity.NoError) { WriteLine(idStr); WriteLine(ConsoleColor.Red, $" {legacyIdStr}"); WriteLine(ConsoleColor.Red, $" Cannot format enhanced to original format: ${legacyId.ValidationErrorDescription}"); return(false); } roundTrip = legacyId.ToString(id.ParseFormat); if (!string.Equals(idStr, roundTrip, StringComparison.OrdinalIgnoreCase)) { WriteLine(idStr); WriteLine(ConsoleColor.Red, $" {legacyIdStr}"); WriteLine(ConsoleColor.Red, $" ID doesn't match when round-tripped through enahcned ID: {roundTrip}"); return(false); } } catch (Exception) { WriteLine(ConsoleColor.Red, idStr); throw; } return(true); }
static ContentSpecIdFormat TestId(string strId, ContentSpecGrade defaultGrade) { try { // Parse the ID and check for errors var id = ContentSpecId.TryParse(strId, defaultGrade); if (id.ParseErrorSeverity != ErrorSeverity.NoError) { // Report a parse error WriteLine(strId); WriteLine(id.ParseErrorSeverity == ErrorSeverity.Corrected ? ConsoleColor.Green : ConsoleColor.Red, $" {id.ParseErrorDescription}"); return(ContentSpecIdFormat.Unknown); } // Check for format-ability if (id.ValidateFor(id.ParseFormat) != ErrorSeverity.NoError) { // Report a validation error WriteLine(strId); WriteLine(ConsoleColor.Red, $" {id.ValidationErrorDescription}"); return(ContentSpecIdFormat.Unknown); } // Make sure the comparison ID has grade in it string compId = (id.ParseFormat != ContentSpecIdFormat.Enhanced) ? RemedyMissingGrade(strId, defaultGrade) : strId; // Check for round-trip match string roundTrip = id.ToString(); if (!string.Equals(compId, roundTrip, StringComparison.OrdinalIgnoreCase)) { WriteLine(strId); WriteLine(ConsoleColor.Red, $" ID doesn't match: {roundTrip}"); return(ContentSpecIdFormat.Unknown); } // If claim or target is not specfied then it cannot be converted to // legacy format. Do not perform the test. if (id.ParseFormat == ContentSpecIdFormat.Enhanced && (id.Claim == ContentSpecClaim.Unspecified || string.IsNullOrEmpty(id.Target))) { return(id.ParseFormat); } // Format legacy as enhnaced and enhanced as legacy ContentSpecIdFormat convertFormat; switch (id.ParseFormat) { case ContentSpecIdFormat.ElaV1: case ContentSpecIdFormat.MathV4: case ContentSpecIdFormat.MathV5: case ContentSpecIdFormat.MathV6: convertFormat = ContentSpecIdFormat.Enhanced; break; case ContentSpecIdFormat.Enhanced: switch (id.Subject) { case ContentSpecSubject.ELA: convertFormat = ContentSpecIdFormat.ElaV1; break; case ContentSpecSubject.Math: convertFormat = ContentSpecIdFormat.MathV4; break; default: throw new ApplicationException("Unexpected subject."); } break; default: throw new ApplicationException("Unexpected format."); } // See if can be reformatted to target format if (id.ValidateFor(convertFormat) != ErrorSeverity.NoError) { WriteLine(strId); WriteLine(ConsoleColor.Red, $" Cannot convert from '{id.ParseFormat} to '{convertFormat}': {id.ValidationErrorDescription}"); return(ContentSpecIdFormat.Unknown); } // Round-trip through conversion format var convertedIdStr = id.ToString(convertFormat); var convertedId = ContentSpecId.TryParse(convertedIdStr); if (!convertedId.ParseSucceeded) { WriteLine(strId); WriteLine(ConsoleColor.Red, $" {convertedIdStr}"); WriteLine(ConsoleColor.Red, $" Failed to parse converted format: {convertedId.ParseErrorDescription}"); return(ContentSpecIdFormat.Unknown); } if (!id.Equals(convertedId)) { WriteLine(strId); WriteLine(ConsoleColor.Red, $" {convertedIdStr}"); WriteLine(ConsoleColor.Red, $" Converted ID is not equal to original."); return(ContentSpecIdFormat.Unknown); } if (convertedId.ValidateFor(id.ParseFormat) != ErrorSeverity.NoError) { WriteLine(strId); WriteLine(ConsoleColor.Red, $" {convertedIdStr}"); WriteLine(ConsoleColor.Red, $" Cannot convert from '{convertedId.ParseFormat}'to original '{id.ParseFormat}': ${convertedId.ValidationErrorDescription}"); return(ContentSpecIdFormat.Unknown); } roundTrip = convertedId.ToString(id.ParseFormat); if (!string.Equals(compId, roundTrip, StringComparison.OrdinalIgnoreCase)) { WriteLine(strId); WriteLine(ConsoleColor.Red, $" {convertedIdStr}"); WriteLine(ConsoleColor.Red, $" ID doesn't match when round-tripped through alternative format: {roundTrip}"); return(ContentSpecIdFormat.Unknown); } return(id.ParseFormat); } catch (Exception) { WriteLine(ConsoleColor.Red, strId); throw; } }