LCOV - code coverage report
Current view: top level - unit_tests/tests/sensor - basic_sensor.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 61 61 100.0 %
Date: 2024-04-21 22:40:51 Functions: 34 34 100.0 %

          Line data    Source code
       1             : #include "pch.h"
       2             : 
       3             : #include "stored_value_sensor.h"
       4             : 
       5             : class SensorBasic : public ::testing::Test {
       6             : protected:
       7           8 :         void SetUp() override {
       8           8 :                 Sensor::resetRegistry();
       9           8 :         }
      10             : 
      11           8 :         void TearDown() override {
      12           8 :                 Sensor::resetRegistry();
      13           8 :         }
      14             : };
      15             : 
      16           4 : TEST_F(SensorBasic, Registry) {
      17             :         // Create a sensor - but don't register it
      18           1 :         MockSensor dut(SensorType::Tps1);
      19             : 
      20             :         // Expect not to find it
      21             :         {
      22           1 :                 auto s = Sensor::getSensorOfType(SensorType::Tps1);
      23           1 :                 EXPECT_FALSE(s);
      24             :         }
      25             : 
      26             :         // Register the sensor now
      27           1 :         EXPECT_TRUE(dut.Register());
      28             : 
      29             :         // It should now get us back our sensor
      30             :         {
      31           1 :                 auto s = Sensor::getSensorOfType(SensorType::Tps1);
      32           1 :                 EXPECT_EQ(s, &dut);
      33             :         }
      34             : 
      35             :         // Reset - it should now be gone!
      36           1 :         Sensor::resetRegistry();
      37             : 
      38             :         {
      39           1 :                 auto s = Sensor::getSensorOfType(SensorType::Tps1);
      40           1 :                 EXPECT_FALSE(s);
      41             :         }
      42           1 : }
      43             : 
      44           4 : TEST_F(SensorBasic, DoubleRegister) {
      45             :         // Create a sensor, register it
      46           1 :         MockSensor dut(SensorType::Tps1);
      47           1 :         ASSERT_TRUE(dut.Register());
      48             : 
      49             :         // And then do it again!
      50           1 :         MockSensor dut2(SensorType::Tps1);
      51           1 :         EXPECT_FATAL_ERROR(dut2.Register());
      52             : 
      53             :         // Make sure that we get the first DUT back - not the second
      54           1 :         auto shouldBeDut = Sensor::getSensorOfType(SensorType::Tps1);
      55           1 :         EXPECT_EQ(shouldBeDut, &dut);
      56           1 :         EXPECT_NE(shouldBeDut, &dut2);
      57             : }
      58             : 
      59           4 : TEST_F(SensorBasic, SensorNotInitialized) {
      60           1 :         auto result = Sensor::get(SensorType::Clt);
      61             : 
      62           1 :         EXPECT_FALSE(result.Valid);
      63           1 : }
      64             : 
      65           4 : TEST_F(SensorBasic, DoubleUnRegister) {
      66             :         // Create a sensor, register it
      67           1 :         MockSensor dut(SensorType::Tps1);
      68           1 :         ASSERT_TRUE(dut.Register());
      69           1 :         Sensor::setMockValue(SensorType::Tps1, 25);
      70           1 :         ASSERT_TRUE(Sensor::get(SensorType::Tps1).Valid);
      71             : 
      72           1 :         dut.unregister();
      73           1 :         ASSERT_TRUE(Sensor::get(SensorType::Tps1).Valid);// huh? is that a bug?
      74             : 
      75           1 :         dut.unregister();
      76           1 :         ASSERT_TRUE(Sensor::get(SensorType::Tps1).Valid);// huh? is that a bug?
      77             : }
      78             : 
      79           4 : TEST_F(SensorBasic, SensorInitialized) {
      80           1 :         MockSensor dut(SensorType::Clt);
      81           1 :         ASSERT_TRUE(dut.Register());
      82             : 
      83             :         // Check before init - make sure it isn't set yet
      84           1 :         auto result = Sensor::get(SensorType::Clt);
      85           1 :         ASSERT_FALSE(result.Valid);
      86             : 
      87             :         // Set a value
      88           1 :         dut.set(75);
      89             : 
      90             :         // Make sure now it's set
      91           1 :         auto result2 = Sensor::get(SensorType::Clt);
      92           1 :         EXPECT_TRUE(result2.Valid);
      93           1 :         EXPECT_FLOAT_EQ(result2.Value, 75);
      94             : }
      95             : 
      96           4 : TEST_F(SensorBasic, HasSensor) {
      97           1 :         MockSensor dut(SensorType::Clt);
      98             : 
      99             :         // Check that we don't have the sensor
     100           1 :         ASSERT_FALSE(Sensor::hasSensor(SensorType::Clt));
     101             : 
     102             :         // Register it
     103           1 :         ASSERT_TRUE(dut.Register());
     104             : 
     105             :         // Now we should!
     106           1 :         ASSERT_TRUE(Sensor::hasSensor(SensorType::Clt));
     107             : 
     108             :         // Check that we can have the sensor report that it's missing
     109           1 :         dut.setHasSensor(false);
     110           1 :         ASSERT_FALSE(Sensor::hasSensor(SensorType::Clt));
     111             : }
     112             : 
     113           4 : TEST_F(SensorBasic, HasSensorMock) {
     114             :         // Check that we don't have the sensor
     115           1 :         ASSERT_FALSE(Sensor::hasSensor(SensorType::Clt));
     116             : 
     117             :         // Mock the sensor - this should count as having it
     118           1 :         Sensor::setMockValue(SensorType::Clt, 25);
     119             : 
     120             :         // Now we should!
     121           1 :         ASSERT_TRUE(Sensor::hasSensor(SensorType::Clt));
     122             : }
     123             : 
     124             : 
     125           4 : TEST_F(SensorBasic, FindByName) {
     126           1 :         ASSERT_EQ(SensorType::Clt, findSensorTypeByName("Clt"));
     127           1 :         ASSERT_EQ(SensorType::Clt, findSensorTypeByName("cLT"));
     128             : }

Generated by: LCOV version 1.14