Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include "chassis.hpp" |
| 17 | #include "device.hpp" |
| 18 | #include "i2c_interface.hpp" |
| 19 | #include "test_utils.hpp" |
| 20 | |
| 21 | #include <memory> |
| 22 | #include <stdexcept> |
| 23 | #include <utility> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | using namespace phosphor::power::regulators; |
| 29 | using namespace phosphor::power::regulators::test_utils; |
| 30 | |
| 31 | TEST(ChassisTests, Constructor) |
| 32 | { |
| 33 | // Test where works: Only required parameters are specified |
| 34 | { |
| 35 | Chassis chassis{2}; |
| 36 | EXPECT_EQ(chassis.getNumber(), 2); |
| 37 | EXPECT_EQ(chassis.getDevices().size(), 0); |
| 38 | } |
| 39 | |
| 40 | // Test where works: All parameters are specified |
| 41 | { |
| 42 | // Create vector of Device objects |
| 43 | std::vector<std::unique_ptr<Device>> devices{}; |
| 44 | devices.push_back(std::make_unique<Device>( |
| 45 | "vdd_reg1", true, "/system/chassis/motherboard/reg1", |
| 46 | std::move(createI2CInterface()))); |
| 47 | devices.push_back(std::make_unique<Device>( |
| 48 | "vdd_reg2", true, "/system/chassis/motherboard/reg2", |
| 49 | std::move(createI2CInterface()))); |
| 50 | |
| 51 | // Create Chassis |
| 52 | Chassis chassis{1, std::move(devices)}; |
| 53 | EXPECT_EQ(chassis.getNumber(), 1); |
| 54 | EXPECT_EQ(chassis.getDevices().size(), 2); |
| 55 | } |
| 56 | |
| 57 | // Test where fails: Invalid chassis number < 1 |
| 58 | try |
| 59 | { |
| 60 | Chassis chassis{0}; |
| 61 | ADD_FAILURE() << "Should not have reached this line."; |
| 62 | } |
| 63 | catch (const std::invalid_argument& e) |
| 64 | { |
| 65 | EXPECT_STREQ(e.what(), "Invalid chassis number: 0"); |
| 66 | } |
| 67 | catch (...) |
| 68 | { |
| 69 | ADD_FAILURE() << "Should not have caught exception."; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | TEST(ChassisTests, GetDevices) |
| 74 | { |
| 75 | // Test where no devices were specified in constructor |
| 76 | { |
| 77 | Chassis chassis{2}; |
| 78 | EXPECT_EQ(chassis.getDevices().size(), 0); |
| 79 | } |
| 80 | |
| 81 | // Test where devices were specified in constructor |
| 82 | { |
| 83 | // Create vector of Device objects |
| 84 | std::vector<std::unique_ptr<Device>> devices{}; |
| 85 | devices.push_back(std::make_unique<Device>( |
| 86 | "vdd_reg1", true, "/system/chassis/motherboard/reg1", |
| 87 | std::move(createI2CInterface()))); |
| 88 | devices.push_back(std::make_unique<Device>( |
| 89 | "vdd_reg2", true, "/system/chassis/motherboard/reg2", |
| 90 | std::move(createI2CInterface()))); |
| 91 | |
| 92 | // Create Chassis |
| 93 | Chassis chassis{1, std::move(devices)}; |
| 94 | EXPECT_EQ(chassis.getDevices().size(), 2); |
| 95 | EXPECT_EQ(chassis.getDevices()[0]->getID(), "vdd_reg1"); |
| 96 | EXPECT_EQ(chassis.getDevices()[1]->getID(), "vdd_reg2"); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | TEST(ChassisTests, GetNumber) |
| 101 | { |
| 102 | Chassis chassis{3}; |
| 103 | EXPECT_EQ(chassis.getNumber(), 3); |
| 104 | } |