From 1fb46a4ad706b87b35ab4a5fadfa230778100c9a Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Fri, 28 Sep 2018 16:27:17 +0200 Subject: [PATCH] ensuring that lambda for computing a default value is only called once per delegate --- .../com/xenomachina/argparser/Default.kt | 8 ++-- .../com/xenomachina/argparser/DefaultTest.kt | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/test/kotlin/com/xenomachina/argparser/DefaultTest.kt diff --git a/src/main/kotlin/com/xenomachina/argparser/Default.kt b/src/main/kotlin/com/xenomachina/argparser/Default.kt index 02f612a..8a8b6a1 100644 --- a/src/main/kotlin/com/xenomachina/argparser/Default.kt +++ b/src/main/kotlin/com/xenomachina/argparser/Default.kt @@ -39,14 +39,14 @@ fun ArgParser.DelegateProvider.default(newDefault: () -> T): ArgParser.De /** * Returns a new `Delegate` with the specified default value. * - * @param newDefault the default value for the resulting [ArgParser.Delegate] + * @param defaultValue the default value for the resulting [ArgParser.Delegate] */ fun ArgParser.Delegate.default(defaultValue: T): ArgParser.Delegate = default { defaultValue } /** * Returns a new `Delegate` with the specified default value as a lambda. * - * @param newDefault the default value for the resulting [ArgParser.Delegate] + * @param defaultValue the default value for the resulting [ArgParser.Delegate] */ fun ArgParser.Delegate.default(defaultValue: () -> T): ArgParser.Delegate { if (hasValidators) { @@ -56,6 +56,8 @@ fun ArgParser.Delegate.default(defaultValue: () -> T): ArgParser.Delegate return object : ArgParser.Delegate() { + private val computedDefault by lazy(defaultValue) + override val hasValidators: Boolean get() = inner.hasValidators @@ -71,7 +73,7 @@ fun ArgParser.Delegate.default(defaultValue: () -> T): ArgParser.Delegate override val value: T get() { inner.parser.force() - return if (inner.hasValue) inner.value else defaultValue() + return if (inner.hasValue) inner.value else computedDefault } override val hasValue: Boolean diff --git a/src/test/kotlin/com/xenomachina/argparser/DefaultTest.kt b/src/test/kotlin/com/xenomachina/argparser/DefaultTest.kt new file mode 100644 index 0000000..c82ec26 --- /dev/null +++ b/src/test/kotlin/com/xenomachina/argparser/DefaultTest.kt @@ -0,0 +1,38 @@ +// Copyright © 2018 Tobias Berger +// +// This file is part of kotlin-argparser, a library which can be found at +// http://github.com/xenomachina/kotlin-argparser +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published by the +// Free Software Foundation; either version 2.1 of the License, or (at your +// option) any later version. +// +// This library is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +// for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this library; if not, see http://www.gnu.org/licenses/ + +package com.xenomachina.argparser + +import io.kotlintest.shouldBe +import io.kotlintest.specs.FunSpec + +class DefaultTest : FunSpec({ + test("Single call on default lambda") { + val parser = parserOf() + + var callCount = 0 + val testDelegate by parser.storing("-t") { toInt() } + .default { ++callCount } + + callCount shouldBe 0 + testDelegate shouldBe 1 + callCount shouldBe 1 + testDelegate shouldBe 1 + callCount shouldBe 1 + } +})