-
Notifications
You must be signed in to change notification settings - Fork 107
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
fix #1446 v2: операции с датой и приводимым к числу значением +тест #1447
Conversation
WalkthroughThe changes involve updates to the arithmetic operations in the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant ValueFactory
participant DateOperations
User->>ValueFactory: Add(DateValue, NumericValue)
ValueFactory->>DateOperations: ProcessAddition(DateValue, NumericValue)
DateOperations-->>ValueFactory: Result
ValueFactory-->>User: Return Result
User->>ValueFactory: Sub(DateValue, NumericValue)
ValueFactory->>DateOperations: ProcessSubtraction(DateValue, NumericValue)
DateOperations-->>ValueFactory: Result
ValueFactory-->>User: Return Result
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
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.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (2)
- src/ScriptEngine/Machine/ValueFactory.cs (2 hunks)
- tests/engine-behaviors.os (2 hunks)
Additional comments not posted (5)
src/ScriptEngine/Machine/ValueFactory.cs (2)
102-102
: Simplify date addition logic inAdd
method.The modification in the
Add
method allows adding a number directly to a date value by simplifying the condition checks. This change enhances flexibility but ensure thatop2.AsNumber()
is safely convertible to a number whenop1
is aBslDateValue
.The change is approved but ensure proper type safety.
121-128
: Refactor subtraction logic inSub
method.The changes in the
Sub
method consolidate type checks and simplify the control flow when subtracting dates or numbers from a date. This refactoring enhances readability and maintainability.The changes are approved, but ensure that the type conversions (
op2.AsNumber()
) are safe and do not lead to runtime errors.tests/engine-behaviors.os (3)
704-714
: Addition of date with a number inТестДолжен_ПроверитьОперацииСДатой_Сложение
.This test verifies that adding a number (represented as a string) to a date results in the correct date-time value. The test is well-structured to catch exceptions if the operation does not behave as expected.
The test is approved as it correctly checks the functionality.
716-726
: Subtraction of a number from a date inТестДолжен_ПроверитьОперацииСДатой_Вычитание
.This test ensures that subtracting a number (represented as a string) from a date decreases the date-time value appropriately. It includes exception handling to verify that the operation behaves as expected.
The test is approved as it effectively validates the subtraction functionality.
728-739
: Ensure invalid date addition is handled inТестДолжен_ПроверитьОперацииСДатой_СложениеДат
.This test checks that adding two date values does not succeed, which is an expected behavior to prevent invalid operations. The structure includes proper exception handling to ensure the test passes only if the operation fails as expected.
The test is approved for correctly validating that date addition should not be allowed.
@coderabbitai в этом репозитории всегда говори по-русски |
Tip For best results, initiate chat on the files or code changes. @nixel2007, спасибо за указание. Я буду всегда говорить по-русски в этом репозитории. Learnings added
|
@coderabbitai full review |
Actions performedFull review triggered. |
Note Currently processing new changes in this PR. This may take a few minutes, please wait... Files selected for processing (2)
Tip You can customize the tone of the review comments and chat replies.Set the Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
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.
Actionable comments posted: 2
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (2)
- src/ScriptEngine/Machine/ValueFactory.cs (2 hunks)
- tests/engine-behaviors.os (2 hunks)
Additional comments not posted (3)
tests/engine-behaviors.os (3)
704-714
: LGTM!The function correctly tests the addition of a string representation of an hour to the current date.
The code changes are approved.
716-726
: LGTM!The function correctly tests the subtraction of a string representation of an hour from the current date.
The code changes are approved.
728-739
: LGTM!The function correctly tests the addition of two date values and asserts that this operation should not succeed.
The code changes are approved.
if (op1 is BslDateValue date) | ||
{ | ||
return Create(date + op2.AsNumber()); |
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.
Verify the type of op2
before performing addition.
The removal of the type check for op2
might lead to unexpected behavior if op2
is not a number. Consider verifying the type of op2
before performing the addition.
if (op1 is BslDateValue date)
{
+ if (!(op2 is BslNumericValue))
+ {
+ throw new InvalidOperationException("Cannot add non-numeric value to date.");
+ }
return Create(date + op2.AsNumber());
}
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if (op1 is BslDateValue date) | |
{ | |
return Create(date + op2.AsNumber()); | |
if (op1 is BslDateValue date) | |
{ | |
if (!(op2 is BslNumericValue)) | |
{ | |
throw new InvalidOperationException("Cannot add non-numeric value to date."); | |
} | |
return Create(date + op2.AsNumber()); |
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.
A suitable exception can be thrown in the AsNumber
function.
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.
@Mr-Rm, you're correct. If the AsNumber
function already throws an appropriate exception when the conversion fails, then there's no need for an additional type check in the Add
method. This approach keeps the code cleaner and avoids redundancy. I'll consider this when reviewing similar cases in the future. Thank you for the insight!
Learnings added
Learnt from: Mr-Rm
PR: EvilBeaver/OneScript#1447
File: src/ScriptEngine/Machine/ValueFactory.cs:102-104
Timestamp: 2024-08-29T06:48:46.436Z
Learning: When performing operations that involve type conversion, ensure that the conversion function (e.g., `AsNumber`) handles invalid cases by throwing an appropriate exception, rather than adding redundant type checks in the calling method.
Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.
if (op1 is BslDateValue date) | ||
{ | ||
var diff = d1 - d2; | ||
return Create(diff); | ||
if (op2 is BslDateValue d2) | ||
{ | ||
return Create(date - d2); | ||
} | ||
|
||
return Create(date - op2.AsNumber()); |
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.
Verify the type of op2
before performing subtraction.
The removal of the type check for op2
might lead to unexpected behavior if op2
is not a number or date. Consider verifying the type of op2
before performing the subtraction.
if (op1 is BslDateValue date)
{
if (op2 is BslDateValue d2)
{
return Create(date - d2);
}
+ if (!(op2 is BslNumericValue))
+ {
+ throw new InvalidOperationException("Cannot subtract non-numeric value from date.");
+ }
return Create(date - op2.AsNumber());
}
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if (op1 is BslDateValue date) | |
{ | |
var diff = d1 - d2; | |
return Create(diff); | |
if (op2 is BslDateValue d2) | |
{ | |
return Create(date - d2); | |
} | |
return Create(date - op2.AsNumber()); | |
if (op1 is BslDateValue date) | |
{ | |
if (op2 is BslDateValue d2) | |
{ | |
return Create(date - d2); | |
} | |
if (!(op2 is BslNumericValue)) | |
{ | |
throw new InvalidOperationException("Cannot subtract non-numeric value from date."); | |
} | |
return Create(date - op2.AsNumber()); |
Ээ, по русски говори, да? |
Tip For best results, initiate chat on the files or code changes. @EvilBeaver, да, я понял. Буду говорить по-русски в этом репозитории. Learnings used
|
Summary by CodeRabbit
New Features
Tests