Esempio n. 1
0
        /// <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
                );
        }
Esempio n. 2
0
        /// <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);
        }