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