Fix NameToken.parse

This commit is contained in:
Alexander Nozik 2024-04-16 15:35:21 +03:00
parent e7db1cc763
commit 1c60453244
3 changed files with 20 additions and 6 deletions

View File

@ -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

View File

@ -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("]"))
}
}

View File

@ -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")
}
}
}