TEST_F(RulesetFixture, OSArgument_ClearValue) {
  std::vector<std::string> choices;
  choices.push_back("On");
  choices.push_back("Off");

  OSArgument boolArgument = OSArgument::makeBoolArgument("bool");
  OSArgument doubleArgument = OSArgument::makeDoubleArgument("double");
  OSArgument integerArgument = OSArgument::makeIntegerArgument("integer");
  OSArgument stringArgument = OSArgument::makeStringArgument("string");
  OSArgument choiceArgument = OSArgument::makeChoiceArgument("choice", choices);

  EXPECT_TRUE(boolArgument.setValue(true));
  EXPECT_TRUE(doubleArgument.setValue(1.0));
  EXPECT_TRUE(doubleArgument.setValue((int)1)); // can also set double arg using int
  EXPECT_TRUE(integerArgument.setValue((int)1));
  EXPECT_TRUE(stringArgument.setValue(std::string("value")));
  EXPECT_TRUE(choiceArgument.setValue(std::string("On")));

  ASSERT_TRUE(boolArgument.hasValue());
  ASSERT_TRUE(doubleArgument.hasValue());
  ASSERT_TRUE(integerArgument.hasValue());
  ASSERT_TRUE(stringArgument.hasValue());
  ASSERT_TRUE(choiceArgument.hasValue());

  boolArgument.clearValue();
  doubleArgument.clearValue();
  integerArgument.clearValue();
  stringArgument.clearValue();
  choiceArgument.clearValue();

  EXPECT_FALSE(boolArgument.hasValue());
  EXPECT_FALSE(doubleArgument.hasValue());
  EXPECT_FALSE(integerArgument.hasValue());
  EXPECT_FALSE(stringArgument.hasValue());
  EXPECT_FALSE(choiceArgument.hasValue());  

  EXPECT_ANY_THROW(boolArgument.valueAsBool());
  EXPECT_ANY_THROW(doubleArgument.valueAsDouble());
  EXPECT_ANY_THROW(integerArgument.valueAsInteger());
  EXPECT_ANY_THROW(stringArgument.valueAsString());
  EXPECT_ANY_THROW(choiceArgument.valueAsString());
}
Esempio n. 2
0
  QVariant toVariant(const OSArgument& argument) {
    QVariantMap argumentData;

    argumentData["uuid"] = toQString(removeBraces(argument.uuid()));
    argumentData["version_uuid"] = toQString(removeBraces(argument.versionUUID()));
    argumentData["name"] = toQString(argument.name());
    if (!argument.displayName().empty()) {
      argumentData["display_name"] = toQString(argument.displayName());
    }
    if (argument.description() && !argument.description()->empty()) {
      argumentData["description"] = toQString(argument.description().get());
    }
    OSArgumentType type = argument.type();
    if (argument.units() && !argument.units()->empty()) {
      argumentData["units"] = toQString(argument.units().get());
    }
    argumentData["type"] = toQString(type.valueName());
    argumentData["required"] = argument.required();
    argumentData["model_dependent"] = argument.modelDependent();
    if (argument.hasValue()) {
      if (type == OSArgumentType::Quantity) {
        Quantity value = argument.valueAsQuantity();
        argumentData["value"] = value.value();
        argumentData["value_units"] = toQString(value.units().standardString());
      }
      else {
        // use QVariant directly
        argumentData["value"] = argument.valueAsQVariant();
      }
    }
    if (argument.hasDefaultValue()) {
      if (type == OSArgumentType::Quantity) {
        Quantity defaultValue = argument.defaultValueAsQuantity();
        argumentData["default_value"] = defaultValue.value();
        argumentData["default_value_units"] = toQString(defaultValue.units().standardString());
      }
      else {
        // use QVariant directly
        argumentData["default_value"] = argument.defaultValueAsQVariant();
      }
    }
    argumentData["domain_type"] = toQString(argument.domainType().valueName());
    if (argument.hasDomain()) {
      QVariantList domainList;
      int index(0);
      for (const QVariant& dval : argument.domainAsQVariant()) {
        QVariantMap domainValueMap;
        domainValueMap["domain_value_index"] = index;
        if (type == OSArgumentType::Quantity) {
          Quantity q = dval.value<openstudio::Quantity>();
          domainValueMap["value"] = q.value();
          domainValueMap["units"] = toQString(q.units().standardString());
        }
        else {
          domainValueMap["value"] = dval;
        }
        domainList.push_back(domainValueMap);
        ++index;
      }
      argumentData["domain"] = domainList;
    }
    if (type == OSArgumentType::Choice) {
      QVariantList choicesList;
      StringVector displayNames = argument.choiceValueDisplayNames();
      int index(0), displayNamesN(displayNames.size());
      for (const std::string& choice : argument.choiceValues()) {
        QVariantMap choiceMap;
        choiceMap["choice_index"] = index;
        choiceMap["value"] = toQString(choice);
        if (index < displayNamesN) {
          choiceMap["display_name"] = toQString(displayNames[index]);
        }
        choicesList.push_back(choiceMap);
        ++index;
      }
      argumentData["choices"] = QVariant(choicesList);
    }
    if (type == OSArgumentType::Path) {
      argumentData["is_read"] = argument.isRead();
      argumentData["extension"] = toQString(argument.extension());
    }

    return QVariant(argumentData);
  }
TEST_F(RulesetFixture, OSArgument_Clone) {

  std::vector<std::string> choices;
  choices.push_back("On");
  choices.push_back("Off");

  OSArgument boolArgument = OSArgument::makeBoolArgument("bool");
  OSArgument doubleArgument = OSArgument::makeDoubleArgument("double");
  OSArgument integerArgument = OSArgument::makeIntegerArgument("integer");
  OSArgument stringArgument = OSArgument::makeStringArgument("string");
  OSArgument choiceArgument = OSArgument::makeChoiceArgument("choice", choices);

  EXPECT_FALSE(boolArgument.hasValue());
  EXPECT_FALSE(doubleArgument.hasValue());
  EXPECT_FALSE(integerArgument.hasValue());
  EXPECT_FALSE(stringArgument.hasValue());
  EXPECT_FALSE(choiceArgument.hasValue());

  EXPECT_TRUE(boolArgument.setValue(true));
  EXPECT_TRUE(doubleArgument.setValue(1.0));
  EXPECT_TRUE(doubleArgument.setValue((int)1)); // can also set double arg using int
  EXPECT_TRUE(integerArgument.setValue((int)1));
  EXPECT_TRUE(stringArgument.setValue(std::string("value")));
  EXPECT_TRUE(choiceArgument.setValue(std::string("On")));

  ASSERT_TRUE(boolArgument.hasValue());
  ASSERT_TRUE(doubleArgument.hasValue());
  ASSERT_TRUE(integerArgument.hasValue());
  ASSERT_TRUE(stringArgument.hasValue());
  ASSERT_TRUE(choiceArgument.hasValue());

  EXPECT_EQ(true, boolArgument.valueAsBool());
  EXPECT_EQ(1.0, doubleArgument.valueAsDouble());
  EXPECT_EQ(1, integerArgument.valueAsInteger());
  EXPECT_EQ("value", stringArgument.valueAsString());
  EXPECT_EQ("On", choiceArgument.valueAsString());

  OSArgument boolArgument2 = boolArgument.clone();
  OSArgument doubleArgument2 = doubleArgument.clone();
  OSArgument integerArgument2 = integerArgument.clone();
  OSArgument stringArgument2 = stringArgument.clone();
  OSArgument choiceArgument2 = choiceArgument.clone();

  ASSERT_TRUE(boolArgument2.hasValue());
  ASSERT_TRUE(doubleArgument2.hasValue());
  ASSERT_TRUE(integerArgument2.hasValue());
  ASSERT_TRUE(stringArgument2.hasValue());
  ASSERT_TRUE(choiceArgument2.hasValue());

  EXPECT_EQ(true, boolArgument2.valueAsBool());
  EXPECT_EQ(1.0, doubleArgument2.valueAsDouble());
  EXPECT_EQ(1, integerArgument2.valueAsInteger());
  EXPECT_EQ("value", stringArgument2.valueAsString());
  EXPECT_EQ("On", choiceArgument2.valueAsString());

  std::vector<OSArgument> argumentVector;
  argumentVector.push_back(boolArgument);
  argumentVector.push_back(doubleArgument);
  argumentVector.push_back(integerArgument);
  argumentVector.push_back(stringArgument);
  argumentVector.push_back(choiceArgument);
  std::map<std::string,OSArgument> argumentMap = convertOSArgumentVectorToMap(argumentVector);

  ASSERT_FALSE(argumentMap.find("bool") == argumentMap.end());
  ASSERT_FALSE(argumentMap.find("double") == argumentMap.end());
  ASSERT_FALSE(argumentMap.find("integer") == argumentMap.end());
  ASSERT_FALSE(argumentMap.find("string") == argumentMap.end());
  ASSERT_FALSE(argumentMap.find("choice") == argumentMap.end());

  ASSERT_TRUE(argumentMap.find("bool")->second.hasValue());
  ASSERT_TRUE(argumentMap.find("double")->second.hasValue());
  ASSERT_TRUE(argumentMap.find("integer")->second.hasValue());
  ASSERT_TRUE(argumentMap.find("string")->second.hasValue());
  ASSERT_TRUE(argumentMap.find("choice")->second.hasValue());

  EXPECT_EQ(true, argumentMap.find("bool")->second.valueAsBool());
  EXPECT_EQ(1.0, argumentMap.find("double")->second.valueAsDouble());
  EXPECT_EQ(1, argumentMap.find("integer")->second.valueAsInteger());
  EXPECT_EQ("value", argumentMap.find("string")->second.valueAsString());
  EXPECT_EQ("On", argumentMap.find("choice")->second.valueAsString());

}