Skip to content

Latest commit

 

History

History
275 lines (224 loc) · 7.34 KB

ordering.md

File metadata and controls

275 lines (224 loc) · 7.34 KB

Ordering

Order properties alphabetically

Serialized properties can optionally be ordering alphabetically, ie ignoring the order they are defined when using reflection.

public static class ModuleInitializer
{
    [ModuleInitializer]
    public static void Init() =>
        VerifierSettings.SortPropertiesAlphabetically();
}

snippet source | anchor

Dictionary order

Dictionaries are ordering by key.

To disable use:

[Fact]
public Task DontOrderDictionaries()
{
    var dictionary = new Dictionary<string, string>
    {
        {
            "Entry_1", "1234"
        },
        {
            "Entry_3", "1234"
        },
        {
            "Entry_2", "5678"
        }
    };

    return Verify(dictionary)
        .DontSortDictionaries();
}

snippet source | anchor

[Fact]
public Task DontOrderDictionaries()
{
    var dictionary = new Dictionary<string, string>
    {
        {
            "Entry_1", "1234"
        },
        {
            "Entry_3", "1234"
        },
        {
            "Entry_2", "5678"
        }
    };

    return Verify(dictionary)
        .DontSortDictionaries();
}

snippet source | anchor

Json/JObject ordered

Json and JObject are not ordered.

To enable ordering use:

public static class ModuleInitializer
{
    [ModuleInitializer]
    public static void Init() =>
        VerifierSettings.SortJsonObjects();
}

snippet source | anchor

Ordering IEnumerable items

Items in an instance of an IEnumerable can be ordered.

This is helpful when verifying items that can have an inconsistent order, for example reading items from a database.

OrderEnumerableBy

Globally

[ModuleInitializer]
public static void OrderEnumerableByInitializer() =>
    VerifierSettings.OrderEnumerableBy<TargetForGlobal>(_ => _.Value);

snippet source | anchor

Instance

[Fact]
public Task EnumerableOrder()
{
    var settings = new VerifySettings();
    settings.OrderEnumerableBy<Target>(_ => _.Value);
    return Verify(
        new List<Target>
        {
            new("a"),
            new("c"),
            new("b")
        },
        settings);
}

snippet source | anchor

Fluent

[Fact]
public Task EnumerableOrderFluent() =>
    Verify(
            new List<Target>
            {
                new("a"),
                new("c"),
                new("b")
            })
        .OrderEnumerableBy<Target>(_ => _.Value);

snippet source | anchor

Result

The resulting file will be:

[
  {
    Value: a
  },
  {
    Value: b
  },
  {
    Value: c
  }
]

snippet source | anchor

OrderEnumerableByDescending

Globally

[ModuleInitializer]
public static void OrderEnumerableByDescendingInitializer() =>
    VerifierSettings.OrderEnumerableByDescending<TargetForGlobalDescending>(_ => _.Value);

snippet source | anchor

Instance

[Fact]
public Task OrderEnumerableByDescending()
{
    var settings = new VerifySettings();
    settings.OrderEnumerableByDescending<Target>(_ => _.Value);
    return Verify(
        new List<Target>
        {
            new("a"),
            new("c"),
            new("b")
        },
        settings);
}

snippet source | anchor

Fluent

[Fact]
public Task OrderEnumerableByDescendingFluent() =>
    Verify(
            new List<Target>
            {
                new("a"),
                new("c"),
                new("b")
            })
        .OrderEnumerableByDescending<Target>(_ => _.Value);

snippet source | anchor

Result

The resulting file will be:

[
  {
    Value: c
  },
  {
    Value: b
  },
  {
    Value: a
  }
]

snippet source | anchor