File: test-graph.cc

package info (click to toggle)
conky 1.22.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,940 kB
  • sloc: cpp: 63,722; ansic: 18,079; python: 813; xml: 324; sh: 243; makefile: 142; javascript: 139
file content (217 lines) | stat: -rw-r--r-- 6,618 bytes parent folder | download | duplicates (2)
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
/*
 *
 * Conky, a system monitor, based on torsmo
 *
 * Any original torsmo code is licensed under the BSD license
 *
 * All code written since the fork of torsmo is licensed under the GPL
 *
 * Please see COPYING for details
 *
 * Copyright (c) 2005-2024 Brenden Matthews, Philip Kovacs, et. al.
 *	(see AUTHORS)
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <tuple>
#include "catch2/catch.hpp"

#include <conky.h>
#include <content/specials.h>
#include <lua/lua-config.hh>

#ifdef BUILD_GUI

#define SF_SHOWLOG (1 << 1)

// Specific value doesn't matter, but we check if the same one comes back
constexpr double default_scale = M_PI;

constexpr double default_width = 0;
constexpr double default_height = 25;

struct graph {
  int id;
  char flags;
  int width, height;
  bool colours_set;
  Colour first_colour, last_colour;
  double scale;
  char tempgrad;
};

static std::pair<struct graph, bool> test_parse(const char *s) {
  struct text_object obj;
  bool result = scan_graph(&obj, s, default_scale,FALSE);
  auto g = static_cast<struct graph *>(obj.special_data);
  struct graph graph = *g;
  free(g);
  return {graph, result};
}

std::string unquote(const std::string &s) {
  auto out = s;
  out.erase(remove(out.begin(), out.end(), '\"'), out.end());
  return out;
}

TEST_CASE("scan_graph correctly parses input strings") {
  state = std::make_unique<lua::state>();
  conky::export_symbols(*state);

  SECTION("Trivial parse") {
    auto [g, success] = test_parse("");

    REQUIRE(g.width == default_width);
    REQUIRE(g.height == default_height);
  }

  /* test parsing combinations of options */
  const char *size_options[][2] = {{"30", "500"}, {"80", ""}, {"", ""}};
  const char *color_options[][2] = {{"orange", "blue"},
                                    {"#deadff", "#392014"},
                                    {"000000", "000000"},
                                    {"", ""}};
  const char *scale_options[] = {"0.5", ""};

  SECTION("subset of [height,width] [color1 color2] [scale] [-t] [-l]") {
    for (auto size : size_options) {
      for (auto colors : color_options) {
        for (auto scale : scale_options) {
          bool ends_at_first_size = false;

          /* build an argument string by combining the selected options */
          std::string s;
          if (*size[0] != '\0') {
            s += size[0];
            s += ",";
            if (*size[1] == '\0') {
              /* if the size is just a height, it has to be the end of the
               * argument string */
              ends_at_first_size = true;
            } else {
              s += size[1];
            }
            s += " ";
          }
          if (!ends_at_first_size) {
            s += colors[0];
            s += " ";
            s += colors[1];
            s += " ";
            s += scale;
          }

          /* parse the argument string */
          auto [g, success] = test_parse(s.c_str());

          printf("command: %s\n", s.c_str());

          /* validate parsing of each component */
          if (*size[0] == '\0') {
            REQUIRE(g.width == default_width);
            REQUIRE(g.height == default_height);
          } else {
            REQUIRE(g.height == atoi(size[0]));
            REQUIRE(g.width == atoi(size[1]));
          }

          /* if second half of size is empty, no subsequent values should be
           * set
           */
          if (ends_at_first_size) {
            REQUIRE(g.colours_set == false);
            continue;
          }

          if (*colors[0] == '\0') {
            REQUIRE(g.colours_set == false);
          } else {
            REQUIRE(g.colours_set == true);
            REQUIRE(g.first_colour == parse_color(colors[0]));
            REQUIRE(g.last_colour == parse_color(colors[1]));
          }

          if (*scale == '\0') {
            REQUIRE(g.scale == default_scale);
          } else {
            REQUIRE(g.scale == 0.5);
          }

          REQUIRE(g.flags == 0);
          REQUIRE(g.tempgrad == 0);
        }
      }
    }
  }

  SECTION("[height,width] [color1 color2] [scale] [-t] [-l]") {
    auto [g, success] = test_parse("21,340 orange blue 0.5 -t -l");

    REQUIRE(success);
    REQUIRE(g.width == 340);
    REQUIRE(g.height == 21);
    REQUIRE(g.colours_set == true);
    REQUIRE(g.scale == 0.5);
    REQUIRE(g.flags == SF_SHOWLOG);
    REQUIRE(g.tempgrad == true);
  }

  SECTION("-t location") {
    {
      auto [g, success] = test_parse("21,340 red blue 0.5");
      REQUIRE(g.tempgrad == false);
    }
    {
      auto [g, success] = test_parse("21,340 red blue 0.5");
      REQUIRE(g.tempgrad == false);
    }
    {
      auto [g, success] = test_parse("-t 21,340 red blue 0.5");
      REQUIRE(g.tempgrad == true);
    }
    {
      auto [g, success] = test_parse("21,340 -t red blue 0.5");
      REQUIRE(g.tempgrad == true);
    }
  }
}

TEST_CASE("scan_command correctly parses input strings") {
  SECTION("parse commands") {
    const char *command_options[][2] = {{"\"foo bar\"", "foo bar"},
                                        {"\"foo bar\"\tbaz", "foo bar"},
                                        {"\"foo bar\"\nbaz", "foo bar"},
                                        {"\"foo bar\" baz", "foo bar"},
                                        {"one two", "one"},
                                        {"\"ls -t\"", "ls -t"},
                                        {"\"ls -t\" 4309", "ls -t"},
                                        {"foo-test", "foo-test"},
                                        {"foo-test a b c", "foo-test"},
                                        {"", ""}};
    for (auto [command, expected_parsed] : command_options) {
      auto [parsed, len] = scan_command(command);
      REQUIRE(std::string(parsed) == expected_parsed);
      if (command[0] == '"') {
        REQUIRE(len == strlen(expected_parsed) + 2);
      } else {
        REQUIRE(len == strlen(expected_parsed));
      }
    }
  }
}

#endif /* BUILD_GUI */