import org.junit.Test
import java.time.LocalDateTime
import kotlin.test.assertEquals
fun User.toUserJson(): UserJson = UserJson(
username = username,
email = email.value,
registrationDate = registrationDate.toString(),
heightCm = height.value,
)
fun UserJson.toUser(): User = User(
username = username,
email = Email(email),
registrationDate = LocalDateTime.parse(registrationDate),
height = heightCm.cm,
)
val Int.cm: Centimeters get() = Centimeters(this)
data class User(
val username: String,
val email: Email,
val registrationDate: LocalDateTime,
val height: Centimeters,
)
data class Email(val value: String)
data class Centimeters(val value: Int)
data class UserJson(
val username: String,
val email: String,
val registrationDate: String,
val heightCm: Int,
)
// TODO: Implement extensions to convert User to UserJson and vice versa,
// and to create Centimeters from Int
fun main() {
val user = User(
username = "alex",
email = Email("alex@example.com"),
registrationDate = LocalDateTime
.of(1410, 7, 15, 10, 13),
height = 170.cm,
)
val userJson = user.toUserJson()
println(userJson)
// UserJson(username=alex, email=alex@example.com,
// registrationDate=1410-07-15T10:13, heightCm=170)
val user2 = userJson.toUser()
println(user2) // User(username=alex,
// email=Email(value=alex@example.com),
// registrationDate=1410-07-15T10:13,
// height=Centimeters(value=170))
}
class DataConversionTest {
@Test
fun `test User to UserJson conversion`() {
val user = User(
username = "alex",
email = Email("alex@example.com"),
registrationDate = LocalDateTime.now(),
height = 170.cm
)
val userJson = user.toUserJson()
assertEquals("alex", userJson.username)
assertEquals("alex@example.com", userJson.email)
assertEquals(user.registrationDate.toString(), userJson.registrationDate)
assertEquals(user.height.value, userJson.heightCm)
}
@Test
fun `test UserJson to User conversion`() {
val userJson = UserJson(
username = "alex",
email = "alex@example.com",
registrationDate = LocalDateTime.now().toString(),
heightCm = 170
)
val user = userJson.toUser()
assertEquals("alex", user.username)
assertEquals("alex@example.com", user.email.value)
assertEquals(userJson.registrationDate, user.registrationDate.toString())
assertEquals(userJson.heightCm.cm, user.height)
}
@Test
fun `test cm extension property`() {
val value = 150
val cm = value.cm
assertEquals(value, cm.value)
}
}
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.