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
0a135252
"vscode:/vscode.git/clone" did not exist on "2937387a5000a9463cd5e7121bdfef567ec5b771"
Commit
0a135252
authored
Jan 07, 2016
by
Scott Wolchok
Committed by
Jesse Beder
Mar 26, 2016
Browse files
add some tests for RegEx
parent
f327b565
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
177 additions
and
0 deletions
+177
-0
test/regex_test.cpp
test/regex_test.cpp
+177
-0
No files found.
test/regex_test.cpp
0 → 100644
View file @
0a135252
#include "gtest/gtest.h"
#include "regex_yaml.h"
#include "stream.h"
using
YAML
::
RegEx
;
using
YAML
::
Stream
;
namespace
{
const
auto
MIN_CHAR
=
Stream
::
eof
()
+
1
;
TEST
(
RegExTest
,
Empty
)
{
RegEx
empty
;
EXPECT_TRUE
(
empty
.
Matches
(
std
::
string
()));
EXPECT_EQ
(
0
,
empty
.
Match
(
std
::
string
()));
for
(
int
i
=
MIN_CHAR
;
i
<
128
;
++
i
)
{
auto
str
=
std
::
string
(
1
,
char
(
i
));
EXPECT_FALSE
(
empty
.
Matches
(
str
));
EXPECT_EQ
(
-
1
,
empty
.
Match
(
str
));
}
}
TEST
(
RegExTest
,
Range
)
{
for
(
int
i
=
MIN_CHAR
;
i
<
128
;
++
i
)
{
for
(
int
j
=
MIN_CHAR
;
j
<
128
;
++
j
)
{
RegEx
ex
((
char
)
i
,
(
char
)
j
);
for
(
int
k
=
MIN_CHAR
;
k
<
128
;
++
k
)
{
auto
str
=
std
::
string
(
1
,
char
(
k
));
if
(
i
<=
k
&&
k
<=
j
)
{
EXPECT_TRUE
(
ex
.
Matches
(
str
));
EXPECT_EQ
(
1
,
ex
.
Match
(
str
));
}
else
{
EXPECT_FALSE
(
ex
.
Matches
(
str
));
EXPECT_EQ
(
-
1
,
ex
.
Match
(
str
));
}
}
}
}
}
TEST
(
RegExTest
,
EmptyString
)
{
RegEx
ex
=
RegEx
(
std
::
string
());
EXPECT_TRUE
(
ex
.
Matches
(
std
::
string
()));
EXPECT_EQ
(
0
,
ex
.
Match
(
std
::
string
()));
// Matches anything, unlike RegEx()!
EXPECT_TRUE
(
ex
.
Matches
(
std
::
string
(
"hello"
)));
EXPECT_EQ
(
0
,
ex
.
Match
(
std
::
string
(
"hello"
)));
}
TEST
(
RegExTest
,
SingleCharacterString
)
{
for
(
int
i
=
MIN_CHAR
;
i
<
128
;
++
i
)
{
RegEx
ex
(
std
::
string
(
1
,
(
char
)
i
));
for
(
int
j
=
MIN_CHAR
;
j
<
128
;
++
j
)
{
auto
str
=
std
::
string
(
1
,
char
(
j
));
if
(
j
==
i
)
{
EXPECT_TRUE
(
ex
.
Matches
(
str
));
EXPECT_EQ
(
1
,
ex
.
Match
(
str
));
// Match at start of string only!
std
::
string
prefixed
=
std
::
string
(
1
,
i
+
1
)
+
std
::
string
(
"prefix: "
)
+
str
;
EXPECT_FALSE
(
ex
.
Matches
(
prefixed
));
EXPECT_EQ
(
-
1
,
ex
.
Match
(
prefixed
));
}
else
{
EXPECT_FALSE
(
ex
.
Matches
(
str
));
EXPECT_EQ
(
-
1
,
ex
.
Match
(
str
));
}
}
}
}
TEST
(
RegExTest
,
MultiCharacterString
)
{
RegEx
ex
(
std
::
string
(
"ab"
));
EXPECT_FALSE
(
ex
.
Matches
(
std
::
string
(
"a"
)));
EXPECT_EQ
(
-
1
,
ex
.
Match
(
std
::
string
(
"a"
)));
EXPECT_TRUE
(
ex
.
Matches
(
std
::
string
(
"ab"
)));
EXPECT_EQ
(
2
,
ex
.
Match
(
std
::
string
(
"ab"
)));
EXPECT_TRUE
(
ex
.
Matches
(
std
::
string
(
"abba"
)));
EXPECT_EQ
(
2
,
ex
.
Match
(
std
::
string
(
"abba"
)));
// match at start of string only!
EXPECT_FALSE
(
ex
.
Matches
(
std
::
string
(
"baab"
)));
EXPECT_EQ
(
-
1
,
ex
.
Match
(
std
::
string
(
"baab"
)));
}
TEST
(
RegExTest
,
OperatorNot
)
{
RegEx
ex
=
!
RegEx
(
std
::
string
(
"ab"
));
EXPECT_TRUE
(
ex
.
Matches
(
std
::
string
(
"a"
)));
EXPECT_EQ
(
1
,
ex
.
Match
(
std
::
string
(
"a"
)));
EXPECT_FALSE
(
ex
.
Matches
(
std
::
string
(
"ab"
)));
EXPECT_EQ
(
-
1
,
ex
.
Match
(
std
::
string
(
"ab"
)));
EXPECT_FALSE
(
ex
.
Matches
(
std
::
string
(
"abba"
)));
EXPECT_EQ
(
-
1
,
ex
.
Match
(
std
::
string
(
"abba"
)));
// match at start of string only!
EXPECT_TRUE
(
ex
.
Matches
(
std
::
string
(
"baab"
)));
// Operator not causes only one character to be matched.
EXPECT_EQ
(
1
,
ex
.
Match
(
std
::
string
(
"baab"
)));
}
TEST
(
RegExTest
,
OperatorOr
)
{
for
(
int
i
=
MIN_CHAR
;
i
<
127
;
++
i
)
{
for
(
int
j
=
i
+
1
;
j
<
128
;
++
j
)
{
auto
iStr
=
std
::
string
(
1
,
char
(
i
));
auto
jStr
=
std
::
string
(
1
,
char
(
j
));
RegEx
ex1
=
RegEx
(
iStr
)
||
RegEx
(
jStr
);
RegEx
ex2
=
RegEx
(
jStr
)
||
RegEx
(
iStr
);
for
(
int
k
=
MIN_CHAR
;
k
<
128
;
++
k
)
{
auto
str
=
std
::
string
(
1
,
char
(
k
));
if
(
i
==
k
||
j
==
k
)
{
EXPECT_TRUE
(
ex1
.
Matches
(
str
));
EXPECT_TRUE
(
ex2
.
Matches
(
str
));
EXPECT_EQ
(
1
,
ex1
.
Match
(
str
));
EXPECT_EQ
(
1
,
ex2
.
Match
(
str
));
}
else
{
EXPECT_FALSE
(
ex1
.
Matches
(
str
));
EXPECT_FALSE
(
ex2
.
Matches
(
str
));
EXPECT_EQ
(
-
1
,
ex1
.
Match
(
str
));
EXPECT_EQ
(
-
1
,
ex2
.
Match
(
str
));
}
}
}
}
}
TEST
(
RegExTest
,
OperatorOrShortCircuits
)
{
RegEx
ex1
=
RegEx
(
std
::
string
(
"aaaa"
))
||
RegEx
(
std
::
string
(
"aa"
));
RegEx
ex2
=
RegEx
(
std
::
string
(
"aa"
))
||
RegEx
(
std
::
string
(
"aaaa"
));
EXPECT_TRUE
(
ex1
.
Matches
(
std
::
string
(
"aaaaa"
)));
EXPECT_EQ
(
4
,
ex1
.
Match
(
std
::
string
(
"aaaaa"
)));
EXPECT_TRUE
(
ex2
.
Matches
(
std
::
string
(
"aaaaa"
)));
EXPECT_EQ
(
2
,
ex2
.
Match
(
std
::
string
(
"aaaaa"
)));
}
TEST
(
RegExTest
,
OperatorAnd
)
{
RegEx
emptySet
=
RegEx
(
'a'
)
&&
RegEx
();
EXPECT_FALSE
(
emptySet
.
Matches
(
std
::
string
(
"a"
)));
}
TEST
(
RegExTest
,
OperatorAndShortCircuits
)
{
RegEx
ex1
=
RegEx
(
std
::
string
(
"aaaa"
))
&&
RegEx
(
std
::
string
(
"aa"
));
RegEx
ex2
=
RegEx
(
std
::
string
(
"aa"
))
&&
RegEx
(
std
::
string
(
"aaaa"
));
EXPECT_TRUE
(
ex1
.
Matches
(
std
::
string
(
"aaaaa"
)));
EXPECT_EQ
(
4
,
ex1
.
Match
(
std
::
string
(
"aaaaa"
)));
EXPECT_TRUE
(
ex2
.
Matches
(
std
::
string
(
"aaaaa"
)));
EXPECT_EQ
(
2
,
ex2
.
Match
(
std
::
string
(
"aaaaa"
)));
}
TEST
(
RegExTest
,
OperatorPlus
)
{
RegEx
ex
=
RegEx
(
std
::
string
(
"hello "
))
+
RegEx
(
std
::
string
(
"there"
));
EXPECT_TRUE
(
ex
.
Matches
(
std
::
string
(
"hello there"
)));
EXPECT_FALSE
(
ex
.
Matches
(
std
::
string
(
"hello "
)));
EXPECT_FALSE
(
ex
.
Matches
(
std
::
string
(
"there"
)));
EXPECT_EQ
(
11
,
ex
.
Match
(
std
::
string
(
"hello there"
)));
}
TEST
(
RegExTest
,
StringOr
)
{
std
::
string
str
=
"abcde"
;
RegEx
ex
=
RegEx
(
str
,
YAML
::
REGEX_OR
);
for
(
size_t
i
=
0
;
i
<
str
.
size
();
++
i
)
{
EXPECT_TRUE
(
ex
.
Matches
(
str
.
substr
(
i
,
1
)));
EXPECT_EQ
(
1
,
ex
.
Match
(
str
.
substr
(
i
,
1
)));
}
EXPECT_EQ
(
1
,
ex
.
Match
(
str
));
}
}
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