|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +@file:OptIn(ExperimentalKotest::class) |
| 18 | + |
| 19 | +package com.google.firebase.dataconnect |
| 20 | + |
| 21 | +import com.google.firebase.dataconnect.testutil.property.arbitrary.threeTenBp |
| 22 | +import com.google.firebase.dataconnect.testutil.shouldContainWithNonAbuttingText |
| 23 | +import com.google.firebase.dataconnect.testutil.toDataConnectLocalDate |
| 24 | +import com.google.firebase.dataconnect.testutil.toJavaTimeLocalDate |
| 25 | +import com.google.firebase.dataconnect.testutil.toKotlinxDatetimeLocalDate |
| 26 | +import io.kotest.assertions.assertSoftly |
| 27 | +import io.kotest.assertions.withClue |
| 28 | +import io.kotest.common.ExperimentalKotest |
| 29 | +import io.kotest.matchers.shouldBe |
| 30 | +import io.kotest.matchers.shouldNotBe |
| 31 | +import io.kotest.matchers.string.shouldEndWith |
| 32 | +import io.kotest.matchers.string.shouldStartWith |
| 33 | +import io.kotest.property.Arb |
| 34 | +import io.kotest.property.PropTestConfig |
| 35 | +import io.kotest.property.arbitrary.int |
| 36 | +import io.kotest.property.arbitrary.of |
| 37 | +import io.kotest.property.assume |
| 38 | +import io.kotest.property.checkAll |
| 39 | +import kotlinx.coroutines.test.runTest |
| 40 | +import kotlinx.datetime.number |
| 41 | +import org.junit.Test |
| 42 | + |
| 43 | +@Suppress("ReplaceCallWithBinaryOperator") |
| 44 | +class LocalDateUnitTest { |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `constructor() should set properties to corresponding arguments`() = runTest { |
| 48 | + checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int()) { year, month, day -> |
| 49 | + val localDate = LocalDate(year = year, month = month, day = day) |
| 50 | + assertSoftly { |
| 51 | + withClue("year") { localDate.year shouldBe year } |
| 52 | + withClue("month") { localDate.month shouldBe month } |
| 53 | + withClue("day") { localDate.day shouldBe day } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun `equals() should return true when invoked with itself`() = runTest { |
| 60 | + checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int()) { year, month, day -> |
| 61 | + val localDate = LocalDate(year = year, month = month, day = day) |
| 62 | + localDate.equals(localDate) shouldBe true |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun `equals() should return true when invoked with a distinct, but equal, instance`() = runTest { |
| 68 | + checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int()) { year, month, day -> |
| 69 | + val localDate1 = LocalDate(year = year, month = month, day = day) |
| 70 | + val localDate2 = LocalDate(year = year, month = month, day = day) |
| 71 | + localDate1.equals(localDate2) shouldBe true |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun `equals() should return false when invoked with null`() = runTest { |
| 77 | + checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int()) { year, month, day -> |
| 78 | + val localDate = LocalDate(year = year, month = month, day = day) |
| 79 | + localDate.equals(null) shouldBe false |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + fun `equals() should return false when invoked with a different type`() = runTest { |
| 85 | + val others = Arb.of("foo", 42, java.time.LocalDate.now()) |
| 86 | + checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int(), others) { year, month, day, other -> |
| 87 | + val localDate = LocalDate(year = year, month = month, day = day) |
| 88 | + localDate.equals(other) shouldBe false |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + fun `equals() should return false when only the year differs`() = runTest { |
| 94 | + checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int(), Arb.int()) { year1, month, day, year2 |
| 95 | + -> |
| 96 | + assume(year1 != year2) |
| 97 | + val localDate1 = LocalDate(year = year1, month = month, day = day) |
| 98 | + val localDate2 = LocalDate(year = year2, month = month, day = day) |
| 99 | + localDate1.equals(localDate2) shouldBe false |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun `equals() should return false when only the month differs`() = runTest { |
| 105 | + checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int(), Arb.int()) { year, month1, day, month2 |
| 106 | + -> |
| 107 | + assume(month1 != month2) |
| 108 | + val localDate1 = LocalDate(year = year, month = month1, day = day) |
| 109 | + val localDate2 = LocalDate(year = year, month = month2, day = day) |
| 110 | + localDate1.equals(localDate2) shouldBe false |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + fun `equals() should return false when only the day differs`() = runTest { |
| 116 | + checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int(), Arb.int()) { year, month, day1, day2 |
| 117 | + -> |
| 118 | + assume(day1 != day2) |
| 119 | + val localDate1 = LocalDate(year = year, month = month, day = day1) |
| 120 | + val localDate2 = LocalDate(year = year, month = month, day = day2) |
| 121 | + localDate1.equals(localDate2) shouldBe false |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + fun `hashCode() should return the same value when invoked repeatedly`() = runTest { |
| 127 | + checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int()) { year, month, day -> |
| 128 | + val localDate = LocalDate(year = year, month = month, day = day) |
| 129 | + val hashCode = localDate.hashCode() |
| 130 | + repeat(5) { withClue("iteration=$it") { localDate.hashCode() shouldBe hashCode } } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + fun `hashCode() should return the same value when invoked on equal, but distinct, objects`() = |
| 136 | + runTest { |
| 137 | + checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int()) { year, month, day -> |
| 138 | + val localDate1 = LocalDate(year = year, month = month, day = day) |
| 139 | + val localDate2 = LocalDate(year = year, month = month, day = day) |
| 140 | + localDate1.hashCode() shouldBe localDate2.hashCode() |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + fun `hashCode() should return different values for different years`() = runTest { |
| 146 | + checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int(), Arb.int()) { year1, month, day, year2 |
| 147 | + -> |
| 148 | + assume(year1.hashCode() != year2.hashCode()) |
| 149 | + val localDate1 = LocalDate(year = year1, month = month, day = day) |
| 150 | + val localDate2 = LocalDate(year = year2, month = month, day = day) |
| 151 | + localDate1.hashCode() shouldNotBe localDate2.hashCode() |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + fun `hashCode() should return different values for different months`() = runTest { |
| 157 | + checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int(), Arb.int()) { year, month1, day, month2 |
| 158 | + -> |
| 159 | + assume(month1.hashCode() != month2.hashCode()) |
| 160 | + val localDate1 = LocalDate(year = year, month = month1, day = day) |
| 161 | + val localDate2 = LocalDate(year = year, month = month2, day = day) |
| 162 | + localDate1.hashCode() shouldNotBe localDate2.hashCode() |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + fun `hashCode() should return different values for different days`() = runTest { |
| 168 | + checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int(), Arb.int()) { year, month, day1, day2 |
| 169 | + -> |
| 170 | + assume(day1.hashCode() != day2.hashCode()) |
| 171 | + val localDate1 = LocalDate(year = year, month = month, day = day1) |
| 172 | + val localDate2 = LocalDate(year = year, month = month, day = day2) |
| 173 | + localDate1.hashCode() shouldNotBe localDate2.hashCode() |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + fun `toString() should return a string conforming to what is expected`() = runTest { |
| 179 | + checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int()) { year, month, day -> |
| 180 | + val localDate = LocalDate(year = year, month = month, day = day) |
| 181 | + val toStringResult = localDate.toString() |
| 182 | + assertSoftly { |
| 183 | + toStringResult shouldStartWith "LocalDate(" |
| 184 | + toStringResult shouldEndWith ")" |
| 185 | + toStringResult shouldContainWithNonAbuttingText "year=$year" |
| 186 | + toStringResult shouldContainWithNonAbuttingText "month=$month" |
| 187 | + toStringResult shouldContainWithNonAbuttingText "day=$day" |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + fun `copy() with no arguments should return an equal, but distinct, instance`() = runTest { |
| 194 | + checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int()) { year, month, day -> |
| 195 | + val localDate1 = LocalDate(year = year, month = month, day = day) |
| 196 | + val localDate2 = localDate1.copy() |
| 197 | + localDate1 shouldBe localDate2 |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + fun `copy() with all arguments should return a new instance with the given arguments`() = |
| 203 | + runTest { |
| 204 | + checkAll(propTestConfig, Arb.int(), Arb.int(), Arb.int(), Arb.int(), Arb.int(), Arb.int()) { |
| 205 | + year1, |
| 206 | + month1, |
| 207 | + day1, |
| 208 | + year2, |
| 209 | + month2, |
| 210 | + day2 -> |
| 211 | + val localDate1 = LocalDate(year = year1, month = month1, day = day1) |
| 212 | + val localDate2 = localDate1.copy(year = year2, month = month2, day = day2) |
| 213 | + localDate2 shouldBe LocalDate(year = year2, month = month2, day = day2) |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + @Test |
| 218 | + fun `toJavaLocalDate() should return an equivalent java time LocalDate object`() = runTest { |
| 219 | + checkAll(propTestConfig, Arb.threeTenBp.localDate()) { testData -> |
| 220 | + val fdcLocalDate: LocalDate = testData.toDataConnectLocalDate() |
| 221 | + val jLocalDate: java.time.LocalDate = fdcLocalDate.toJavaLocalDate() |
| 222 | + assertSoftly { |
| 223 | + withClue("year") { jLocalDate.year shouldBe testData.year } |
| 224 | + withClue("month") { jLocalDate.month.number shouldBe testData.monthValue } |
| 225 | + withClue("dayOfMonth") { jLocalDate.dayOfMonth shouldBe testData.dayOfMonth } |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + fun `toKotlinxLocalDate() should return an equivalent java time LocalDate object`() = runTest { |
| 232 | + checkAll(propTestConfig, Arb.threeTenBp.localDate()) { testData -> |
| 233 | + val fdcLocalDate: LocalDate = testData.toDataConnectLocalDate() |
| 234 | + val kLocalDate: kotlinx.datetime.LocalDate = fdcLocalDate.toKotlinxLocalDate() |
| 235 | + assertSoftly { |
| 236 | + withClue("year") { kLocalDate.year shouldBe testData.year } |
| 237 | + withClue("month") { kLocalDate.month.number shouldBe testData.monthValue } |
| 238 | + withClue("dayOfMonth") { kLocalDate.dayOfMonth shouldBe testData.dayOfMonth } |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + @Test |
| 244 | + fun `toDataConnectLocalDate() on java time LocalDate should return an equivalent LocalDate object`() = |
| 245 | + runTest { |
| 246 | + checkAll(propTestConfig, Arb.threeTenBp.localDate()) { testData -> |
| 247 | + val jLocalDate: java.time.LocalDate = testData.toJavaTimeLocalDate() |
| 248 | + val fdcLocalDate: LocalDate = jLocalDate.toDataConnectLocalDate() |
| 249 | + assertSoftly { |
| 250 | + withClue("year") { fdcLocalDate.year shouldBe testData.year } |
| 251 | + withClue("month") { fdcLocalDate.month shouldBe testData.monthValue } |
| 252 | + withClue("day") { fdcLocalDate.day shouldBe testData.dayOfMonth } |
| 253 | + } |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + @Test |
| 258 | + fun `toDataConnectLocalDate() on kotlinx datetime LocalDate should return an equivalent LocalDate object`() = |
| 259 | + runTest { |
| 260 | + checkAll(propTestConfig, Arb.threeTenBp.localDate()) { testData -> |
| 261 | + val kLocalDate: kotlinx.datetime.LocalDate = testData.toKotlinxDatetimeLocalDate() |
| 262 | + val fdcLocalDate: LocalDate = kLocalDate.toDataConnectLocalDate() |
| 263 | + assertSoftly { |
| 264 | + withClue("year") { fdcLocalDate.year shouldBe testData.year } |
| 265 | + withClue("month") { fdcLocalDate.month shouldBe testData.monthValue } |
| 266 | + withClue("day") { fdcLocalDate.day shouldBe testData.dayOfMonth } |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + private companion object { |
| 272 | + val propTestConfig = PropTestConfig(iterations = 50) |
| 273 | + } |
| 274 | +} |
0 commit comments