Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
peephole
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
peephole
Commits
d0a4e367
Commit
d0a4e367
authored
Dec 19, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added representation functions for statement and apply_filter for block, along with unit tests.
parent
7b1f59f5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
5 deletions
+29
-5
src/utils.py
src/utils.py
+12
-0
tests/test_utils.py
tests/test_utils.py
+17
-5
No files found.
src/utils.py
View file @
d0a4e367
...
...
@@ -17,6 +17,13 @@ class Statement:
return
self
.
stype
==
other
.
stype
and
self
.
name
==
other
.
name
\
and
self
.
args
==
other
.
args
def
__str__
(
self
):
# pragma: nocover
return
'<Statement type=%s name=%s args=%s>'
\
%
(
self
.
stype
,
self
.
name
,
self
.
args
)
def
__repr__
(
self
):
# pragma: nocover
return
str
(
self
)
def
is_comment
(
self
):
return
self
.
stype
==
'comment'
...
...
@@ -120,6 +127,11 @@ class Block:
self.statements = before + replacement + after
self.pointer = start + len(replacement)
def apply_filter(self, callback):
"""Apply a filter to the statement list. If the callback returns True,
the statement will remain in the list.."""
self.statements = filter(callback, self.statements)
def find_leaders(statements):
"""Determine the leaders, which are:
...
...
tests/test_utils.py
View file @
d0a4e367
...
...
@@ -11,9 +11,13 @@ class TestUtils(unittest.TestCase):
self
.
statements
=
[
add
,
S
(
'command'
,
'j'
,
'foo'
),
add
,
add
,
\
S
(
'label'
,
'foo'
)]
self
.
block
=
B
([
S
(
'command'
,
'foo'
),
\
S
(
'comm
and
'
,
'bar'
),
S
(
'comm
ent
'
,
'bar'
),
S
(
'command'
,
'baz'
)])
def
tearDown
(
self
):
del
self
.
statements
del
self
.
block
def
test_find_leaders
(
self
):
self
.
assertEqual
(
find_leaders
(
self
.
statements
),
[
0
,
2
,
4
])
...
...
@@ -46,9 +50,12 @@ class TestUtils(unittest.TestCase):
self
.
assertEqual
(
S
(
'command'
,
'j'
,
'foo'
).
jump_target
(),
'foo'
)
self
.
assertRaises
(
Exception
,
S
(
'command'
,
'foo'
).
jump_target
)
def
test_iter
(
self
):
self
.
assertEqual
(
self
.
block
.
statements
,
list
(
self
.
block
))
def
test_read
(
self
):
self
.
assertEqual
(
self
.
block
.
read
(),
S
(
'command'
,
'foo'
))
self
.
assertEqual
(
self
.
block
.
read
(),
S
(
'comm
and
'
,
'bar'
))
self
.
assertEqual
(
self
.
block
.
read
(),
S
(
'comm
ent
'
,
'bar'
))
self
.
assertEqual
(
self
.
block
.
read
(),
S
(
'command'
,
'baz'
))
def
test_end
(
self
):
...
...
@@ -61,13 +68,18 @@ class TestUtils(unittest.TestCase):
def
test_peek
(
self
):
self
.
assertEqual
(
self
.
block
.
peek
(),
S
(
'command'
,
'foo'
))
self
.
assertEqual
(
self
.
block
.
peek
(
2
),
[
S
(
'command'
,
'foo'
),
\
S
(
'comm
and
'
,
'bar'
)])
S
(
'comm
ent
'
,
'bar'
)])
self
.
block
.
read
()
self
.
assertEqual
(
self
.
block
.
peek
(),
S
(
'comm
and
'
,
'bar'
))
self
.
assertEqual
(
self
.
block
.
peek
(),
S
(
'comm
ent
'
,
'bar'
))
def
test_replace
(
self
):
self
.
block
.
replace
(
1
,
[
S
(
'command'
,
'foobar'
)])
self
.
assertEqual
(
self
.
block
.
pointer
,
1
)
self
.
assertEqual
(
self
.
block
.
statements
,
[
S
(
'command'
,
'foobar'
),
\
S
(
'command'
,
'bar'
),
\
S
(
'comment'
,
'bar'
),
\
S
(
'command'
,
'baz'
)])
def
test_apply_filter
(
self
):
self
.
block
.
apply_filter
(
lambda
s
:
s
.
is_command
())
self
.
assertEqual
(
self
.
block
.
statements
,
[
S
(
'command'
,
'foo'
),
\
S
(
'command'
,
'baz'
)])
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