Exemplo n.º 1
0
TEST_F(RulesetFixture, OSArgument_Domain) {
  
  OSArgument doubleArg = OSArgument::makeDoubleArgument("double", true);
  OSArgument integerArg = OSArgument::makeIntegerArgument("integer", true);
  OSArgument stringArg = OSArgument::makeStringArgument("string", true);

  EXPECT_FALSE(doubleArg.hasDomain());
  EXPECT_FALSE(integerArg.hasDomain());
  EXPECT_FALSE(stringArg.hasDomain());

  double d = 0.0;
  int i = 0;

  EXPECT_TRUE(doubleArg.setMinValue(d));
  EXPECT_TRUE(integerArg.setMinValue(i));
  EXPECT_FALSE(stringArg.setMinValue(0.0));

  ASSERT_TRUE(doubleArg.hasDomain());
  ASSERT_TRUE(integerArg.hasDomain());
  EXPECT_FALSE(stringArg.hasDomain());

  EXPECT_EQ(0.0, doubleArg.domainAsDouble()[0]);
  EXPECT_EQ(std::numeric_limits<double>::max(), doubleArg.domainAsDouble()[1]);
  EXPECT_EQ(0, integerArg.domainAsInteger()[0]);
  EXPECT_EQ(std::numeric_limits<int>::max(), integerArg.domainAsDouble()[1]);

  d = 1.0;
  i = 1;

  EXPECT_TRUE(doubleArg.setMaxValue(i));
  EXPECT_TRUE(integerArg.setMaxValue(d));
  EXPECT_FALSE(stringArg.setMaxValue(1));

  ASSERT_TRUE(doubleArg.hasDomain());
  ASSERT_TRUE(integerArg.hasDomain());
  EXPECT_FALSE(stringArg.hasDomain());

  EXPECT_EQ(0.0, doubleArg.domainAsDouble()[0]);
  EXPECT_EQ(1.0, doubleArg.domainAsDouble()[1]);
  EXPECT_EQ(0, integerArg.domainAsInteger()[0]);
  EXPECT_EQ(1, integerArg.domainAsDouble()[1]);

  // the domain is not currently used to validate these values
  EXPECT_TRUE(doubleArg.setValue(-1.0));
  EXPECT_TRUE(doubleArg.setValue(0.0));
  EXPECT_TRUE(doubleArg.setValue(0.5));
  EXPECT_TRUE(doubleArg.setValue(1.0));
  EXPECT_TRUE(doubleArg.setValue(2.0));

  EXPECT_TRUE(integerArg.setValue(-1));
  EXPECT_TRUE(integerArg.setValue(0));
  EXPECT_TRUE(integerArg.setValue(1));
  EXPECT_TRUE(integerArg.setValue(2));
}
Exemplo 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);
  }