C++ Only Features: Difference between revisions

From STRIDE Wiki
Jump to navigation Jump to search
(Created page with '=== Use Operator << to Augment Test Macros === In C++ test code Test Point, Test Log and Test Code macros support adding to the annotation using the << operator. For…')
 
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=== Use Operator << to Augment Test Macros ===
=== Use Operator << to Augment Test Macros ===


In C++ test code [[Test Point]], [[Test Log]] and [[Test Code]] macros support adding to the annotation using the << operator. For example:
In C++ test code [[Test Point]], [[Test Log]] and [[Test Code Macros|Test Code]] macros support adding to the annotation using the << operator. For example:


<source lang="cpp">
<source lang="cpp">
srEXPECT_TRUE(a != b) << "My custom message";
srEXPECT_TRUE(a != b) << "My custom message" << " with more data " << 1234;
</source>
</source>


Line 11: Line 11:
* C string (char* or wchar_t*), and  
* C string (char* or wchar_t*), and  
* types allowing implicit cast to numeric type or "C" string.  
* types allowing implicit cast to numeric type or "C" string.  
=== Overloading the << operator ===


You can also overload the << operator in order to annotate reports using your own custom type. An example is below.
You can also overload the << operator in order to annotate reports using your own custom type. An example is below.
Line 44: Line 46:
</source>
</source>


[[Category:Testing]]
[[Category:Test Units]]

Latest revision as of 23:13, 23 February 2012

Use Operator << to Augment Test Macros

In C++ test code Test Point, Test Log and Test Code macros support adding to the annotation using the << operator. For example:

srEXPECT_TRUE(a != b) << "My custom message" << " with more data " << 1234;

As delivered, the macros will support stream input annotations for:

  • all numeric types,
  • C string (char* or wchar_t*), and
  • types allowing implicit cast to numeric type or "C" string.

Overloading the << operator

You can also overload the << operator in order to annotate reports using your own custom type. An example is below.

The following will compile and execute successfully given that the << operator is overloaded as shown:

#include <srtest.h>

// MyCustomClass implementation
class MyCustomClass
{
public:
   MyCustomClass(int i) : m_int(i) {}

private: 
   int m_int; 
   friend stride::Message& operator<<(stride::Message& ss, const MyCustomClass& obj);
}; 

stride::Message& operator<<(stride::Message& ss, const MyCustomClass& obj)
{
   ss << obj.m_int;
   return ss;
}

void test()
{
    MyCustomClass custom(34); 

    srEXPECT_FALSE(true) << custom;
}