File: test_date.cc

package info (click to toggle)
metkit 1.15.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,380 kB
  • sloc: cpp: 16,089; sh: 2,532; python: 1,757; ansic: 71; makefile: 18
file content (98 lines) | stat: -rw-r--r-- 3,584 bytes parent folder | download
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
/*
 * (C) Copyright 1996- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */

/// @file   test_date.cc
/// @author Emanuele Danovaro
/// @date   March 2025

#include <string>
#include <vector>

#include "eckit/testing/Test.h"
#include "eckit/types/Date.h"
#include "eckit/value/Value.h"

#include "metkit/mars/MarsExpandContext.h"
#include "metkit/mars/MarsLanguage.h"
#include "metkit/mars/TypeDate.h"

namespace metkit::mars::test {

using ::eckit::BadValue;
using ::eckit::Value;

//-----------------------------------------------------------------------------

void assertTypeExpansion(const std::string& name, std::vector<std::string> values,
                         const std::vector<std::string>& expected) {
    static MarsLanguage language("retrieve");
    language.type(name)->expand(DummyContext{}, values);
    EXPECT_EQUAL(expected, values);
}

std::string date(long d) {
    if (d <= 0) {
        eckit::Date day(d);
        d = day.yyyymmdd();
    }
    return std::to_string(d);
}

CASE("Test TypeDate expansions") {

    TypeDate tdate("date", Value());
    Type& td(tdate);

    assertTypeExpansion("date", {"20140506"}, {"20140506"});
    assertTypeExpansion("date", {"2014-05-06"}, {"20140506"});
    assertTypeExpansion("date", {"20140506", "20140507"}, {"20140506", "20140507"});
    assertTypeExpansion("date", {"20140506", "to", "20140506"}, {"20140506"});
    assertTypeExpansion("date", {"20140506", "to", "20140507"}, {"20140506", "20140507"});
    assertTypeExpansion("date", {"20140506", "to", "20140508"}, {"20140506", "20140507", "20140508"});
    assertTypeExpansion("date", {"20140504", "20140506", "to", "20140508"},
                        {"20140504", "20140506", "20140507", "20140508"});

    assertTypeExpansion("date", {"-1", "0"}, {date(-1), date(0)});
    assertTypeExpansion("date", {"-1", "to", "-3"}, {date(-1), date(-2), date(-3)});
    assertTypeExpansion("date", {"-3", "to", "-1"}, {date(-3), date(-2), date(-1)});
    assertTypeExpansion("date", {"-5", "to", "-1", "by", "2"}, {date(-5), date(-3), date(-1)});

    assertTypeExpansion("date", {"2"}, {"feb"});
    assertTypeExpansion("date", {"jan"}, {"jan"});
    assertTypeExpansion("date", {"september"}, {"sep"});
    assertTypeExpansion("date", {"9"}, {"sep"});
    assertTypeExpansion("date", {"1-01"}, {"jan-1"});
    assertTypeExpansion("date", {"jan-01"}, {"jan-1"});
    assertTypeExpansion("date", {"january-01"}, {"jan-1"});
    assertTypeExpansion("date", {"feb-23"}, {"feb-23"});
    assertTypeExpansion("date", {"2018-23"}, {"20180123"});
    assertTypeExpansion("date", {"2018-41"}, {"20180210"});

    {
        EXPECT_THROWS(auto vv = td.tidy("20141506"));  // throws BadDate that is not exported
    }
    {
        EXPECT_THROWS(auto vv = td.tidy("20180132"));  // throws BadDate that is not exported
    }
    {
        EXPECT_THROWS(auto vv = td.tidy("202401366"));  // throws BadDate that is not exported
    }
    { EXPECT_THROWS_AS(auto vv = td.tidy("abc"), BadValue); }
    { EXPECT_THROWS_AS(auto vv = td.tidy("abc-01"), BadValue); }
}


//-----------------------------------------------------------------------------

}  // namespace metkit::mars::test

int main(int argc, char** argv) {
    return eckit::testing::run_tests(argc, argv);
}