Henry Speiser | e5f05aa | 2022-05-02 21:24:26 -0700 | [diff] [blame^] | 1 | #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 | |
| 13 | namespace frc971::constants { |
| 14 | namespace testing { |
| 15 | |
| 16 | using aos::testing::ArtifactPath; |
| 17 | |
| 18 | class 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 | |
| 35 | TEST_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"); |
| 43 | test_event_loop->MakeWatcher("/constants", |
| 44 | [](const testdata::ConstantsData &data) { |
| 45 | EXPECT_EQ(data.max_roller_voltage(), 12); |
| 46 | EXPECT_EQ(data.min_roller_voltage(), -12); |
| 47 | }); |
| 48 | 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 | |
| 54 | TEST_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"); |
| 60 | test_event_loop->MakeWatcher("/constants", |
| 61 | [](const testdata::ConstantsData &data) { |
| 62 | EXPECT_EQ(data.max_roller_voltage(), 6); |
| 63 | EXPECT_EQ(data.min_roller_voltage(), -6); |
| 64 | }); |
| 65 | event_loop_factory_.RunFor(std::chrono::seconds(1)); |
| 66 | } |
| 67 | |
| 68 | // When given a team number that it not recognized we kill the program. |
| 69 | |
| 70 | TEST_F(ConstantSenderTest, TeamNotFound) { |
| 71 | EXPECT_DEATH( |
| 72 | ({ |
| 73 | ConstantSender<testdata::ConstantsData, testdata::ConstantsList> |
| 74 | test_no_team(constants_sender_event_loop_.get(), |
| 75 | "frc971/constants/testdata/test_constants.json", 254, |
| 76 | "/constants"); |
| 77 | event_loop_factory_.RunFor(std::chrono::seconds(1)); |
| 78 | }), |
| 79 | "There was no match for 254"); |
| 80 | } |
| 81 | |
| 82 | // If the json file has syntax errors it will die. |
| 83 | |
| 84 | TEST_F(ConstantSenderTest, SyntaxErrorDeath) { |
| 85 | EXPECT_DEATH( |
| 86 | ({ |
| 87 | ConstantSender<testdata::ConstantsData, testdata::ConstantsList> |
| 88 | test_syntax(constants_sender_event_loop_.get(), |
| 89 | "frc971/constants/testdata/syntaxerror.json", 971, |
| 90 | "/constants"); |
| 91 | event_loop_factory_.RunFor(std::chrono::seconds(1)); |
| 92 | }), |
| 93 | "Error on line 0"); |
| 94 | } |
| 95 | |
| 96 | } // namespace testing |
| 97 | } // namespace frc971::constants |