/// <summary> /// Resolves the snapshot name for the running unit test. /// The default generated snapshot name can either be overwritten /// with a given snapshot name, or can be extended by the snapshot name extensions, /// or both. /// </summary> /// <param name="snapshotName"> /// The snapshot name given by the user, this snapshot name will overwrite /// the automatically generated snapshot name. /// </param> /// <param name="snapshotNameExtension"> /// The snapshot name extension will extend the snapshot name with /// this given extensions. It can be used to make a snapshot name even more /// specific. /// </param> /// <returns>The full name of a snapshot.</returns> public SnapshotFullName ResolveSnapshotFullName( string snapshotName, string snapshotNameExtension) { SnapshotFullName snapshotFullName = _snapshotFullNameReader.ReadSnapshotFullName(); if (snapshotFullName == null) { throw new SnapshotTestException( "The snapshot full name could not be evaluated."); } if (string.IsNullOrWhiteSpace(snapshotFullName.Filename) && string.IsNullOrWhiteSpace(snapshotName)) { throw new SnapshotTestException( "No snapshot name could be resolved."); } if (string.IsNullOrWhiteSpace(snapshotName)) { snapshotName = snapshotFullName.Filename; } snapshotName = AddSnapshotNameExtension(snapshotName, snapshotNameExtension); snapshotName = EnsureSnapshotFileExtension(snapshotName); return(new SnapshotFullName(snapshotName, snapshotFullName.FolderPath)); }
private void CompareSnapshots( string actualSnapshotSerialized, string savedSnapshotSerialized, SnapshotFullName snapshotFullName, Func <MatchOptions, MatchOptions> matchOptions) { if (savedSnapshotSerialized == null) { _snapshotFileHandler .SaveNewSnapshot(snapshotFullName, actualSnapshotSerialized); return; } try { _snapshotComparer.CompareSnapshots( savedSnapshotSerialized, actualSnapshotSerialized, matchOptions); } catch (Exception) { _snapshotFileHandler .SaveMismatchSnapshot(snapshotFullName, actualSnapshotSerialized); throw; } }
/// <summary> /// Resolves the snapshot name for the running unit test. /// The default generated snapshot name can either be overwritten /// with a given snapshot name, or can be extended by the snapshot name extensions, /// or both. /// </summary> /// <param name="snapshotName"> /// The snapshot name given by the user, this snapshot name will overwrite /// the automatically generated snapshot name. /// </param> /// <param name="snapshotNameExtension"> /// The snapshot name extension will extend the snapshot name with /// this given extensions. It can be used to make a snapshot name even more /// specific. /// Example: /// Snapshot name = 'NumberAdditionTest' /// Snapshot name extension = '5', '6', 'Result', '11' /// Result: 'NumberAdditionTest_5_6_Result_11' /// </param> /// <returns>The full name of a snapshot.</returns> public SnapshotFullName ResolveSnapshotFullName( string snapshotName = null, SnapshotNameExtension snapshotNameExtension = null) { SnapshotFullName snapshotFullName = _snapshotFullNameResolver .ResolveSnapshotFullName( snapshotName, snapshotNameExtension?.ToParamsString()); return(snapshotFullName); }
/// <summary> /// Checks for a live unit test session. /// </summary> /// <param name="snapshotFullName">The snapshot full name.</param> /// <returns>The resolved snapshot full name.</returns> public static SnapshotFullName CheckForSession(SnapshotFullName snapshotFullName) { if (string.IsNullOrEmpty(snapshotFullName.FolderPath)) { snapshotFullName = TryResolveName(snapshotFullName.Filename); if (string.IsNullOrEmpty(snapshotFullName?.FolderPath)) { throw new SnapshotTestException("Could not resolve directory for SnapShot"); } } return(snapshotFullName); }
/// <summary> /// Compares the snapshot against the given result object. If the snapshot is /// new then it will be saved directly. If the snapshot is not matching with the /// given result object, then the given result object will be snapshot and saved /// in the given folder. /// </summary> /// <param name="currentResult"> /// The object to compare. /// </param> /// <param name="snapshotFullName"> /// The name and folder of the snapshot. /// </param> /// <param name="matchOptions"> /// Additional match actions, which can be applied during the comparison /// </param> public void AssertSnapshot( object currentResult, SnapshotFullName snapshotFullName, Func <MatchOptions, MatchOptions> matchOptions = null) { if (currentResult == null) { throw new ArgumentNullException(nameof(currentResult)); } if (snapshotFullName == null) { throw new ArgumentNullException(nameof(snapshotFullName)); } _snapshotEnvironmentCleaner.Cleanup(snapshotFullName); string actualSnapshotSerialized = _snapshotSerializer.SerializeObject(currentResult); string savedSnapshotSerialized = _snapshotFileHandler.ReadSnapshot(snapshotFullName); if (savedSnapshotSerialized == null) { string value = Environment.GetEnvironmentVariable("SNAPSHOOTER_STRICT_MODE"); if (string.Equals(value, "on", StringComparison.Ordinal) || (bool.TryParse(value, out bool b) && b)) { _snapshotFileHandler .SaveMismatchSnapshot(snapshotFullName, actualSnapshotSerialized); throw new SnapshotNotFoundException( "Strict mode is enabled and no snapshot has been found " + "for the current test. Create a new snapshot locally and " + "rerun your tests."); } _snapshotFileHandler.SaveNewSnapshot(snapshotFullName, actualSnapshotSerialized); savedSnapshotSerialized = _snapshotFileHandler.ReadSnapshot(snapshotFullName); } CompareSnapshots( actualSnapshotSerialized, savedSnapshotSerialized, snapshotFullName, matchOptions ); }
/// <summary> /// Creates a snapshot of the given object and compares it with the /// already existing snapshot of the test. /// If no snapshot exists, a new snapshot will be created from the current result /// and saved under a certain file path, given by the snapshot full name. /// </summary> /// <param name="currentResult">The object to match.</param> /// <param name="snapshotFullName"> /// The full name of a snapshot with folder and file name.</param> /// <param name="matchOptions"> /// Additional compare actions, which can be applied during the snapshot comparison. /// </param> public void AssertSnapshot( object currentResult, SnapshotFullName snapshotFullName, Func <MatchOptions, MatchOptions> matchOptions = null) { if (currentResult == null) { throw new ArgumentNullException(nameof(currentResult)); } if (snapshotFullName == null) { throw new ArgumentNullException(nameof(snapshotFullName)); } _snapshotAssert.AssertSnapshot( currentResult, snapshotFullName, matchOptions); }
/// <summary> /// Compares the snapshot against the given result object. If the snapshot is /// new then it will be saved directly. If the snapshot is not matching with the /// given result object, then the given result object will be snapshot and saved /// in the given folder. /// </summary> /// <param name="currentResult"> /// The object to compare. /// </param> /// <param name="snapshotFullName"> /// The name and folder of the snapshot. /// </param> /// <param name="matchOptions"> /// Additional match actions, which can be applied during the comparison /// </param> public void AssertSnapshot( object currentResult, SnapshotFullName snapshotFullName, Func <MatchOptions, MatchOptions> matchOptions = null) { if (currentResult == null) { throw new ArgumentNullException(nameof(currentResult)); } if (snapshotFullName == null) { throw new ArgumentNullException(nameof(snapshotFullName)); } _snapshotEnvironmentCleaner.Cleanup(snapshotFullName); string actualSnapshotSerialized = _snapshotSerializer.SerializeObject(currentResult); string savedSnapshotSerialized = _snapshotFileHandler.ReadSnapshot(snapshotFullName); CompareSnapshots(actualSnapshotSerialized, savedSnapshotSerialized, snapshotFullName, matchOptions); }