|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: GPL-3.0-only |
| 3 | + * MuseScore-CLA-applies |
| 4 | + * |
| 5 | + * MuseScore |
| 6 | + * Music Composition & Notation |
| 7 | + * |
| 8 | + * Copyright (C) 2024 MuseScore BVBA and others |
| 9 | + * |
| 10 | + * This program is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU General Public License version 3 as |
| 12 | + * published by the Free Software Foundation. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public License |
| 20 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 21 | + */ |
| 22 | +#include <gtest/gtest.h> |
| 23 | + |
| 24 | +#include "uicomponents/view/validators/doubleinputvalidator.h" |
| 25 | + |
| 26 | +using namespace muse; |
| 27 | +using namespace muse::uicomponents; |
| 28 | + |
| 29 | +namespace muse::uicomponents { |
| 30 | +class DoubleInputValidatorTests : public ::testing::Test, public QObject |
| 31 | +{ |
| 32 | +public: |
| 33 | + void SetUp() override |
| 34 | + { |
| 35 | + m_validator = new DoubleInputValidator(nullptr); |
| 36 | + m_validator->setTop(100.0); |
| 37 | + m_validator->setBottom(-100.0); |
| 38 | + m_validator->setDecimal(2); |
| 39 | + } |
| 40 | + |
| 41 | + void TearDown() override |
| 42 | + { |
| 43 | + delete m_validator; |
| 44 | + } |
| 45 | + |
| 46 | +protected: |
| 47 | + DoubleInputValidator* m_validator = nullptr; |
| 48 | +}; |
| 49 | + |
| 50 | +TEST_F(DoubleInputValidatorTests, Validate) { |
| 51 | + struct Input |
| 52 | + { |
| 53 | + QString str; |
| 54 | + QValidator::State expectedState; |
| 55 | + QString fixedStr; |
| 56 | + }; |
| 57 | + |
| 58 | + std::vector<Input> validInputs = { |
| 59 | + { "-0.1", QValidator::Acceptable }, |
| 60 | + { "0", QValidator::Acceptable }, |
| 61 | + { "1.23", QValidator::Acceptable }, |
| 62 | + { "99.99", QValidator::Acceptable }, |
| 63 | + { "-100", QValidator::Acceptable }, |
| 64 | + { "2.5", QValidator::Acceptable }, |
| 65 | + { "0.0", QValidator::Intermediate, "0" }, |
| 66 | + { "00.", QValidator::Intermediate, "0" }, |
| 67 | + { "2.00", QValidator::Intermediate, "2" }, |
| 68 | + { "2.", QValidator::Intermediate, "2" }, |
| 69 | + { "-100.1", QValidator::Intermediate, "-100" }, |
| 70 | + { "100.1", QValidator::Intermediate, "100" }, |
| 71 | + { "1.123", QValidator::Intermediate, "1.12" }, |
| 72 | + { "abc", QValidator::Invalid, "" } |
| 73 | + }; |
| 74 | + |
| 75 | + int pos = 0; |
| 76 | + for (Input& input : validInputs) { |
| 77 | + EXPECT_EQ(m_validator->validate(input.str, pos), input.expectedState); |
| 78 | + |
| 79 | + if (QValidator::Invalid == input.expectedState) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + QString fixInput = input.str; |
| 84 | + m_validator->fixup(fixInput); |
| 85 | + |
| 86 | + QString expectedStr = QValidator::Acceptable == input.expectedState ? input.str : input.fixedStr; |
| 87 | + EXPECT_EQ(expectedStr, fixInput); |
| 88 | + } |
| 89 | +} |
| 90 | +} |
0 commit comments