aboutsummaryrefslogtreecommitdiffstats
path: root/checks/tests.cpp
blob: db934735ef91479c9237de038f149ba36db44bf2 (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
#include "validate.h"
#include <iostream>

void run_tests_bb(std::istream& src,
                  const std::string& name_key,
                  const std::string& output_key,
                  bool clear_between_cb,
                  std::function<bool (std::map<std::string, std::string>)> cb)
   {
   std::map<std::string, std::string> vars;
   size_t test_cnt = 0;
   size_t test_fail = 0;
   bool verbose = true;

   while(src.good())
      {
      std::string line;
      std::getline(src, line);

      if(line == "")
         continue;

      if(line[0] == '#')
         continue;

      const std::string key = line.substr(0, line.find_first_of(' '));
      const std::string val = line.substr(line.find_last_of(' ') + 1, std::string::npos);

      vars[key] = val;

      if(key == output_key)
         {
         ++test_cnt;
         try
            {
            if(!cb(vars))
               ++test_fail;
            }
         catch(std::exception& e)
            {
            std::cout << e.what() << "\n";
            ++test_fail;
            }

         if(clear_between_cb)
            vars.clear();
         }
      }

   if(verbose)
      std::cout << test_cnt << " " << name_key << " tests completed "
                << test_fail << " failed\n";
   }

void run_tests(std::istream& src,
               const std::string& name_key,
               const std::string& output_key,
               bool clear_between_cb,
               std::function<std::string (std::map<std::string, std::string>)> cb)
   {
   run_tests_bb(src, name_key, output_key, clear_between_cb,
                [name_key,output_key,cb](std::map<std::string, std::string> vars)
                {
                const std::string got = cb(vars);
                if(got != vars[output_key])
                   {
                   std::cout << name_key << " got " << got
                             << " expected " << vars[output_key] << std::endl;
                   return false;
                   }
                return true;
                });
   }