aboutsummaryrefslogtreecommitdiff
path: root/projects/runners/iTchRunner/internal/iTchRunnerMainView.h
blob: c6a6394ea57b24de1c8927d939e886abef619b49 (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
/*
 *  iTchRunnerMainView.h
 *  iTchRunner
 *
 *  Created by Phil on 07/02/2011.
 *  Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
 *
 */
#ifndef TWOBLUECUBES_ITCHRUNNERMAINVIEW_H_INCLUDED
#define TWOBLUECUBES_ITCHRUNNERMAINVIEW_H_INCLUDED

#include "internal/catch_config.hpp"
#include "internal/catch_runner_impl.hpp"
#include "internal/catch_context_impl.hpp"
#include "catch.hpp"

#include "iTchRunnerReporter.h"

#import <UIKit/UIKit.h>

@interface iTchRunnerMainView : UIView<iTchRunnerDelegate, UIActionSheetDelegate>
{
    UITextField* appName;
}

-(void) showAlert;

@end

@implementation iTchRunnerMainView


///////////////////////////////////////////////////////////////////////////////
-(id) initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
    {
        // Initialization code
        self.backgroundColor = [UIColor blackColor];

        appName = [[UITextField alloc] initWithFrame: CGRectMake( 0, 50, 320, 50 )];
        [self addSubview: appName];
        arcSafeRelease( appName );
        appName.textColor = [[UIColor alloc] initWithRed:0.35 green:0.35 blue:1 alpha:1];
        arcSafeRelease( appName.textColor );
        appName.textAlignment = NSTextAlignmentCenter;

        appName.text = [NSString stringWithFormat:@"CATCH tests"];
//        [self performSelector: @selector(showAlert) withObject:nil afterDelay:0.1];
        [self performSelectorOnMainThread:@selector(showAlert) withObject:nil waitUntilDone:NO];

    }
    return self;
}

///////////////////////////////////////////////////////////////////////////////
-(void) dealloc
{
    [appName removeFromSuperview];
#if !CATCH_ARC_ENABLED
    [super dealloc];
#endif
}

///////////////////////////////////////////////////////////////////////////////
-(void) showAlert
{
    UIActionSheet* menu = [[UIActionSheet alloc] initWithTitle:@"Options"
                                                      delegate:self
                                             cancelButtonTitle:nil
                                        destructiveButtonTitle:nil
                                             otherButtonTitles:@"Run all tests", nil];
    [menu showInView: self];
    arcSafeRelease( menu );

}

// This is a copy & paste from Catch::Runner2 to get us bootstrapped (this is due to all be
// replaced anyway)
inline Catch::Totals runTestsForGroup( Catch::RunContext& context, const Catch::TestCaseFilters& filterGroup ) {
    using namespace Catch;
    Totals totals;
    std::vector<TestCase>::const_iterator it = getRegistryHub().getTestCaseRegistry().getAllTests().begin();
    std::vector<TestCase>::const_iterator itEnd = getRegistryHub().getTestCaseRegistry().getAllTests().end();
    int testsRunForGroup = 0;
    for(; it != itEnd; ++it ) {
        if( filterGroup.shouldInclude( *it ) ) {
            testsRunForGroup++;

            if( context.aborting() )
                break;

            totals += context.runTest( *it );
        }
    }
    if( testsRunForGroup == 0 )
        std::cerr << "\n[No test cases matched with: " << filterGroup.getName() << "]" << std::endl;
    return totals;

}

///////////////////////////////////////////////////////////////////////////////
-(void) actionSheet: (UIActionSheet*) sheet clickedButtonAtIndex: (NSInteger) index
{
    Catch::Ptr<Catch::Config> config = new Catch::Config();
    Catch::IReporter* reporter = new Catch::iTchRunnerReporter( self );
    Catch::LegacyReporterAdapter* reporterAdapter = new Catch::LegacyReporterAdapter( reporter );
    Catch::RunContext runner( config.get(), reporterAdapter );


    std::vector<Catch::TestCaseFilters> filterGroups;
    Catch::TestCaseFilters filterGroup( "" );
    filterGroups.push_back( filterGroup );

    Catch::Totals totals;

    std::vector<Catch::TestCaseFilters>::const_iterator it = filterGroups.begin();
    std::vector<Catch::TestCaseFilters>::const_iterator itEnd = filterGroups.end();

    std::size_t groupCount = filterGroups.size();
    std::size_t groupIndex = 0;
    for(; it != itEnd && !runner.aborting(); ++it, ++index ) {
        runner.testGroupStarting( it->getName(), groupIndex, groupCount );
        totals += runTestsForGroup( runner, *it );
        runner.testGroupEnded( it->getName(), totals, groupIndex, groupCount );
    }


    if( totals.assertions.failed == 0 )
    {
        NSLog( @"no failures" );
        if( totals.assertions.passed > 0 )
            appName.textColor = [[UIColor alloc] initWithRed:0.35 green:1 blue:0.35 alpha:1];
    }
    else
    {
        NSLog( @"%lu failures", totals.assertions.failed );
        appName.textColor = [[UIColor alloc] initWithRed:1 green:0.35 blue:0.35 alpha:1];
    }
}

///////////////////////////////////////////////////////////////////////////////
-(void) testWasRun: (const Catch::AssertionResult*) pResultInfo
{
    const Catch::AssertionResult& resultInfo = *pResultInfo;
    std::ostringstream oss;

    if( resultInfo.hasExpression() )
    {
        oss << resultInfo.getExpression();
        if( resultInfo.isOk() )
            oss << " succeeded";
        else
            oss << " failed";
    }
    switch( resultInfo.getResultType() )
    {
        case Catch::ResultWas::ThrewException:
            if( resultInfo.hasExpression() )
                oss << " with unexpected";
            else
                oss << "Unexpected";
            oss << " exception with message: '" << resultInfo.getMessage() << "'";
            break;
        case Catch::ResultWas::Info:
            oss << "info: '" << resultInfo.getMessage() << "'";
            break;
        case Catch::ResultWas::Warning:
            oss << "warning: '" << resultInfo.getMessage() << "'";
            break;
        case Catch::ResultWas::ExplicitFailure:
            oss << "failed with message: '" << resultInfo.getMessage() << "'";
            break;
        default:
            break;
    }

    if( resultInfo.hasExpression() )
    {
        oss << " for: " << resultInfo.getExpandedExpression();
    }
    oss << std::endl;
    NSLog( @"%s", oss.str().c_str() );
}


@end

#endif // TWOBLUECUBES_ITCHRUNNERMAINVIEW_H_INCLUDED