IronPython lives!

No comments

Thursday, August 22, 2013

A few years ago I’ve decided to learn IronPython and so I’ve read a book, wrote some code and blogged about my experience.

I really enjoyed coding in Python and the connection with .NET seemed to be promising – that is until Microsoft pulled the plug on IronPython and its sister language IronRuby.

I felt saddened by the untimely end of Python & .NET and moved on.

Two days ago I powered my podcast player and I thought found a new episode on Hanselminutes - Python Tools for Visual Studio with Dino Viehland.

At first the title seemed very 2009 – since I believed that Microsoft has ditched Python – but decided to listen.

It seems  that IronPython is live and kicking – mostly due to community contribution –At the time of this post a new version of IronPython (2.7.4) is making it’s way from RC to stable version.

logo ptvs 20 betaAnd Microsoft has been developing it’s Python tooling a.k.a Python Tools for Visual Studio again by using the community as well as a (small) team dedicated (more or less) to the cause of bringing Python development to Visual Studio.

Why has Microsoft been investing in Python you ask – simple:

Azure + Django, that’s right you can use Python (CPython) to create web sites hosted on Azure – although I haven’t tried it myself – the tutorials are already in place.

 

So it seems that the reports of IronPython death have been greatly exaggerated, or at least premature.

Simple Mocking in C++

No comments

Sunday, August 11, 2013

I’ve been working in C++ for a few months now – after three years we’ve been apart.
I always amazes me how simple things I took for granted do not exist (at least not out of the box) in C++, one of which are reflection.
Two weeks ago I needed to make sure that a method was called in a test, I could have used Google’s mocking framework but I needed simpler and quicker solution. I just wanted to know in a specific point at the test if a method was called.
Although C++ does not have reflection it does have macros (i.e. #Define) and I’ve utilized the C++ macros to create the following class:
#ifndef __TESTLISTENERBASE_H__
#define __TESTLISTENERBASE_H__

#include <unordered_set>
#include <string>

class TestListenerBase
{
protected:
    void AddMethodCall(std::string methodName)
    {
        _methodsCalled.insert(methodName);
    }

    bool WasCalled(std::string methodName)
    {
        return _methodsCalled.count(methodName) > 0;
    }
private:
    std::unordered_set<std::string> _methodsCalled;
};

#define REGISTER_METHOD(METHODNAME) \
    void METHODNAME () override\
    { \
        AddMethodCall(#METHODNAME); \
    } \
    bool METHODNAME##WasCalled() \
    { \
        return WasCalled(#METHODNAME); \
    } 

#endif

By inheriting from the class above I could define the “mocked” methods, each method would have another method  auto-generated so I can test if the method was called.

#ifndef __MY_CLASS_MOCK__
#define __MY_CLASS_MOCK__

#include "TestListenerBase.h"
#include "MyClass.h"

class MyClassMock : public MyClass, public TestListenerBase
{
public:
    REGISTER_METHOD(DoSomething);
};

#endif

Using this solution I needed to declare each method, the good news is that renaming the method would cause the mocked method name to change as well.

Using the new "mock object" is a simple as writing code:

#include "stdafx.h"
#include <memory>
#include "MyClassMock.h"

int _tmain(int argc, _TCHAR* argv[])
{
    MyClassMock mock;

    bool result = mock.DoSomethingWasCalled(); // return false

    mock.DoSomething();

    result = mock.DoSomethingWasCalled(); // result true

    return 0;
}

That’s it – it’s not perfect but it works.

In case you like parameters in your method you’ll have to improve this example – it’s not hard and I know you’re up to the task.

Happy coding…
Related Posts Plugin for WordPress, Blogger...