Merge pull request #902 from SimonMarquis/resource-type-to-string
Replace `NewsResourceType` enum with simpler String typepull/909/head
commit
6645ec9fce
@ -1,96 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2022 The Android Open Source Project
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
*
|
|
||||||
* https://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 com.google.samples.apps.nowinandroid.core.database.util
|
|
||||||
|
|
||||||
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType
|
|
||||||
import org.junit.Test
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
|
|
||||||
class NewsResourceTypeConverterTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_room_news_resource_type_converter_for_video() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.Video,
|
|
||||||
NewsResourceTypeConverter().stringToNewsResourceType("Video 📺"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_room_news_resource_type_converter_for_article() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.Article,
|
|
||||||
NewsResourceTypeConverter().stringToNewsResourceType("Article 📚"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_room_news_resource_type_converter_for_api_change() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.APIChange,
|
|
||||||
NewsResourceTypeConverter().stringToNewsResourceType("API change"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_room_news_resource_type_converter_for_codelab() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.Codelab,
|
|
||||||
NewsResourceTypeConverter().stringToNewsResourceType("Codelab"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_room_news_resource_type_converter_for_podcast() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.Podcast,
|
|
||||||
NewsResourceTypeConverter().stringToNewsResourceType("Podcast 🎙"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_room_news_resource_type_converter_for_docs() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.Docs,
|
|
||||||
NewsResourceTypeConverter().stringToNewsResourceType("Docs 📑"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_room_news_resource_type_converter_for_event() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.Event,
|
|
||||||
NewsResourceTypeConverter().stringToNewsResourceType("Event 📆"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_room_news_resource_type_converter_for_dac() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.DAC,
|
|
||||||
NewsResourceTypeConverter().stringToNewsResourceType("DAC"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_room_news_resource_type_converter_for_umm() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.Unknown,
|
|
||||||
NewsResourceTypeConverter().stringToNewsResourceType("umm"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,80 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2022 The Android Open Source Project
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
*
|
|
||||||
* https://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 com.google.samples.apps.nowinandroid.core.model.data
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Type for [NewsResource]
|
|
||||||
*/
|
|
||||||
enum class NewsResourceType(
|
|
||||||
val serializedName: String,
|
|
||||||
val displayText: String,
|
|
||||||
// TODO: descriptions should probably be string resources
|
|
||||||
val description: String,
|
|
||||||
) {
|
|
||||||
Video(
|
|
||||||
serializedName = "Video 📺",
|
|
||||||
displayText = "Video 📺",
|
|
||||||
description = "A video published on YouTube",
|
|
||||||
),
|
|
||||||
APIChange(
|
|
||||||
serializedName = "API change",
|
|
||||||
displayText = "API change",
|
|
||||||
description = "An addition, deprecation or change to the Android platform APIs.",
|
|
||||||
),
|
|
||||||
Article(
|
|
||||||
serializedName = "Article 📚",
|
|
||||||
displayText = "Article 📚",
|
|
||||||
description = "An article, typically on Medium or the official Android blog",
|
|
||||||
),
|
|
||||||
Codelab(
|
|
||||||
serializedName = "Codelab",
|
|
||||||
displayText = "Codelab",
|
|
||||||
description = "A new or updated codelab",
|
|
||||||
),
|
|
||||||
Podcast(
|
|
||||||
serializedName = "Podcast 🎙",
|
|
||||||
displayText = "Podcast 🎙",
|
|
||||||
description = "A podcast",
|
|
||||||
),
|
|
||||||
Docs(
|
|
||||||
serializedName = "Docs 📑",
|
|
||||||
displayText = "Docs 📑",
|
|
||||||
description = "A new or updated piece of documentation",
|
|
||||||
),
|
|
||||||
Event(
|
|
||||||
serializedName = "Event 📆",
|
|
||||||
displayText = "Event 📆",
|
|
||||||
description = "Information about a developer event e.g. Android Developer Summit",
|
|
||||||
),
|
|
||||||
DAC(
|
|
||||||
serializedName = "DAC",
|
|
||||||
displayText = "DAC",
|
|
||||||
description = "Android version features - Information about features in an Android",
|
|
||||||
),
|
|
||||||
Unknown(
|
|
||||||
serializedName = "Unknown",
|
|
||||||
displayText = "Unknown",
|
|
||||||
description = "Unknown",
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
fun String?.asNewsResourceType() = when (this) {
|
|
||||||
null -> NewsResourceType.Unknown
|
|
||||||
else -> NewsResourceType.values()
|
|
||||||
.firstOrNull { type -> type.serializedName == this }
|
|
||||||
?: NewsResourceType.Unknown
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2022 The Android Open Source Project
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
*
|
|
||||||
* https://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 com.google.samples.apps.nowinandroid.core.network.model.util
|
|
||||||
|
|
||||||
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType
|
|
||||||
import com.google.samples.apps.nowinandroid.core.model.data.asNewsResourceType
|
|
||||||
import kotlinx.serialization.KSerializer
|
|
||||||
import kotlinx.serialization.descriptors.PrimitiveKind.STRING
|
|
||||||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
|
|
||||||
import kotlinx.serialization.descriptors.SerialDescriptor
|
|
||||||
import kotlinx.serialization.encoding.Decoder
|
|
||||||
import kotlinx.serialization.encoding.Encoder
|
|
||||||
|
|
||||||
object NewsResourceTypeSerializer : KSerializer<NewsResourceType> {
|
|
||||||
override fun deserialize(decoder: Decoder): NewsResourceType =
|
|
||||||
decoder.decodeString().asNewsResourceType()
|
|
||||||
|
|
||||||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(
|
|
||||||
serialName = "type",
|
|
||||||
kind = STRING,
|
|
||||||
)
|
|
||||||
|
|
||||||
override fun serialize(encoder: Encoder, value: NewsResourceType) =
|
|
||||||
encoder.encodeString(value.serializedName)
|
|
||||||
}
|
|
@ -1,106 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2022 The Android Open Source Project
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
*
|
|
||||||
* https://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 com.google.samples.apps.nowinandroid.core.network.model.util
|
|
||||||
|
|
||||||
import com.google.samples.apps.nowinandroid.core.model.data.NewsResourceType
|
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
import org.junit.Test
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
|
|
||||||
class NewsResourceTypeSerializerTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_news_resource_serializer_video() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.Video,
|
|
||||||
Json.decodeFromString(NewsResourceTypeSerializer, """"Video 📺""""),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_news_resource_serializer_article() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.Article,
|
|
||||||
Json.decodeFromString(NewsResourceTypeSerializer, """"Article 📚""""),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_news_resource_serializer_api_change() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.APIChange,
|
|
||||||
Json.decodeFromString(NewsResourceTypeSerializer, """"API change""""),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_news_resource_serializer_codelab() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.Codelab,
|
|
||||||
Json.decodeFromString(NewsResourceTypeSerializer, """"Codelab""""),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_news_resource_serializer_podcast() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.Podcast,
|
|
||||||
Json.decodeFromString(NewsResourceTypeSerializer, """"Podcast 🎙""""),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_news_resource_serializer_docs() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.Docs,
|
|
||||||
Json.decodeFromString(NewsResourceTypeSerializer, """"Docs 📑""""),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_news_resource_serializer_event() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.Event,
|
|
||||||
Json.decodeFromString(NewsResourceTypeSerializer, """"Event 📆""""),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_news_resource_serializer_dac() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.DAC,
|
|
||||||
Json.decodeFromString(NewsResourceTypeSerializer, """"DAC""""),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_news_resource_serializer_unknown() {
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.Unknown,
|
|
||||||
Json.decodeFromString(NewsResourceTypeSerializer, """"umm""""),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_serialize_and_deserialize() {
|
|
||||||
val json = Json.encodeToString(NewsResourceTypeSerializer, NewsResourceType.Video)
|
|
||||||
assertEquals(
|
|
||||||
NewsResourceType.Video,
|
|
||||||
Json.decodeFromString(NewsResourceTypeSerializer, json),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue