コード例 #1
0
    /**
     * Test BoolSchemaItem no default value
     *
     * Create SchemaItem without default value. Method setDefaultValue should always return false
     * and leave SmartObject in previous state.
     **/
    TEST(test_no_default_value, test_BoolSchemaItemTest)
    {
        NsSmartDeviceLink::NsSmartObjects::SmartObject obj;

        utils::SharedPtr<NsSmartDeviceLink::NsSmartObjects::CBoolSchemaItem> item =
            NsSmartDeviceLink::NsSmartObjects::CBoolSchemaItem::create(); // No default value

        obj = 5;
        ASSERT_EQ(5, obj.asInt());

        int resultType = item->validate(obj);
        EXPECT_EQ(NsSmartDeviceLink::NsSmartObjects::Errors::INVALID_VALUE, resultType);

        obj = true;
        ASSERT_TRUE(obj.asBool());

        resultType = item->validate(obj);
        EXPECT_EQ(NsSmartDeviceLink::NsSmartObjects::Errors::OK, resultType);
        bool resDefault = item->setDefaultValue(obj);
        EXPECT_FALSE(resDefault);
        EXPECT_TRUE(obj.asBool());

        obj = "Test";
        ASSERT_EQ(std::string("Test"), obj.asString());

        resultType = item->validate(obj);
        EXPECT_EQ(NsSmartDeviceLink::NsSmartObjects::Errors::INVALID_VALUE, resultType);
        resDefault = item->setDefaultValue(obj);
        EXPECT_FALSE(resDefault);
        resultType = item->validate(obj);
        EXPECT_EQ(NsSmartDeviceLink::NsSmartObjects::Errors::INVALID_VALUE, resultType);
    }
コード例 #2
0
void NsSmartDeviceLink::NsJSONHandler::Formatters::CFormatterJsonBase::
    objToJsonValue(const NsSmartDeviceLink::NsSmartObjects::SmartObject& obj,
                   utils::json::JsonValueRef item) {
  using namespace utils::json;
  try {
    if (NsSmartDeviceLink::NsSmartObjects::SmartType_Array == obj.getType()) {
      item = ValueType::ARRAY_VALUE;

      for (uint32_t i = 0; i < obj.length(); i++) {
        JsonValue value;

        objToJsonValue(obj.getElement(i), value);

        item.Append(value);
      }
    } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Map ==
               obj.getType()) {
      item = ValueType::OBJECT_VALUE;
      std::set<std::string> keys = obj.enumerate();

      for (std::set<std::string>::const_iterator key_itr = keys.begin(),
                                                 end = keys.end();
           key_itr != end;
           ++key_itr) {
        JsonValue value;

        objToJsonValue(obj.getElement(*key_itr), value);

        item[*key_itr] = value;
      }
    } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Boolean ==
               obj.getType()) {
      item = obj.asBool();
    } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Integer ==
               obj.getType()) {
      item = obj.asInt();
    } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Double ==
               obj.getType()) {
      item = obj.asDouble();
    } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Null ==
               obj.getType()) {
      item = ValueType::NULL_VALUE;
    } else {
      item = obj.asString();
    }
  } catch (...) {
  }
}