From eac77d7419d9c81cc9c23b42221db62ecb4d6b47 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Wed, 31 May 2023 01:52:53 +0200 Subject: [PATCH] WIP: add more tests --- .../fr/postgresjson/definition/Function.kt | 33 +---------- .../postgresjson/definition/FunctionTest.kt | 57 +++++++++++++++++++ 2 files changed, 58 insertions(+), 32 deletions(-) diff --git a/src/main/kotlin/fr/postgresjson/definition/Function.kt b/src/main/kotlin/fr/postgresjson/definition/Function.kt index eccd959..8078d5e 100644 --- a/src/main/kotlin/fr/postgresjson/definition/Function.kt +++ b/src/main/kotlin/fr/postgresjson/definition/Function.kt @@ -81,7 +81,7 @@ class Function( try { return getNextScript { status.isNotEscaped() && listOf("(", " ", "\n").any { afterBeginBy(it) } } } catch (e: NameMalformed) { - throw FunctionNameMalformed() + throw FunctionNameMalformed(null, e) } } @@ -93,37 +93,6 @@ class Function( return ScriptPart(restOfScript.run(block)) } - /** - * Get a name. - * You can define a list of characters that end the name. Like `(` or space. - */ - private fun ScriptPart.getAbstractName(endString: String, includeEnd: Boolean = false): NextScript = - getAbstractName(listOf(endString), includeEnd) - - /** - * Get a name. - * You can define a list of characters that end the name. Like `(` or space. - */ - @Deprecated("replace by getNextScript", ReplaceWith("getNextScript")) - private fun ScriptPart.getAbstractName(endStrings: List, includeEnd: Boolean = false): NextScript { - var nameIsEscaped = false - for ((i, c) in restOfScript.withIndex()) { - val isEndOfString = endStrings.filter { restOfScript.substring(i).take(it.length) == it }.length > 0 - - if (c == '"' && i == 0) { - nameIsEscaped = true - } else if (c == '"' && i > 0 && (restOfScript[i + 1] == '"' || restOfScript[i - 1] == '"')) { - continue - } else if (c == '"' && i > 0 && !nameIsEscaped) { - throw NameMalformed() - } else if ((c == '"' && i > 0 && nameIsEscaped) || (!nameIsEscaped && isEndOfString)) { - val dropCount = i + if (includeEnd) 1 else 0 - return NextScript(restOfScript.take(i).trim('"').replace("\"\"", "\""), restOfScript.drop(dropCount)) - } - } - throw NameMalformed() - } - data class Status( var doubleQuoted: Boolean = false, // " var simpleQuoted: Boolean = false, // ' diff --git a/src/test/kotlin/fr/postgresjson/definition/FunctionTest.kt b/src/test/kotlin/fr/postgresjson/definition/FunctionTest.kt index 1ebdf03..4f0722a 100644 --- a/src/test/kotlin/fr/postgresjson/definition/FunctionTest.kt +++ b/src/test/kotlin/fr/postgresjson/definition/FunctionTest.kt @@ -129,6 +129,63 @@ class FunctionTest: FreeSpec({ param[1].type.name shouldBe "int" } } + + "parameters with `character varying(255)`" - { + val param = Function( + // language=PostgreSQL + """ + create or replace function myfun(one character varying(255)) returns text language plpgsql as + $$ begin end;$$; + """.trimIndent() + ).parameters + + "should have 1 parameters" { + param shouldHaveSize 1 + } + + "should have name" { + param[0].name shouldBe "one" + } + + "should have type name" { + param[0].type.name shouldBe "character varying" + } + + "should have type precision" { + param[0].type.precision shouldBe 255 + param[0].type.scale shouldBe null + } + } + + "parameters with `numeric(16, 8)`" - { + val param = Function( + // language=PostgreSQL + """ + create or replace function myfun(one numeric(16, 8)) returns text language plpgsql as + $$ begin end;$$; + """.trimIndent() + ).parameters + + "should have 1 parameters" { + param shouldHaveSize 1 + } + + "should have name" { + param[0].name shouldBe "one" + } + + "should have type name" { + param[0].type.name shouldBe "numeric" + } + + "should have type precision" { + param[0].type.precision shouldBe 16 + } + + "should have type scale" { + param[0].type.scale shouldBe 8 + } + } } // "function returns" - {