using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using Shiji.Base;
using Shiji.HR.Dal;
namespace Test.Bll
{
/// <summary>
/// PythonTest 的摘要说明
/// </summary>
[TestClass]
public class PythonTest
{
ScriptEngine engine;
ScriptScope scope;
public PythonTest()
{
engine = Python.CreateEngine();
scope = engine.CreateScope();
}
private TestContext testContextInstance;
/// <summary>
///获取或设置测试上下文,该上下文提供
///有关当前测试运行及其功能的信息。
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
[TestMethod]
public void 测试简单表达式()
{
var strExpression = "1+2";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
var actual = sourceCode.Execute<int>();
Assert.AreEqual(3, actual);
}
[TestMethod]
public void 测试传入变量()
{
var strExpression = "\"Hello:\" + str";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("str", "Python");
var actual = sourceCode.Execute<string>(scope);
scope.RemoveVariable("str");
Assert.AreEqual("Hello:Python", actual);
}
[TestMethod]
public void 测试C引用Python函数()
{
var strExpression = @"
def MyFunction(n):
return 2*n
";
var sourceCode = engine.CreateScriptSourceFromString(strExpression).Compile().Execute(scope);
var func = engine.GetVariable<Func<int, int>>(scope, "MyFunction");
Assert.AreEqual(0, func(0));
Assert.AreEqual(2, func(1));
Assert.AreEqual(4, func(2));
}
[TestMethod]
public void 测试Python引用C函数()
{
var strExpression = "CMethod('Python')";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("CMethod", (Func<string, string>)TMethod);
var actual = sourceCode.Execute<string>(scope);
scope.RemoveVariable("CMethod");
Assert.AreEqual("Hello:Python", actual);
}
public string TMethod(string info)
{
return "Hello:" + info;
}
[TestMethod]
public void 测试Python引用C函数2()
{
var strExpression = "CMethod('Python')";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("CMethod", (Func<string, string>)T2Method);
var actual = sourceCode.Execute<string>(scope);
scope.RemoveVariable("CMethod");
Assert.AreEqual("Hello:Python", actual);
}
public string T2Method(string info)
{
return TMethod(info);
}
[TestMethod]
public void 测试Linq1()
{
var mc = App.DB.GetTable<HR_MetaClass>().First(p => p.Code == "HR_Catalog");
//mc.HR_MetaProperty.Count
//此处调用属性
var strExpression = "mc.HR_MetaProperty.Count";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("mc", mc);
var actual = sourceCode.Execute<int>(scope);
scope.RemoveVariable("mc");
Assert.AreEqual(12, actual);
}
[TestMethod]
public void 测试Linq2()
{
var mc = App.DB.GetTable<HR_MetaClass>().First(p => p.Code == "HR_Catalog");
//mc.HR_MetaProperty.Count
//此处调用方法,具体可以参看http://blogs.msdn.com/sreekarc/archive/2007/05/01/extension-methods-and-delegates.aspx
var strExpression = @"
import clr
clr.AddReference('System.Core')
from System.Linq import*
Enumerable.Count(mc.HR_MetaProperty)";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("mc", mc);
var actual = sourceCode.Execute<int>(scope);
scope.RemoveVariable("mc");
Assert.AreEqual(12, actual);
}
[TestMethod]
public void 测试Linq3()
{
var mc = App.DB.GetTable<HR_MetaClass>().First(p => p.Code == "HR_Catalog");
var strExpression = @"
import clr
clr.AddReference('System.Core')
from System.Linq import*
Enumerable.First(mc.HR_MetaProperty,lambda p:p.Code=='Code')";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("mc", mc);
var actual = sourceCode.Execute<HR_MetaProperty>(scope);
scope.RemoveVariable("mc");
Assert.AreEqual("Code", actual.Code);
}
}
}
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using Shiji.Base;
using Shiji.HR.Dal;
namespace Test.Bll
{
/// <summary>
/// PythonTest 的摘要说明
/// </summary>
[TestClass]
public class PythonTest
{
ScriptEngine engine;
ScriptScope scope;
public PythonTest()
{
engine = Python.CreateEngine();
scope = engine.CreateScope();
}
private TestContext testContextInstance;
/// <summary>
///获取或设置测试上下文,该上下文提供
///有关当前测试运行及其功能的信息。
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
[TestMethod]
public void 测试简单表达式()
{
var strExpression = "1+2";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
var actual = sourceCode.Execute<int>();
Assert.AreEqual(3, actual);
}
[TestMethod]
public void 测试传入变量()
{
var strExpression = "\"Hello:\" + str";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("str", "Python");
var actual = sourceCode.Execute<string>(scope);
scope.RemoveVariable("str");
Assert.AreEqual("Hello:Python", actual);
}
[TestMethod]
public void 测试C引用Python函数()
{
var strExpression = @"
def MyFunction(n):
return 2*n
";
var sourceCode = engine.CreateScriptSourceFromString(strExpression).Compile().Execute(scope);
var func = engine.GetVariable<Func<int, int>>(scope, "MyFunction");
Assert.AreEqual(0, func(0));
Assert.AreEqual(2, func(1));
Assert.AreEqual(4, func(2));
}
[TestMethod]
public void 测试Python引用C函数()
{
var strExpression = "CMethod('Python')";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("CMethod", (Func<string, string>)TMethod);
var actual = sourceCode.Execute<string>(scope);
scope.RemoveVariable("CMethod");
Assert.AreEqual("Hello:Python", actual);
}
public string TMethod(string info)
{
return "Hello:" + info;
}
[TestMethod]
public void 测试Python引用C函数2()
{
var strExpression = "CMethod('Python')";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("CMethod", (Func<string, string>)T2Method);
var actual = sourceCode.Execute<string>(scope);
scope.RemoveVariable("CMethod");
Assert.AreEqual("Hello:Python", actual);
}
public string T2Method(string info)
{
return TMethod(info);
}
[TestMethod]
public void 测试Linq1()
{
var mc = App.DB.GetTable<HR_MetaClass>().First(p => p.Code == "HR_Catalog");
//mc.HR_MetaProperty.Count
//此处调用属性
var strExpression = "mc.HR_MetaProperty.Count";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("mc", mc);
var actual = sourceCode.Execute<int>(scope);
scope.RemoveVariable("mc");
Assert.AreEqual(12, actual);
}
[TestMethod]
public void 测试Linq2()
{
var mc = App.DB.GetTable<HR_MetaClass>().First(p => p.Code == "HR_Catalog");
//mc.HR_MetaProperty.Count
//此处调用方法,具体可以参看http://blogs.msdn.com/sreekarc/archive/2007/05/01/extension-methods-and-delegates.aspx
var strExpression = @"
import clr
clr.AddReference('System.Core')
from System.Linq import*
Enumerable.Count(mc.HR_MetaProperty)";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("mc", mc);
var actual = sourceCode.Execute<int>(scope);
scope.RemoveVariable("mc");
Assert.AreEqual(12, actual);
}
[TestMethod]
public void 测试Linq3()
{
var mc = App.DB.GetTable<HR_MetaClass>().First(p => p.Code == "HR_Catalog");
var strExpression = @"
import clr
clr.AddReference('System.Core')
from System.Linq import*
Enumerable.First(mc.HR_MetaProperty,lambda p:p.Code=='Code')";
var sourceCode = engine.CreateScriptSourceFromString(strExpression);
scope.SetVariable("mc", mc);
var actual = sourceCode.Execute<HR_MetaProperty>(scope);
scope.RemoveVariable("mc");
Assert.AreEqual("Code", actual.Code);
}
}
}