Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CICD 2022
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Antoine Gaudron-Desjardins
CICD 2022
Commits
3b27dd28
Commit
3b27dd28
authored
2 years ago
by
Antoine Gaudron-Desjardins
Browse files
Options
Downloads
Patches
Plain Diff
fix: fix the calculator
parent
65ae3892
Branches
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#46283
failed
2 years ago
Stage: dependencies
Stage: lint
Stage: test
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
calculator/calculator.py
+3
-3
3 additions, 3 deletions
calculator/calculator.py
calculator/test_calculator.py
+13
-4
13 additions, 4 deletions
calculator/test_calculator.py
with
16 additions
and
7 deletions
calculator/calculator.py
+
3
−
3
View file @
3b27dd28
...
...
@@ -54,7 +54,7 @@ class Calculator:
#####################################################################
### ATTENTION: Est-ce que c'est vraiment un < (non c'est un <=) ? ###
#####################################################################
if
operator
is
None
or
token
.
precedence
<
operator
.
precedence
:
if
operator
is
None
or
token
.
precedence
<
=
operator
.
precedence
:
operator
=
token
operator_index
=
i
...
...
@@ -66,5 +66,5 @@ class Calculator:
return
OperatorExpression
(
operator
,
self
.
parse
(
left
),
self
.
parse
(
right
))
def
__call__
(
self
,
expression
:
str
)
->
Term
:
return
5
# Au moins il y a pas de surprise 😐
#
return self.parse(self.tokenize(expression))()
#
return 5 # Au moins il y a pas de surprise 😐
return
self
.
parse
(
self
.
tokenize
(
expression
))()
This diff is collapsed.
Click to expand it.
calculator/test_calculator.py
+
13
−
4
View file @
3b27dd28
...
...
@@ -3,7 +3,7 @@ Test module for the calculator module.
"""
import
pytest
from
calculator.calculator
import
Calculator
from
calculator.operators
import
STANDARD_OPERATORS
,
Operator
from
calculator.operators
import
STANDARD_OPERATORS
@pytest.fixture
(
scope
=
"
module
"
,
name
=
"
setup
"
)
...
...
@@ -26,20 +26,29 @@ def test_tokenizer(setup):
plus
,
minus
,
times
,
divide
,
calc
=
setup
# À toi de tester la fonction tokenize de Calculator.
assert
calc
.
tokenize
(
"
1 + 2
"
)
==
[
1
,
plus
,
2
]
assert
calc
.
tokenize
(
"
3 / 4
"
)
==
[
3
,
divide
,
4
]
assert
calc
.
tokenize
(
"
4 * 2
"
)
==
[
4
,
times
,
2
]
assert
calc
.
tokenize
(
"
4 - 2
"
)
==
[
4
,
minus
,
2
]
def
test_parser
(
setup
):
"""
Test the parser.
"""
plus
,
minus
,
times
,
divide
,
calc
=
setup
plus
,
minus
,
_
,
divide
,
calc
=
setup
# À toi de tester la fonction parse de Calculator.
assert
repr
([
1
,
plus
,
2
])
==
'
(1 + 2)
'
assert
repr
(
calc
.
parse
([
1
,
plus
,
2
]))
==
'
(1 + 2)
'
assert
repr
(
calc
.
parse
([
4
,
minus
,
2
]))
==
'
(4 - 2)
'
# assert repr(calc.parse([4, times, 2])) == '(4 * 2)'
assert
repr
(
calc
.
parse
([
4
,
minus
,
2
,
divide
,
5
,
minus
,
3
]))
==
'
((4 - (2 / 5)) - 3)
'
def
test_evaluation
(
setup
):
"""
Test the evaluation.
"""
plus
,
minus
,
times
,
divide
,
calc
=
setup
_
,
_
,
_
,
_
,
calc
=
setup
# À toi de tester la fonction __call__ de Calculator.
assert
calc
(
"
1 + 2
"
)
==
3
assert
calc
(
"
1 - 3
"
)
==
-
2
assert
calc
(
"
2 / 1
"
)
==
2
assert
calc
(
"
5 * 2
"
)
==
10
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