Skip to content

Commit

Permalink
fix char+octet type checks
Browse files Browse the repository at this point in the history
add unit tests
fix #1489
  • Loading branch information
Markus Raab committed May 8, 2017
1 parent 19433aa commit 349539d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/plugins/type/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
52 changes: 52 additions & 0 deletions src/plugins/type/testmod_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
5 changes: 3 additions & 2 deletions src/plugins/type/type_checker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ class TypeChecker
types.insert (pair<string, Type *> ("float", new TType<kdb::float_t> ()));
types.insert (pair<string, Type *> ("double", new TType<kdb::double_t> ()));
types.insert (pair<string, Type *> ("long_double", new TType<kdb::long_double_t> ()));
types.insert (pair<string, Type *> ("char", new TType<kdb::char_t> ()));
types.insert (pair<string, Type *> ("boolean", new TType<kdb::boolean_t> ()));
types.insert (pair<string, Type *> ("octet", new TType<kdb::octet_t> ()));

types.insert (pair<string, Type *> ("char", new CharType ()));
types.insert (pair<string, Type *> ("octet", new CharType ()));

types.insert (pair<string, Type *> ("any", new AnyType ()));
types.insert (pair<string, Type *> ("string", new StringType ()));
Expand Down
9 changes: 9 additions & 0 deletions src/plugins/type/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions tests/shell/shell_recorder/tutorial_wrapper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 349539d

Please sign in to comment.