Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
muppet-strings
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hugo Hörnquist
muppet-strings
Commits
0397a11f
Commit
0397a11f
authored
1 year ago
by
Hugo Hörnquist
Browse files
Options
Downloads
Patches
Plain Diff
Add more tests.
parent
1293b96f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
tests/test_parser_combinator.py
+191
-0
191 additions, 0 deletions
tests/test_parser_combinator.py
tests/test_util.py
+14
-0
14 additions, 0 deletions
tests/test_util.py
with
205 additions
and
0 deletions
tests/test_parser_combinator.py
0 → 100644
+
191
−
0
View file @
0397a11f
from
muppet.parser_combinator
import
(
MatchLiteral
,
MatchObject
,
ParseDirective
,
ParseError
,
ParserCombinator
,
all_
,
char
,
complement
,
count
,
digit
,
hexdig
,
line_comment
,
many
,
many1
,
name
,
nop
,
not_
,
optional
,
s
,
tag
,
ws
,
delimited
,
discard
,
space
,
)
def
lit
(
x
):
return
MatchLiteral
(
matched
=
x
)
def
test_partial
():
data
=
"
123 Hello
"
parser
=
ParserCombinator
(
data
)
assert
[
lit
(
"
123
"
)]
==
parser
.
get
(
many
(
digit
))
assert
"
Hello
"
==
parser
.
remaining
()
def
test_char
():
parser
=
ParserCombinator
(
"
Hello!
"
)
assert
[
lit
(
'
H
'
)]
==
parser
.
get
(
char
)
assert
[
lit
(
'
e
'
)]
==
parser
.
get
(
char
)
assert
"
llo!
"
==
parser
.
remaining
()
def
test_nop
():
parser
=
ParserCombinator
(
"
Hello!
"
)
assert
[]
==
parser
.
get
(
nop
)
assert
"
Hello!
"
==
parser
.
remaining
()
def
test_digit
():
p1
=
ParserCombinator
(
"
123
"
)
assert
[
lit
(
'
1
'
)]
==
p1
.
get
(
digit
)
p2
=
ParserCombinator
(
"
Hello
"
)
try
:
p2
.
get
(
digit
)
assert
False
,
"
Parser should have failed, but didn
'
t
"
except
ParseError
:
assert
"
Hello
"
==
p2
.
remaining
()
def
test_consume
():
p
=
ParserCombinator
(
"
Hello
"
)
try
:
p
.
get
([
char
,
digit
])
assert
False
,
"
Parser should have failed, but didn
'
t
"
except
ParseError
:
assert
"
ello
"
==
p
.
remaining
()
def
test_hexdig
():
pass
def
test_space
():
pass
# --------------------------------------------------
def
test_many
():
p1
=
ParserCombinator
(
"
Hello, World!
"
)
assert
[
lit
(
"
Hello, World!
"
)]
==
p1
.
get
(
many
(
char
))
p2
=
ParserCombinator
(
""
)
assert
[]
==
p2
.
get
(
many
(
char
))
def
test_many1
():
p1
=
ParserCombinator
(
"
Hello, World!
"
)
assert
[
lit
(
"
Hello, World!
"
)]
==
p1
.
get
(
many1
(
char
))
p2
=
ParserCombinator
(
""
)
try
:
p2
.
get
(
many1
(
char
))
assert
False
,
"
Parser should have failed, but didn
'
t
"
except
ParseError
:
assert
True
def
test_count
():
p1
=
ParserCombinator
(
"
ABCDE
"
)
assert
[
lit
(
"
A
"
)]
==
p1
.
get
(
count
(
s
(
"
A
"
),
1
,
3
))
p2
=
ParserCombinator
(
"
AAAAA
"
)
assert
[
lit
(
"
AAA
"
)]
==
p2
.
get
(
count
(
s
(
"
A
"
),
1
,
3
))
p3
=
ParserCombinator
(
"
BBBBB
"
)
assert
[]
==
p3
.
get
(
count
(
s
(
"
A
"
),
3
))
p4
=
ParserCombinator
(
"
AAAAA
"
)
assert
[
lit
(
"
AAA
"
)]
==
p4
.
get
(
count
(
s
(
"
A
"
),
3
))
def
test_optional
():
p1
=
ParserCombinator
(
"
ABC
"
)
assert
[
lit
(
"
A
"
)]
==
p1
.
get
(
optional
(
s
(
"
A
"
)))
assert
"
BC
"
==
p1
.
remaining
()
p2
=
ParserCombinator
(
"
ABC
"
)
assert
[]
==
p2
.
get
(
optional
(
s
(
"
B
"
)))
assert
"
ABC
"
==
p2
.
remaining
()
def
test_ws
():
p1
=
ParserCombinator
(
"
Hello
"
)
assert
[]
==
p1
.
get
(
ws
)
p2
=
ParserCombinator
(
"
\t
\n\r
Hello
"
)
assert
[
lit
(
"
\t
\n\r
"
)]
==
p2
.
get
(
ws
)
def
test_discard
():
p1
=
ParserCombinator
(
"
Hello
"
)
assert
[]
==
p1
.
get
(
discard
(
s
(
"
He
"
)))
assert
"
llo
"
==
p1
.
remaining
()
p2
=
ParserCombinator
(
"
Hello!
"
)
assert
[
lit
(
"
ll
"
)]
==
p2
.
get
(
discard
(
s
(
"
He
"
))
&
s
(
"
ll
"
)
&
discard
(
s
(
"
o
"
)))
assert
"
!
"
==
p2
.
remaining
()
def
handle_int
(
xs
:
list
[
MatchObject
])
->
list
[
int
]:
"""
Convert matched to an integer.
"""
return
[
int
(
xs
[
0
].
matched
)]
def
test_delimited
():
number
=
many
(
digit
)
@
handle_int
p1
=
ParserCombinator
(
"
1,20,2
"
)
assert
[
1
,
lit
(
"
,
"
),
20
,
lit
(
"
,
"
),
2
]
==
p1
.
get
(
delimited
(
s
(
"
,
"
),
number
))
p2
=
ParserCombinator
(
"
1,20,2
"
)
assert
[
1
,
20
,
2
]
==
p2
.
get
(
delimited
(
discard
(
s
(
"
,
"
)),
number
))
def
test_all
():
p1
=
ParserCombinator
(
"
123
"
)
assert
[
lit
(
"
1
"
)]
==
p1
.
get
(
all_
(
char
,
digit
))
def
test_complement_1
():
p1
=
ParserCombinator
(
"
Hello, World!
"
)
assert
[
lit
(
"
H
"
)]
==
p1
.
get
(
all_
(
~
space
,
char
))
def
test_complement_2
():
p1
=
ParserCombinator
(
"
Hello, World!
"
)
assert
[
lit
(
"
Hello,
"
)]
==
p1
.
get
(
many
(
all_
(
~
space
,
char
)))
def
test_complement
():
p
=
ParserCombinator
(
"
Hello
"
)
assert
[
lit
(
"
He
"
)]
==
p
.
get
(
many
(
complement
(
"
l
"
)))
def
test_stringifiers
():
assert
"'
a
'"
==
str
(
s
(
"
a
"
))
assert
"
~
'
a
'"
==
repr
(
~
s
(
"
a
"
))
assert
"
x
"
==
str
(
name
(
"
x
"
,
space
&
space
))
assert
"
(
'
a
'
&
'
b
'
)
"
==
str
(
s
(
'
a
'
)
&
s
(
'
b
'
))
assert
"
(
'
a
'
|
'
b
'
)
"
==
str
(
s
(
'
a
'
)
|
s
(
'
b
'
))
assert
"
char
"
==
str
(
char
)
assert
"
nop
"
==
str
(
nop
)
This diff is collapsed.
Click to expand it.
tests/test_util.py
0 → 100644
+
14
−
0
View file @
0397a11f
from
muppet.util
import
group_by
,
concatenate
def
test_group_by
():
groups
=
group_by
(
lambda
x
:
x
%
3
,
range
(
10
))
assert
len
(
groups
)
==
3
assert
groups
[
0
]
==
[
0
,
3
,
6
,
9
]
assert
groups
[
1
]
==
[
1
,
4
,
7
]
assert
groups
[
2
]
==
[
2
,
5
,
8
]
def
test_concatenate
():
assert
concatenate
([[
1
,
2
],
[
3
,
4
]])
==
[
1
,
2
,
3
,
4
]
assert
concatenate
([[
1
,
[
2
]],
[
3
,
4
]])
==
[
1
,
[
2
],
3
,
4
]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment