summaryrefslogtreecommitdiff
path: root/test/test-query-parser.cc
blob: d6d10cd8fcf06832d9145f7703e0d0670eaa33a3 (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
#include "common.hh"

#include <cstdarg>
#include <cstdlib>
#include <iostream>

#include "query_parser.hh"

using namespace stuff;

namespace {

bool test(const char* in, ...) {
    std::map<std::string, std::string> tmp;
    if (!QueryParser::parse(in, &tmp)) {
        std::cerr << "expected success: " << in << std::endl;
        return false;
    }
    va_list args;
    va_start(args, in);
    while (true) {
        const char* key = va_arg(args, const char*);
        if (!key) break;
        const char* value = va_arg(args, const char*);
        auto it = tmp.find(key);
        if (it == tmp.end()) {
            std::cerr << "expected a value for " << key
                      << " in: " << in << std::endl;
            return false;
        }
        if (it->second.compare(value) != 0) {
            std::cerr << "expected value for " << key << " not to be "
                      << it->second << " in: " << in << std::endl;
            return false;
        }
        tmp.erase(it);
    }
    va_end(args);
    for (auto& pair : tmp) {
        std::cerr << "unexpected value for " << pair.first << ": "
                  << pair.second << " in: " << in << std::endl;
        return false;
    }
    return true;
}

bool test_fail(const std::string& in) {
    std::map<std::string, std::string> tmp;
    if (!QueryParser::parse(in, &tmp)) return true;
    std::cerr << "expected fail: " << in << std::endl;
    return false;
}

}  // namespace

int main() {
    unsigned int ok = 0, tot = 0;

    tot++; if (test("", NULL)) ok++;
    tot++; if (test("key=value", "key", "value", NULL)) ok++;
    tot++; if (test("?key=value", "key", "value", NULL)) ok++;
    tot++; if (test("?key=value&", "key", "value", NULL)) ok++;
    tot++; if (test("key=value&foo=bar",
                    "key", "value", "foo", "bar", NULL)) ok++;
    tot++; if (test("key=value&key=bar", "key", "bar", NULL)) ok++;
    tot++; if (test("key=", "key", "", NULL)) ok++;
    tot++; if (test("key=%c3%A5", "key", "\xc3\xa5", NULL)) ok++;
    tot++; if (test("key=foo%20bar", "key", "foo bar", NULL)) ok++;
    tot++; if (test("key=foo+bar", "key", "foo bar", NULL)) ok++;
    tot++;
    if (test("first=this+is+a+field&second=was+it+clear+%28already%29%3F",
             "first", "this is a field",
             "second", "was it clear (already)?",
             NULL)) ok++;
    tot++; if (test_fail("=value")) ok++;
    tot++; if (test_fail("=")) ok++;
    tot++; if (test_fail("\xc3\xa5")) ok++;
    tot++; if (test_fail("&&")) ok++;
    tot++; if (test_fail("key=&&")) ok++;
    tot++; if (test_fail("==")) ok++;
    tot++; if (test_fail("=&=")) ok++;
    tot++; if (test_fail("%")) ok++;
    tot++; if (test_fail("%A")) ok++;
    tot++; if (test_fail("%1z")) ok++;

    std::cout << "OK " << ok << "/" << tot << std::endl;
    return ok == tot ? EXIT_SUCCESS : EXIT_FAILURE;
}