Test cases in C/C++ source files

Unit tests are tightly related to specific sections of source code, usually this means each tests belongs to one function. Therefore it makes sense to keep test specifications in the same file with source code. To make this possible, testIDEA can parse special C/C++ comments, which contain test specifications. Comments must start with string '/*#', which is a standard C/++ multi-line comment followed by '#'. Example:
    /*#
    - func: [min_int, [], rv]
      params: [3, 5]
      expect:
      - rv == 3
    */

    int min_int(int a, int b)
    {
      if (a < b)
        return a;

      return b;
    }
    
This way we can easily adapt tests to any changes in the source code. Of course it is also possible to have more than one test specification in one comment block.

Test specifications can be edited either directly in the source code editor, or the source code file is opened and edited in testIDEA. testIDEA modifies only test specifications in the special code comments, while the rest of the file is left intact. testIDEA also does not add any new test comments to the source file - all test specifications are stored into the existing comments. Therefore it is a good idea to tell testIDEA where to put test specifications by manually adding short test comments to places, where we want to have test specification. For example, the following snippet shows test specification added manually - the rest will be specified in testIDEA:

    /*#
       - id: min
     */

    int min_int(int a, int b)
    {
      if (a < b)
        return a;

      return b;
    }
    

Rules for source code test comments

  1. Test comments must start with /*#. These three characters must be the first non-space characters in the line. Example:

    Valid:

       /*# - func: [f, [], rv] */
       
    Not valid:
       a = 3; /*# - func: [f, [], rv] */
       // testIDEA will ignore such test specifications.
       
    Similar applies to the end of comment token '*/'. It must the last token in line. The following is not valid:
       /*# - func: [f, [], rv] */ a = 3;
       
    testIDEA will report an error in such case.

    Test specification may start immediately in the same line as comment start, but for readability purposes it is recommended to start it on a new line:

       /*#
           - func: [f, [], rv] 
        */
       
    Note: If there is more than one line in test spec, it is highly recommended to start test spec on the line after comment start for readability reasons. The following is NOT recommended:
       /*# - func: [f]
          expect:
          - rv == 0
        */
       
  2. testIDEA loads files with extension iyaml as pure YAML files. If the file has any other extension, it is loaded as source code file, where test specifications are stored in special source code comments, as described in this section.

    If file is opened as iyaml file, it is not possible to save it as source code file, because it contains no source code. However, if it is opened as source code file, we can save it as iyaml file (the source code is not saved).

  3. Indentation may differ between comments, but it must be consistent inside one comment.

  4. Tabs inside test specifications are forbidden. See: http://www.yaml.org/faq.html. It is not a good idea to use tabs in source code anyway.

  5. Hierarchy of test specifications can be defined in one of two ways:

  6. Test configuration must start with special comment /*~. There may be only one such comment in one source file.

  7. testIDEA never changes anything outside of test comments. Inside test comments testIDEA tries to do its best to keep the formatting of test specifications intact. For example, if you open the source code file and then save it without changes, indentation of test specifications does not change. However there are two cases, when formatting changes (semantic is preserved!):