📄 test/JsonLdRecipeParser.Tests/AmountTests/FromShould.cs
using JsonLdRecipeParser.Json;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;

namespace JsonLdRecipeParser.Tests.AmountTests;

[TestClass]
public class FromShould
{
    [TestMethod]
    public void MapAStringQuantity()
    {
        // Arrange
        var quantity = new Quantity { Value = "4 servings" };

        // Act
        var result = Amount.From(quantity);

        // Assert
        result.ShouldBeEquivalentTo(new Amount { Value = new ExactAmountValue(4.0d), Unit = "servings" });
    }

    [TestMethod]
    public void MapAQuantitativeValueWithDoubleValue()
    {
        // Arrange
        var quantity = new Quantity { Value = new QuantitativeValue { Value = new ValueProperty { Value = 400.0d } } };

        // Act
        var result = Amount.From(quantity);

        // Assert
        result.ShouldBeEquivalentTo(new Amount { Value = new ExactAmountValue(400.0d), Unit = null });
    }

    [TestMethod]
    public void MapAQuantitativeValueWithStringValue()
    {
        // Arrange
        var quantity = new Quantity { Value = new QuantitativeValue { Value = new ValueProperty { Value = "2.5" } } };

        // Act
        var result = Amount.From(quantity);

        // Assert
        result.ShouldBeEquivalentTo(new Amount { Value = new ExactAmountValue(2.5d), Unit = null });
    }

    [TestMethod]
    public void MapAQuantitativeValueWithUnitText()
    {
        // Arrange
        var quantity = new Quantity
        {
            Value = new QuantitativeValue
            {
                Value = new ValueProperty { Value = 400.0d },
                UnitText = "ml",
            },
        };

        // Act
        var result = Amount.From(quantity);

        // Assert
        result.ShouldBeEquivalentTo(new Amount { Value = new ExactAmountValue(400.0d), Unit = "ml" });
    }

    [TestMethod]
    public void MapAQuantitativeValueWithUnitCodeWhenUnitTextIsMissing()
    {
        // Arrange
        var quantity = new Quantity
        {
            Value = new QuantitativeValue
            {
                Value = new ValueProperty { Value = 250.0d },
                UnitCode = "GRM",
            },
        };

        // Act
        var result = Amount.From(quantity);

        // Assert
        result.ShouldBeEquivalentTo(new Amount { Value = new ExactAmountValue(250.0d), Unit = "GRM" });
    }

    [TestMethod]
    public void PreferUnitTextOverUnitCode()
    {
        // Arrange
        var quantity = new Quantity
        {
            Value = new QuantitativeValue
            {
                Value = new ValueProperty { Value = 10.0d },
                UnitText = "dl",
                UnitCode = "DLT",
            },
        };

        // Act
        var result = Amount.From(quantity);

        // Assert
        result.ShouldBeEquivalentTo(new Amount { Value = new ExactAmountValue(10.0d), Unit = "dl" });
    }
}