0

I have something like this:

fun doSomething(specifyInput: Base.() -> Unit) {
    val spec = BaseImpl()
    spec.apply {
        specifyInput()
        validate()
    }

interface Base {
    var a: String?
    var b: String?
}

class BaseImpl(override var a: String?, override var b: String?) : Base {
    constructor() : this(null, null)
fun validate() {
        if (a == null) throw ExceptionForA()
        if (b == null) throw ExceptionForB()
    }
}

This is gradle task.

class A : DefaultTask(), Base by BaseImpl()

@TaskAction
fun doSomething() {
    doSomething {
       a = c
    }
}

This is my test:

val buildFile = File(testProjectDir, "build.gradle.kts")
        buildFile.createNewFile()
        buildFile.writeText("""
                tasks.register("task", A::class.java) {
                    c = "something"
                }
            """.trimIndent())
        val result = testProjectDir.executeAndFailGradleRunner("task")

        assertThat(result.output).contains("ExceptionForB")

Do you know what I need to do that the test passed?

Thank you in advance.

Update: In the meantime I have extend the code example.

Found the solution that I am looking for.

borac
  • 19
  • 5
  • Inside the lambda you get a `this` receiver, you can just do `download { url = /*your url*/; target = /*your file*/ }` (in seperate lines ; is not necessary) – Animesh Sahu May 07 '20 at 08:50
  • What do you mean? what is task here? Edit your question and summarize the problem. – Animesh Sahu May 07 '20 at 09:16
  • I edited my question and summarized the problem. – borac May 07 '20 at 12:54
  • You've to set both the variable, as if either of the variable is null then exception is raised. – Animesh Sahu May 08 '20 at 13:16
  • I don't understand where I should set these both variables. Can you please provide example? – borac May 08 '20 at 13:58
  • `doSomething { a = "something"; b = "something else" }` – Animesh Sahu May 08 '20 at 13:59
  • I understand your point but this value "something" I passed already in my test stored in 'a' variable and I don't know how to get this value of 'a'? – borac May 08 '20 at 14:24
  • Where do you want to get the variable? – Animesh Sahu May 08 '20 at 14:26
  • In my test I have something like this: buildFile.writeText(""" ... import java.net.URL tasks.register("task", A::class.java) { url = URL("http://somefile.txt") } """.trimIndent()) val result = testProjectDir.executeAndFailGradleRunner("task") Now I want to reference this url in instance of A task. – borac May 08 '20 at 15:01
  • Man you never mentioned what is `tasks` in `tasks.register` I already asked in one of the comments... – Animesh Sahu May 08 '20 at 15:06
  • class A : DefaultTask() this class is the gradle task – borac May 08 '20 at 15:12
  • There is no register function in [DefaultTask](https://docs.gradle.org/current/javadoc/org/gradle/api/DefaultTask.html), how are you even calling `tasks.register()`? I'm flagging question as very low quality, you're not able to ask question or answer what is asked properly. – Animesh Sahu May 08 '20 at 15:16
  • Can you then explain me why when I execute this tasks.register("task", A::class.java) in other test the test is passed? – borac May 08 '20 at 15:26
  • `if (a == null) throw RuntimeException() if (b == null) throw RuntimeException()` You're checking both variable must not be null and you're only passing only 1 variable `url` other is null by default and hence RuntimeException is raised as it should be. – Animesh Sahu May 08 '20 at 15:28
  • If I understood you well you want to say that I cannot call the task and passed the value like this: tasks.register("task", A::class.java) { a = URL("somefile.txt") } – borac May 08 '20 at 15:35
  • You have to set `b` as well if you want the test to be succeed. +_+ – Animesh Sahu May 08 '20 at 15:36
  • But in this test case I expect to throw Exception because I want to pass a but not b and I have validation for a and b. But the problem is that is a is also null what I don't want. – borac May 08 '20 at 15:41

1 Answers1

1

I found the solution. I have to use another variable and assign in my variable from delegation, a = c, cannot use a = a.

borac
  • 19
  • 5