Skip to content

Commit

Permalink
Add tests matching the documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Jan 25, 2022
1 parent acb33ce commit 8d93a1f
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 18 deletions.
97 changes: 97 additions & 0 deletions src/Tomlyn.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,109 @@
// See license.txt file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using NUnit.Framework;
using Tomlyn.Model;

namespace Tomlyn.Tests
{
public class BasicTests
{
[Test]
public void TestHelloWorld()
{
var toml = @"global = ""this is a string""
# This is a comment of a table
[my_table]
key = 1 # Comment a key
value = true
list = [4, 5, 6]
";

var model = Toml.ToModel(toml);
// Prints "this is a string"
var global = model["global"];
Console.WriteLine($"found global = \"{global}\"");
Assert.AreEqual("this is a string", global);
// Prints 1
var key = ((TomlTable)model["my_table"]!)["key"];
Console.WriteLine($"found key = {key}");
Assert.AreEqual(1L, key);
// Check list
var list = (TomlArray)((TomlTable)model["my_table"]!)["list"]!;
Console.WriteLine($"found list = {string.Join(", ", list)}");
Assert.AreEqual(new TomlArray() { 4, 5, 6 }, list);
}

[Test]
public void TestHelloWorldWithCustomModel()
{
var toml = @"global = ""this is a string""
# This is a comment of a table
[my_table]
key = 1 # Comment a key
value = true
list = [4, 5, 6]
";

var model = Toml.ToModel<MyModel>(toml);
// Prints "this is a string"
Console.WriteLine($"found global = \"{model.Global}\"");
Assert.AreEqual("this is a string", model.Global);
// Prints 1
var key = model.MyTable!.Key;
Console.WriteLine($"found key = {key}");
Assert.AreEqual(1L, key);
// Check list
var list = model.MyTable!.ListOfIntegers;
Console.WriteLine($"found list = {string.Join(", ", list)}");
Assert.AreEqual(new List<int>() { 4, 5, 6 }, list);

model.MyTable.ThisPropertyIsIgnored = "This should not be printed";

var toml2 = Toml.FromModel(model);
StandardTests.DisplayHeader("toml from model");
Console.WriteLine(toml2);

AssertHelper.AreEqualNormalizeNewLine(toml, toml2);
}

class MyModel : ITomlMetadataProvider
{
public string? Global { get; set; }

public MyTable? MyTable { get; set; }

/// <summary>
/// Allows to store comments and whitespaces
/// </summary>
TomlPropertiesMetadata? ITomlMetadataProvider.PropertiesMetadata { get; set; }
}

class MyTable : ITomlMetadataProvider
{
public MyTable()
{
ListOfIntegers = new List<int>();
}

public int Key { get; set; }

public bool Value { get; set; }

[DataMember(Name = "list")]
public List<int> ListOfIntegers { get; }

[IgnoreDataMember]
public string? ThisPropertyIsIgnored { get; set; }

/// <summary>
/// Allows to store comments and whitespaces
/// </summary>
TomlPropertiesMetadata? ITomlMetadataProvider.PropertiesMetadata { get; set; }
}

[Test]
public void TestEmptyComment()
{
Expand Down
25 changes: 7 additions & 18 deletions src/Tomlyn/Toml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static string FromModel(object model, TomlModelOptions? options = null)
/// <param name="options">Optional parameters for the serialization.</param>
/// <returns>The TOML string representation from the specified model.</returns>
/// <returns><c>true</c> if the conversion was successful; <c>false</c> otherwise.</returns>
public static bool TryFromModel(object model, [NotNullWhen(true)] out string? modelAsToml, [NotNullWhen(false)] out DiagnosticsBag? diagnostics, TomlModelOptions? options = null)
public static bool TryFromModel(object model, [NotNullWhen(true)] out string? modelAsToml, out DiagnosticsBag diagnostics, TomlModelOptions? options = null)
{
modelAsToml = null;
var writer = new StringWriter();
Expand All @@ -120,18 +120,13 @@ public static bool TryFromModel(object model, [NotNullWhen(true)] out string? mo
/// <param name="options">Optional parameters for the serialization.</param>
/// <returns>The TOML string representation from the specified model.</returns>
/// <returns><c>true</c> if the conversion was successful; <c>false</c> otherwise.</returns>
public static bool TryFromModel(object model, TextWriter writer, [NotNullWhen(false)] out DiagnosticsBag? diagnostics, TomlModelOptions? options = null)
public static bool TryFromModel(object model, TextWriter writer, out DiagnosticsBag diagnostics, TomlModelOptions? options = null)
{
diagnostics = null;
var context = new DynamicModelWriteContext(options ?? new TomlModelOptions(), writer);
var serializer = new ModelToTomlTransform(model, context);
serializer.Run();
if (context.Diagnostics.HasErrors)
{
diagnostics = context.Diagnostics;
return false;
}
return true;
diagnostics = context.Diagnostics;
return !context.Diagnostics.HasErrors;
}

/// <summary>
Expand Down Expand Up @@ -223,20 +218,14 @@ public static TomlTable ToModel(this DocumentSyntax syntax)
/// <param name="diagnostics">The diagnostics if this method returns false.</param>
/// <param name="options">The options for the mapping.</param>
/// <returns><c>true</c> if the mapping was successful; <c>false</c> otherwise. In that case the output <paramref name="diagnostics"/> will contain error messages.</returns>
public static bool TryToModel<T>(this DocumentSyntax syntax, [NotNullWhen(true)] out T? model, [NotNullWhen(false)] out DiagnosticsBag? diagnostics, TomlModelOptions? options = null) where T : class, new()
public static bool TryToModel<T>(this DocumentSyntax syntax, [NotNullWhen(true)] out T? model, out DiagnosticsBag diagnostics, TomlModelOptions? options = null) where T : class, new()
{
model = new T();
var context = new DynamicModelReadContext(options ?? new TomlModelOptions());
SyntaxToModelTransform visitor = new SyntaxToModelTransform(context, model);
visitor.Visit(syntax);
if (context.Diagnostics.HasErrors)
{
diagnostics = context.Diagnostics;
return false;
}

diagnostics = null;
return true;
diagnostics = context.Diagnostics;
return !context.Diagnostics.HasErrors;
}
}
}

0 comments on commit 8d93a1f

Please sign in to comment.