📄 test/JsonLdRecipeParser.Tests/Json/JsonLdSerializerContextTests/DeserializeShould.cs
using System;
using System.Text.Json;
using JsonLdRecipeParser.Json;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;

namespace JsonLdRecipeParser.Tests.Json.JsonLdSerializerContextTests;

[TestClass]
public class DeserializeShould
{
    [TestMethod]
    public void DeserializeAnEmptyArray()
    {
        // Arrange
        var json = "[]";

        // Act
        var result = JsonSerializer.Deserialize(json, JsonLdSerializerContext.Default.JsonLd);

        // Assert
        result.ShouldNotBeNull();
        var things = result.Value.ShouldBeOfType<Thing[]>();
        things.ShouldBeEmpty();
    }

    [TestMethod]
    public void DeserializeJsonLdFromGoogleExample()
    {
        // Act
        var result = JsonSerializer.Deserialize(TestData.GoogleExample, JsonLdSerializerContext.Default.JsonLd);

        // Assert
        result.ShouldNotBeNull();
        var recipe = result.Value.ShouldBeOfType<JsonLdRecipeParser.Json.Recipe>();
        recipe.ShouldBeEquivalentTo(
            new JsonLdRecipeParser.Json.Recipe
            {
                Name = "Non-Alcoholic Piña Colada",
                Description = "This non-alcoholic pina colada is everyone's favorite!",
                Author = new Authors
                {
                    Value = new JsonLdRecipeParser.Json.Author { Value = new Person { Name = "Mary Stone" } },
                },
                PrepTime = TimeSpan.FromMinutes(1),
                TotalTime = TimeSpan.FromMinutes(3),
                CookTime = TimeSpan.FromMinutes(2),
                Nutrition = new NutritionInformation { Calories = new Quantity { Value = "120 calories" } },
                RecipeCategory = new RecipeCategories { Value = "Drink" },
                RecipeCuisine = new RecipeCuisines { Value = "American" },
                RecipeIngredient =
                [
                    new RecipeIngredient { Value = "400ml of pineapple juice" },
                    new RecipeIngredient { Value = "100ml cream of coconut" },
                    new RecipeIngredient { Value = "ice" },
                ],
                RecipeInstructions =
                [
                    new RecipeInstruction
                    {
                        Value = new HowToStep
                        {
                            Name = "Blend",
                            Text = "Blend 400ml of pineapple juice and 100ml cream of coconut until smooth.",
                        },
                    },
                    new RecipeInstruction
                    {
                        Value = new HowToStep { Name = "Fill", Text = "Fill a glass with ice." },
                    },
                    new RecipeInstruction
                    {
                        Value = new HowToStep
                        {
                            Name = "Pour",
                            Text = "Pour the pineapple juice and coconut mixture over ice.",
                        },
                    },
                ],
                RecipeYield = new Quantity { Value = "4 servings" },
            }
        );
    }

    [TestMethod]
    public void DeserializeJsonLdFromGoogleExampleInArray()
    {
        // Arrange
        const string json = $"[{TestData.GoogleExample}]";

        // Act
        var result = JsonSerializer.Deserialize(json, JsonLdSerializerContext.Default.JsonLd);

        // Assert
        result.ShouldNotBeNull();
        var things = result.Value.ShouldBeOfType<Thing[]>();
        things.Length.ShouldBe(1);
        things[0].ShouldBeOfType<JsonLdRecipeParser.Json.Recipe>();
    }
}