|
| 1 | +/* |
| 2 | + * Copyright (c) 2019-2020 AnimatedLEDStrip |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | + |
| 24 | +#ifndef ANIMATEDLEDSTRIP_SECTIONTEST_HPP |
| 25 | +#define ANIMATEDLEDSTRIP_SECTIONTEST_HPP |
| 26 | + |
| 27 | +#include "Section.hpp" |
| 28 | +#include "nlohmann/json.hpp" |
| 29 | +#include "gtest/gtest.h" |
| 30 | + |
| 31 | + |
| 32 | +namespace { |
| 33 | + |
| 34 | + TEST(Section, DefaultConstructor) { |
| 35 | + const Section sect = Section(); |
| 36 | + |
| 37 | + EXPECT_STREQ(sect.name.c_str(), ""); |
| 38 | + EXPECT_EQ(sect.startPixel, -1); |
| 39 | + EXPECT_EQ(sect.endPixel, -1); |
| 40 | + EXPECT_EQ(sect.physicalStart, -1); |
| 41 | + EXPECT_EQ(sect.numLEDs, 0); |
| 42 | + } |
| 43 | + |
| 44 | + TEST(Section, SetName) { |
| 45 | + Section sect = Section(); |
| 46 | + EXPECT_STREQ(sect.name.c_str(), ""); |
| 47 | + char test[5]; |
| 48 | + strcpy(test, "Sect"); |
| 49 | + sect.setName(test); |
| 50 | + EXPECT_STREQ(sect.name.c_str(), "Sect"); |
| 51 | + std::string test2 = "TestSect"; |
| 52 | + sect.setName(test2); |
| 53 | + EXPECT_STREQ(sect.name.c_str(), "TestSect"); |
| 54 | + } |
| 55 | + |
| 56 | + TEST(Section, SetStartPixel) { |
| 57 | + Section sect = Section(); |
| 58 | + EXPECT_EQ(sect.startPixel, -1); |
| 59 | + sect.setStartPixel(5); |
| 60 | + EXPECT_EQ(sect.startPixel, 5); |
| 61 | + } |
| 62 | + |
| 63 | + TEST(Section, SetEndPixel) { |
| 64 | + Section sect = Section(); |
| 65 | + EXPECT_EQ(sect.endPixel, -1); |
| 66 | + sect.setEndPixel(5); |
| 67 | + EXPECT_EQ(sect.endPixel, 5); |
| 68 | + } |
| 69 | + |
| 70 | + TEST(Section, SetPhysicalStart) { |
| 71 | + Section sect = Section(); |
| 72 | + EXPECT_EQ(sect.physicalStart, -1); |
| 73 | + sect.setPhysicalStart(5); |
| 74 | + EXPECT_EQ(sect.physicalStart, 5); |
| 75 | + } |
| 76 | + |
| 77 | + TEST(Section, SetNumLEDs) { |
| 78 | + Section sect = Section(); |
| 79 | + EXPECT_EQ(sect.numLEDs, 0); |
| 80 | + sect.setNumLEDs(5); |
| 81 | + EXPECT_EQ(sect.numLEDs, 5); |
| 82 | + } |
| 83 | + |
| 84 | + TEST(Section, JSON) { |
| 85 | + Section sect = Section(); |
| 86 | + |
| 87 | + sect.setName("Section"); |
| 88 | + sect.setStartPixel(15); |
| 89 | + sect.setEndPixel(30); |
| 90 | + |
| 91 | + char * data_str = new char[1000]; |
| 92 | + sect.json(&data_str); |
| 93 | + EXPECT_STREQ(data_str, "SECT:{\"name\":\"Section\",\"startPixel\":15,\"endPixel\":30}"); |
| 94 | + delete[] data_str; |
| 95 | + } |
| 96 | + |
| 97 | + TEST(Section, FromJSON) { |
| 98 | + std::string data_str = R"({"name":"NewSection","startPixel":20,"endPixel":55,"physicalStart":40,"numLEDs":36})"; |
| 99 | + |
| 100 | + nlohmann::json data_json = nlohmann::json::parse(data_str); |
| 101 | + Section sect = Section(data_json); |
| 102 | + |
| 103 | + EXPECT_STREQ(sect.name.c_str(), "NewSection"); |
| 104 | + EXPECT_EQ(sect.startPixel, 20); |
| 105 | + EXPECT_EQ(sect.endPixel, 55); |
| 106 | + EXPECT_EQ(sect.physicalStart, 40); |
| 107 | + EXPECT_EQ(sect.numLEDs, 36); |
| 108 | + } |
| 109 | + |
| 110 | + TEST(Section, FromBadJSON) { |
| 111 | + std::string data_str = "{}"; |
| 112 | + |
| 113 | + nlohmann::json data_json = nlohmann::json::parse(data_str); |
| 114 | + Section sect = Section(data_json); |
| 115 | + |
| 116 | + EXPECT_STREQ(sect.name.c_str(), ""); |
| 117 | + EXPECT_EQ(sect.startPixel, -1); |
| 118 | + EXPECT_EQ(sect.endPixel, -1); |
| 119 | + EXPECT_EQ(sect.physicalStart, -1); |
| 120 | + EXPECT_EQ(sect.numLEDs, 0); |
| 121 | + } |
| 122 | + |
| 123 | +} |
| 124 | + |
| 125 | +#endif //ANIMATEDLEDSTRIP_SECTIONTEST_HPP |
0 commit comments