Skip to content

Commit

Permalink
Add sample FormattedResourcesTest using dateTimeConverter substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
drewhamilton committed Nov 3, 2023
1 parent da33b5d commit 83c9523
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sample/library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ androidComponents {

dependencies {
testImplementation(libs.icu4j)
testImplementation(libs.junit)
testImplementation(libs.truth)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2023 Cash App
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.paraphrase.sample.library

import androidx.annotation.StringRes
import com.google.common.truth.Truth.assertThat
import com.ibm.icu.text.MessageFormat
import java.time.LocalDate
import java.time.LocalTime
import java.time.Month
import java.util.Locale
import org.junit.Before
import org.junit.Test

class FormattedResourcesTest {

private val stringResolver = FakeStringResolver(
R.string.library_date_argument to "{release_date, date, short}",
R.string.library_time_argument to "{showtime, time, short}",
)

@Before fun substituteDateTimeConverter() {
FormattedResources.dateTimeConverter = JvmDateTimeConverter
}

@Test fun date() {
val formattedResource =
FormattedResources.library_date_argument(LocalDate.of(2023, Month.NOVEMBER, 3))
val result = MessageFormat(stringResolver.getString(formattedResource.id), Locale.US)
.format(formattedResource.arguments)
assertThat(result).isEqualTo("11/3/23")
}

@Test fun time() {
val formattedResource =
FormattedResources.library_time_argument(LocalTime.of(14, 37, 21))
val result = MessageFormat(stringResolver.getString(formattedResource.id), Locale.US)
.format(formattedResource.arguments)
assertThat(result).isEqualTo("2:37 PM")
}

private class FakeStringResolver(
private val strings: Map<Int, String>,
) {

constructor(vararg strings: Pair<Int, String>) : this(mapOf(*strings))

fun getString(@StringRes id: Int): String = strings.getValue(id)
}
}

0 comments on commit 83c9523

Please sign in to comment.