nanotap.h Source File

- Main Page
- Files

# nanotap.h

Go to the documentation of this file.

    00001 #ifndef NANOTAP_H_
    00002 #define NANOTAP_H_
    00003
    00004 #include <stdio.h>
    00005 #include <string.h>
    00006 #include <stdlib.h>
    00007
    00008 #ifdef __GNUC__
    00009 #define NANOTAP_DECLARE static __attribute__((__used__))
    00010 #else
    00011 #define NANOTAP_DECLARE static
    00012 #endif
    00013
    00014 #ifdef __cplusplus
    00015 #define NANOTAP_INLINE inline
    00016 #else
    00017 #define NANOTAP_INLINE __inline__
    00018 #endif
    00019
    00020 static int TEST_COUNT = 0;
    00021
    00022 /**
    00023  * This simply evaluates any expression ("$got eq $expected" is just a
    00024  * simple example) and uses that to determine if the test succeeded or
    00025  * failed.  A true expression passes, a false one fails.  Very simple.
    00026  */
    00027 NANOTAP_INLINE NANOTAP_DECLARE void ok(int x, const char *msg) {
    00028     printf("%s %d - %s\n", (x ? "ok" : "not ok"), ++TEST_COUNT, msg ? msg : "");
    00029 }
    00030
    00031 /**
    00032  * display diagnostics message.
    00033  */
    00034 NANOTAP_INLINE NANOTAP_DECLARE void diag(const char *msg) {
    00035     fprintf(stderr, "# %s\n", msg ? msg : "");
    00036 }
    00037 /**
    00038  * display note message.
    00039  */
    00040 NANOTAP_INLINE NANOTAP_DECLARE void note(const char *msg) {
    00041     fprintf(stdout, "# %s\n", msg ? msg : "");
    00042 }
    00043
    00044 /**
    00045  * contains_string() searches for $substring in $string.
    00046  */
    00047 NANOTAP_INLINE NANOTAP_DECLARE void contains_string(const char *string, const char *substring, const char *msg) {
    00048     ok(strstr(string, substring) != NULL, msg);
    00049 }
    00050
    00051 /**
    00052  *  If you don’t know how many tests you’re going to run, you can issue
    00053  * the plan when you’re done running tests.
    00054  */
    00055 NANOTAP_INLINE NANOTAP_DECLARE void done_testing() {
    00056     printf("1..%d\n", TEST_COUNT);
    00057     exit(0);
    00058 }
    00059
    00060 #ifdef __cplusplus
    00061 // you can use more convinient stuff if you are using c++.
    00062
    00063 #include <string>
    00064 #include <iostream>
    00065
    00066 /**
    00067  * shorthand for std::string
    00068  */
    00069 inline NANOTAP_DECLARE void diag(const std::string &msg) {
    00070     diag(msg.c_str());
    00071 }
    00072
    00073 /**
    00074  * flexible is() based on C++ template.
    00075  */
    00076 template <class T>
    00077 inline NANOTAP_DECLARE void is(T got, T expected, const char *msg) {
    00078     if (got == expected) {
    00079         ok(true, msg);
    00080     } else {
    00081         ok(false, msg);
    00082         std::cout << "  # got      : " << got << std::endl;
    00083         std::cout << "  # expected : " << expected << std::endl;
    00084     }
    00085 }
    00086
    00087 inline NANOTAP_DECLARE void is(const std::string& got, const char *expected, const char *msg) {
    00088     is(got, std::string(expected), msg);
    00089 }
    00090
    00091 inline NANOTAP_DECLARE void is(const char* got, const std::string & expected, const char *msg) {
    00092     is(std::string(got), expected, msg);
    00093 }
    00094
    00095 template <class T, class U>
    00096 inline NANOTAP_DECLARE void is(T got, U expected) {
    00097     is(got, std::string(expected), NULL);
    00098 }
    00099
    00100 /**
    00101  * shorthand for lazy person
    00102  */
    00103 inline NANOTAP_DECLARE void ok(int x) {
    00104     ok(x, "");
    00105 }
    00106
    00107 /**
    00108  * shorthand for std::string
    00109  */
    00110 inline NANOTAP_DECLARE void contains_string(const std::string &str, const char *substr, const char *msg) {
    00111     contains_string(str.c_str(), substr, msg);
    00112 }
    00113 #endif
    00114
    00115 #endif /* NANOTAP_H_ */