From 1c6045324423463e4df2852c2160d80efb3a2b10 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 16 Apr 2024 15:35:21 +0300 Subject: [PATCH] Fix NameToken.parse --- CHANGELOG.md | 4 +++- .../kscience/dataforge/names/NameToken.kt | 2 +- .../kscience/dataforge/names/NameTest.kt | 20 +++++++++++++++---- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7c134e6..f709b8be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Added - Name index comparator +- Specialized ByteArrayValue ### Changed - DataSink `branch` is replaced with `putAll` to avoid confusion with DataTree methods @@ -15,7 +16,8 @@ ### Fixed - `listOfScheme` and `listOfConvertable` delegates provides correct items order. - Scheme meta setter works with proper sub-branch. -- +- NameToken.parse improper work with indices + ### Security diff --git a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/names/NameToken.kt b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/names/NameToken.kt index 0dc83c57..83752b9a 100644 --- a/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/names/NameToken.kt +++ b/dataforge-meta/src/commonMain/kotlin/space/kscience/dataforge/names/NameToken.kt @@ -69,7 +69,7 @@ public class NameToken(public val body: String, public val index: String? = null public fun parse(string: String): NameToken { val body = string.substringBefore('[') val index = string.substringAfter('[', "") - if (index.isNotEmpty() && index.endsWith(']')) error("NameToken with index must end with ']'") + if (index.isNotEmpty() && !index.endsWith(']')) error("NameToken with index must end with ']'") return NameToken(body, index.removeSuffix("]")) } } diff --git a/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/names/NameTest.kt b/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/names/NameTest.kt index a5bdf3fc..db630487 100644 --- a/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/names/NameTest.kt +++ b/dataforge-meta/src/commonTest/kotlin/space/kscience/dataforge/names/NameTest.kt @@ -1,9 +1,6 @@ package space.kscience.dataforge.names -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFalse -import kotlin.test.assertTrue +import kotlin.test.* class NameTest { @Test @@ -50,4 +47,19 @@ class NameTest { val name = Name.parse("a.b.c") assertEquals("a.b".parseAsName(), name.cutLast()) } + + @Test + fun tokenParseTest(){ + val token1 = NameToken.parse("token[index]") + assertEquals("token", token1.body) + assertEquals("index", token1.index) + + val token2 = NameToken.parse("token-body") + assertEquals("token-body", token2.body) + assertEquals("", token2.index) + + assertFails { + NameToken.parse("token[22") + } + } } \ No newline at end of file