<commit_before>package com.natpryce.krouton

import com.natpryce.hamkrest.absent
import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import com.oneeyedmen.minutest.junit.JupiterTests
import com.oneeyedmen.minutest.junit.context
import java.time.LocalDate
import java.util.Locale

class SingleElementRouteTests : JupiterTests {
    enum class Axis {
        X, Y, Z
    }
    
    override val tests = context<Unit> {
        test("strings") {
            assertThat(string.parse("/foo"), equalTo("foo"))
            
            assertThat(string.parse("/"), absent())
            assertThat(string.parse("/foo/bar"), absent())
            
            assertThat(string.path("foo"), equalTo("/foo"))
        }
        
        test("ints") {
            assertThat(int.parse("/1"), equalTo(1))
            assertThat(int.parse("/-4"), equalTo(-4))
            assertThat(int.parse("/010"), equalTo(10))
            
            assertThat(int.parse("/bob"), absent())
            
            assertThat(int.path(1), equalTo("/1"))
            assertThat(int.path(-1), equalTo("/-1"))
        }
        
        test("doubles") {
            assertThat(double.parse("/1"), equalTo(1.0))
            assertThat(double.parse("/1.0"), equalTo(1.0))
            assertThat(double.parse("/-2.0"), equalTo(-2.0))
            
            assertThat(double.parse("/bob"), absent())
            
            assertThat(double.path(1.5), equalTo("/1.5"))
            assertThat(double.path(-3.25), equalTo("/-3.25"))
        }
        
        
        test("iso_dates") {
            assertThat(isoLocalDate.parse("/2016-02-25"), equalTo(LocalDate.of(2016, 2, 25)))
            assertThat(isoLocalDate.parse("/2016-XX-88"), absent())
        }
        
        
        test("enums") {
            val axis: VariablePathElement<Axis> by enum()
            
            assertThat(axis.parse("/X"), equalTo(Axis.X))
            assertThat(axis.parse("/Y"), equalTo(Axis.Y))
            assertThat(axis.parse("/Z"), equalTo(Axis.Z))
            
            assertThat(axis.parse("/x"), absent())
            assertThat(axis.parse("/a"), absent())
            assertThat(axis.parse("/ddd"), absent())
            
            assertThat(axis.path(Axis.Z), equalTo("/Z"))
        }
        
        test("locales") {
            assertThat(locale.parse("/fr-FR"), equalTo(Locale.FRANCE))
            assertThat(locale.path(Locale.FRANCE), equalTo("/fr-FR"))
        }
    }
}<commit_msg>SingleElementRouteTests take advantage of Minutest<commit_after>package com.natpryce.krouton

import com.natpryce.hamkrest.absent
import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.describe
import com.natpryce.hamkrest.equalTo
import com.oneeyedmen.minutest.Context
import com.oneeyedmen.minutest.TestContext
import com.oneeyedmen.minutest.junit.JupiterTests
import com.oneeyedmen.minutest.junit.context
import java.time.LocalDate
import java.util.Locale

class SingleElementRouteTests : JupiterTests {
    enum class Axis { X, Y, Z }
    val axis: VariablePathElement<Axis> by enum()
    
    
    override val tests = context<Unit> {
        behaviourOf(string,
            routing = listOf("/foo" to "foo"),
            invalidPaths = listOf("/", "/foo/bar"))
        
        behaviourOf(int,
            routing = listOf("/0" to 0, "/1" to 1, "/-1" to -1, "/-4" to -4),
            forwardRouting = listOf("/010" to 10),
            reverseRouting = listOf(10 to "/10"),
            invalidPaths = listOf("/", "/foo"))
        
        behaviourOf(double,
            routing = listOf("/1.0" to 1.0, "/-2.0" to -2.0, "/1.5" to 1.5, "/-3.25" to -3.25),
            forwardRouting = listOf("/1" to 1.0),
            invalidPaths = listOf("/", "/foo"))
        
        behaviourOf(isoLocalDate,
            routing = listOf("/2016-02-25" to LocalDate.of(2016, 2, 25)),
            invalidPaths = listOf("/2016-XX-88", "/foo"))
        
        behaviourOf(axis, name="enums",
            routing = listOf("/X" to Axis.X, "/Y" to Axis.Y, "/Z" to Axis.Z),
            invalidPaths = listOf("/", "/x", "/a", "/ddd"))
        
        behaviourOf(locale,
            routing = listOf("/fr-FR" to Locale.FRANCE, "/fr-CA" to Locale.CANADA_FRENCH, "/fr" to Locale.FRENCH),
            forwardRouting = listOf("/fr-" to Locale.FRENCH, "/FR-FR" to Locale.FRANCE),
            invalidPaths = listOf("/"))
    }
    
    inline fun <reified T> TestContext<Unit>.behaviourOf(
        template: PathTemplate<T>,
        name: String = T::class.simpleName ?: template.toUrlTemplate(),
        routing: List<Pair<String, T>> = emptyList(),
        forwardRouting: List<Pair<String, T>> = emptyList(),
        reverseRouting: List<Pair<T, String>> = emptyList(),
        invalidPaths: List<String> = emptyList(),
        noinline moreTests: TestContext<PathTemplate<T>>.() -> Unit = {}
    ) {
        testContextFor(name, template, routing, forwardRouting, reverseRouting, invalidPaths, moreTests)
    }
    
    fun <T> TestContext<Unit>.testContextFor(name: String, template: PathTemplate<T>, routing: List<Pair<String, T>>, forwardRouting: List<Pair<String, T>>, reverseRouting: List<Pair<T, String>>, invalidPaths: List<String>, moreTests: TestContext<PathTemplate<T>>.() -> Unit) {
        derivedContext<PathTemplate<T>>(name) {
            fixture { template }
            
            context("parsing valid paths to values") {
                (routing + forwardRouting).forEach { (validPath, value) ->
                    testParse(validPath, value)
                }
            }
            
            context("reverse routing: generating paths from values") {
                (routing + reverseRouting.flipAll()).forEach { (validPath, value) ->
                    testToPath(value, validPath)
                }
            }
            
            context("parsing invalid paths") {
                invalidPaths.forEach {
                    test("$it is an invalid path") {
                        assertThat(parse(it), absent())
                    }
                }
            }
            
            moreTests()
        }
    }
    
    private fun <T, U> Iterable<Pair<T, U>>.flipAll(): List<Pair<U, T>> = map { it.second to it.first }
    
    fun <T> Context<PathTemplate<T>, PathTemplate<T>>.testToPath(value: T, validPath: String) {
        test("${describe(value)} generates the path $validPath") {
            assertThat(path(value), equalTo(validPath))
        }
    }
    
    fun <T> Context<PathTemplate<T>, PathTemplate<T>>.testParse(validPath: String, value: T) {
        test("$validPath parses to ${describe(value)}") {
            assertThat(parse(validPath), equalTo(value))
        }
    }
}
{'fraction_non_alphanumeric': 0.15003695491500368, 'fraction_numerical': 0.016851441241685146, 'mean_word_length': 2.0740572467060425, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4939340', 'n_tokens_mistral': 2064, 'n_tokens_neox': 2029, 'n_words': 375}