From 8e81ef19fc346747e971cd61c8ca486ca9b9a528 Mon Sep 17 00:00:00 2001
From: Owen Arnold <owen.arnold@stfc.ac.uk>
Date: Fri, 13 Nov 2015 10:39:49 +0000
Subject: [PATCH] refs #14421. allowedValues specalization.

We should be able to use the drop down list property widget type if we can provide the allowed values for the OptionalBool.
---
 .../inc/MantidKernel/PropertyWithValue.h      | 19 ++++++++++++++++++-
 Framework/Kernel/test/PropertyWithValueTest.h | 15 ++++++++++++++-
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/Framework/Kernel/inc/MantidKernel/PropertyWithValue.h b/Framework/Kernel/inc/MantidKernel/PropertyWithValue.h
index 31fc3b40d41..cef85abbfab 100644
--- a/Framework/Kernel/inc/MantidKernel/PropertyWithValue.h
+++ b/Framework/Kernel/inc/MantidKernel/PropertyWithValue.h
@@ -258,6 +258,23 @@ inline void addingOperator(boost::shared_ptr<T> &,
   throw Exception::NotImplementedError(
       "PropertyWithValue.h: += operator not implemented for boost::shared_ptr");
 }
+
+template <typename T>
+inline std::vector<std::string>
+determineAllowedValues(const T &, const IValidator &validator) {
+  return validator.allowedValues();
+}
+
+template <>
+inline std::vector<std::string> determineAllowedValues(const OptionalBool &,
+                                                       const IValidator &) {
+  auto enumMap = OptionalBool::enumToStrMap();
+  std::vector<std::string> values;
+  for (auto it = enumMap.begin(); it != enumMap.end(); ++it) {
+    values.push_back(it->second);
+  }
+  return values;
+}
 }
 //------------------------------------------------------------------------------------------------
 // Now the PropertyWithValue class itself
@@ -496,7 +513,7 @@ public:
    * an empty vector.
    */
   virtual std::vector<std::string> allowedValues() const {
-    return m_validator->allowedValues();
+    return determineAllowedValues(m_value, *m_validator);
   }
 
   /**
diff --git a/Framework/Kernel/test/PropertyWithValueTest.h b/Framework/Kernel/test/PropertyWithValueTest.h
index 350f32b8b5b..5a2964b9ace 100644
--- a/Framework/Kernel/test/PropertyWithValueTest.h
+++ b/Framework/Kernel/test/PropertyWithValueTest.h
@@ -377,6 +377,7 @@ public:
     TS_ASSERT(dProp->allowedValues().empty());
     TS_ASSERT(sProp->allowedValues().empty());
     TS_ASSERT(lProp->allowedValues().empty());
+    TS_ASSERT(!bProp->allowedValues().empty())
     // Tests using a ListValidator are below
   }
 
@@ -676,12 +677,24 @@ public:
   void test_optional_bool_to_setValue() {
 
     std::string input = OptionalBool::StrTrue;
-
     PropertyWithValue<OptionalBool> property("myproperty", OptionalBool::Unset,
                                              Direction::Input);
     property.setValue(input);
   }
 
+  void test_optional_bool_allowed_values() {
+    PropertyWithValue<OptionalBool> property("myproperty", OptionalBool::Unset,
+                                             Direction::Input);
+
+    auto values = property.allowedValues();
+    auto possibilities = OptionalBool::strToEmumMap();
+    TSM_ASSERT_EQUALS("3 states allowed", possibilities.size(), values.size());
+    for (auto it = values.begin(); it != values.end(); ++it) {
+      TSM_ASSERT("value not a known state",
+                 possibilities.find(*it) != possibilities.end());
+    }
+  }
+
 private:
   PropertyWithValue<int> *iProp;
   PropertyWithValue<double> *dProp;
-- 
GitLab