| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include <catch2/catch_all.hpp> |
| 5 | |
| 6 | #include "core/idatasource.h" |
| 7 | #include "core/info/hwidtranslator.h" |
| 8 | |
| 9 | namespace Tests::HWIDTranslator { |
| 10 | |
| 11 | class HWIDDataSourceStub : public IDataSource<std::vector<char>> |
| 12 | { |
| 13 | std::string source() const override |
| 14 | { |
| 15 | return ""; |
| 16 | } |
| 17 | |
| 18 | bool read(std::vector<char> &data) override |
| 19 | { |
| 20 | data.clear(); |
| 21 | std::copy(data_.cbegin(), data_.cend(), std::back_inserter(data)); |
| 22 | return true; |
| 23 | } |
| 24 | |
| 25 | // In pci.ids format (https://pci-ids.ucw.cz) |
| 26 | static constexpr std::string_view data_{ |
| 27 | // clang-format off |
| 28 | "\ |
| 29 | # commentary\n\ |
| 30 | # commentary\n\ |
| 31 | \ |
| 32 | 1002 Vendor Name # (something)\n\ |
| 33 | aaaa Device Name # [something]\n\ |
| 34 | bbbb cccc Subdevice Name (something) [something] more text #\n\ |
| 35 | \n\ |
| 36 | C 11 Class Name # (something) [something]\n\ |
| 37 | aa Subclass Name # (something) [something]\n\ |
| 38 | bb Programming Interface (something) mode text #\n\ |
| 39 | " |
| 40 | // clang-format on |
| 41 | }; |
| 42 | }; |
| 43 | |
| 44 | TEST_CASE("HWIDTranslator tests", "[Info][HWIDTranslator]") |
| 45 | { |
| 46 | std::vector<Vendor> vendors{Vendor::AMD}; |
| 47 | ::HWIDTranslator ts(vendors, std::make_unique<HWIDDataSourceStub>()); |
| 48 | |
| 49 | SECTION("Parses pci-ids formatted data...") |
| 50 | { |
| 51 | SECTION("Extract vendors") |
| 52 | { |
| 53 | REQUIRE(ts.vendor("1002") == "Vendor Name #"); |
| 54 | } |
| 55 | |
| 56 | SECTION("Extract devices") |
| 57 | { |
| 58 | REQUIRE(ts.device("1002", "aaaa") == "Device Name #"); |
| 59 | } |
| 60 | |
| 61 | SECTION("Extract subdevices") |
| 62 | { |
| 63 | REQUIRE(ts.subdevice("1002", "aaaa", "bbbb", "cccc") == "Subdevice Name"); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | } // namespace Tests::HWIDTranslator |