﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Dotexp.Core;
using System.Linq.Expressions;
using System.Text;

namespace Dotexp.Tests
{
    [TestClass]
    public class ParserAndSerializerTests
    {
        [TestMethod]
        public void ParseBasicExpression()
        {
            var parser = new ExpressionParser();
            var exp = parser.Parse("(Lambda (Add (Add 1 (MakeMemberAccess \"abc\" :System.String.Length)) 5))");

            Assert.AreEqual(9, ((LambdaExpression)exp).Compile().DynamicInvoke());
        }

        [TestMethod]
        public void SerializeParseBasicExpression()
        {
            var code = new StringBuilder();
            var serializer = new ExpressionSerializer(code);

            Expression<Func<int>> originalExp = () => "abc".Length + 1 + 5;

            serializer.Visit(originalExp);

            Console.WriteLine(code.ToString());

            var exp = new ExpressionParser().Parse(code.ToString());

            Assert.AreEqual(9, ((LambdaExpression)exp).Compile().DynamicInvoke());
        }
    }
}
