summaryrefslogtreecommitdiff
path: root/expression-test/expression_test_parser.hpp
blob: 561ccd964731cea90b675b2469bd7de16fdbc34d (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
#pragma once

#include "filesystem.hpp"

#include <mbgl/style/expression/expression.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/rapidjson.hpp>

#include <vector>
#include <string>

using namespace mbgl;

struct Input {
    Input(optional<float> zoom_, optional<double> heatmapDensity_, Feature feature_)
        : zoom(std::move(zoom_)),
          heatmapDensity(std::move(heatmapDensity_)),
          feature(std::move(feature_)) {}
    optional<float> zoom;
    optional<double> heatmapDensity;
    Feature feature;
};

struct Compiled {
    bool operator==(const Compiled& other) const {
        bool typeEqual = success == other.success &&
                         isFeatureConstant == other.isFeatureConstant &&
                         isZoomConstant == other.isZoomConstant &&
                         serializedType == other.serializedType &&
                         errors == other.errors;
        return typeEqual;
    }

    bool success = false;
    bool isFeatureConstant = false;
    bool isZoomConstant = false;
    std::string serializedType;
    std::vector<Value> errors;
};

struct TestResult {
    Compiled compiled;
    optional<Value> expression;
    optional<Value> outputs;
    optional<Value> serialized;
};

struct PropertySpec {
    std::string type;
    std::string value;
    std::size_t length = 0;
    bool isDataDriven = false;
    optional<Value> expression;
};

class TestData {
public:
    std::vector<Input> inputs;
    TestResult expected;
    TestResult result;
    TestResult recompiled;
    optional<PropertySpec> spec;
    JSDocument document;
};

struct Ignore {
    Ignore(std::string id_, std::string reason_)
        : id(std::move(id_)),
          reason(std::move(reason_)) {}

    std::string id;
    std::string reason;
};

using Arguments = std::tuple<filesystem::path, std::vector<filesystem::path>, bool, uint32_t>;
Arguments parseArguments(int argc, char** argv);

using Ignores = std::vector<Ignore>;
Ignores parseExpressionIgnores();
optional<TestData> parseTestData(const filesystem::path&);

std::string toJSON(const Value& value, unsigned indent = 0, bool singleLine = false);
JSDocument toDocument(const Value&);
Value toValue(const Compiled&);
optional<Value> toValue(const style::expression::Value&);

std::unique_ptr<style::expression::Expression> parseExpression(const JSValue&,
                                                               optional<PropertySpec>&,
                                                               TestResult&);
std::unique_ptr<style::expression::Expression> parseExpression(const optional<Value>&,
                                                               optional<PropertySpec>&,
                                                               TestResult&);