Skip to content

Commit ca0bc23

Browse files
divy9881hsluoyz
authored andcommitted
Add util tests.
1 parent e2c6bff commit ca0bc23

File tree

6 files changed

+63
-5
lines changed

6 files changed

+63
-5
lines changed

casbin/model/scope_config.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
#include "./scope_config.h"
66

7-
void* InitializeScope() {
8-
return (void*)new int;
7+
Scope InitializeScope() {
8+
return duk_create_heap_default();
99
}
1010

1111
void PushFunctionValue(Scope scope, Function f, int nargs){

casbin/model/scope_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ typedef duk_ret_t ReturnType;
2323
typedef duk_c_function Function;
2424
typedef duk_idx_t Index;
2525

26-
void* InitializeScope();
26+
Scope InitializeScope();
2727
void PushFunctionValue(Scope scope, Function f, int nargs);
2828
void PushBooleanValue(Scope scope, bool expression);
2929
void PushTrueValue(Scope scope);

casbin/rbac_api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ vector<string> Enforcer :: GetImplicitUsersForPermission(vector<string> permissi
202202

203203
vector<string> res;
204204
for (int i = 0 ; i < users.size() ; i++) {
205-
Scope scope = (Scope)InitializeScope();
205+
Scope scope = InitializeScope();
206206
PushObject(scope);
207207
PushStringPropToObject(scope, "r", users[i], "sub");
208208
PushStringPropToObject(scope, "r", permission[0], "obj");

casbin/util/built_in_functions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ReturnType KeyMatch(Scope scope) {
2121
string key1 = GetString(scope, 0);
2222
string key2 = GetString(scope, 1);
2323

24-
size_t pos = key1.find("*");
24+
size_t pos = key2.find("*");
2525

2626
if (pos == string :: npos) {
2727
PushBooleanValue(scope, key1 == key2);

test/test.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
4545
</ClCompile>
4646
<ClCompile Include="test_model.cpp" />
47+
<ClCompile Include="test_util.cpp" />
4748
</ItemGroup>
4849
<ItemGroup>
4950
<None Include="packages.config" />

test/test_util.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#pragma once
2+
3+
#include "pch.h"
4+
5+
#include <util.h>
6+
7+
using namespace std;
8+
9+
class TestUtil : public ::testing::Test {
10+
11+
};
12+
13+
void TestEscapeAssertion(string s, string res) {
14+
string my_res = EscapeAssertion(s);
15+
EXPECT_EQ(my_res, res);
16+
}
17+
18+
TEST_F(TestUtil, TestEscapeAssertion) {
19+
TestEscapeAssertion("r.attr.value == p.attr", "r_attr.value == p_attr");
20+
TestEscapeAssertion("r.attp.value || p.attr", "r_attp.value || p_attr");
21+
TestEscapeAssertion("r.attp.value &&p.attr", "r_attp.value &&p_attr");
22+
TestEscapeAssertion("r.attp.value >p.attr", "r_attp.value >p_attr");
23+
TestEscapeAssertion("r.attp.value <p.attr", "r_attp.value <p_attr");
24+
TestEscapeAssertion("r.attp.value +p.attr", "r_attp.value +p_attr");
25+
TestEscapeAssertion("r.attp.value -p.attr", "r_attp.value -p_attr");
26+
TestEscapeAssertion("r.attp.value *p.attr", "r_attp.value *p_attr");
27+
TestEscapeAssertion("r.attp.value /p.attr", "r_attp.value /p_attr");
28+
TestEscapeAssertion("!r.attp.value /p.attr", "!r_attp.value /p_attr");
29+
TestEscapeAssertion("g(r.sub, p.sub) == p.attr", "g(r_sub, p_sub) == p_attr");
30+
TestEscapeAssertion("g(r.sub,p.sub) == p.attr", "g(r_sub,p_sub) == p_attr");
31+
TestEscapeAssertion("(r.attp.value || p.attr)p.u", "(r_attp.value || p_attr)p_u");
32+
}
33+
34+
void TestRemoveComments(string s, string res) {
35+
string my_res = RemoveComments(s);
36+
EXPECT_EQ(my_res, res);
37+
}
38+
39+
TEST_F(TestUtil, TestRemoveComments) {
40+
TestRemoveComments("r.act == p.act # comments", "r.act == p.act");
41+
TestRemoveComments("r.act == p.act#comments", "r.act == p.act");
42+
TestRemoveComments("r.act == p.act###", "r.act == p.act");
43+
TestRemoveComments("### comments", "");
44+
TestRemoveComments("r.act == p.act", "r.act == p.act");
45+
}
46+
47+
void TestArrayEquals(vector<string> a, vector<string> b, bool res) {
48+
bool my_res = ArrayEquals(a, b);
49+
EXPECT_EQ(my_res, res);
50+
}
51+
52+
TEST_F(TestUtil, TestArrayEquals) {
53+
TestArrayEquals(vector<string> {"a", "b", "c"}, vector<string> {"a", "b", "c"}, true);
54+
TestArrayEquals(vector<string> {"a", "b", "c"}, vector<string> {"a", "b"}, false);
55+
TestArrayEquals(vector<string> {"a", "b", "c"}, vector<string> {"a", "c", "b"}, false);
56+
TestArrayEquals(vector<string> {"a", "b", "c"}, vector<string> {}, false);
57+
}

0 commit comments

Comments
 (0)