diff --git a/src/plugins/type/README.md b/src/plugins/type/README.md index 7714b24eee6..22ef56fafd4 100644 --- a/src/plugins/type/README.md +++ b/src/plugins/type/README.md @@ -35,6 +35,21 @@ type was valid. For example, the type `string empty` equals the type types specify. It is now deprecated due to a more general sum type facility. +## Example + +```sh +sudo kdb mount typetest.dump user/typetest dump type +kdb set user/typetest/key a +kdb setmeta user/typetest/key check/type char +# RET:0 + +kdb get user/typetest/key +#> a + +kdb rm user/typetest/key +kdb umount user/typetest +``` + ## Limitations diff --git a/src/plugins/type/testmod_type.cpp b/src/plugins/type/testmod_type.cpp index e514612f96c..c5584bc49f5 100644 --- a/src/plugins/type/testmod_type.cpp +++ b/src/plugins/type/testmod_type.cpp @@ -484,3 +484,55 @@ TEST (type, minmax) k.setString (""); succeed_if (tc.check (k), "should succeed (empty value)"); } + +TEST (type, char) +{ + KeySet config; + TypeChecker tc (config); + + Key k ("user/anything", KEY_VALUE, "a", KEY_META, "check/type", "char", KEY_END); + EXPECT_TRUE (tc.check (k)) << "a should check successfully as char"; + k.setString ("b"); + EXPECT_TRUE (tc.check (k)) << "b should check successfully as char"; + k.setString (" "); + EXPECT_TRUE (tc.check (k)) << "space should check successfully as char"; + k.setString (""); + EXPECT_FALSE (tc.check (k)) << "empty string should not check successfully as char"; + k.setString ("ab"); + EXPECT_FALSE (tc.check (k)) << "two chars should not check successfully as char"; + + for (int i = 1; i < 255; ++i) + { + char x[2]; + x[0] = i; + x[1] = 0; + k.setString (x); + EXPECT_TRUE (tc.check (k)) << x << " should check successfully as char"; + } +} + +TEST (type, octet) +{ + KeySet config; + TypeChecker tc (config); + + Key k ("user/anything", KEY_VALUE, "a", KEY_META, "check/type", "octet", KEY_END); + EXPECT_TRUE (tc.check (k)) << "a should check successfully as octet"; + k.setString ("b"); + EXPECT_TRUE (tc.check (k)) << "b should check successfully as octet"; + k.setString (" "); + EXPECT_TRUE (tc.check (k)) << "space should check successfully as octet"; + k.setString (""); + EXPECT_FALSE (tc.check (k)) << "empty string should not check successfully as octet"; + k.setString ("ab"); + EXPECT_FALSE (tc.check (k)) << "two chars should not check successfully as octet"; + + for (int i = 1; i < 255; ++i) + { + char x[2]; + x[0] = i; + x[1] = 0; + k.setString (x); + EXPECT_TRUE (tc.check (k)) << x << " should check successfully as octet"; + } +} diff --git a/src/plugins/type/type_checker.hpp b/src/plugins/type/type_checker.hpp index eacb447d471..833c1be4a07 100644 --- a/src/plugins/type/type_checker.hpp +++ b/src/plugins/type/type_checker.hpp @@ -49,9 +49,10 @@ class TypeChecker types.insert (pair ("float", new TType ())); types.insert (pair ("double", new TType ())); types.insert (pair ("long_double", new TType ())); - types.insert (pair ("char", new TType ())); types.insert (pair ("boolean", new TType ())); - types.insert (pair ("octet", new TType ())); + + types.insert (pair ("char", new CharType ())); + types.insert (pair ("octet", new CharType ())); types.insert (pair ("any", new AnyType ())); types.insert (pair ("string", new StringType ())); diff --git a/src/plugins/type/types.hpp b/src/plugins/type/types.hpp index 2e407443aff..1ec1b1c0b25 100644 --- a/src/plugins/type/types.hpp +++ b/src/plugins/type/types.hpp @@ -53,6 +53,15 @@ class EmptyType : public Type } }; +class CharType : public Type +{ +public: + bool check (Key k) override + { + return k.getString ().length () == 1; + } +}; + class StringType : public Type { public: diff --git a/tests/shell/shell_recorder/tutorial_wrapper/CMakeLists.txt b/tests/shell/shell_recorder/tutorial_wrapper/CMakeLists.txt index 3b87797d010..e285a222d22 100644 --- a/tests/shell/shell_recorder/tutorial_wrapper/CMakeLists.txt +++ b/tests/shell/shell_recorder/tutorial_wrapper/CMakeLists.txt @@ -28,6 +28,7 @@ add_plugin_shell_test (mini) add_plugin_shell_test (mozprefs) add_plugin_shell_test (xerces) add_plugin_shell_test (range) +add_plugin_shell_test (type) # Only works with super user privileges, since it writes to `/etc/hosts`: # add_s_test (tutorial_mount "${CMAKE_SOURCE_DIR}/doc/tutorials/mount.md")