-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to dynamically execute a code written from Visual Studio. #32
Comments
This can be done in multiple ways. Let's start with a simple example of compiling an extending the Person Object
var visualStudio = new VisualStudio_2010();
return visualStudio.dte().ActiveDocument; which in this case is
var person = new VS_REPL_Examples.Person();
person.Name = "This is the Name";
return person;
//O2File:c:\users\o2\documents\visual studio 2010\Projects\VS_REPL_Examples\Person.cs
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public bool Admin { get; set; }
}
}
var person = new VS_REPL_Examples.Person();
person.Name = "This is the Name..";
person.Admin = true;
person.Age = 42;
return person;
//O2File:c:\users\o2\documents\visual studio 2010\Projects\VS_REPL_Examples\Person.cs
return new VS_REPL_Examples.Person
{
Name = "Now using Properties",
Admin = true,
Age = 42
};
//O2File:c:\users\o2\documents\visual studio 2010\Projects\VS_REPL_Examples\Person.cs
Func<string,Person> newPerson=
(name)=>{
return new Person
{
Name = name,
Admin = true,
Age = 42
};
};
return newPerson("Now using a Lamdba method");
//using VS_REPL_Examples
//O2File:c:\users\o2\documents\visual studio 2010\Projects\VS_REPL_Examples\Person.cs |
Here is a variation of the above example (in this case we are going to run the code changes on top of a large VS projet which contains an WinForms control that hosts an WPF control (in this case GraphX).
the FluentSharp.REPL package and the FluentSharp.WPF package
using FluentSharp.NUnit;
using NUnit.Framework;
namespace GraphX.FluentSharp.Tests
{
[TestFixture]
public class Test_WindowsFormsProject
{
[Test]
public void Assert_Test_Runs()
{
var value = 1;
(value == 1).assert_True();
(value != 2).assert_False();
}
}
} which I executed using Resharper's Nunit runner: as expected the original test failed (since
After adding a reference to the WindowsFormsProject I am able to create and instance of the WindowsFormsProject's Form1 object: directly from my UnitTest using WindowsFormsProject;
using FluentSharp.NUnit;
using NUnit.Framework;
namespace GraphX.FluentSharp.Tests
{
[TestFixture]
public class Test_WindowsFormsProject
{
[Test]
public void Create_WindowsFormsProject_MainControl()
{
var form1 = new Form1();
form1.assert_Not_Null();
}
}
} Here is the execution result of the test shown above: (note that in the example shown above, I'm using NCrunch to execute the tests and show code coverage)
[Test]
public void Create_WindowsFormsProject_MainControl()
{
var form1 = O2Thread.staThread(()=>new Form1());
form1.assert_Not_Null();
}
a) top-left the code/control we are testing
using System.Windows.Forms;
using WindowsFormsProject;
using FluentSharp.CoreLib;
using FluentSharp.CoreLib.API;
using FluentSharp.NUnit;
using FluentSharp.WinForms;
using NUnit.Framework;
namespace GraphX.FluentSharp.Tests
{
[TestFixture]
public class Test_WindowsFormsProject
{
[Test]
public void Create_WindowsFormsProject_Form_Directly()
{
Form1 form1 = null;
O2Thread.staThread(()=>
{
form1= new Form1();
form1.ShowDialog();
});
Processes.getCurrentProcess().waitFor_MainWindowHandle();
form1.assert_Not_Null();
form1.controls().assert_Size_Is(3);
form1.waitForClose();
}
[Test]
public void Create_WindowsFormsProject_Form_Via_Exe()
{
var assembly = "WindowsFormsProject".assembly().assert_Not_Null();
var program = assembly.type("Program").assert_Not_Null();
var main = program.method("Main").assert_Not_Null();
O2Thread.staThread(()=>main.invokeStatic());
Processes.getCurrentProcess().waitFor_MainWindowHandle();
var form1 = Application.OpenForms.assert_Not_Empty()
.first().cast<Form1>()
.assert_Not_Null();
//form1.script_Me_WaitForClose();
} Note that both tests execute in less that 2 secs
var elementHost = form1.control<ElementHost>();
return elementHost;
//O2Ref:WindowsFormsProject.exe
//O2Ref:FluentSharp.WPF
//using FluentSharp.WPF
//using System.Windows.Forms.Integration
//O2Ref:WindowsFormsIntegration.dll
//O2Ref:PresentationCore.dll
//O2Ref:WindowsBase.dll
//O2Ref:PresentationFramework.dll
//O2Ref:System.Xaml.dll
var elementHost = form1.control<ElementHost>();
var vertextControls = elementHost.Child.controls_Wpf<GraphX.VertexControl>();
return vertextControls;
//O2Ref:GraphX.Controls.dll
var elementHost = form1.control<ElementHost>();
var vertextControls = elementHost.Child.controls_Wpf<GraphX.VertexControl>();
var graphAreaExample = elementHost.Child.control_Wpf<WindowsFormsProject.GraphAreaExample>();
//graphAreaExample.add_Label_Wpf("This is a new Label");
//return graphAreaExample.type().baseTypes();
return graphAreaExample.controls_Wpf();
//O2Ref:GraphX.Controls.dll
//O2Ref:GraphX.Common.dll
//O2Ref:QuickGraph.dll
//O2Ref:FluentSharp.WPF
//using FluentSharp.WPF
//using System.Windows.Forms.Integration
//O2Ref:WindowsFormsProject.exe
//O2Ref:WindowsFormsIntegration.dll
//O2Ref:PresentationCore.dll
//O2Ref:WindowsBase.dll
//O2Ref:PresentationFramework.dll
//O2Ref:System.Xaml.dll
Which will (after building the project) make the first unit test written fail To fix the unit test we need to change the expected size in |
(based on this question on the O2 Platform mailing list)
For a simplified example,in Visual Studio:
...and from O2 REPL
The text was updated successfully, but these errors were encountered: