Skip to content

Commit

Permalink
Merge pull request #22 from BlueDotBrigade/Features/17-AsJson
Browse files Browse the repository at this point in the history
Merge branch to `main` for: `Daten.AsJson()` support #17
  • Loading branch information
Pressacco authored Nov 26, 2024
2 parents c13d2d3 + 70860d1 commit bf56009
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Src/BlueDotBrigade.DatenLokator.TestTools/Daten.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Runtime.CompilerServices;
using System.Text.Json;
using BlueDotBrigade.DatenLokator.TestTools.Configuration;

public class Daten
Expand Down Expand Up @@ -372,5 +373,67 @@ public System.IO.StreamReader AsStreamReader(From fromSource)

return new System.IO.StreamReader(_coordinator.OsFile.OpenRead(sourceFilePath));
}

/// <summary>
/// Retrieves the data that is appropriate for the test that is currently executing.
/// </summary>
/// <returns>
/// Returns the source file as an instance of <typeparamref name="T"/>.
/// </returns>
/// <remarks>
/// Directory search order:
/// 1. the given directory
/// 2. a compressed file that is similar to the given directory
/// 3. the global directory for shared files
/// </remarks>
public T AsJson<T>()
{
var sourceFilePath = _coordinator.GetFilePath(_callingMethodName, _callingClassPath);

ThrowIfFileMissing(sourceFilePath);

var jsonContent = _coordinator.OsFile.ReadAllText(sourceFilePath);
return JsonSerializer.Deserialize<T>(jsonContent);
}

/// <summary>
/// Retrieves the data that is stored within the given <paramref name="fileName"/>.
/// </summary>
/// <returns>
/// Returns the source file as an instance of <typeparamref name="T"/>.
/// </returns>
/// <remarks>
/// Directory search order:
/// 1. the given directory
/// 2. a compressed file that is similar to the given directory
/// 3. the global directory for shared files
/// </remarks>
public T AsJson<T>(string fileName)
{
var sourceFilePath = _coordinator.GetFilePath(fileName, _callingClassPath);

ThrowIfFileMissing(sourceFilePath);

var jsonContent = _coordinator.OsFile.ReadAllText(sourceFilePath);
return JsonSerializer.Deserialize<T>(jsonContent);
}

/// <summary>
/// Retrieves the data that was registered with <see cref="Lokator"/>.
/// </summary>
/// <param name="fromSource">Determines which registered file to retrieve.</param>
/// <returns>
/// Returns the source file as an instance of <typeparamref name="T"/>.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException"/>
public T AsJson<T>(From fromSource)
{
var sourceFilePath = GetGlobalDefaultPath(fromSource);

ThrowIfFileMissing(sourceFilePath);

var jsonContent = _coordinator.OsFile.ReadAllText(sourceFilePath);
return JsonSerializer.Deserialize<T>(jsonContent);
}
}
}

0 comments on commit bf56009

Please sign in to comment.