Chapter 2. Automatically Create Mock Object Sources

Table of Contents

2.1. Parsing The Header File
2.2. Generating The Mock Object Files

The process for the creation of the source files from C++ headers is split into two parts:

Splitting the process into two parts enables postprocessing the XML file with simple methods. Otherwise a rather complicated C++ parser based on less known tools like yacc and lex would have to be changed.

Additionally one could create a different application for the second step. Based on the XML file it could create totally different user defined C++ sources.

If you integrate the two applications into your build process, your mock object files are automatically kept up to date with the production sources. You simply make the mock object files depend on the according header file. If you need specialized constuctors you will place them in the user defined third file. This file is never updated automatically. Instead a template file is written from where you can copy-and-paste what you need and adjust the rest manually.

2.1. Parsing The Header File

In the first step mockpp2xml parses the header file and outputs the according XML data stream to standard output. If you want to have an XML file you must redirect standard outout to the desired file. The only possible parameter is the name of the C++ header file. All the classes in the header file are written to the XML stream.

The parser needs markers whch methods and constructors shall be written to the XML stream. This is done by appending a pseudo keyword after one of the access modifiers public, protected or private before the definition.

The parser distinguishes between constructors and methods. Each of them uses a different keyword:

mockpp_constructor

This keyword includes the directly following constructor (and only this one) in the XML stream.

mockpp_methods

All the methods after this keyword are written to the XML stream. The inclusion stops at the next access modifier unless it contains this keyword again.

Destructors are never included even if they have a starting marker. To make these keywords invisible for the compiler you define these two with empty content.

A sample class could then look like this:


#define mockpp_constructor
#define mockpp_methods

class MockppIdlTest : public IdlInterface
{
  public mockpp_constructor:
    MockppIdlTest(int i, const std::string &s)
      : IdlInterface("name")
    {
    }

  private mockpp_methods:
    virtual std::string * url4() = 0;
};

The following line shows how to invoke the C++ parser and write the XML stream to a file:


  mockpp2xml   header.h  >header-data.xml

If you are familiar with with development around KDE and QT you are probably familiar with this approach.

It might also be interesting to know that the basis for this parser was taken from the KDE Project. For that reason it is able to parse (in the meaning of to ignore) the special keywords related to DCOP and QOBJECT like k_dcop or signals.

[Note]The Difficulty of Parsing C++

Parsing even valid C++ source code is is a difficult task. xml2mockpp handles most of the common files. But there may be problems with some code, for example nested templates. In the worst case you may have to remove or comment out the problematic parts while parsing. Probably it is possible to use #define's with the preprocessor to do this automatically.

The test files mockpp/generator/mockpp2xml/dcopidl_test.h and mockpp/generator/mockpp2xml/mockppidl_test.h give an overview of what the parser currently is able to process.

In case you have a file which is not parsed correctly but you think this should be the case since it looks trivial: please post a bug report.