aboutsummaryrefslogtreecommitdiff
path: root/src/stable/unitsuite.h
blob: b1e2953c161712483c70e95aca9764ea2156a45e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * Copyright (C) 2007-2023 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef BU_UNIT_SUITE_H
#define BU_UNIT_SUITE_H

#include <stdint.h>
#include "bu/list.h"
#include "bu/string.h"
#include "bu/file.h"
#include "bu/array.h"
#include "bu/hash.h"

namespace Bu
{
    class Json;
    /**
     * Provides a unit testing framework.  This is pretty easy to use, probably
     * the best way to get started is to use ch to generate a template, or use
     * the code below with appropriate tweaks:
     *@code
     * #include "unitsuite.h"
     * 
     * class Unit : public Bu::UnitSuite
     * {
     * public:
     *  Unit()
     *  {
     *      setName("Example");
     *      addTest( Unit::test );
     *  }
     * 
     *  virtual ~Unit() { }
     * 
     *  //
     *  // Tests go here
     *  //
     *  void test()
     *  {
     *      unitTest( 1 == 1 );
     *  }
     * };
     * 
     * int main( int argc, char *argv[] )
     * {
     *  return Unit().run( argc, argv );
     * }
     *
     @endcode
     * The main function can contain other things, but using this one exactly
     * makes all of the test suites work exactly the same.  Using the optional
     * setName at the top of the constructor replaces the class name with the
     * chosen name when printing out stats and info.
     */
    class UnitSuite
    {
    public:
        UnitSuite();
        UnitSuite( int iOptions );
        virtual ~UnitSuite();

        int run( int argc=0, char *argv[]=NULL );

        Bu::File tempFile( Bu::String &sFileName );

        typedef void (UnitSuite::*Test)();

        class Failed
        {
        public:
            Failed() : str(""), bFile( false ) { }
            Failed( const String &s ) : str( s ), bFile( false ) { }
            Failed( const String &s, const String &sFile, int nLine ) :
                str( s ), sFile( sFile ), nLine( nLine ), bFile( true ) { }

            String str;
            String sFile;
            int nLine;
            bool bFile;
        };

        enum
        {
            optStopOnError      = 0x000001
        };

        enum Expect
        {
            expectPass,
            expectFail
        };
        
    protected:
        void add( Test fTest, const Bu::String &sName, Expect e=expectPass );
        void setName( const String &sName );
        String getName() const;
        virtual void cleanup();

        void dispProgress();
        void setStepCount( int iSteps );
        void incProgress( int iAmnt = 1 );
        void setProgress( int iAmnt );

    private:
        int onListCases( Bu::Array<Bu::String> aParam );
        int onPrintName( Bu::Array<Bu::String> aParam );
        int onAddTest( Bu::Array<Bu::String> aParam );

    private:
        typedef struct TestInfo
        {
            String sName;
            Test fTest;
            Expect eExpect;
        } TestInfo;

        typedef Bu::List<TestInfo> TestList;
        TestList lTests;
        String sSuiteName;

        int iOptions;

        typedef Bu::List<Bu::String> StrList;
        StrList lFileCleanup;
        int iNameWidth;
        int iStepCount;
        int iProgress;
        time_t tLastUpdate;

        Bu::Hash<Bu::String, bool> hSelTests;

    public:
        class Report
        {
        public:
            Report();
            virtual ~Report();

            virtual void suiteStarting( const UnitSuite &rSuite,
                    const TestList &lTests )=0;
            virtual void testStarting( const TestInfo &rTest )=0;
            virtual void updateProgress( int iProgress, int iStepCount )=0;
            virtual void testEnded( const TestInfo &rTest )=0;
            virtual void testEnded( const TestInfo &rTest,
                    const Failed &rFail )=0;
            virtual void testException( const TestInfo &rTest,
                    std::exception &e )=0;
            virtual void suiteEnded()=0;
        };

        class ReportConsole : public Report
        {
        public:
            ReportConsole();
            virtual ~ReportConsole();

            virtual void suiteStarting( const UnitSuite &rSuite,
                    const TestList &lTests );
            virtual void testStarting( const TestInfo &rTest );
            virtual void updateProgress( int iProgress, int iStepCount ); 
            virtual void testEnded( const TestInfo &rTest );
            virtual void testEnded( const TestInfo &rTest,
                    const Failed &rFail );
            virtual void testException( const TestInfo &rTest,
                    std::exception &e );
            virtual void suiteEnded();

        private:
            int iTestCount;
            int iNameWidth;
            int iEPass;
            int iEFail;
            int iUPass;
            int iUFail;
        };

        class ReportJson : public Report
        {
        public:
            ReportJson();
            virtual ~ReportJson();

            virtual void suiteStarting( const UnitSuite &rSuite,
                    const TestList &lTests );
            virtual void testStarting( const TestInfo &rTest );
            virtual void updateProgress( int iProgress, int iStepCount ); 
            virtual void testEnded( const TestInfo &rTest );
            virtual void testEnded( const TestInfo &rTest,
                    const Failed &rFail );
            virtual void testException( const TestInfo &rTest,
                    std::exception &e );
            virtual void suiteEnded();

        private:
            class Bu::Json *pReport;
        };

    private:
        Report *pReport;
    };

Bu::Formatter &operator<<( Bu::Formatter &f, const Bu::UnitSuite::Expect &e );
}

#define addTest( fn ) add( static_cast<Bu::UnitSuite::Test>(&fn), #fn )
#define unitTest( tst ) if( !(tst) )                            \
{                                                               \
    throw Bu::UnitSuite::Failed( #tst, __FILE__, __LINE__ );    \
} else (void)0

#define unitTestCatch( tst, exception ) try                     \
{                                                               \
    tst;                                                        \
    throw Bu::UnitSuite::Failed( #tst, __FILE__, __LINE__ );    \
} catch( exception & ) { }                                      \
catch(...) {                                                    \
    throw Bu::UnitSuite::Failed( #tst, __FILE__, __LINE__ );    \
} (void)0

#define unitFailed( msg ) throw Bu::UnitSuite::Failed(msg, __FILE__, __LINE__)

#endif