The most elegant way to get two properties at once

Suppose you have hierarchical data and want to get the combined value of individual properties, what is the most elegant or groovy way to do this?

The following example contains information about failed and missed tests. Of course, it makes sense that these values ​​are separated, but to use a list of all the tests that did not succeed, I came across two possible solutions that both of them did not satisfy me.

def junitResultList = [
    [
        name: "Testsuite A",
        children: [
            failedTests: ["Test 1", "Test 2"],
            skippedTests: []
        ]
    ],
    [
        name: "Testsuite B",
        children: [
            failedTests: ["CursorTest"],
            skippedTests: ["ClickTest", "DragNDropTest"]
        ]
    ]
]

To be more specific, I want the value to be ["Test 1", "Test 2", "CursorTest", "ClickTest", "DragNDropTest"].

The first approach was to simply add a list of test extensions:

(junitResultList*.children*.failedTests + 
 junitResultList*.children*.skippedTests).flatten()

, , groovy , , - :

(junitResultList*.children*.findAll {
    ['skippedTests', 'failedTests'].contains(it.key)
})*.values().flatten()
+4
3

:

junitResultList.children.collect { it.failedTests + it.skippedTests }.flatten()

junitResultList.children.collect { [it.failedTests, it.skippedTests] }.flatten()
+2

:

//Define the keys to find
def requiredKeys = ['failedTests', 'skippedTests']
println requiredKeys.collect{ junitResultList.children."$it"}.flatten()

-

+3

You can get subMap(), and then values():

junitResultList*.children*.subMap(["failedTests","skippedTests"])*.values().flatten()
+2
source

Source: https://habr.com/ru/post/1684113/


All Articles