Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
yaml-cpp
Commits
72f699f5
Commit
72f699f5
authored
Nov 04, 2019
by
Igor [hyperxor]
Committed by
Jesse Beder
Nov 04, 2019
Browse files
Remove redundant checks and add more unit tests (#783)
parent
a8ba6a8d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
33 deletions
+26
-33
include/yaml-cpp/node/impl.h
include/yaml-cpp/node/impl.h
+1
-33
test/node/node_test.cpp
test/node/node_test.cpp
+25
-0
No files found.
include/yaml-cpp/node/impl.h
View file @
72f699f5
...
@@ -172,8 +172,6 @@ inline const std::string& Node::Tag() const {
...
@@ -172,8 +172,6 @@ inline const std::string& Node::Tag() const {
}
}
inline
void
Node
::
SetTag
(
const
std
::
string
&
tag
)
{
inline
void
Node
::
SetTag
(
const
std
::
string
&
tag
)
{
if
(
!
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
EnsureNodeExists
();
EnsureNodeExists
();
m_pNode
->
set_tag
(
tag
);
m_pNode
->
set_tag
(
tag
);
}
}
...
@@ -185,8 +183,6 @@ inline EmitterStyle::value Node::Style() const {
...
@@ -185,8 +183,6 @@ inline EmitterStyle::value Node::Style() const {
}
}
inline
void
Node
::
SetStyle
(
EmitterStyle
::
value
style
)
{
inline
void
Node
::
SetStyle
(
EmitterStyle
::
value
style
)
{
if
(
!
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
EnsureNodeExists
();
EnsureNodeExists
();
m_pNode
->
set_style
(
style
);
m_pNode
->
set_style
(
style
);
}
}
...
@@ -202,15 +198,11 @@ inline bool Node::is(const Node& rhs) const {
...
@@ -202,15 +198,11 @@ inline bool Node::is(const Node& rhs) const {
template
<
typename
T
>
template
<
typename
T
>
inline
Node
&
Node
::
operator
=
(
const
T
&
rhs
)
{
inline
Node
&
Node
::
operator
=
(
const
T
&
rhs
)
{
if
(
!
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
Assign
(
rhs
);
Assign
(
rhs
);
return
*
this
;
return
*
this
;
}
}
inline
Node
&
Node
::
operator
=
(
const
Node
&
rhs
)
{
inline
Node
&
Node
::
operator
=
(
const
Node
&
rhs
)
{
if
(
!
m_isValid
||
!
rhs
.
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
if
(
is
(
rhs
))
if
(
is
(
rhs
))
return
*
this
;
return
*
this
;
AssignNode
(
rhs
);
AssignNode
(
rhs
);
...
@@ -233,29 +225,21 @@ inline void Node::Assign(const T& rhs) {
...
@@ -233,29 +225,21 @@ inline void Node::Assign(const T& rhs) {
template
<
>
template
<
>
inline
void
Node
::
Assign
(
const
std
::
string
&
rhs
)
{
inline
void
Node
::
Assign
(
const
std
::
string
&
rhs
)
{
if
(
!
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
EnsureNodeExists
();
EnsureNodeExists
();
m_pNode
->
set_scalar
(
rhs
);
m_pNode
->
set_scalar
(
rhs
);
}
}
inline
void
Node
::
Assign
(
const
char
*
rhs
)
{
inline
void
Node
::
Assign
(
const
char
*
rhs
)
{
if
(
!
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
EnsureNodeExists
();
EnsureNodeExists
();
m_pNode
->
set_scalar
(
rhs
);
m_pNode
->
set_scalar
(
rhs
);
}
}
inline
void
Node
::
Assign
(
char
*
rhs
)
{
inline
void
Node
::
Assign
(
char
*
rhs
)
{
if
(
!
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
EnsureNodeExists
();
EnsureNodeExists
();
m_pNode
->
set_scalar
(
rhs
);
m_pNode
->
set_scalar
(
rhs
);
}
}
inline
void
Node
::
AssignData
(
const
Node
&
rhs
)
{
inline
void
Node
::
AssignData
(
const
Node
&
rhs
)
{
if
(
!
m_isValid
||
!
rhs
.
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
EnsureNodeExists
();
EnsureNodeExists
();
rhs
.
EnsureNodeExists
();
rhs
.
EnsureNodeExists
();
...
@@ -264,7 +248,7 @@ inline void Node::AssignData(const Node& rhs) {
...
@@ -264,7 +248,7 @@ inline void Node::AssignData(const Node& rhs) {
}
}
inline
void
Node
::
AssignNode
(
const
Node
&
rhs
)
{
inline
void
Node
::
AssignNode
(
const
Node
&
rhs
)
{
if
(
!
m_isValid
||
!
rhs
.
m_isValid
)
if
(
!
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
throw
InvalidNode
(
m_invalidKey
);
rhs
.
EnsureNodeExists
();
rhs
.
EnsureNodeExists
();
...
@@ -320,8 +304,6 @@ inline void Node::push_back(const T& rhs) {
...
@@ -320,8 +304,6 @@ inline void Node::push_back(const T& rhs) {
}
}
inline
void
Node
::
push_back
(
const
Node
&
rhs
)
{
inline
void
Node
::
push_back
(
const
Node
&
rhs
)
{
if
(
!
m_isValid
||
!
rhs
.
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
EnsureNodeExists
();
EnsureNodeExists
();
rhs
.
EnsureNodeExists
();
rhs
.
EnsureNodeExists
();
...
@@ -382,8 +364,6 @@ std::string key_to_string(const Key& key) {
...
@@ -382,8 +364,6 @@ std::string key_to_string(const Key& key) {
// indexing
// indexing
template
<
typename
Key
>
template
<
typename
Key
>
inline
const
Node
Node
::
operator
[](
const
Key
&
key
)
const
{
inline
const
Node
Node
::
operator
[](
const
Key
&
key
)
const
{
if
(
!
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
EnsureNodeExists
();
EnsureNodeExists
();
detail
::
node
*
value
=
static_cast
<
const
detail
::
node
&>
(
*
m_pNode
).
get
(
detail
::
node
*
value
=
static_cast
<
const
detail
::
node
&>
(
*
m_pNode
).
get
(
detail
::
to_value
(
key
),
m_pMemory
);
detail
::
to_value
(
key
),
m_pMemory
);
...
@@ -395,8 +375,6 @@ inline const Node Node::operator[](const Key& key) const {
...
@@ -395,8 +375,6 @@ inline const Node Node::operator[](const Key& key) const {
template
<
typename
Key
>
template
<
typename
Key
>
inline
Node
Node
::
operator
[](
const
Key
&
key
)
{
inline
Node
Node
::
operator
[](
const
Key
&
key
)
{
if
(
!
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
EnsureNodeExists
();
EnsureNodeExists
();
detail
::
node
&
value
=
m_pNode
->
get
(
detail
::
to_value
(
key
),
m_pMemory
);
detail
::
node
&
value
=
m_pNode
->
get
(
detail
::
to_value
(
key
),
m_pMemory
);
return
Node
(
value
,
m_pMemory
);
return
Node
(
value
,
m_pMemory
);
...
@@ -404,15 +382,11 @@ inline Node Node::operator[](const Key& key) {
...
@@ -404,15 +382,11 @@ inline Node Node::operator[](const Key& key) {
template
<
typename
Key
>
template
<
typename
Key
>
inline
bool
Node
::
remove
(
const
Key
&
key
)
{
inline
bool
Node
::
remove
(
const
Key
&
key
)
{
if
(
!
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
EnsureNodeExists
();
EnsureNodeExists
();
return
m_pNode
->
remove
(
detail
::
to_value
(
key
),
m_pMemory
);
return
m_pNode
->
remove
(
detail
::
to_value
(
key
),
m_pMemory
);
}
}
inline
const
Node
Node
::
operator
[](
const
Node
&
key
)
const
{
inline
const
Node
Node
::
operator
[](
const
Node
&
key
)
const
{
if
(
!
m_isValid
||
!
key
.
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
EnsureNodeExists
();
EnsureNodeExists
();
key
.
EnsureNodeExists
();
key
.
EnsureNodeExists
();
m_pMemory
->
merge
(
*
key
.
m_pMemory
);
m_pMemory
->
merge
(
*
key
.
m_pMemory
);
...
@@ -425,8 +399,6 @@ inline const Node Node::operator[](const Node& key) const {
...
@@ -425,8 +399,6 @@ inline const Node Node::operator[](const Node& key) const {
}
}
inline
Node
Node
::
operator
[](
const
Node
&
key
)
{
inline
Node
Node
::
operator
[](
const
Node
&
key
)
{
if
(
!
m_isValid
||
!
key
.
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
EnsureNodeExists
();
EnsureNodeExists
();
key
.
EnsureNodeExists
();
key
.
EnsureNodeExists
();
m_pMemory
->
merge
(
*
key
.
m_pMemory
);
m_pMemory
->
merge
(
*
key
.
m_pMemory
);
...
@@ -435,8 +407,6 @@ inline Node Node::operator[](const Node& key) {
...
@@ -435,8 +407,6 @@ inline Node Node::operator[](const Node& key) {
}
}
inline
bool
Node
::
remove
(
const
Node
&
key
)
{
inline
bool
Node
::
remove
(
const
Node
&
key
)
{
if
(
!
m_isValid
||
!
key
.
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
EnsureNodeExists
();
EnsureNodeExists
();
key
.
EnsureNodeExists
();
key
.
EnsureNodeExists
();
return
m_pNode
->
remove
(
*
key
.
m_pNode
,
m_pMemory
);
return
m_pNode
->
remove
(
*
key
.
m_pNode
,
m_pMemory
);
...
@@ -445,8 +415,6 @@ inline bool Node::remove(const Node& key) {
...
@@ -445,8 +415,6 @@ inline bool Node::remove(const Node& key) {
// map
// map
template
<
typename
Key
,
typename
Value
>
template
<
typename
Key
,
typename
Value
>
inline
void
Node
::
force_insert
(
const
Key
&
key
,
const
Value
&
value
)
{
inline
void
Node
::
force_insert
(
const
Key
&
key
,
const
Value
&
value
)
{
if
(
!
m_isValid
)
throw
InvalidNode
(
m_invalidKey
);
EnsureNodeExists
();
EnsureNodeExists
();
m_pNode
->
force_insert
(
detail
::
to_value
(
key
),
detail
::
to_value
(
value
),
m_pNode
->
force_insert
(
detail
::
to_value
(
key
),
detail
::
to_value
(
value
),
m_pMemory
);
m_pMemory
);
...
...
test/node/node_test.cpp
View file @
72f699f5
...
@@ -59,6 +59,18 @@ TEST(NodeTest, SequenceElementRemoval) {
...
@@ -59,6 +59,18 @@ TEST(NodeTest, SequenceElementRemoval) {
EXPECT_EQ
(
"c"
,
node
[
1
].
as
<
std
::
string
>
());
EXPECT_EQ
(
"c"
,
node
[
1
].
as
<
std
::
string
>
());
}
}
TEST
(
NodeTest
,
SequenceFirstElementRemoval
)
{
Node
node
;
node
[
0
]
=
"a"
;
node
[
1
]
=
"b"
;
node
[
2
]
=
"c"
;
node
.
remove
(
0
);
EXPECT_TRUE
(
node
.
IsSequence
());
EXPECT_EQ
(
2
,
node
.
size
());
EXPECT_EQ
(
"b"
,
node
[
0
].
as
<
std
::
string
>
());
EXPECT_EQ
(
"c"
,
node
[
1
].
as
<
std
::
string
>
());
}
TEST
(
NodeTest
,
SequenceLastElementRemoval
)
{
TEST
(
NodeTest
,
SequenceLastElementRemoval
)
{
Node
node
;
Node
node
;
node
[
0
]
=
"a"
;
node
[
0
]
=
"a"
;
...
@@ -71,6 +83,19 @@ TEST(NodeTest, SequenceLastElementRemoval) {
...
@@ -71,6 +83,19 @@ TEST(NodeTest, SequenceLastElementRemoval) {
EXPECT_EQ
(
"b"
,
node
[
1
].
as
<
std
::
string
>
());
EXPECT_EQ
(
"b"
,
node
[
1
].
as
<
std
::
string
>
());
}
}
TEST
(
NodeTest
,
NodeAssignment
)
{
Node
node1
;
Node
node2
;
node1
[
1
]
=
1
;
node1
[
2
]
=
2
;
node1
[
3
]
=
3
;
node2
=
node1
;
EXPECT_EQ
(
node1
,
node2
);
EXPECT_EQ
(
node1
[
1
],
node2
[
1
]);
EXPECT_EQ
(
node1
[
2
],
node2
[
2
]);
EXPECT_EQ
(
node1
[
3
],
node2
[
3
]);
}
TEST
(
NodeTest
,
MapElementRemoval
)
{
TEST
(
NodeTest
,
MapElementRemoval
)
{
Node
node
;
Node
node
;
node
[
"foo"
]
=
"bar"
;
node
[
"foo"
]
=
"bar"
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment