psu-ng: Refactor EXPECT calls for PMBus STATUS_*

Continuing to copy and paste and update all these EXPECT_CALL() lines
for the various STATUS_* commands seems a bit tedious. Refactor that
code to pull it out into a helper function.

Change-Id: If981de19cbc771ac8da17a2dfe0df50e40a1a190
Signed-off-by: Brandon Wyman <bjwyman@gmail.com>
diff --git a/phosphor-power-supply/test/power_supply_tests.cpp b/phosphor-power-supply/test/power_supply_tests.cpp
index ef87a15..04c0fc7 100644
--- a/phosphor-power-supply/test/power_supply_tests.cpp
+++ b/phosphor-power-supply/test/power_supply_tests.cpp
@@ -22,6 +22,32 @@
 static auto PSUInventoryPath = "/xyz/bmc/inv/sys/chassis/board/powersupply0";
 static auto PSUGPIOLineName = "presence-ps0";
 
+// Helper function to setup expectations for various STATUS_* commands
+void setPMBusExpectations(MockedPMBus& mockPMBus, uint16_t statusWordValue,
+                          uint8_t statusInputValue = 0,
+                          uint8_t statusMFRValue = 0,
+                          uint8_t statusCMLValue = 0)
+{
+    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
+        .Times(1)
+        .WillOnce(Return(statusWordValue));
+
+    if (statusWordValue != 0)
+    {
+        // If fault bits are on in STATUS_WORD, there will also be a read of
+        // STATUS_INPUT, STATUS_MFR, and STATUS_CML.
+        EXPECT_CALL(mockPMBus, read(STATUS_INPUT, _))
+            .Times(1)
+            .WillOnce(Return(statusInputValue));
+        EXPECT_CALL(mockPMBus, read(STATUS_MFR, _))
+            .Times(1)
+            .WillOnce(Return(statusMFRValue));
+        EXPECT_CALL(mockPMBus, read(STATUS_CML, _))
+            .Times(1)
+            .WillOnce(Return(statusCMLValue));
+    }
+}
+
 class PowerSupplyTests : public ::testing::Test
 {
   public:
@@ -96,6 +122,7 @@
 
         EXPECT_EQ(psu->isPresent(), false);
         EXPECT_EQ(psu->isFaulted(), false);
+        EXPECT_EQ(psu->hasCommFault(), false);
         EXPECT_EQ(psu->hasInputFault(), false);
         EXPECT_EQ(psu->hasMFRFault(), false);
         EXPECT_EQ(psu->hasVINUVFault(), false);
@@ -153,13 +180,16 @@
     EXPECT_EQ(psu2.isPresent(), false);
 
     MockedPMBus& mockPMBus = static_cast<MockedPMBus&>(psu2.getPMBus());
+    // Presence change from missing to present will trigger write to
+    // ON_OFF_CONFIG.
+    EXPECT_CALL(mockPMBus, writeBinary(ON_OFF_CONFIG, _, _));
     // Presence change from missing to present will trigger in1_input read in
     // an attempt to get CLEAR_FAULTS called.
     EXPECT_CALL(mockPMBus, read(READ_VIN, _)).Times(1).WillOnce(Return(206000));
     // STATUS_WORD 0x0000 is powered on, no faults.
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(1)
-        .WillOnce(Return(0x0000));
+    uint16_t statusWordValue = 0;
+    uint8_t statusInputValue = 0;
+    setPMBusExpectations(mockPMBus, statusWordValue);
     psu2.analyze();
     EXPECT_EQ(psu2.isPresent(), true);
     EXPECT_EQ(psu2.isFaulted(), false);
@@ -169,20 +199,11 @@
     EXPECT_EQ(psu2.hasCommFault(), false);
 
     // STATUS_WORD input fault/warn
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(1)
-        .WillOnce(Return(status_word::INPUT_FAULT_WARN));
-    // Due to the fault bit on in STATUS_WORD, there will also be a read of
-    // STATUS_INPUT, STATUS_MFR, and STATUS_CML, so there should be 4 reads
-    // total expected.
+    statusWordValue = (status_word::INPUT_FAULT_WARN);
     // STATUS_INPUT fault bits ... on.
-    EXPECT_CALL(mockPMBus, read(STATUS_INPUT, _))
-        .Times(1)
-        .WillOnce(Return(0x38));
-    // STATUS_MFR don't care
-    EXPECT_CALL(mockPMBus, read(STATUS_MFR, _)).Times(1).WillOnce(Return(0));
-    // STATUS_CML don't care
-    EXPECT_CALL(mockPMBus, read(STATUS_CML, _)).Times(1).WillOnce(Return(0));
+    statusInputValue = 0x38;
+    // STATUS_MFR and STATUS_CML don't care
+    setPMBusExpectations(mockPMBus, statusWordValue, statusInputValue);
     psu2.analyze();
     EXPECT_EQ(psu2.isPresent(), true);
     EXPECT_EQ(psu2.isFaulted(), true);
@@ -193,20 +214,16 @@
 
     // STATUS_WORD INPUT/UV fault.
     // First need it to return good status, then the fault
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(2)
-        .WillOnce(Return(0x0000))
-        .WillOnce(Return(
-            (status_word::INPUT_FAULT_WARN | status_word::VIN_UV_FAULT)));
-    // STATUS_INPUT fault bits ... on.
-    EXPECT_CALL(mockPMBus, read(STATUS_INPUT, _))
-        .Times(1)
-        .WillOnce(Return(0x38));
-    // STATUS_MFR don't care
-    EXPECT_CALL(mockPMBus, read(STATUS_MFR, _)).Times(1).WillOnce(Return(0));
-    // STATUS_CML don't care
-    EXPECT_CALL(mockPMBus, read(STATUS_CML, _)).Times(1).WillOnce(Return(0));
+    statusWordValue = 0;
+    setPMBusExpectations(mockPMBus, statusWordValue);
     psu2.analyze();
+    // Now set fault bits in STATUS_WORD
+    statusWordValue =
+        (status_word::INPUT_FAULT_WARN | status_word::VIN_UV_FAULT);
+    // STATUS_INPUT fault bits ... on.
+    statusInputValue = 0x38;
+    // STATUS_MFR and STATUS_CML don't care
+    setPMBusExpectations(mockPMBus, statusWordValue, statusInputValue);
     psu2.analyze();
     EXPECT_EQ(psu2.isPresent(), true);
     EXPECT_EQ(psu2.isFaulted(), true);
@@ -217,19 +234,18 @@
 
     // STATUS_WORD MFR fault.
     // First need it to return good status, then the fault
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(2)
-        .WillOnce(Return(0x0000))
-        .WillOnce(Return(status_word::MFR_SPECIFIC_FAULT));
-    // STATUS_INPUT fault bits ... don't care.
-    EXPECT_CALL(mockPMBus, read(STATUS_INPUT, _))
-        .Times(1)
-        .WillOnce(Return(0x00));
-    // STATUS_MFR bits on.
-    EXPECT_CALL(mockPMBus, read(STATUS_MFR, _)).Times(1).WillOnce(Return(0xFF));
-    // STATUS_CML don't care
-    EXPECT_CALL(mockPMBus, read(STATUS_CML, _)).Times(1).WillOnce(Return(0));
+    statusWordValue = 0;
+    setPMBusExpectations(mockPMBus, statusWordValue);
     psu2.analyze();
+    // Now STATUS_WORD with MFR fault bit on.
+    statusWordValue = (status_word::MFR_SPECIFIC_FAULT);
+    // STATUS_INPUT fault bits ... don't care.
+    statusInputValue = 0;
+    // STATUS_MFR bits on.
+    uint8_t statusMFRValue = 0xFF;
+    // STATUS_CML don't care
+    setPMBusExpectations(mockPMBus, statusWordValue, statusInputValue,
+                         statusMFRValue);
     psu2.analyze();
     EXPECT_EQ(psu2.isPresent(), true);
     EXPECT_EQ(psu2.isFaulted(), true);
@@ -240,21 +256,13 @@
 
     // Ignore Temperature fault.
     // First STATUS_WORD with no bits set, then with temperature fault.
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(2)
-        .WillOnce(Return(0x0000))
-        .WillOnce(Return(status_word::TEMPERATURE_FAULT_WARN));
-    // If the STATUS_WORD has bits on, STATUS_MFR_SPECIFIC, STATUS_INPUT, and
-    // STATUS_CML will also be read.
-    // STATUS_INPUT fault bits ... don't care.
-    EXPECT_CALL(mockPMBus, read(STATUS_INPUT, _))
-        .Times(1)
-        .WillOnce(Return(0x00));
-    // STATUS_MFR don't care
-    EXPECT_CALL(mockPMBus, read(STATUS_MFR, _)).Times(1).WillOnce(Return(0));
-    // STATUS_CML don't care
-    EXPECT_CALL(mockPMBus, read(STATUS_CML, _)).Times(1).WillOnce(Return(0));
+    statusWordValue = 0;
+    setPMBusExpectations(mockPMBus, statusWordValue);
     psu2.analyze();
+    // STATUS_WORD with temperature fault bit on.
+    statusWordValue = (status_word::TEMPERATURE_FAULT_WARN);
+    // STATUS_INPUT, STATUS_MFR, and STATUS_CML fault bits ... don't care.
+    setPMBusExpectations(mockPMBus, statusWordValue);
     psu2.analyze();
     EXPECT_EQ(psu2.isPresent(), true);
     EXPECT_EQ(psu2.isFaulted(), false);
@@ -265,20 +273,19 @@
 
     // CML fault
     // First STATUS_WORD wit no bits set, then with CML fault.
-    // STATUS_WORD with CML fault bit on.
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(2)
-        .WillOnce(Return(0x0000))
-        .WillOnce(Return(status_word::CML_FAULT));
+    statusWordValue = 0;
+    setPMBusExpectations(mockPMBus, statusWordValue);
     psu2.analyze();
-    // If the STATUS_WORD has bits on, STATUS_MFR_SPECIFIC, STATUS_INPUT, and
-    // STATUS_CML will also be read.
-    EXPECT_CALL(mockPMBus, read(STATUS_INPUT, _))
-        .Times(1)
-        .WillOnce(Return(0x00));
-    EXPECT_CALL(mockPMBus, read(STATUS_MFR, _)).Times(1).WillOnce(Return(0x00));
+    // STATUS_WORD with CML fault bit on.
+    statusWordValue = (status_word::CML_FAULT);
+    // STATUS_INPUT fault bits ... don't care.
+    statusInputValue = 0;
+    // STATUS_MFR don't care
+    statusMFRValue = 0;
     // Turn on STATUS_CML fault bit(s)
-    EXPECT_CALL(mockPMBus, read(STATUS_CML, _)).Times(1).WillOnce(Return(0xFF));
+    uint8_t statusCMLValue = 0xFF;
+    setPMBusExpectations(mockPMBus, statusWordValue, statusInputValue,
+                         statusMFRValue, statusCMLValue);
     psu2.analyze();
     EXPECT_EQ(psu2.isPresent(), true);
     EXPECT_EQ(psu2.isFaulted(), true);
@@ -288,20 +295,14 @@
     EXPECT_EQ(psu2.hasCommFault(), true);
 
     // Ignore fan fault
-    // First STATUS_WORD with no bits set, then with fan fault.
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(2)
-        .WillOnce(Return(0x0000))
-        .WillOnce(Return(status_word::FAN_FAULT));
-    // If the STATUS_WORD has bits on, STATUS_MFR_SPECIFIC, STATUS_INPUT, and
-    // STATUS_CML will also be read.
-    // Don't care if bits set or not.
-    EXPECT_CALL(mockPMBus, read(STATUS_INPUT, _))
-        .Times(1)
-        .WillOnce(Return(0x00));
-    EXPECT_CALL(mockPMBus, read(STATUS_MFR, _)).Times(1).WillOnce(Return(0));
-    EXPECT_CALL(mockPMBus, read(STATUS_CML, _)).Times(1).WillOnce(Return(0));
+    // First STATUS_WORD with no bits set, then with fan
+    // fault.
+    statusWordValue = 0;
+    setPMBusExpectations(mockPMBus, statusWordValue);
     psu2.analyze();
+    statusWordValue = (status_word::FAN_FAULT);
+    // STATUS_INPUT, STATUS_MFR, and STATUS_CML: Don't care if bits set or not.
+    setPMBusExpectations(mockPMBus, statusWordValue);
     psu2.analyze();
     EXPECT_EQ(psu2.isPresent(), true);
     EXPECT_EQ(psu2.isFaulted(), false);
@@ -367,7 +368,12 @@
     // GPIO read return 1 to indicate present.
     ON_CALL(*mockPresenceGPIO, read()).WillByDefault(Return(1));
     MockedPMBus& mockPMBus = static_cast<MockedPMBus&>(psu.getPMBus());
-    ON_CALL(mockPMBus, read(STATUS_WORD, _)).WillByDefault(Return(0));
+    // Presence change from missing to present will trigger in1_input read in
+    // an attempt to get CLEAR_FAULTS called.
+    EXPECT_CALL(mockPMBus, read(READ_VIN, _)).Times(1).WillOnce(Return(206000));
+    // STATUS_WORD 0x0000 is powered on, no faults.
+    uint16_t statusWordValue = 0;
+    setPMBusExpectations(mockPMBus, statusWordValue);
     psu.analyze();
     EXPECT_EQ(psu.isPresent(), true);
     EXPECT_EQ(psu.isFaulted(), false);
@@ -376,19 +382,15 @@
     EXPECT_EQ(psu.hasVINUVFault(), false);
     EXPECT_EQ(psu.hasCommFault(), false);
     // STATUS_WORD with fault bits galore!
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(1)
-        .WillOnce(Return(0xFFFF));
-    // If STATUS_WORD has any fault bits on, STATUS_MFR_SPECIFIC, STATUS_INPUT
-    // and STATUS_CML will be read.
+    statusWordValue = 0xFFFF;
     // STATUS_INPUT with fault bits on.
-    EXPECT_CALL(mockPMBus, read(STATUS_INPUT, _))
-        .Times(1)
-        .WillOnce(Return(0xFF));
+    uint8_t statusInputValue = 0xFF;
     // STATUS_MFR_SPEFIC with bits on.
-    EXPECT_CALL(mockPMBus, read(STATUS_MFR, _)).Times(1).WillOnce(Return(0xFF));
+    uint8_t statusMFRValue = 0xFF;
     // STATUS_CML with bits on.
-    EXPECT_CALL(mockPMBus, read(STATUS_CML, _)).Times(1).WillOnce(Return(0xFF));
+    uint8_t statusCMLValue = 0xFF;
+    setPMBusExpectations(mockPMBus, statusWordValue, statusInputValue,
+                         statusMFRValue, statusCMLValue);
     psu.analyze();
     EXPECT_EQ(psu.isPresent(), true);
     EXPECT_EQ(psu.isFaulted(), true);
@@ -485,19 +487,15 @@
     EXPECT_EQ(psu.isFaulted(), false);
     MockedPMBus& mockPMBus = static_cast<MockedPMBus&>(psu.getPMBus());
     // STATUS_WORD with fault bits on.
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(1)
-        .WillOnce(Return(0xFFFF));
-    // Fault bit(s) on in STATUS_WORD causes read of STATUS_MFR_SPECIFIC,
-    // STATUS_INPUT, and STATUS_CML.
+    uint16_t statusWordValue = 0xFFFF;
     // STATUS_INPUT with fault bits on.
-    EXPECT_CALL(mockPMBus, read(STATUS_INPUT, _))
-        .Times(1)
-        .WillOnce(Return(0xFF));
+    uint8_t statusInputValue = 0xFF;
     // STATUS_MFR_SPECIFIC with faults bits on.
-    EXPECT_CALL(mockPMBus, read(STATUS_MFR, _)).Times(1).WillOnce(Return(0xFF));
+    uint8_t statusMFRValue = 0xFF;
     // STATUS_CML with faults bits on.
-    EXPECT_CALL(mockPMBus, read(STATUS_CML, _)).Times(1).WillOnce(Return(0xFF));
+    uint8_t statusCMLValue = 0xFF;
+    setPMBusExpectations(mockPMBus, statusWordValue, statusInputValue,
+                         statusMFRValue, statusCMLValue);
     psu.analyze();
     EXPECT_EQ(psu.isFaulted(), true);
 }
@@ -514,29 +512,22 @@
     psu.analyze();
     MockedPMBus& mockPMBus = static_cast<MockedPMBus&>(psu.getPMBus());
     EXPECT_EQ(psu.hasInputFault(), false);
-    EXPECT_CALL(mockPMBus, read(_, _)).Times(1).WillOnce(Return(0x0000));
+    // STATUS_WORD 0x0000 is powered on, no faults.
+    uint16_t statusWordValue = 0;
+    setPMBusExpectations(mockPMBus, statusWordValue);
     psu.analyze();
     EXPECT_EQ(psu.hasInputFault(), false);
     // STATUS_WORD with input fault/warn on.
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(1)
-        .WillOnce(Return(status_word::INPUT_FAULT_WARN));
-    // Fault bit(s) on in STATUS_WORD causes read of STATUS_MFR_SPECIFIC,
-    // STATUS_INPUT, and STATUS_CML.
+    statusWordValue = (status_word::INPUT_FAULT_WARN);
     // STATUS_INPUT with an input fault bit on.
-    EXPECT_CALL(mockPMBus, read(STATUS_INPUT, _))
-        .Times(1)
-        .WillOnce(Return(0x80));
-    // STATUS_MFR don't care.
-    EXPECT_CALL(mockPMBus, read(STATUS_MFR, _)).Times(1).WillOnce(Return(0x00));
-    // STATUS_CML don't care.
-    EXPECT_CALL(mockPMBus, read(STATUS_CML, _)).Times(1).WillOnce(Return(0x00));
+    uint8_t statusInputValue = 0x80;
+    // STATUS_MFR and STATUS_CML don't care.
+    setPMBusExpectations(mockPMBus, statusWordValue, statusInputValue);
     psu.analyze();
     EXPECT_EQ(psu.hasInputFault(), true);
     // STATUS_WORD with no bits on.
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(1)
-        .WillOnce(Return(0x0000));
+    statusWordValue = 0;
+    setPMBusExpectations(mockPMBus, statusWordValue);
     psu.analyze();
     EXPECT_EQ(psu.hasInputFault(), false);
 }
@@ -554,31 +545,25 @@
     MockedPMBus& mockPMBus = static_cast<MockedPMBus&>(psu.getPMBus());
     EXPECT_EQ(psu.hasMFRFault(), false);
     // First return STATUS_WORD with no bits on.
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(1)
-        .WillOnce(Return(0x0000));
+    // STATUS_WORD 0x0000 is powered on, no faults.
+    uint16_t statusWordValue = 0;
+    setPMBusExpectations(mockPMBus, statusWordValue);
     psu.analyze();
     EXPECT_EQ(psu.hasMFRFault(), false);
     // Next return STATUS_WORD with MFR fault bit on.
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(1)
-        .WillOnce(Return(status_word::MFR_SPECIFIC_FAULT));
-    // Fault bit(s) on in STATUS_WORD causes read of STATUS_MFR_SPECIFIC,
-    // STATUS_INPUT, and STATUS_CML.
+    statusWordValue = (status_word::MFR_SPECIFIC_FAULT);
     // STATUS_INPUT don't care
-    EXPECT_CALL(mockPMBus, read(STATUS_INPUT, _))
-        .Times(1)
-        .WillOnce(Return(0x00));
+    uint8_t statusInputValue = 0;
     // STATUS_MFR_SPEFIC with bit(s) on.
-    EXPECT_CALL(mockPMBus, read(STATUS_MFR, _)).Times(1).WillOnce(Return(0xFF));
+    uint8_t statusMFRValue = 0xFF;
     // STATUS_CML don't care.
-    EXPECT_CALL(mockPMBus, read(STATUS_CML, _)).Times(1).WillOnce(Return(0x00));
+    setPMBusExpectations(mockPMBus, statusWordValue, statusInputValue,
+                         statusMFRValue);
     psu.analyze();
     EXPECT_EQ(psu.hasMFRFault(), true);
     // Back to no bits on in STATUS_WORD
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(1)
-        .WillOnce(Return(0x0000));
+    statusWordValue = 0;
+    setPMBusExpectations(mockPMBus, statusWordValue);
     psu.analyze();
     EXPECT_EQ(psu.hasMFRFault(), false);
 }
@@ -595,33 +580,23 @@
     psu.analyze();
     MockedPMBus& mockPMBus = static_cast<MockedPMBus&>(psu.getPMBus());
     EXPECT_EQ(psu.hasVINUVFault(), false);
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(1)
-        .WillOnce(Return(0x0000));
+    // STATUS_WORD 0x0000 is powered on, no faults.
+    uint16_t statusWordValue = 0;
+    setPMBusExpectations(mockPMBus, statusWordValue);
     psu.analyze();
     EXPECT_EQ(psu.hasVINUVFault(), false);
     // Turn fault on.
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(1)
-        .WillOnce(Return(status_word::VIN_UV_FAULT));
-    // Fault bit(s) on in STATUS_WORD causes read of STATUS_MFR_SPECIFIC,
-    // STATUS_INPUT, and STATUS_CML.
+    statusWordValue = (status_word::VIN_UV_FAULT);
     // Curious disagreement between PMBus Spec. Part II Figure 16 and 33. Go by
     // Figure 16, and assume bits on in STATUS_INPUT.
-    EXPECT_CALL(mockPMBus, read(STATUS_INPUT, _))
-        .Times(1)
-        .WillOnce(Return(0x18));
-    // STATUS_MFR don't care.
-    EXPECT_CALL(mockPMBus, read(STATUS_MFR, _)).Times(1).WillOnce(Return(0x00));
-    // STATUS_CML don't care.
-    EXPECT_CALL(mockPMBus, read(STATUS_CML, _)).Times(1).WillOnce(Return(0x00));
-
+    uint8_t statusInputValue = 0x18;
+    // STATUS_MFR and STATUS_CML don't care.
+    setPMBusExpectations(mockPMBus, statusWordValue, statusInputValue);
     psu.analyze();
     EXPECT_EQ(psu.hasVINUVFault(), true);
     // Back to no fault bits on in STATUS_WORD
-    EXPECT_CALL(mockPMBus, read(STATUS_WORD, _))
-        .Times(1)
-        .WillOnce(Return(0x0000));
+    statusWordValue = 0;
+    setPMBusExpectations(mockPMBus, statusWordValue);
     psu.analyze();
     EXPECT_EQ(psu.hasVINUVFault(), false);
 }