mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 01:40:04 +00:00 
			
		
		
		
	Settings variable minimum/maximum, additional tests
This commit is contained in:
		
							parent
							
								
									db665ed9ec
								
							
						
					
					
						commit
						08b1d6e152
					
				
					 3 changed files with 67 additions and 11 deletions
				
			
		|  | @ -1,3 +1,5 @@ | ||||||
|  | #define SETTINGS_VARIABLE_IMPLEMENTATION | ||||||
|  | 
 | ||||||
| #include <scwx/qt/settings/settings_variable.hpp> | #include <scwx/qt/settings/settings_variable.hpp> | ||||||
| #include <scwx/util/logger.hpp> | #include <scwx/util/logger.hpp> | ||||||
| 
 | 
 | ||||||
|  | @ -8,11 +10,6 @@ namespace qt | ||||||
| namespace settings | namespace settings | ||||||
| { | { | ||||||
| 
 | 
 | ||||||
| template class SettingsVariable<bool>; |  | ||||||
| template class SettingsVariable<int64_t>; |  | ||||||
| template class SettingsVariable<std::string>; |  | ||||||
| template class SettingsVariable<std::vector<int64_t>>; |  | ||||||
| 
 |  | ||||||
| static const std::string logPrefix_ = "scwx::qt::settings::settings_variable"; | static const std::string logPrefix_ = "scwx::qt::settings::settings_variable"; | ||||||
| 
 | 
 | ||||||
| template<class T> | template<class T> | ||||||
|  | @ -84,6 +81,14 @@ bool SettingsVariable<T>::SetValueOrDefault(const T& value) | ||||||
|       p->value_ = value; |       p->value_ = value; | ||||||
|       validated = true; |       validated = true; | ||||||
|    } |    } | ||||||
|  |    else if (p->minimum_.has_value() && value < p->minimum_) | ||||||
|  |    { | ||||||
|  |       p->value_ = *p->minimum_; | ||||||
|  |    } | ||||||
|  |    else if (p->maximum_.has_value() && value > p->maximum_) | ||||||
|  |    { | ||||||
|  |       p->value_ = *p->maximum_; | ||||||
|  |    } | ||||||
|    else |    else | ||||||
|    { |    { | ||||||
|       p->value_ = p->default_; |       p->value_ = p->default_; | ||||||
|  | @ -92,6 +97,12 @@ bool SettingsVariable<T>::SetValueOrDefault(const T& value) | ||||||
|    return validated; |    return validated; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | template<class T> | ||||||
|  | void SettingsVariable<T>::SetValueToDefault() | ||||||
|  | { | ||||||
|  |    p->value_ = p->default_; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| template<class T> | template<class T> | ||||||
| bool SettingsVariable<T>::StageValue(const T& value) | bool SettingsVariable<T>::StageValue(const T& value) | ||||||
| { | { | ||||||
|  | @ -111,7 +122,7 @@ void SettingsVariable<T>::Commit() | ||||||
| { | { | ||||||
|    if (p->staged_.has_value()) |    if (p->staged_.has_value()) | ||||||
|    { |    { | ||||||
|       p->value_ = std::move(p->staged_.value()); |       p->value_ = std::move(*p->staged_); | ||||||
|       p->staged_.reset(); |       p->staged_.reset(); | ||||||
|    } |    } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -29,6 +29,7 @@ public: | ||||||
|    T    GetValue() const; |    T    GetValue() const; | ||||||
|    bool SetValue(const T& value); |    bool SetValue(const T& value); | ||||||
|    bool SetValueOrDefault(const T& value); |    bool SetValueOrDefault(const T& value); | ||||||
|  |    void SetValueToDefault(); | ||||||
| 
 | 
 | ||||||
|    bool StageValue(const T& value); |    bool StageValue(const T& value); | ||||||
|    void Commit(); |    void Commit(); | ||||||
|  | @ -45,11 +46,11 @@ private: | ||||||
|    std::unique_ptr<Impl> p; |    std::unique_ptr<Impl> p; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| // Instantiated templates:
 | #ifdef SETTINGS_VARIABLE_IMPLEMENTATION | ||||||
| //    template class SettingsVariable<bool>;
 | template class SettingsVariable<bool>; | ||||||
| //    template class SettingsVariable<int64_t>;
 | template class SettingsVariable<int64_t>; | ||||||
| //    template class SettingsVariable<std::string>;
 | template class SettingsVariable<std::string>; | ||||||
| //    template class SettingsVariable<std::vector<int64_t>>;
 | #endif | ||||||
| 
 | 
 | ||||||
| } // namespace settings
 | } // namespace settings
 | ||||||
| } // namespace qt
 | } // namespace qt
 | ||||||
|  |  | ||||||
|  | @ -9,6 +9,50 @@ namespace qt | ||||||
| namespace settings | namespace settings | ||||||
| { | { | ||||||
| 
 | 
 | ||||||
|  | TEST(SettingsVariableTest, Boolean) | ||||||
|  | { | ||||||
|  |    SettingsVariable<bool> boolVariable {"bool"}; | ||||||
|  |    boolVariable.SetDefault(true); | ||||||
|  |    boolVariable.SetValue(false); | ||||||
|  | 
 | ||||||
|  |    EXPECT_EQ(boolVariable.name(), "bool"); | ||||||
|  |    EXPECT_EQ(boolVariable.GetValue(), false); | ||||||
|  |    EXPECT_EQ(boolVariable.SetValue(true), true); | ||||||
|  |    EXPECT_EQ(boolVariable.GetValue(), true); | ||||||
|  |    EXPECT_EQ(boolVariable.SetValueOrDefault(false), true); | ||||||
|  |    EXPECT_EQ(boolVariable.GetValue(), false); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | TEST(SettingsVariableTest, Integer) | ||||||
|  | { | ||||||
|  |    SettingsVariable<int64_t> intVariable {"int64_t"}; | ||||||
|  |    intVariable.SetDefault(42); | ||||||
|  |    intVariable.SetMinimum(10); | ||||||
|  |    intVariable.SetMaximum(99); | ||||||
|  |    intVariable.SetValue(50); | ||||||
|  | 
 | ||||||
|  |    EXPECT_EQ(intVariable.name(), "int64_t"); | ||||||
|  |    EXPECT_EQ(intVariable.GetValue(), 50); | ||||||
|  |    EXPECT_EQ(intVariable.SetValue(0), false); | ||||||
|  |    EXPECT_EQ(intVariable.GetValue(), 50); | ||||||
|  |    EXPECT_EQ(intVariable.SetValueOrDefault(0), false); // < Minimum
 | ||||||
|  |    EXPECT_EQ(intVariable.GetValue(), 10); | ||||||
|  |    EXPECT_EQ(intVariable.SetValueOrDefault(100), false); // > Maximum
 | ||||||
|  |    EXPECT_EQ(intVariable.GetValue(), 99); | ||||||
|  |    intVariable.SetValueToDefault(); | ||||||
|  |    EXPECT_EQ(intVariable.GetValue(), 42); | ||||||
|  |    EXPECT_EQ(intVariable.SetValue(43), true); | ||||||
|  |    EXPECT_EQ(intVariable.GetValue(), 43); | ||||||
|  |    EXPECT_EQ(intVariable.SetValueOrDefault(57), true); | ||||||
|  |    EXPECT_EQ(intVariable.GetValue(), 57); | ||||||
|  | 
 | ||||||
|  |    EXPECT_EQ(intVariable.StageValue(0), false); | ||||||
|  |    EXPECT_EQ(intVariable.StageValue(50), true); | ||||||
|  |    EXPECT_EQ(intVariable.GetValue(), 57); | ||||||
|  |    intVariable.Commit(); | ||||||
|  |    EXPECT_EQ(intVariable.GetValue(), 50); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| TEST(SettingsVariableTest, String) | TEST(SettingsVariableTest, String) | ||||||
| { | { | ||||||
|    SettingsVariable<std::string> stringVariable {"string"}; |    SettingsVariable<std::string> stringVariable {"string"}; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat