From 2096558837c0f583044c6a98e7fb7542af6517d1 Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Wed, 22 Aug 2018 15:40:09 -0700 Subject: [PATCH] init: always allow clearing a property An unintended consequence of property types is that it makes clearing a property, by setting it to an empty string, impossible. This change explicitly allows that case: Test: new (and old) unit tests Change-Id: I188693bfd3a71b64c194c3858544230b87d8d891 --- init/property_type.cpp | 5 +++++ init/property_type_test.cpp | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/init/property_type.cpp b/init/property_type.cpp index 249b12b99..7d805552b 100644 --- a/init/property_type.cpp +++ b/init/property_type.cpp @@ -29,6 +29,11 @@ namespace android { namespace init { bool CheckType(const std::string& type_string, const std::string& value) { + // Always allow clearing a property such that the default value when it is not set takes over. + if (value.empty()) { + return true; + } + auto type_strings = Split(type_string, " "); if (type_strings.empty()) { return false; diff --git a/init/property_type_test.cpp b/init/property_type_test.cpp index 068bccc8d..6d7f9270b 100644 --- a/init/property_type_test.cpp +++ b/init/property_type_test.cpp @@ -32,7 +32,7 @@ TEST(property_type, CheckType_string) { } TEST(property_type, CheckType_int) { - EXPECT_FALSE(CheckType("int", "")); + EXPECT_TRUE(CheckType("int", "")); EXPECT_FALSE(CheckType("int", "abc")); EXPECT_FALSE(CheckType("int", "-abc")); EXPECT_TRUE(CheckType("int", "0")); @@ -43,7 +43,7 @@ TEST(property_type, CheckType_int) { } TEST(property_type, CheckType_uint) { - EXPECT_FALSE(CheckType("uint", "")); + EXPECT_TRUE(CheckType("uint", "")); EXPECT_FALSE(CheckType("uint", "abc")); EXPECT_FALSE(CheckType("uint", "-abc")); EXPECT_TRUE(CheckType("uint", "0")); @@ -53,7 +53,7 @@ TEST(property_type, CheckType_uint) { } TEST(property_type, CheckType_double) { - EXPECT_FALSE(CheckType("double", "")); + EXPECT_TRUE(CheckType("double", "")); EXPECT_FALSE(CheckType("double", "abc")); EXPECT_FALSE(CheckType("double", "-abc")); EXPECT_TRUE(CheckType("double", "0.0")); @@ -64,7 +64,7 @@ TEST(property_type, CheckType_double) { } TEST(property_type, CheckType_size) { - EXPECT_FALSE(CheckType("size", "")); + EXPECT_TRUE(CheckType("size", "")); EXPECT_FALSE(CheckType("size", "ab")); EXPECT_FALSE(CheckType("size", "abcd")); EXPECT_FALSE(CheckType("size", "0")); @@ -80,7 +80,7 @@ TEST(property_type, CheckType_size) { } TEST(property_type, CheckType_enum) { - EXPECT_FALSE(CheckType("enum abc", "")); + EXPECT_TRUE(CheckType("enum abc", "")); EXPECT_FALSE(CheckType("enum abc", "ab")); EXPECT_FALSE(CheckType("enum abc", "abcd")); EXPECT_FALSE(CheckType("enum 123 456 789", "0"));