-
Notifications
You must be signed in to change notification settings - Fork 18
Fix splitting of negative numbers #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,243 @@ | ||||||
| from unittest import TestCase | ||||||
| from mathparse import mathparse | ||||||
|
|
||||||
|
|
||||||
| class NegativeNumberTestCase(TestCase): | ||||||
| """ | ||||||
| Test cases for basic negative number operations. | ||||||
| """ | ||||||
|
|
||||||
| def test_leading_negative_with_addition(self): | ||||||
| """ | ||||||
| Test: -3 + 3 should equal 0 | ||||||
| """ | ||||||
| result = mathparse.parse('-3 + 3') | ||||||
| self.assertEqual(result, 0) | ||||||
|
|
||||||
| def test_leading_negative_with_subtraction(self): | ||||||
| """ | ||||||
| Test: -3 - 5 should equal -8 | ||||||
| """ | ||||||
| result = mathparse.parse('-3 - 5') | ||||||
| self.assertEqual(result, -8) | ||||||
|
|
||||||
| def test_leading_negative_with_multiplication(self): | ||||||
| """ | ||||||
| Test: -10 * 2 should equal -20 | ||||||
| """ | ||||||
| result = mathparse.parse('-10 * 2') | ||||||
| self.assertEqual(result, -20) | ||||||
|
|
||||||
| def test_negative_in_parentheses(self): | ||||||
| """ | ||||||
| Test: (-3) should equal -3 | ||||||
| """ | ||||||
| result = mathparse.parse('(-3)') | ||||||
| self.assertEqual(result, -3) | ||||||
|
|
||||||
| def test_negative_in_parentheses_with_addition(self): | ||||||
| """ | ||||||
| Test: (-3) + 5 should equal 2 | ||||||
| """ | ||||||
| result = mathparse.parse('(-3) + 5') | ||||||
| self.assertEqual(result, 2) | ||||||
|
|
||||||
| def test_addition_with_negative_in_parentheses(self): | ||||||
| """ | ||||||
| Test: 5 + (-3) should equal 2 | ||||||
| """ | ||||||
| result = mathparse.parse('5 + (-3)') | ||||||
| self.assertEqual(result, 2) | ||||||
|
|
||||||
| def test_two_negatives_in_parentheses(self): | ||||||
| """ | ||||||
| Test: (-3) * (-2) should equal 6 | ||||||
| """ | ||||||
| result = mathparse.parse('(-3) * (-2)') | ||||||
| self.assertEqual(result, 6) | ||||||
|
|
||||||
| def test_multiplication_by_negative(self): | ||||||
| """ | ||||||
| Test: 3 * -2 should equal -6 | ||||||
| """ | ||||||
| result = mathparse.parse('3 * -2') | ||||||
| self.assertEqual(result, -6) | ||||||
|
|
||||||
| def test_subtraction_of_negative(self): | ||||||
| """ | ||||||
| Test: 3 - -2 should equal 5 (subtracting a negative) | ||||||
| """ | ||||||
| result = mathparse.parse('3--2') | ||||||
| self.assertEqual(result, 5) | ||||||
|
|
||||||
| def test_two_separate_negatives_with_addition(self): | ||||||
| """ | ||||||
| Test: -3 + -5 should equal -8 | ||||||
| """ | ||||||
| result = mathparse.parse('-3 + -5') | ||||||
| self.assertEqual(result, -8) | ||||||
|
|
||||||
| def test_complex_expression_with_leading_negative(self): | ||||||
| """ | ||||||
| Test: (-3 + 5) * 2 should equal 4 | ||||||
| """ | ||||||
| result = mathparse.parse('(-3 + 5) * 2') | ||||||
| self.assertEqual(result, 4) | ||||||
|
|
||||||
| def test_negative_after_operator_in_expression(self): | ||||||
| """ | ||||||
| Test: 2 * (-3 + 5) should equal 4 | ||||||
| """ | ||||||
| result = mathparse.parse('2 * (-3 + 5)') | ||||||
| self.assertEqual(result, 4) | ||||||
|
|
||||||
| def test_negative_of_expression(self): | ||||||
| """ | ||||||
| Test: -(3 + 5) should equal -8 | ||||||
| """ | ||||||
| result = mathparse.parse('-(3 + 5)') | ||||||
| self.assertEqual(result, -8) | ||||||
|
|
||||||
| def test_negative_decimal(self): | ||||||
| """ | ||||||
| Test: -3.5 should equal -3.5 (parsed as -3 . 5) | ||||||
| """ | ||||||
| result = mathparse.parse('-3.5') | ||||||
| self.assertEqual(float(result), -3.5) | ||||||
|
|
||||||
| def test_negative_decimal_in_expression(self): | ||||||
| """ | ||||||
| Test: -10.25 + 5 should work correctly | ||||||
| """ | ||||||
| result = mathparse.parse('-10.25 + 5') | ||||||
| # Parses as (-10) . 25 + 5 = -10.25 + 5 = -5.25 | ||||||
|
||||||
| # Parses as (-10) . 25 + 5 = -10.25 + 5 = -5.25 | |
| # Parses as ['-10.25', '+', '5'] = -10.25 + 5 = -5.25 |
Copilot
AI
Oct 11, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] This TODO comment should include a reference to a tracking issue or be more specific about the timeline for this improvement.
| # in the future | |
| # in the future (see issue #123 for tracking: https://github.com/yourorg/yourrepo/issues/123) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment incorrectly describes how -3.5 is parsed. Based on the implementation, -3.5 is tokenized as a single token '-3.5', not as separate tokens '-3', '.', and '5'.