I wonder how I can import an Interface Class in C++.NET which was written in VB.NET.
The Dll contains only a Interface class. If I write "Implement iInterface" in VB.NET, it'll load all Functions and Methods from the Interface.dll.
But how can I do this in C++.NET?
This is what the Interface.dll looks like:
Public Interface IPlugin
Function CreateInstance( _
ByRef pntMemory As MemoryArbiter, _
ByRef pntMessageQueue As clsMessageQueue, _
ByRef pntGPIO As clsGPIO, _
ByRef pntProgramSettings As Types, _
ByRef pntDisplayDriver As DisplayDriver _
) As Boolean
Function DeleteInstance() As Boolean
Sub Main_Loop()
Sub ForceUnload()
Sub Interrupt()
End Interface
This structure has to be imported to the c++ code.
In VB.NET it looks like this:
Imports System.ComponentModel.Composition
Imports System.Windows.Forms.Application
Imports Plugin_Interface
Imports SharedLibrary
Imports SharedLibrary.DisplayDriver
<Export(GetType(IPlugin))>
Public Class cPlugin
Implements IPlugin
'.... Class functions come here
I, however, just need a tanslation of the file above so I can import the C++ DLL via MEF in my VB.NET 2010 Application.
Hope someone knows how to handle this ... :)
Related
I am new to Golang and have been exploring but not clear about mocking in unit tests. Can anyone explain following specific questions ?
Question1: For writing unit tests in Golang, why we need to have interfaces to mock methods, why not only struct ?
Question2: Why we inject the interface in struct(where we call external method)
With struct -
type GlobalData struct {}
var (
GlobalObj = GlobalData{}
)
func (g GlobalData) GetGlobalData(a string) string{
return a
}
With interface definition-
type GlobalInterface interface {
GetGlobalData(a string) string
}
type GlobalData struct {}
var (
GlobalObj = GlobalData{}
)
func (g GlobalData) GetGlobalData(a string) string{
return a
}
Thanks
Question 1: For writing unit tests in Golang, why we need to have interfaces to mock methods, why not only struct ?
Answer: Its not mandatory
Question 2: Why we inject the interface in struct(where we call external method)
Answer: Because, it helps you to replace the actual function call (that might trigger some out of scope actions as a part of unit test , such as database call, some API call etc) by injecting a MockStruct (which will be implementing the same interface that is there in the actual code). Polymorphism in simple words.
So, you create a MockStruct and define your own mockMethods to it. As polymorphism, your unit test pick MockStruct without complaining. Calling actual DB or http endpoints do not come under unit testing.
Just for reference, I can point you to one of my github codebase where I wrote a small test case for a file. As you can see I mocked :
GuestCartHandler interface , that allowed me to not call the actual implementation
Mocked sql connection using "github.com/DATA-DOG/go-sqlmock" package. This helped me to avoid establishing actual db client (so, no dependency of database while unit testing)
Let me know if you get the idea conceptually or do you need some more clarification.
If you have methods on types in package user let's say, ex.
package user
type User struct {
name string
}
func (u *User) GetUserProfile() UserProfile{}
And now on import in catalog package :
package catalog
import user
func getUserCatalog(user user.User) []catalog {
user.GetUserProfile()
}
Now to test getUserCatalog method there are 2 ways:
1. var getUserProfileFunc = user.GetUserProfile
using this approach mock can be easily passed at test run time like:
getUserProfile = func() UserProfile {
return fakeUserProfile
}
this is the easiest way to test it.
Now there is another way using interface, in package user add an interface like
type UserInterface interface {
GetUserProfile() UserProfile
}
if User package is a library on which you don't have control then create your own interface, type and use this.
In this case testing in catalog package will become like:
because now methods will be invoked from UserInterface type not from UserType, hence while testing :
UserInterface = fakeUserStruct
and follow below steps
//1. define type of func to return
type typeGetUserProfile func() UserProfile
//2. create a var to return
var mockedGetUserProfile typeGetUserProfile
//3. create a type
type FakeUser struct{}
//4. implement method interface
func (user *FakeUserStruct) GetUserProfile() UserProfile{
return mockedGetUserProfile
}
now when running test :
mockerGetUserProfile = func() UserProfile {
return fakeUserProfile
}
There is mock library which helps in creating boilerplate code for mocking. Check this https://github.com/stretchr/testify
There are many other mock library, but I had used this one, this was really cool.
I hope this helps.
if not please let me know, i'll give some example code and push it to Github.
Also please check https://levelup.gitconnected.com/utilizing-the-power-of-interfaces-when-mocking-and-testing-external-apis-in-golang-1178b0db5a32
I have a given interface specification in the module.mli file. I have to write its implementation in the module.ml file.
module.mli provides an abstract type
type abstract_type
I'm using OUnit to create the tests. I need to use the type's implementation in them. (for example to compare the values) One solution would be to extend the interface to contain additional functions used in the tests.
But is it possible to do such a thing without modifying the interface?
The only way to expose tests without touching the module interface would be to register the tests with some global container. If you have a module called Tests that provides a function register, your module.ml would contain something like this:
let some_test = ...
let () = Tests.register some_test
I don't recommend this approach because the Tests module loses control over what tests it's going to run.
Instead I recommend exporting the tests, i.e. adding them to module.mli.
Note that without depending on OUnit, you can export tests of the following type that anyone can run. Our tests look like this:
let test_cool_feature () =
...
assert ...;
...
assert ...;
true
let test_super_feature () =
...
a = b
let tests = [
"cool feature", test_cool_feature;
"super feature", test_super_feature;
]
The interface is:
...
(**/**)
(* begin section ignored by ocamldoc *)
val test_cool_feature : unit -> bool
val test_super_feature : unit -> bool
val tests : (string * (unit -> bool)) list
some time ago I prepared an idl file to define the interface for plugins of a VB6 application.
Reviewing code I found there is an interface which looks like this:
[
odl,
uuid(<some guid>),
version(1.0),
nonextensible, oleautomation
]
interface IPlugin : IUnknown {
HRESULT DoSomething();
}
This interface is used in some VB6 and C++ components (dlls) to expose the main plugin class, also the exe refers to the typelibrary to keep a reference of plugin classes.
What I would like to do now is to remove odl, version and nonextensible because not required, and then add the object attribute to correctly define a COM interface.
Now that the entire system is in production, can I change attributes on the interface?
Can I do it without compatibility issues?
Thank you
I'm trying to migrate VB.NET to C#:
Public Sub customfunc()
"my codes here"
End Sub
Public Sub main()
Call customfunc()
End Sub
So i'm trying to call customfunc() inside main() but there is no call function in c#.
How do I do this in c#?
Just do it without the Call:
customfunc();
This seems like an easy enough issue but I can't seem to find the keywords to effect my searches.
I'm trying to unit test by mocking out all objects within this method call. I am able to do so to all of my own creations except for this one:
public void MyFunc(MyVarClass myVar)
{
Image picture;
...
picture = Image.FromStream(new MemoryStream(myVar.ImageStream));
...
}
FromStream is a static call from the Image class (part of c#). So how can I refactor my code to mock this out because I really don't want to provide a image stream to the unit test.
You can create an IImageLoader interface. The "normal" implementation just calls Image.FromStream, while your unit test version can do whatever you need it to do.
Moq and most other mocking frameworks don't support mocking out static methods. However, TypeMock does support mocking out static methods and that might be of interest to you if you're willing to purchase it. Otherwise, you'll have to refactor so that an interface can be mocked...
You could wrap the static function into Func type property which can be set by the unit test with a mock or stub.
public class MyClass
{
..
public Func<Image, MemoryStream> ImageFromStream =
(stream) => Image.FromStream(stream);
public void MyFunc(MyVarClass myVar)
{
...
picture = ImageFromStream(new MemoryStream(myVar.ImageStream));
...
}
..
}
This can be acheived with Moles, a Visual Studio 2010 Power Tools. The Moles code would look like this:
// replace Image.FromStream(MemoryStream) with a delegate
MImage.FromStreamMemoryStream = stream => null;