blob: 5e551c1a9933acd9593c498f0e04e90a67286f7c [file] [log] [blame]
Philipp Schrader790cb542023-07-05 21:06:52 -07001#include "glog/logging.h"
2#include "gtest/gtest.h"
3
Henry Speisere5f05aa2022-05-02 21:24:26 -07004#include "aos/configuration.h"
5#include "aos/events/event_loop.h"
6#include "aos/events/simulated_event_loop.h"
7#include "aos/flatbuffer_merge.h"
8#include "aos/json_to_flatbuffer.h"
9#include "aos/testing/path.h"
10#include "frc971/constants/constants_sender_lib.h"
11#include "frc971/constants/testdata/constants_data_generated.h"
12#include "frc971/constants/testdata/constants_list_generated.h"
Henry Speisere5f05aa2022-05-02 21:24:26 -070013
14namespace frc971::constants {
15namespace testing {
16
17using aos::testing::ArtifactPath;
18
19class ConstantSenderTest : public ::testing::Test {
20 public:
21 ConstantSenderTest()
22 : config_(aos::configuration::ReadConfig(
23 ArtifactPath("frc971/constants/testdata/aos_config.json"))),
24 event_loop_factory_(&config_.message()),
25 constants_sender_event_loop_(
26 event_loop_factory_.MakeEventLoop("sender")) {}
27
28 aos::FlatbufferDetachedBuffer<aos::Configuration> config_;
29 aos::SimulatedEventLoopFactory event_loop_factory_;
30 std::unique_ptr<aos::EventLoop> constants_sender_event_loop_;
31};
32
33// For team 971, compares the data that is recived from the program, to the data
34// that is expected
35
36TEST_F(ConstantSenderTest, HasData971) {
37 aos::network::OverrideTeamNumber(971);
38
39 std::unique_ptr<aos::EventLoop> test_event_loop =
40 event_loop_factory_.MakeEventLoop("constants");
41 ConstantSender<testdata::ConstantsData, testdata::ConstantsList> test971(
42 constants_sender_event_loop_.get(),
43 "frc971/constants/testdata/test_constants.json", "/constants");
James Kuszmauld463cb42023-02-05 17:32:31 -080044 ConstantsFetcher<testdata::ConstantsData> fetcher(test_event_loop.get());
45 EXPECT_EQ(fetcher.constants().max_roller_voltage(), 12);
46 EXPECT_EQ(fetcher.constants().min_roller_voltage(), -12);
47 // Ensure that the watcher in ConstantsFetcher never triggers.
Henry Speisere5f05aa2022-05-02 21:24:26 -070048 event_loop_factory_.RunFor(std::chrono::seconds(1));
49}
50
51// For team 9971, compares the data that is recived from the program, to the
52// data that is expected.
53
54TEST_F(ConstantSenderTest, HasData9971) {
55 std::unique_ptr<aos::EventLoop> test_event_loop =
56 event_loop_factory_.MakeEventLoop("constants");
57 ConstantSender<testdata::ConstantsData, testdata::ConstantsList> test971(
58 constants_sender_event_loop_.get(),
59 "frc971/constants/testdata/test_constants.json", 9971, "/constants");
James Kuszmauld463cb42023-02-05 17:32:31 -080060 ConstantsFetcher<testdata::ConstantsData> fetcher(test_event_loop.get());
61 EXPECT_EQ(fetcher.constants().max_roller_voltage(), 6);
62 EXPECT_EQ(fetcher.constants().min_roller_voltage(), -6);
Henry Speisere5f05aa2022-05-02 21:24:26 -070063 event_loop_factory_.RunFor(std::chrono::seconds(1));
64}
65
James Kuszmauld463cb42023-02-05 17:32:31 -080066// Tests that the ConstantsFetcher dies when there is no data available during
67// construction.
68TEST_F(ConstantSenderTest, NoDataOnStartup) {
69 std::unique_ptr<aos::EventLoop> test_event_loop =
70 event_loop_factory_.MakeEventLoop("constants");
71 EXPECT_DEATH(ConstantsFetcher<testdata::ConstantsData>(test_event_loop.get()),
72 "information must be available at startup");
73}
74
75// Tests that the ConstantsFetcher dies when there is a change to the constants
76// data.
77TEST_F(ConstantSenderTest, DieOnDataUpdate) {
78 std::unique_ptr<aos::EventLoop> test_event_loop =
79 event_loop_factory_.MakeEventLoop("constants");
80 ConstantSender<testdata::ConstantsData, testdata::ConstantsList> test971(
81 constants_sender_event_loop_.get(),
82 "frc971/constants/testdata/test_constants.json", 9971, "/constants");
83 ConstantsFetcher<testdata::ConstantsData> fetcher(test_event_loop.get());
84 auto sender =
85 constants_sender_event_loop_->MakeSender<testdata::ConstantsData>(
86 "/constants");
87 constants_sender_event_loop_->OnRun([&sender]() {
Philipp Schrader790cb542023-07-05 21:06:52 -070088 auto builder = sender.MakeBuilder();
89 builder.CheckOk(
90 builder.Send(builder.MakeBuilder<testdata::ConstantsData>().Finish()));
91 });
James Kuszmauld463cb42023-02-05 17:32:31 -080092 EXPECT_DEATH(event_loop_factory_.RunFor(std::chrono::seconds(1)),
93 "changes to constants");
94}
95
Henry Speisere5f05aa2022-05-02 21:24:26 -070096// When given a team number that it not recognized we kill the program.
97
98TEST_F(ConstantSenderTest, TeamNotFound) {
99 EXPECT_DEATH(
100 ({
101 ConstantSender<testdata::ConstantsData, testdata::ConstantsList>
102 test_no_team(constants_sender_event_loop_.get(),
103 "frc971/constants/testdata/test_constants.json", 254,
104 "/constants");
105 event_loop_factory_.RunFor(std::chrono::seconds(1));
106 }),
107 "There was no match for 254");
108}
109
110// If the json file has syntax errors it will die.
111
112TEST_F(ConstantSenderTest, SyntaxErrorDeath) {
113 EXPECT_DEATH(
114 ({
115 ConstantSender<testdata::ConstantsData, testdata::ConstantsList>
116 test_syntax(constants_sender_event_loop_.get(),
Alexander Yeee61cac32023-02-11 19:40:40 -0800117 "frc971/constants/testdata/syntax_error.json", 971,
Henry Speisere5f05aa2022-05-02 21:24:26 -0700118 "/constants");
119 event_loop_factory_.RunFor(std::chrono::seconds(1));
120 }),
Alexander Yeee61cac32023-02-11 19:40:40 -0800121 "Invalid field name");
Henry Speisere5f05aa2022-05-02 21:24:26 -0700122}
123
124} // namespace testing
125} // namespace frc971::constants