All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonTest.io.islandtime.parser.StringParserTest.kt Maven / Gradle / Ivy

The newest version!
package io.islandtime.parser

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue

class StringParserTest {
    @Test
    fun `parser with no length restrictions`() {
        val expectedCharMap = mapOf(
            0 to 'T',
            1 to 'e',
            2 to 's',
            3 to 't'
        )

        val parser = dateTimeParser {
            +' '
            string {
                onEachChar { char, index ->
                    assertEquals(char, expectedCharMap[index]!!)
                    StringParseAction.ACCEPT_AND_CONTINUE
                }
                onParsed { timeZoneId = it }
            }
        }

        val result = parser.parse(" Test")
        assertTrue { result.fields.isEmpty() }
        assertEquals("Test", result.timeZoneId)
    }

    @Test
    fun `parsing can be stopped with REJECT_AND_STOP`() {
        val parser = dateTimeParser {
            +' '
            string {
                onEachChar { char, index ->
                    assertEquals('.', char)
                    assertEquals(0, index)
                    StringParseAction.REJECT_AND_STOP
                }
                onParsed { timeZoneId = it }
            }
            +'.'
        }

        val result = parser.parse(" .")
        assertTrue { result.fields.isEmpty() }
        assertTrue { result.timeZoneId!!.isEmpty() }
    }

    @Test
    fun `reports an error when there are no characters to parse`() {
        val parser = dateTimeParser {
            +' '
            string {
                onEachChar { _, _ ->  StringParseAction.ACCEPT_AND_CONTINUE }
            }
        }

        val exception = assertFailsWith { parser.parse(" ") }
        assertEquals(1, exception.errorIndex)
        assertEquals(" ", exception.parsedString)
    }

    @Test
    fun `reports an error when the min length isn't satisfied`() {
        val parser = dateTimeParser {
            +' '
            string(2..10) {
                onEachChar { char, _ ->
                    if (char in 'A'..'Z') {
                        StringParseAction.ACCEPT_AND_CONTINUE
                    } else {
                        StringParseAction.REJECT_AND_STOP
                    }
                }
                onParsed { timeZoneId = it }
            }
            +'.'
        }

        val exception = assertFailsWith { parser.parse(" T.") }
        assertEquals(1, exception.errorIndex)
        assertEquals(" T.", exception.parsedString)
    }

    @Test
    fun `reports an error when the max length isn't satisfied`() {
        val parser = dateTimeParser {
            +' '
            string(1..4) {
                onEachChar { char, _ ->
                    if (char in 'A'..'Z') {
                        StringParseAction.ACCEPT_AND_CONTINUE
                    } else {
                        StringParseAction.REJECT_AND_STOP
                    }
                }
                onParsed { timeZoneId = it }
            }
            +'.'
        }

        val exception = assertFailsWith { parser.parse(" TESTS.") }
        assertEquals(1, exception.errorIndex)
        assertEquals(" TESTS.", exception.parsedString)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy