kaylumah / hosting

MIT License
1 stars 1 forks source link

Investigate Predicates #888

Open kaylumah opened 1 month ago

kaylumah commented 1 month ago
// Copyright (c) Kaylumah, 2024. All rights reserved.
// See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using Ssg.Extensions.Metadata.Abstractions;
using Xunit;

namespace Test.Unit.FormerXunit;

public class TempTest
{
    [Fact]
    public void Test1()
    {

        Serialize.Linq.Serializers.JsonSerializer innerSerializer = new Serialize.Linq.Serializers.JsonSerializer();
        Serialize.Linq.Serializers.ExpressionSerializer serializer 
            = new Serialize.Linq.Serializers.ExpressionSerializer(innerSerializer);

        List<PageMetaData> list = new List<PageMetaData>();
        Expression<Func<PageMetaData, bool>> expression = x => x.Title == "test";

        string expressionText = serializer.SerializeText(expression);
    }

    [Fact]
    public void Test2()
    {

        Serialize.Linq.Serializers.XmlSerializer innerSerializer = new Serialize.Linq.Serializers.XmlSerializer();
        Serialize.Linq.Serializers.ExpressionSerializer serializer 
            = new Serialize.Linq.Serializers.ExpressionSerializer(innerSerializer);

        List<PageMetaData> list = new List<PageMetaData>();
        Expression<Func<PageMetaData, bool>> expression = x => x.Title == "test";

        string expressionText = serializer.SerializeText(expression);

        // Deserialize the expression and cast it back to the original type
        Expression<Func<PageMetaData, bool>> deserializedExpression = serializer.DeserializeText(expressionText) as Expression<Func<PageMetaData, bool>>;

        // Use the deserialized expression
        Func<PageMetaData, bool> func = deserializedExpression.Compile();
    }

    [Fact]
    public void Test3()
    {
        // static bool TestInner(PageMetaData pageMetaData)
        // {
        //     return true;
        // }
        Serialize.Linq.Serializers.XmlSerializer innerSerializer = new Serialize.Linq.Serializers.XmlSerializer();
        Serialize.Linq.Serializers.ExpressionSerializer serializer 
            = new Serialize.Linq.Serializers.ExpressionSerializer(innerSerializer);
        Expression<Func<PageMetaData, bool>> expression = x => TestInner2(x);
        string expressionText = serializer.SerializeText(expression);
    }

    static bool TestInner2(PageMetaData pageMetaData)
    {
        Debug.Assert(pageMetaData != null);
        return false;
    }
}
<ExpressionNode i:type="LambdaExpressionNode"
    xmlns="http://schemas.datacontract.org/2004/07/Serialize.Linq.Nodes"
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <NodeType>Lambda</NodeType>
    <Type>
        <GenericArguments>
            <TypeNode>
                <Name>Ssg.Extensions.Metadata.Abstractions.PageMetaData</Name>
            </TypeNode>
            <TypeNode>
                <Name>System.Boolean</Name>
            </TypeNode>
        </GenericArguments>
        <Name>System.Func`2</Name>
    </Type>
    <Body i:type="MethodCallExpressionNode">
        <NodeType>Call</NodeType>
        <Type>
            <Name>System.Boolean</Name>
        </Type>
        <Arguments>
            <ExpressionNode i:type="ParameterExpressionNode">
                <NodeType>Parameter</NodeType>
                <Type>
                    <Name>Ssg.Extensions.Metadata.Abstractions.PageMetaData</Name>
                </Type>
                <Name>x</Name>
            </ExpressionNode>
        </Arguments>
        <Method>
            <DeclaringType>
                <Name>Test.Unit.FormerXunit.TempTest</Name>
            </DeclaringType>
            <Signature>Boolean TestInner2(Ssg.Extensions.Metadata.Abstractions.PageMetaData)</Signature>
        </Method>
    </Body>
    <Parameters>
        <ExpressionNode i:type="ParameterExpressionNode">
            <NodeType>Parameter</NodeType>
            <Type>
                <Name>Ssg.Extensions.Metadata.Abstractions.PageMetaData</Name>
            </Type>
            <Name>x</Name>
        </ExpressionNode>
    </Parameters>
</ExpressionNode>