|
| 1 | +/* |
| 2 | + * Copyright 2019-2023 JetBrains s.r.o. and contributors. |
| 3 | + * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. |
| 4 | + */ |
| 5 | + |
| 6 | +package kotlinx.datetime.test.format |
| 7 | + |
| 8 | +import kotlinx.datetime.* |
| 9 | +import kotlinx.datetime.format.* |
| 10 | +import kotlin.test.* |
| 11 | + |
| 12 | +class ValueBagFormatTest { |
| 13 | + @Test |
| 14 | + fun testRfc1123() { |
| 15 | + val bags = buildMap<ValueBag, Pair<String, Set<String>>> { |
| 16 | + put(valueBag(LocalDate(2008, 6, 3), LocalTime(11, 5, 30), UtcOffset.ZERO), ("Tue, 3 Jun 2008 11:05:30 GMT" to setOf())) |
| 17 | + put(valueBag(LocalDate(2008, 6, 30), LocalTime(11, 5, 30), UtcOffset.ZERO), ("Mon, 30 Jun 2008 11:05:30 GMT" to setOf())) |
| 18 | + put(valueBag(LocalDate(2008, 6, 3), LocalTime(11, 5, 30), UtcOffset(hours = 2)), ("Tue, 3 Jun 2008 11:05:30 +0200" to setOf())) |
| 19 | + put(valueBag(LocalDate(2008, 6, 30), LocalTime(11, 5, 30), UtcOffset(hours = -3)), ("Mon, 30 Jun 2008 11:05:30 -0300" to setOf())) |
| 20 | + } |
| 21 | + test(ValueBagFormat.RFC_1123, bags) |
| 22 | + } |
| 23 | + |
| 24 | + private fun valueBag( |
| 25 | + date: LocalDate? = null, |
| 26 | + time: LocalTime? = null, |
| 27 | + offset: UtcOffset? = null, |
| 28 | + zone: TimeZone? = null |
| 29 | + ) = ValueBag().apply { |
| 30 | + date?.let { populateFrom(it) } |
| 31 | + time?.let { populateFrom(it) } |
| 32 | + offset?.let { populateFrom(it) } |
| 33 | + timeZoneId = zone?.id |
| 34 | + } |
| 35 | + |
| 36 | + private fun assertValueBagsEqual(a: ValueBag, b: ValueBag, message: String? = null) { |
| 37 | + assertEquals(a.toLocaldate(), b.toLocaldate(), message) |
| 38 | + assertEquals(a.toLocalTime(), b.toLocalTime(), message) |
| 39 | + assertEquals(a.toUtcOffset(), b.toUtcOffset(), message) |
| 40 | + assertEquals(a.timeZoneId, b.timeZoneId, message) |
| 41 | + } |
| 42 | + |
| 43 | + private fun test(format: ValueBagFormat, strings: Map<ValueBag, Pair<String, Set<String>>>) { |
| 44 | + for ((value, stringsForValue) in strings) { |
| 45 | + val (canonicalString, otherStrings) = stringsForValue |
| 46 | + assertEquals(canonicalString, format.format(value), "formatting $value with $format") |
| 47 | + assertValueBagsEqual(value, format.parse(canonicalString), "parsing '$canonicalString' with $format") |
| 48 | + for (otherString in otherStrings) { |
| 49 | + assertValueBagsEqual(value, format.parse(otherString), "parsing '$otherString' with $format") |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments