TimeTest.kt (1690B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2020 Taler Systems S.A. 4 * 5 * GNU Taler is free software; you can redistribute it and/or modify it under the 6 * terms of the GNU General Public License as published by the Free Software 7 * Foundation; either version 3, or (at your option) any later version. 8 * 9 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 package net.taler.common 18 19 import kotlinx.serialization.json.Json.Default.decodeFromString 20 import kotlinx.serialization.json.Json.Default.encodeToString 21 import org.junit.Assert.assertEquals 22 import org.junit.Test 23 import kotlin.random.Random 24 25 // TODO test other functionality of Timestamp and Duration 26 class TimeTest { 27 28 @Test 29 fun testSerialize() { 30 for (i in 0 until 42) { 31 val t = Random.nextLong() 32 assertEquals("""{"t_s":$t}""", encodeToString(Timestamp.serializer(), Timestamp(t))) 33 } 34 assertEquals("""{"t_s":"never"}""", encodeToString(Timestamp.serializer(), Timestamp.never())) 35 } 36 37 @Test 38 fun testDeserialize() { 39 for (i in 0 until 42) { 40 val t = Random.nextLong() 41 assertEquals(Timestamp(t), decodeFromString(Timestamp.serializer(), """{ "t_s": $t }""")) 42 } 43 assertEquals(Timestamp.never(), decodeFromString(Timestamp.serializer(), """{ "t_s": "never" }""")) 44 } 45 46 }