How to use haveSameYear method of io.kotest.matchers.date.matchers class

Best Kotest code snippet using io.kotest.matchers.date.matchers.haveSameYear

matchers.kt

Source:matchers.kt Github

copy

Full Screen

...33 *34 * firstDate shouldHaveSameYearAs secondDate // Assertion fails, 2018 != 199835 ```36 */37infix fun LocalDate.shouldHaveSameYearAs(date: LocalDate) = this should haveSameYear(date)38/**39 * Asserts that this year is NOT the same as [date]'s year40 *41 * Verifies that this year isn't the same as [date]'s year, ignoring any other fields.42 * For example, 09/02/1998 doesn't have the same year as 09/02/2018, and this assertion should pass for this comparison43 *44 * Opposite of [LocalDate.shouldHaveSameYearAs]45 *46 * ```47 * val firstDate = LocalDate.of(2018, 2, 9)48 * val secondDate = LocalDate.of(1998, 2, 9)49 *50 * firstDate shouldNotHaveSameYearAs secondDate // Assertion passes51 *52 *53 * val firstDate = LocalDate.of(1998, 2, 9)54 * val secondDate = LocalDate.of(1998, 3, 10)55 *56 * firstDate shouldNotHaveSameYearAs secondDate // Assertion fails, 1998 == 1998, and we expected a difference57 * ```58 */59infix fun LocalDate.shouldNotHaveSameYearAs(date: LocalDate) = this shouldNot haveSameYear(date)60/**61 * Matcher that compares years of LocalDates62 *63 * Verifies that two dates have exactly the same year, ignoring any other fields.64 * For example, 09/02/1998 has the same year as 10/03/1998, and the matcher will have a positive result for this comparison65 *66 * ```67 * val firstDate = LocalDate.of(1998, 2, 9)68 * val secondDate = LocalDate.of(1998, 3, 10)69 *70 * firstDate should haveSameYear(secondDate) // Assertion passes71 *72 *73 * val firstDate = LocalDate.of(1998, 2, 9)74 * val secondDate = LocalDate.of(2018, 2, 9)75 *76 * firstDate shouldNot haveSameYear(secondDate) // Assertion passes77 * ```78 *79 * @see [LocalDate.shouldHaveSameYearAs]80 * @see [LocalDate.shouldNotHaveSameYearAs]81 */82fun haveSameYear(date: LocalDate): Matcher<LocalDate> = object : Matcher<LocalDate> {83 override fun test(value: LocalDate): MatcherResult =84 MatcherResult(85 value.year == date.year,86 { "$value should have year ${date.year}" },87 {88 "$value should not have year ${date.year}"89 })90}91/**92 * Asserts that this year is the same as [date]'s year93 *94 * Verifies that this year is the same as [date]'s year, ignoring any other fields.95 * For example, 09/02/1998 10:00:00 has the same year as 10/03/1998 11:30:30, and this assertion should pass for this comparison96 *97 * Opposite of [LocalDateTime.shouldNotHaveSameYearAs]98 *99 * ```100 * val firstDate = LocalDateTime.of(1998, 2, 9, 10, 0, 0)101 * val secondDate = LocalDateTime.of(1998, 3, 10, 11, 30, 30)102 *103 * firstDate shouldHaveSameYearAs secondDate // Assertion passes104 *105 *106 * val firstDate = LocalDateTime.of(2018, 2, 9, 10, 0, 0)107 * val secondDate = LocalDateTime.of(1998, 2, 9, 10, 0, 0)108 *109 * firstDate shouldHaveSameYearAs secondDate // Assertion fails, 2018 != 1998110 * ```111 */112infix fun LocalDateTime.shouldHaveSameYearAs(date: LocalDateTime) = this should haveSameYear(date)113/**114 * Asserts that this year is NOT the same as [date]'s year115 *116 * Verifies that this year isn't the same as [date]'s year, ignoring any other fields.117 * For example, 09/02/1998 10:00:00 doesn't have the same year as 09/02/2018 10:00:00, and this assertion should pass for this comparison118 *119 * Opposite of [LocalDateTime.shouldHaveSameYearAs]120 *121 * ```122 * val firstDate = LocalDateTime.of(2018, 2, 9, 10, 0, 0)123 * val secondDate = LocalDateTime.of(1998, 3, 10, 11, 30, 30)124 *125 * firstDate shouldNotHaveSameYearAs secondDate // Assertion passes126 *127 *128 * val firstDate = LocalDateTime.of(1998, 2, 9, 10, 0, 0)129 * val secondDate = LocalDateTime.of(1998, 3, 10, 1, 30, 30)130 *131 * firstDate shouldNotHaveSameYearAs secondDate // Assertion fails, 1998 == 1998, and we expected a difference132 * ```133 */134infix fun LocalDateTime.shouldNotHaveSameYearAs(date: LocalDateTime) = this shouldNot haveSameYear(date)135/**136 * Matcher that compares years of LocalDateTimes137 *138 * Verifies that two DateTimes have exactly the same year, ignoring any other fields.139 * For example, 09/02/1998 10:00:00 has the same year as 10/03/1998 11:30:30, and the matcher will have a positive result for this comparison140 *141 * ```142 * val firstDate = LocalDateTime.of(1998, 2, 9, 10, 0, 0)143 * val secondDate = LocalDateTime.of(1998, 3, 10, 11, 30, 30)144 *145 * firstDate should haveSameYear(secondDate) // Assertion passes146 *147 *148 * val firstDate = LocalDateTime.of(1998, 2, 9, 10, 0, 0)149 * val secondDate = LocalDateTime.of(2018, 2, 9, 10, 0, 0)150 *151 * firstDate shouldNot haveSameYear(secondDate) // Assertion passes152 * ```153 *154 * @see [LocalDateTime.shouldHaveSameYearAs]155 * @see [LocalDateTime.shouldNotHaveSameYearAs]156 */157fun haveSameYear(date: LocalDateTime): Matcher<LocalDateTime> = object : Matcher<LocalDateTime> {158 override fun test(value: LocalDateTime): MatcherResult =159 MatcherResult(160 value.year == date.year,161 { "$value should have year ${date.year}" },162 {163 "$value should not have year ${date.year}"164 })165}166/**167 * Asserts that this year is the same as [date]'s year168 *169 * Verifies that this year is the same as [date]'s year, ignoring any other fields.170 * For example, 09/02/1998 10:00:00 -03:00 America/Sao_Paulo has the same year as 10/03/1998 11:30:30 -05:00 America/Chicago,171 * and this assertion should pass for this comparison172 *173 * Opposite of [ZonedDateTime.shouldNotHaveSameYearAs]174 *175 * ```176 * val firstDate = ZonedDateTime.of(1998, 2, 9, 10, 0, 0, 0, ZoneId.of("America/Sao_Paulo"))177 * val secondDate = ZonedDateTime.of(1998, 3, 10, 11, 30, 30, 30, ZoneId.of("America/Chicago"))178 *179 * firstDate shouldHaveSameYearAs secondDate // Assertion passes180 *181 *182 * val firstDate = ZonedDateTime.of(2018, 2, 9, 10, 0, 0, 0, ZoneId.of("America/Sao_Paulo"))183 * val secondDate = ZonedDateTime.of(1998, 2, 9, 10, 0, 0, 0, ZoneId.of("America/Sao_Paulo"))184 *185 * firstDate shouldHaveSameYearAs secondDate // Assertion fails, 2018 != 1998186 * ```187 */188infix fun ZonedDateTime.shouldHaveSameYearAs(date: ZonedDateTime) = this should haveSameYear(date)189/**190 * Asserts that this year is NOT the same as [date]'s year191 *192 * Verifies that this year isn't the same as [date]'s year, ignoring any other fields.193 * For example, 09/02/1998 10:00:00 -03:00 America/Sao_Paulo doesn't have the same year as 09/02/2018 10:00:00 -03:00 America/Sao_Paulo,194 * and this assertion should pass for this comparison195 *196 * Opposite of [ZonedDateTime.shouldHaveSameYearAs]197 *198 * ```199 * val firstDate = ZonedDateTime.of(2018, 2, 9, 10, 0, 0, 0, ZoneId.of("America/Sao_Paulo"))200 * val secondDate = ZonedDateTime.of(1998, 2, 9, 19, 0, 0, 0, ZoneId.of("America/Sao_Paulo"))201 *202 * firstDate shouldNotHaveSameYearAs secondDate // Assertion passes203 *204 *205 * val firstDate = ZonedDateTime.of(1998, 2, 9, 10, 0, 0, 0, ZoneId.of("America/Sao_Paulo"))206 * val secondDate = ZonedDateTime.of(1998, 3, 10, 1, 30, 30, 30, ZoneId.of("America/Chicago"))207 *208 * firstDate shouldNotHaveSameYearAs secondDate // Assertion fails, 1998 == 1998, and we expected a difference209 * ```210 */211infix fun ZonedDateTime.shouldNotHaveSameYearAs(date: ZonedDateTime) = this shouldNot haveSameYear(date)212/**213 * Matcher that compares years of ZonedDateTimes214 *215 * Verifies that two ZonedDateTimes have exactly the same year, ignoring any other fields.216 * For example, 09/02/1998 10:00:00 -03:00 America/Sao_Paulo has the same year as 10/03/1998 11:30:30 -05:00 America/Chicago,217 * and the matcher will have a positive result for this comparison218 *219 * ```220 * val firstDate = ZonedDateTime.of(1998, 2, 9, 10, 0, 0, 0, ZoneId.of("America/Sao_Paulo"))221 * val secondDate = ZonedDateTime.of(1998, 3, 10, 11, 30, 30, 30, ZoneId.of("America/Chicago"))222 *223 * firstDate should haveSameYear(secondDate) // Assertion passes224 *225 *226 * val firstDate = ZonedDateTime.of(1998, 2, 9, 10, 0, 0, 0, ZoneId.of("America/Sao_Paulo"))227 * val secondDate = ZonedDateTime.of(2018, 2, 9, 10, 0, 0, 0, ZoneId.of("America/Sao_Paulo"))228 *229 * firstDate shouldNot haveSameYear(secondDate) // Assertion passes230 * ```231 *232 * @see [ZonedDateTime.shouldHaveSameYearAs]233 * @see [ZonedDateTime.shouldNotHaveSameYearAs]234 */235fun haveSameYear(date: ZonedDateTime): Matcher<ZonedDateTime> = object : Matcher<ZonedDateTime> {236 override fun test(value: ZonedDateTime): MatcherResult =237 MatcherResult(238 value.year == date.year,239 { "$value should have year ${date.year}" },240 {241 "$value should not have year ${date.year}"242 })243}244/**245 * Asserts that this year is the same as [date]'s year246 *247 * Verifies that this year is the same as [date]'s year, ignoring any other fields.248 * For example, 09/02/1998 10:00:00 -03:00 has the same year as 10/03/1998 11:30:30 -05:00,249 * and this assertion should pass for this comparison250 *251 * Opposite of [OffsetDateTime.shouldNotHaveSameYearAs]252 *253 * ```254 * val firstDate = OffsetDateTime.of(1998, 2, 9, 10, 0, 0, 0, ZoneOffset.ofHours(-3))255 * val secondDate = OffsetDateTime.of(1998, 3, 10, 11, 30, 30, 30, ZoneOffset.ofHours(-5))256 *257 * firstDate shouldHaveSameYearAs secondDate // Assertion passes258 *259 *260 * val firstDate = OffsetDateTime.of(2018, 2, 9, 10, 0, 0, 0, ZoneOffset.ofHours(-3))261 * val secondDate = OffsetDateTime.of(1998, 2, 9, 10, 0, 0, 0, ZoneOffset.ofHours(-3))262 *263 * firstDate shouldHaveSameYearAs secondDate // Assertion fails, 2018 != 1998264 * ```265 */266infix fun OffsetDateTime.shouldHaveSameYearAs(date: OffsetDateTime) = this should haveSameYear(date)267/**268 * Asserts that this year is NOT the same as [date]'s year269 *270 * Verifies that this year isn't the same as [date]'s year, ignoring any other fields.271 * For example, 09/02/1998 10:00:00 -03:00 doesn't have the same year as 09/02/2018 10:00:00 -03:00,272 * and this assertion should pass for this comparison273 *274 * Opposite of [OffsetDateTime.shouldHaveSameYearAs]275 *276 * ```277 * val firstDate = OffsetDateTime.of(1998, 2, 9, 10, 0, 0, 0, ZoneOffset.ofHours(-3))278 * val secondDate = OffsetDateTime.of(1999, 2, 9, 10, 0, 0, 0, ZoneOffset.ofHours(-3))279 * firstDate shouldNotHaveSameYearAs secondDate // Assertion passes280 *281 *282 * val firstDate = OffsetDateTime.of(1998, 2, 9, 10, 0, 0, 0, ZoneOffset.ofHours(-3))283 * val secondDate = OffsetDateTime.of(1998, 3, 10, 11, 30, 30, 0, ZoneOffset.ofHours(-3))284 *285 * firstDate shouldNotHaveSameYearAs secondDate // Assertion fails, 1998 == 1998 and we expected a difference286 *287 * ```288 */289infix fun OffsetDateTime.shouldNotHaveSameYearAs(date: OffsetDateTime) = this shouldNot haveSameYear(date)290/**291 * Matcher that compares years of OffsetDateTimes292 *293 * Verifies that two ZonedDateTimes have exactly the same year, ignoring any other fields.294 * For example, 09/02/1998 10:00:00 -03:00 has the same year as 10/03/1998 11:30:30 -05:00,295 * and the matcher will have a positive result for this comparison296 *297 * ```298 * val firstDate = OffsetDateTime.of(2018, 2, 9, 10, 0, 0, 0, ZoneOffset.ofHours(-3))299 * val secondDate = OffsetDateTime.of(1998, 2, 9, 19, 0, 0, 0, ZoneOffset.ofHours(-3))300 *301 * firstDate shouldNotHaveSameYearAs secondDate // Assertion passes302 *303 *304 * val firstDate = OffsetDateTime.of(1998, 2, 9, 10, 0, 0, 0, ZoneOffset.ofHours(-3))305 * val secondDate = OffsetDateTime.of(1998, 3, 10, 1, 30, 30, 30, ZoneOffset.ofHours(-5))306 *307 * firstDate shouldNotHaveSameYearAs secondDate // Assertion fails, 1998 == 1998, and we expected a difference308 * ```309 *310 * @see [OffsetDateTime.shouldHaveSameYearAs]311 * @see [OffsetDateTime.shouldNotHaveSameYearAs]312 */313fun haveSameYear(date: OffsetDateTime): Matcher<OffsetDateTime> = object : Matcher<OffsetDateTime> {314 override fun test(value: OffsetDateTime): MatcherResult =315 MatcherResult(316 value.year == date.year,317 { "$value should have year ${date.year}" },318 {319 "$value should not have year ${date.year}"320 })321}322/**323 * Asserts that this month is the same as [date]'s month324 *325 * Verifies that month year is the same as [date]'s month, ignoring any other fields.326 * For example, 09/02/1998 has the same month as 10/02/2018, and this assertion should pass for this comparison327 *328 * Opposite of [LocalDate.shouldNotHaveSameMonthAs]329 *330 * ```331 * val firstDate = LocalDate.of(1998, 2, 9)332 * val secondDate = LocalDate.of(1998, 3, 10)333 *334 * firstDate should haveSameYear(secondDate) // Assertion passes335 *336 *337 * val firstDate = LocalDate.of(1998, 2, 9)338 * val secondDate = LocalDate.of(2018, 2, 9)339 *340 * firstDate shouldNot haveSameYear(secondDate) // Assertion passes341 * ```342 */343infix fun LocalDate.shouldHaveSameMonthAs(date: LocalDate) = this should haveSameMonth(date)344/**345 * Asserts that this month is NOT the same as [date]'s month346 *347 * Verifies that this month isn't the same as [date]'s month, ignoring any other fields.348 * For example, 09/02/1998 doesn't have the same month as 09/03/1998, and this assertion should pass for this comparison349 *350 * Opposite of [LocalDate.shouldHaveSameMonthAs]351 *352 * ```353 * val firstDate = LocalDate.of(1998, 2, 9)354 * val secondDate = LocalDate.of(2018, 2, 10)...

Full Screen

Full Screen

datetime.kt

Source:datetime.kt Github

copy

Full Screen

...31 *32 * firstDate shouldHaveSameYearAs secondDate // Assertion fails, 2018 != 199833 ```34 */35infix fun LocalDate.shouldHaveSameYearAs(date: LocalDate) = this should haveSameYear(date)36/**37 * Asserts that this year is NOT the same as [date]'s year38 *39 * Verifies that this year isn't the same as [date]'s year, ignoring any other fields.40 * For example, 09/02/1998 doesn't have the same year as 09/02/2018, and this assertion should pass for this comparison41 *42 * Opposite of [LocalDate.shouldHaveSameYearAs]43 *44 * ```45 * val firstDate = LocalDate(2018, 2, 9)46 * val secondDate = LocalDate(1998, 2, 9)47 *48 * firstDate shouldNotHaveSameYearAs secondDate // Assertion passes49 *50 *51 * val firstDate = LocalDate(1998, 2, 9)52 * val secondDate = LocalDate(1998, 3, 10)53 *54 * firstDate shouldNotHaveSameYearAs secondDate // Assertion fails, 1998 == 1998, and we expected a difference55 * ```56 */57infix fun LocalDate.shouldNotHaveSameYearAs(date: LocalDate) = this shouldNot haveSameYear(date)58/**59 * Matcher that compares years of LocalDates60 *61 * Verifies that two dates have exactly the same year, ignoring any other fields.62 * For example, 09/02/1998 has the same year as 10/03/1998, and the matcher will have a positive result for this comparison63 *64 * ```65 * val firstDate = LocalDate(1998, 2, 9)66 * val secondDate = LocalDate(1998, 3, 10)67 *68 * firstDate should haveSameYear(secondDate) // Assertion passes69 *70 *71 * val firstDate = LocalDate(1998, 2, 9)72 * val secondDate = LocalDate(2018, 2, 9)73 *74 * firstDate shouldNot haveSameYear(secondDate) // Assertion passes75 * ```76 *77 * @see [LocalDate.shouldHaveSameYearAs]78 * @see [LocalDate.shouldNotHaveSameYearAs]79 */80fun haveSameYear(date: LocalDate): Matcher<LocalDate> = object : Matcher<LocalDate> {81 override fun test(value: LocalDate): MatcherResult =82 MatcherResult(value.year == date.year, "$value should have year ${date.year}", "$value should not have year ${date.year}")83}84/**85 * Asserts that this year is the same as [date]'s year86 *87 * Verifies that this year is the same as [date]'s year, ignoring any other fields.88 * For example, 09/02/1998 10:00:00 has the same year as 10/03/1998 11:30:30, and this assertion should pass for this comparison89 *90 * Opposite of [LocalDateTime.shouldNotHaveSameYearAs]91 *92 * ```93 * val firstDate = LocalDateTime(1998, 2, 9, 10, 0, 0)94 * val secondDate = LocalDateTime(1998, 3, 10, 11, 30, 30)95 *96 * firstDate shouldHaveSameYearAs secondDate // Assertion passes97 *98 *99 * val firstDate = LocalDateTime(2018, 2, 9, 10, 0, 0)100 * val secondDate = LocalDateTime(1998, 2, 9, 10, 0, 0)101 *102 * firstDate shouldHaveSameYearAs secondDate // Assertion fails, 2018 != 1998103 * ```104 */105infix fun LocalDateTime.shouldHaveSameYearAs(date: LocalDateTime) = this should haveSameYear(date)106/**107 * Asserts that this year is NOT the same as [date]'s year108 *109 * Verifies that this year isn't the same as [date]'s year, ignoring any other fields.110 * For example, 09/02/1998 10:00:00 doesn't have the same year as 09/02/2018 10:00:00, and this assertion should pass for this comparison111 *112 * Opposite of [LocalDateTime.shouldHaveSameYearAs]113 *114 * ```115 * val firstDate = LocalDateTime(2018, 2, 9, 10, 0, 0)116 * val secondDate = LocalDateTime(1998, 3, 10, 11, 30, 30)117 *118 * firstDate shouldNotHaveSameYearAs secondDate // Assertion passes119 *120 *121 * val firstDate = LocalDateTime(1998, 2, 9, 10, 0, 0)122 * val secondDate = LocalDateTime(1998, 3, 10, 1, 30, 30)123 *124 * firstDate shouldNotHaveSameYearAs secondDate // Assertion fails, 1998 == 1998, and we expected a difference125 * ```126 */127infix fun LocalDateTime.shouldNotHaveSameYearAs(date: LocalDateTime) = this shouldNot haveSameYear(date)128/**129 * Matcher that compares years of LocalDateTimes130 *131 * Verifies that two DateTimes have exactly the same year, ignoring any other fields.132 * For example, 09/02/1998 10:00:00 has the same year as 10/03/1998 11:30:30, and the matcher will have a positive result for this comparison133 *134 * ```135 * val firstDate = LocalDateTime(1998, 2, 9, 10, 0, 0)136 * val secondDate = LocalDateTime(1998, 3, 10, 11, 30, 30)137 *138 * firstDate should haveSameYear(secondDate) // Assertion passes139 *140 *141 * val firstDate = LocalDateTime(1998, 2, 9, 10, 0, 0)142 * val secondDate = LocalDateTime(2018, 2, 9, 10, 0, 0)143 *144 * firstDate shouldNot haveSameYear(secondDate) // Assertion passes145 * ```146 *147 * @see [LocalDateTime.shouldHaveSameYearAs]148 * @see [LocalDateTime.shouldNotHaveSameYearAs]149 */150fun haveSameYear(date: LocalDateTime): Matcher<LocalDateTime> = object : Matcher<LocalDateTime> {151 override fun test(value: LocalDateTime): MatcherResult =152 MatcherResult(value.year == date.year, "$value should have year ${date.year}", "$value should not have year ${date.year}")153}154/**155 * Asserts that this month is the same as [date]'s month156 *157 * Verifies that month year is the same as [date]'s month, ignoring any other fields.158 * For example, 09/02/1998 has the same month as 10/02/2018, and this assertion should pass for this comparison159 *160 * Opposite of [LocalDate.shouldNotHaveSameMonthAs]161 *162 * ```163 * val firstDate = LocalDate(1998, 2, 9)164 * val secondDate = LocalDate(1998, 3, 10)165 *166 * firstDate should haveSameYear(secondDate) // Assertion passes167 *168 *169 * val firstDate = LocalDate(1998, 2, 9)170 * val secondDate = LocalDate(2018, 2, 9)171 *172 * firstDate shouldNot haveSameYear(secondDate) // Assertion passes173 * ```174 */175infix fun LocalDate.shouldHaveSameMonthAs(date: LocalDate) = this should haveSameMonth(date)176/**177 * Asserts that this month is NOT the same as [date]'s month178 *179 * Verifies that this month isn't the same as [date]'s month, ignoring any other fields.180 * For example, 09/02/1998 doesn't have the same month as 09/03/1998, and this assertion should pass for this comparison181 *182 * Opposite of [LocalDate.shouldHaveSameMonthAs]183 *184 * ```185 * val firstDate = LocalDate(1998, 2, 9)186 * val secondDate = LocalDate(2018, 2, 10)...

Full Screen

Full Screen

DateMatchersTest.kt

Source:DateMatchersTest.kt Github

copy

Full Screen

...11import io.kotest.matchers.date.haveSameMinutes12import io.kotest.matchers.date.haveSameMonth13import io.kotest.matchers.date.haveSameNanos14import io.kotest.matchers.date.haveSameSeconds15import io.kotest.matchers.date.haveSameYear16import io.kotest.matchers.date.shouldBeAfter17import io.kotest.matchers.date.shouldBeBefore18import io.kotest.matchers.date.shouldBeBetween19import io.kotest.matchers.date.shouldBeToday20import io.kotest.matchers.date.shouldBeWithin21import io.kotest.matchers.date.shouldHaveDayOfMonth22import io.kotest.matchers.date.shouldHaveDayOfWeek23import io.kotest.matchers.date.shouldHaveDayOfYear24import io.kotest.matchers.date.shouldHaveHour25import io.kotest.matchers.date.shouldHaveMinute26import io.kotest.matchers.date.shouldHaveMonth27import io.kotest.matchers.date.shouldHaveNano28import io.kotest.matchers.date.shouldHaveSameDayAs29import io.kotest.matchers.date.shouldHaveSameHoursAs30import io.kotest.matchers.date.shouldHaveSameInstantAs31import io.kotest.matchers.date.shouldHaveSameMinutesAs32import io.kotest.matchers.date.shouldHaveSameMonthAs33import io.kotest.matchers.date.shouldHaveSameNanosAs34import io.kotest.matchers.date.shouldHaveSameSecondsAs35import io.kotest.matchers.date.shouldHaveSameYearAs36import io.kotest.matchers.date.shouldHaveSecond37import io.kotest.matchers.date.shouldNotBeAfter38import io.kotest.matchers.date.shouldNotBeBefore39import io.kotest.matchers.date.shouldNotBeBetween40import io.kotest.matchers.date.shouldNotBeToday41import io.kotest.matchers.date.shouldNotBeWithin42import io.kotest.matchers.date.shouldNotHaveSameDayAs43import io.kotest.matchers.date.shouldNotHaveSameHoursAs44import io.kotest.matchers.date.shouldNotHaveSameInstantAs45import io.kotest.matchers.date.shouldNotHaveSameMinutesAs46import io.kotest.matchers.date.shouldNotHaveSameMonthAs47import io.kotest.matchers.date.shouldNotHaveSameNanosAs48import io.kotest.matchers.date.shouldNotHaveSameSecondsAs49import io.kotest.matchers.date.shouldNotHaveSameYearAs50import io.kotest.matchers.date.within51import io.kotest.matchers.shouldBe52import io.kotest.matchers.should53import io.kotest.matchers.shouldNot54import io.kotest.matchers.shouldNotBe55import java.time.DayOfWeek.SATURDAY56import java.time.Duration57import java.time.LocalDate58import java.time.LocalDateTime59import java.time.LocalTime60import java.time.Month61import java.time.OffsetDateTime62import java.time.Period63import java.time.ZoneId64import java.time.ZoneOffset65import java.time.ZonedDateTime66class DateMatchersTest : StringSpec() {67 init {68 "LocalTime should have same nanos ignoring other fields" {69 LocalTime.of(1, 2, 3, 4) should haveSameNanos(LocalTime.of(5, 6, 7, 4))70 LocalTime.of(1, 2, 3, 4) shouldNot haveSameNanos(LocalTime.of(1, 2, 3, 8))71 LocalTime.of(1, 2, 3, 4).shouldHaveSameNanosAs(LocalTime.of(5, 6, 7, 4))72 LocalTime.of(1, 2, 3, 4).shouldNotHaveSameNanosAs(LocalTime.of(1, 2, 3, 8))73 }74 "LocalTime should have same seconds ignoring other fields" {75 LocalTime.of(1, 2, 3, 4) should haveSameSeconds(LocalTime.of(5, 6, 3, 4))76 LocalTime.of(1, 2, 3, 4) shouldNot haveSameSeconds(LocalTime.of(1, 2, 5, 4))77 LocalTime.of(1, 2, 3, 4).shouldHaveSameSecondsAs(LocalTime.of(5, 6, 3, 4))78 LocalTime.of(1, 2, 3, 4).shouldNotHaveSameSecondsAs(LocalTime.of(1, 2, 5, 4))79 }80 "LocalTime should have same minutes ignoring other fields" {81 LocalTime.of(1, 2, 3, 4) should haveSameMinutes(LocalTime.of(5, 2, 7, 8))82 LocalTime.of(1, 2, 3, 4) shouldNot haveSameMinutes(LocalTime.of(1, 5, 3, 4))83 LocalTime.of(1, 2, 3, 4).shouldHaveSameMinutesAs(LocalTime.of(5, 2, 7, 8))84 LocalTime.of(1, 2, 3, 4).shouldNotHaveSameMinutesAs(LocalTime.of(1, 5, 3, 4))85 }86 "LocalTime should have same hours ignoring other fields" {87 LocalTime.of(12, 1, 2, 7777) should haveSameHours(LocalTime.of(12, 59, 58, 9999))88 LocalTime.of(3, 59, 58, 9999) shouldNot haveSameHours(LocalTime.of(12, 59, 58, 9999))89 LocalTime.of(12, 1, 2, 7777).shouldHaveSameHoursAs(LocalTime.of(12, 59, 58, 9999))90 LocalTime.of(3, 59, 58, 9999).shouldNotHaveSameHoursAs(LocalTime.of(12, 59, 58, 9999))91 }92 "LocalDate should have same year ignoring other fields" {93 LocalDate.of(2014, 1, 2) should haveSameYear(LocalDate.of(2014, 5, 6))94 LocalDate.of(2014, 1, 2) shouldNot haveSameYear(LocalDate.of(2018, 5, 6))95 LocalDate.of(2014, 1, 2).shouldHaveSameYearAs(LocalDate.of(2014, 5, 6))96 LocalDate.of(2014, 1, 2).shouldNotHaveSameYearAs(LocalDate.of(2018, 5, 6))97 }98 "LocalDateTime should have same year ignoring other fields" {99 LocalDateTime.of(2014, 1, 2, 4, 3, 2) should haveSameYear(LocalDateTime.of(2014, 5, 6, 3, 2, 1))100 LocalDateTime.of(2014, 1, 2, 3, 2, 1) shouldNot haveSameYear(LocalDateTime.of(2018, 5, 6, 3, 2, 1))101 LocalDateTime.of(2014, 1, 2, 4, 3, 2).shouldHaveSameYearAs(LocalDateTime.of(2014, 5, 6, 3, 2, 1))102 LocalDateTime.of(2014, 1, 2, 3, 2, 1).shouldNotHaveSameYearAs(LocalDateTime.of(2018, 5, 6, 3, 2, 1))103 }104 "ZonedDateTime should have same year ignoring other fields" {105 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")) should haveSameYear(LocalDateTime.of(2014, 5, 6, 3, 2, 1).atZone(ZoneId.of("Z")))106 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")) shouldNot haveSameYear(LocalDateTime.of(2018, 5, 6, 3, 2, 1).atZone(ZoneId.of("Z")))107 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atZone(ZoneId.of("Z")).shouldHaveSameYearAs(LocalDateTime.of(2014, 5, 6, 3, 2, 1).atZone(ZoneId.of("Z")))108 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atZone(ZoneId.of("Z")).shouldNotHaveSameYearAs(LocalDateTime.of(2018, 5, 6, 3, 2, 1).atZone(ZoneId.of("Z")))109 }110 "OffsetDateTime should have same year ignoring other fields" {111 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atOffset(ZoneOffset.UTC) should haveSameYear(LocalDateTime.of(2014, 5, 6, 3, 2, 1).atOffset(ZoneOffset.UTC))112 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC) shouldNot haveSameYear(LocalDateTime.of(2018, 5, 6, 3, 2, 1).atOffset(ZoneOffset.UTC))113 LocalDateTime.of(2014, 1, 2, 4, 3, 2).atOffset(ZoneOffset.UTC).shouldHaveSameYearAs(LocalDateTime.of(2014, 5, 6, 3, 2, 1).atOffset(ZoneOffset.UTC))114 LocalDateTime.of(2014, 1, 2, 3, 2, 1).atOffset(ZoneOffset.UTC).shouldNotHaveSameYearAs(LocalDateTime.of(2018, 5, 6, 3, 2, 1).atOffset(ZoneOffset.UTC))115 }116 "LocalDate should have same month ignoring other fields" {117 LocalDate.of(2014, 1, 2) should haveSameMonth(LocalDate.of(2016, 1, 6))118 LocalDate.of(2014, 1, 2) shouldNot haveSameMonth(LocalDate.of(2018, 4, 6))119 LocalDate.of(2014, 1, 2).shouldHaveSameMonthAs(LocalDate.of(2016, 1, 6))120 LocalDate.of(2014, 1, 2).shouldNotHaveSameMonthAs(LocalDate.of(2018, 4, 6))121 }122 "LocalDateTime should have same month ignoring other fields" {123 LocalDateTime.of(2014, 1, 2, 4, 3, 2) should haveSameMonth(LocalDateTime.of(2014, 1, 6, 3, 2, 1))124 LocalDateTime.of(2014, 1, 2, 3, 2, 1) shouldNot haveSameMonth(LocalDateTime.of(2018, 2, 6, 3, 2, 1))125 LocalDateTime.of(2014, 1, 2, 4, 3, 2).shouldHaveSameMonthAs(LocalDateTime.of(2014, 1, 6, 3, 2, 1))126 LocalDateTime.of(2014, 1, 2, 3, 2, 1).shouldNotHaveSameMonthAs(LocalDateTime.of(2018, 2, 6, 3, 2, 1))...

Full Screen

Full Screen

LocalDateTimeTests.kt

Source:LocalDateTimeTests.kt Github

copy

Full Screen

...15import kotlinx.datetime.todayAt16import kotlin.time.days17class LocalDateTimeTests : StringSpec({18 "LocalDateTime should have same year ignoring other fields" {19 LocalDateTime(2014, 1, 2, 4, 3, 2) should haveSameYear(LocalDateTime(2014, 5, 6, 3, 2, 1))20 LocalDateTime(2014, 1, 2, 3, 2, 1) shouldNot haveSameYear(LocalDateTime(2018, 5, 6, 3, 2, 1))21 LocalDateTime(2014, 1, 2, 4, 3, 2).shouldHaveSameYearAs(LocalDateTime(2014, 5, 6, 3, 2, 1))22 LocalDateTime(2014, 1, 2, 3, 2, 1).shouldNotHaveSameYearAs(LocalDateTime(2018, 5, 6, 3, 2, 1))23 }24 "LocalDate should have same month ignoring other fields" {25 LocalDate(2014, 1, 2) should haveSameMonth(LocalDate(2016, 1, 6))26 LocalDate(2014, 1, 2) shouldNot haveSameMonth(LocalDate(2018, 4, 6))27 LocalDate(2014, 1, 2).shouldHaveSameMonthAs(LocalDate(2016, 1, 6))28 LocalDate(2014, 1, 2).shouldNotHaveSameMonthAs(LocalDate(2018, 4, 6))29 }30 "LocalDateTime should have same month ignoring other fields" {31 LocalDateTime(2014, 1, 2, 4, 3, 2) should haveSameMonth(LocalDateTime(2014, 1, 6, 3, 2, 1))32 LocalDateTime(2014, 1, 2, 3, 2, 1) shouldNot haveSameMonth(LocalDateTime(2018, 2, 6, 3, 2, 1))33 LocalDateTime(2014, 1, 2, 4, 3, 2).shouldHaveSameMonthAs(LocalDateTime(2014, 1, 6, 3, 2, 1))34 LocalDateTime(2014, 1, 2, 3, 2, 1).shouldNotHaveSameMonthAs(LocalDateTime(2018, 2, 6, 3, 2, 1))...

Full Screen

Full Screen

date.kt

Source:date.kt Github

copy

Full Screen

2import com.soywiz.klock.Date3import com.soywiz.klock.DateTime4import com.soywiz.klock.DayOfWeek5import io.kotest.matchers.*6infix fun Date.shouldHaveSameYearAs(date: Date) = this should haveSameYear(date)7infix fun Date.shouldNotHaveSameYearAs(date: Date) = this shouldNot haveSameYear(date)8fun haveSameYear(date: Date) = object : Matcher<Date> {9 override fun test(value: Date) =10 MatcherResult(11 value.year == date.year,12 { "$value should have year ${date.year}" },13 { "$value should not have year ${date.year}" }14 )15}16infix fun Date.shouldHaveYear(year: Int) = this should haveYear(year)17infix fun Date.shouldNotHaveYear(year: Int) = this shouldNot haveYear(year)18fun haveYear(year: Int) = object : Matcher<Date> {19 override fun test(value: Date) =20 MatcherResult(21 value.year == year,22 { "$value should have year $year" },...

Full Screen

Full Screen

haveSameYear

Using AI Code Generation

copy

Full Screen

1val date = Date()2date.should.haveSameYear(Date())3val date = Date()4date.should.haveSameMonth(Date())5val date = Date()6date.should.haveSameDay(Date())7val date = Date()8date.should.haveSameHour(Date())9val date = Date()10date.should.haveSameMinute(Date())11val date = Date()12date.should.haveSameSecond(Date())13val date = Date()14date.should.haveSameMillisecond(Date())15val date = Date()16date.should.haveSameTime(Date())17val date = Date()18date.should.haveSameYear(Date())19val date = Date()20date.should.haveSameMonth(Date())21val date = Date()22date.should.haveSameDay(Date())23val date = Date()24date.should.haveSameHour(Date())25val date = Date()26date.should.haveSameMinute(Date())27val date = Date()28date.should.haveSameSecond(Date())29val date = Date()30date.should.haveSameMillisecond(Date())31val date = Date()32date.should.haveSameTime(Date())

Full Screen

Full Screen

haveSameYear

Using AI Code Generation

copy

Full Screen

1 haveSameYear(2021) shouldBe true2 haveSameMonth(8) shouldBe true3 haveSameDay(30) shouldBe true4 haveSameHour(2) shouldBe true5 haveSameMinute(30) shouldBe true6 haveSameSecond(30) shouldBe true7 haveSameMillisecond(300) shouldBe true8 haveSameDayOfWeek(Calendar.SATURDAY) shouldBe true9 haveSameDayOfYear(243) shouldBe true10 haveSameWeekOfYear(35) shouldBe true11 haveSameWeekOfMonth(4) shouldBe true12 haveSameQuarterOfYear(3) shouldBe true13 beBefore(Date()) shouldBe true14 beAfter(Date()) shouldBe true15 beBetween(Date(), Date()) shouldBe true16 beInTheFuture() shouldBe true

Full Screen

Full Screen

haveSameYear

Using AI Code Generation

copy

Full Screen

1haveSameYear ( 2020 ) shouldBe true2haveSameYear ( 2021 ) shouldBe false3haveSameYear ( 2021 ) shouldBe true4haveSameYear ( 2020 ) shouldBe false5haveSameYear ( 2021 ) shouldBe false6haveSameYear ( 2020 ) shouldBe false7haveSameYear ( 2020 ) shouldBe true8haveSameYear ( 2021 ) shouldBe false9haveSameYear ( 2020 ) shouldBe true10haveSameYear ( 2021 ) shouldBe false11haveSameYear ( 2021 ) shouldBe true12haveSameYear ( 2020 ) shouldBe false13haveSameYear ( 2020 ) shouldBe true14haveSameYear ( 2021 ) shouldBe false15haveSameYear ( 2020 ) shouldBe true

Full Screen

Full Screen

haveSameYear

Using AI Code Generation

copy

Full Screen

1haveSameYear ( 2018 )2shouldNotHaveSameYear ( 2018 )3haveSameYear ( 2018 )4shouldNotHaveSameYear ( 2018 )5haveSameYear ( 2018 )6shouldNotHaveSameYear ( 2018 )7haveSameYear ( 2018 )8shouldNotHaveSameYear ( 2018 )9haveSameYear ( 2018 )10shouldNotHaveSameYear ( 2018 )11haveSameYear ( 2018 )12shouldNotHaveSameYear ( 2018 )13haveSameYear ( 2018 )14shouldNotHaveSameYear ( 2018 )15haveSameYear ( 2018 )16shouldNotHaveSameYear ( 2018 )17haveSameYear ( 2018 )

Full Screen

Full Screen

haveSameYear

Using AI Code Generation

copy

Full Screen

1haveSameYear ( 2019 ) shouldNotBe true2haveSameYear ( 2019 ) shouldBe false3haveSameYear ( 2019 ) shouldNotBe false4haveSameYear ( 2019 ) shouldBe null5haveSameYear ( 2019 ) shouldNotBe null6haveSameYear ( 2019 ) shouldBe 17haveSameYear ( 2019 ) shouldNotBe 18haveSameYear ( 2019 ) shouldBe "abc"9haveSameYear ( 2019 ) shouldNotBe "abc"10haveSameYear ( 2019 ) shouldBe 'a'11haveSameYear ( 2019 ) shouldNotBe 'a'12haveSameYear ( 2019 ) shouldBe "a"13haveSameYear ( 2019 ) shouldNotBe "a"14haveSameYear ( 2019 ) shouldBe 'a'15haveSameYear ( 2019 ) shouldNotBe 'a'16haveSameYear ( 2019

Full Screen

Full Screen

haveSameYear

Using AI Code Generation

copy

Full Screen

1val date1 = LocalDate.of(2019, 1, 1)2val date2 = LocalDate.of(2019, 2, 1)3date1 should haveSameYear(date2)4val date1 = LocalDate.of(2019, 1, 1)5val date2 = LocalDate.of(2020, 2, 1)6date1 should haveSameYear(date2)7val date1 = LocalDate.of(2019, 1, 1)8val date2 = LocalDate.of(2019, 2, 1)9date1 shouldNot haveSameYear(date2)10val date1 = LocalDate.of(2019, 1, 1)11val date2 = LocalDate.of(2020, 2, 1)12date1 shouldNot haveSameYear(date2)13val date1 = LocalDate.of(2019, 1, 1)14val date2 = LocalDate.of(2020, 2, 1)15date1 should haveSameYear(2020)16val date1 = LocalDate.of(2019, 1, 1)17val date2 = LocalDate.of(2020, 2, 1)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful