aboutsummaryrefslogtreecommitdiff
path: root/src/tools/rununits.cpp
blob: 769d3ab4704eb79777275250a3094b1d691d82f5 (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
#include <bu/sio.h>
#include <bu/json.h>
#include <bu/process.h>
#include <bu/optparser.h>
#include <bu/blob.h>
#include <bu/blobbuilder.h>

#include <sys/types.h>
#include <dirent.h>

Bu::Blob getSuiteName( const Bu::Blob &bSuiteExec )
{
    Bu::Process proc(
        Bu::Process::StdOut,
        bSuiteExec.getData(),
        bSuiteExec.getData(),
        "--print-name",
        NULL
        );

    Bu::String sResult = proc.readAll().trimWhitespace();
    Bu::Blob bRet( sResult.getStr(), sResult.getSize() );
    return bRet;
}

Bu::Json *runSuite( const Bu::Blob &bSuiteExec )
{
    Bu::Process proc(
        Bu::Process::StdOut,
        bSuiteExec.getData(),
        bSuiteExec.getData(),
        "--interop",
        NULL
        );
    Bu::BlobBuilder bbReport;
    while( proc.isRunning() )
    {
        int iRead = 0;
        char dRead[4096];
        iRead = proc.read( dRead, 4096 );
        if( iRead > 0 )
        {
            bbReport.append( dRead, iRead );
        }
    }
    Bu::Json *pReport = new Bu::Json();
    pReport->parse( bbReport.getBlob() );
    return pReport;
}

typedef Bu::List<Bu::Blob> BlobList;

BlobList getSuitePaths( const Bu::Blob &bDir )
{
    BlobList lPaths;
    DIR *dir = opendir( bDir.getData() );
    if( dir == NULL )
    {
        Bu::println("Could not open directory: %1").arg( bDir );
        return lPaths;
    }
    struct dirent *de = NULL;
    while( (de = readdir( dir )) != NULL )
    {
        if( de->d_type != DT_REG )
            continue;

        Bu::BlobBuilder bbPath;
        bbPath.append( bDir );
        bbPath.append("/");
        bbPath.append( de->d_name );
        Bu::Blob bPath = bbPath.getBlob();
        if( access( bPath.getData(), X_OK ) != 0 )
            continue;

        lPaths.append( bPath );
    }
    closedir( dir );

    return lPaths;
}

int main( int /*argc*/, char * /*argv*/[] )
{
    Bu::Blob bDir("unit");
    BlobList lPaths = getSuitePaths( bDir );

    int iNumLen = Bu::String("%1").arg( lPaths.getSize() ).end().getSize();
    int iMaxSuiteName = 0;
    Bu::Hash<Bu::Blob, Bu::Blob> hName;
    for( BlobList::iterator i = lPaths.begin(); i; i++ )
    {
        Bu::Blob bSuiteName = getSuiteName( *i );
        if( iMaxSuiteName < bSuiteName.getSize() )
            iMaxSuiteName = bSuiteName.getSize();
        hName.insert( *i, bSuiteName );
    }

    Bu::List<Bu::Json *> lReport;

    int iTest = 0;
    for( BlobList::iterator i = lPaths.begin(); i; i++ )
    {
        iTest++;
        Bu::Blob bSuiteName = hName.get( *i );
        Bu::print("\rRunning test suite [%1/%2]: %3")
            .arg( iTest, Bu::Fmt().width( iNumLen ).right() )
            .arg( lPaths.getSize(), Bu::Fmt().width( iNumLen ) )
            .arg( bSuiteName, Bu::Fmt().width( iMaxSuiteName ).fill(' ').left() );
        Bu::sio << Bu::sio.flush;

        Bu::Json *pReport = runSuite( (*i) );
        int iUnexpected = 0;
        int iTotal = 0;
        iTotal = (*pReport)["tests"].getSize();
        for( int j = 0; j < iTotal; j++ )
        {
            if( !((*pReport)["tests"][j]["expected"].getString() ==
                (*pReport)["tests"][j]["result"].getString()) )
            {
                iUnexpected++;
            }
        }
        if( iUnexpected == 0 )
        {
            delete pReport;
        }
        else
        {
            lReport.append( pReport );
        }
    }
    Bu::println("\rCompleted %1 unit test suites.%2")
        .arg( lPaths.getSize(), Bu::Fmt().width( iNumLen ) )
        .arg( Bu::Blob(""), Bu::Fmt().width( iMaxSuiteName ).fill(' ').left() );

    if( lReport.getSize() == 0 )
    {
        Bu::println("\nNothing unexpected in unit tests.");
    }
    else
    {
        for( Bu::List<Bu::Json *>::iterator i = lReport.begin(); i; i++ )
        {
            Bu::println("\nUnexpected results in: %1")
                .arg( (**i)["suite"].getString().get() );

            for( Bu::Json::iterator iTest = (**i)["tests"].begin();
                    iTest; iTest++ )
            {
                if( (**iTest)["expected"].getString() ==
                    (**iTest)["result"].getString() )
                {
                    continue;
                }

                Bu::println("  %1: unexpected %2")
                    .arg( (**iTest)["name"].getString().get() )
                    .arg( (**iTest)["result"].getString().get() );
                if( (**iTest).has("fail") )
                {
                    if( (**iTest)["fail"].has("action") )
                    {
                        Bu::println("    unitTest( %1 );")
                            .arg( (**iTest)["fail"]["action"].getString().get() );
                        Bu::println("    at %1:%2")
                            .arg( (**iTest)["fail"]["file"].getString().get() )
                            .arg( (int)(**iTest)["fail"]["line"].getNumber() );
                    }
                    else if( (**iTest)["fail"].has("what") )
                    {
                        Bu::println("    Unexpected exception: %1")
                            .arg( (**iTest)["fail"]["what"].getString().get() );
                    }
                }
                else
                {
                    Bu::println("    No further details.");
                }
            }
            delete *i;
        }
    }
    
    return 0;
}