/// <summary> /// Loads the testdata for the given type. /// The name of the type defines the name of the file containing the testdata. /// If the type ends with Test or Tests the name of the tesdata file will be used without the ending /// </summary> /// <returns></returns> public SnapshotCollection Read(SnapshotSetup snapshotId) { var file = snapshotId.GetFilePath(); if (!File.Exists(file)) { return(null); } var snapshots = new SnapshotCollection(); Snapshot snapsot = null; var reader = _dataReaders[ReaderType.data]; foreach (var line in ReadAllLines(file)) { if (line.StartsWith("'Errors", System.StringComparison.InvariantCultureIgnoreCase) || line.StartsWith("'Raw", System.StringComparison.InvariantCultureIgnoreCase)) { return(snapshots); } if (line.StartsWith("'")) { continue; } if (line == "---") { reader = _dataReaders[ReaderType.data]; continue; } if (line.StartsWith("---") && line.Length > 3) { var property = line.Substring(3); if (Enum.TryParse <ReaderType>(property, out var key)) { reader = _dataReaders[key]; if (reader.NewSnapshot(snapsot)) { snapsot = null; } continue; } } if (snapsot == null) { snapsot = new Snapshot(); snapshots.Add(snapsot); } reader.ReadLine(line, snapsot); } return(snapshots); }
/// <summary> /// write the snapshot to file /// </summary> /// <param name="snapshot"></param> /// <param name="setup"></param> public void Write(Snapshot snapshot, SnapshotSetup setup) { var collection = new SnapshotCollection(); collection.Add(snapshot); var file = setup.GetFilePath(); if (File.Exists(file)) { var reader = new SnapshotReader(); var tmp = reader.Read(setup); foreach (var token in tmp) { if (!token.SnapshotContainsMetadata(snapshot.Metadata)) { collection.Add(token); } } } Directory.CreateDirectory(Path.GetDirectoryName(setup.GetFilePath())); using (var writer = new StreamWriter(setup.GetFilePath(), false)) { foreach (var token in collection) { if (snapshot.HasMetadata()) { writer.WriteLine("---metadata"); } foreach (var info in token.Metadata) { writer.WriteLine($"{info.Key}: {info.Value}"); } writer.WriteLine("---data"); foreach (var line in token) { writer.WriteLine(line.Value); } } } }