Skip to content

Orthogonal DateTimePeriod implementation #80

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

Merged
merged 12 commits into from
Feb 25, 2021

Conversation

dkhalanskyjb
Copy link
Collaborator

No description provided.

@dkhalanskyjb dkhalanskyjb requested a review from ilya-g December 11, 2020 14:05

@Test
fun parseIsoString() {
assertEquals(DateTimePeriod(years = 1), DateTimePeriod.parse("P1Y"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to assert the expected property values rather than equality as a whole in this test. Because otherwise it's unclear what values these properties would get as a result of parsing.

Copy link
Collaborator Author

@dkhalanskyjb dkhalanskyjb Jan 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You raise a good point, though I think that the values of properties and the results of parsing are separate concerns, so I added another test to check the normalization.

assertEquals(DateTimePeriod(nanoseconds = -1L), DateTimePeriod.parse("-PT0.000000001S"))
assertEquals(DateTimePeriod(days = 1, nanoseconds = -1L), DateTimePeriod.parse("P1DT-0.000000001S"))
assertEquals(DateTimePeriod(seconds = -1, nanoseconds = 1L), DateTimePeriod.parse("-PT0.999999999S"))
assertEquals(DateTimePeriod(days = 1, seconds = -1, nanoseconds = 1L), DateTimePeriod.parse("P1DT-0.999999999S"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of negative tests won't hurt. For example that DatePeriod.parse fails on non-zero time components.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some.

@dkhalanskyjb dkhalanskyjb requested a review from ilya-g January 18, 2021 12:49
@@ -12,6 +12,39 @@ import kotlin.time.*

class DateTimePeriodTest {

@Test
fun normalization() {
assertPeriodComponents(DateTimePeriod(years = 1) as DatePeriod, years = 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use this assertion function in tests for parse as well?

Copy link
Collaborator Author

@dkhalanskyjb dkhalanskyjb Jan 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but, in my opinion, it would make those tests less clear. For example,

         assertEquals(DateTimePeriod(months = 14), DateTimePeriod.parse("P14M"))

clearly implies that DateTimePeriod(months = m) == DateTimePeriod.parse("P${m}M"), which is a property that we want. Seeing the result in terms of values of class properties would obscure this. Moreover, the values of class properties are now being tested with normalization().

What benefits do you see in using this function for tests of parsing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By mixing parsing and normalized equality this test makes an impression that the resulting properties of the parsed DateTimePeriod would be the same as those passed to the constructor-like function of expected value. This is misleading.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some unfathomable reason, I didn't see a notification about your response. I am sorry for the delay.

I still disagree and consider these concerns orthogonal: we want parsing to behave the same way construction does, and we want construction to produce specific values of properties. Parsing is not semantically connected to the values of properties, and mixing these rubs me the wrong way. However, I don't consider this issue important enough to die on this hill, so I added a commit to accommodate your preferences, though I would be glad to revert it were you to change your mind about this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a compromise, we could leave the equality assertions in the parsing test, but add assertPeriodComponents for those values that cause normalization to happen. This would be done mostly for explanatory reasons.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added checks for actual values of properties to places where normalization does affect them.

@dkhalanskyjb dkhalanskyjb force-pushed the orthogonal-dateTimePeriod branch from 1b1e33c to 3214ee3 Compare January 19, 2021 08:01
@dkhalanskyjb dkhalanskyjb force-pushed the orthogonal-dateTimePeriod branch from a8d3aac to 5d7fb04 Compare February 17, 2021 07:45
Copy link
Member

@ilya-g ilya-g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refer #81 in (one of) the final commit message.

@dkhalanskyjb dkhalanskyjb merged commit c0748c6 into master Feb 25, 2021
@dkhalanskyjb dkhalanskyjb deleted the orthogonal-dateTimePeriod branch February 25, 2021 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants