aboutsummaryrefslogtreecommitdiff
path: root/components/app/remote-test-runner/remote_test_runner.cpp
blob: 681c7467f13744480985ca42f00ad81f50840fef (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
/*
 * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "remote_test_runner.h"
#include <protocols/service/test_runner/packed-c/status.h>
#include <vector>
#include <string>
#include <cstring>
#include <cstdio>

remote_test_runner::remote_test_runner() :
    m_client(NULL)
{

}

remote_test_runner::remote_test_runner(test_runner_client *client) :
    m_client(client)
{

}

remote_test_runner::~remote_test_runner()
{

}

void remote_test_runner::set_client(test_runner_client *client)
{
    m_client = client;
}

int remote_test_runner::execute(int argc, char *argv[])
{
    int test_status = TS_TEST_RUNNER_STATUS_ERROR;
    struct test_spec spec;

    /* Parse command line parameters */
    bool list_only = option_selected("-l", argc, argv);
    parse_test_spec_params(argc, argv, spec);

    /* Run or list tests qualified bu spec */
    struct test_summary summary;
    std::vector<struct test_result> results;

    if (list_only) {

        test_status = m_client->list_tests(spec, summary, results);
        output_list(summary, results);
    }
    else {

        test_status = m_client->run_tests(spec, summary, results);
        output_results(summary, results);
    }

    if (test_status != TS_TEST_RUNNER_STATUS_SUCCESS) {

        printf("Tests failed to run with error: %d\n", test_status);
    }

    return test_status;
}

void remote_test_runner::parse_test_spec_params(int argc, char *argv[], struct test_spec &spec) const
{
    std::string name = parse_option("-n", argc, argv);
    std::string group = parse_option("-g", argc, argv);

    memset(spec.name, 0, TEST_NAME_MAX_LEN);
    name.copy(spec.name, TEST_NAME_MAX_LEN - 1);

    memset(spec.group, 0, TEST_GROUP_MAX_LEN);
    group.copy(spec.group, TEST_GROUP_MAX_LEN - 1);
}

std::string remote_test_runner::parse_option(const char *option_switch, int argc, char *argv[]) const
{
    std::string option;

    for (int i = 1; i + 1 < argc; ++i) {

        if (strcmp(argv[i], option_switch) == 0) {

            option = std::string(argv[i +1]);
            break;
        }
    }

    return option;
}

bool remote_test_runner::option_selected(const char *option_switch, int argc, char *argv[]) const
{
    bool selected = false;

    for (int i = 1; (i < argc) && !selected; ++i) {

        selected = (strcmp(argv[i], option_switch) == 0);
    }

    return selected;
}

void remote_test_runner::output_summary(const struct test_summary &summary)
{
    printf("\n");

    if (summary.num_failed == 0)    printf("OK (");
    else                            printf("Errors (%d failures, ", summary.num_failed);

    printf("%d tests, %d ran)\n", summary.num_tests, summary.num_failed + summary.num_passed);
}


void remote_test_runner::output_list(const struct test_summary &summary,
                                    const std::vector<struct test_result> &results)
{

}

void remote_test_runner::output_results(const struct test_summary &summary,
                                    const std::vector<struct test_result> &results)
{
    for (int i = 0; i < results.size(); ++i) {

        printf("TEST(%s, %s) ", results[i].group, results[i].name);

        if (results[i].run_state == TEST_RUN_STATE_PASSED) {

            printf("OK\n");
        }
        else if (results[i].run_state == TEST_RUN_STATE_FAILED) {

            printf("error\n");
        }
        else {

            printf("did not run\n");
        }
    }

    output_summary(summary);
}