I have a file with n lines.
In my Spock test, I download, read the file, and assert each line of it.
Is there a way to produce n tests in the report instead of a one?
Maybe you know how to #Unroll Spock tests and feature method names like this:
package de.scrum_master.stackoverflow.q63002164
import spock.lang.Specification
import spock.lang.Unroll
class FixedInputBasedParametrisedTest extends Specification {
#Unroll
def "verify #inputLine"() {
expect:
inputLine.contains("et")
where:
inputLine << ["weather", "whether", "getters & setters"]
}
}
The result when running the test e.g. in IntelliJ IDEA looks like this:
But you can also use dynamic data providers, not just fixed sets of values. They just need to be Iterable.
If for example you have a resource file like this
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit
amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem
ipsum dolor sit amet.
you can use it as a data provider like this:
package de.scrum_master.stackoverflow.q63002164
import spock.lang.Specification
import spock.lang.Unroll
class InputFileBasedParametrisedTest extends Specification {
#Unroll
def "verify #inputLine"() {
expect:
inputLine.contains("et")
where:
inputLine << new File("src/test/resources/test.txt").readLines()
}
}
The result will look like this:
Related
I have a text file with a lot of content. I want to extract the following text fragments beginning with TXT_ and ending with a ).
e.g.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. (TXT_I_WANT_TO_EXTRACT_THIS). At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. (TXT_AND_THIS) Lorem ipsum dolor sit amet.
Expected result:
TXT_I_WANT_TO_EXTRACT_THIS
TXT_AND_THIS
I just the need the regex for the result.
Thank you so much for your help.
Greetings
Not sure in which language you are working. In R, for example, you can use str_extract_all:
str_extract_all(txt, "TXT\\w+")
[[1]]
[1] "TXT_I_WANT_TO_EXTRACT_THIS" "TXT_AND_THIS"
Even if you don't work in R, the pattern used in the solution will not change greatly; it is in fact simple: supposing that all target strings start with the same literal pattern, say "TXT", and only contain alphabetic characters and the underscore, the parts after "TXT" can conveniently be matched by \\w+, a character class for alphanumeric characters and the underscore.
txt <- "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. (TXT_I_WANT_TO_EXTRACT_THIS). At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. (TXT_AND_THIS) Lorem ipsum dolor sit amet."
I'm trying to implement a View where I have a long text and the text scrolls with a Drag gesture. (ScrollView). The trick I'm trying to achieve is to have a gradient so at the to and at the Botton the text should have a kind o opacity:
I was able to do that using a ZStack and a mass gradient but as soon I move the rectangle over my text, the scroll from ScrollView doesn't work anymore.
Here the animated version what I'm trying to do: https://media.giphy.com/media/j3bXQOV1vZMxZS6GAk/giphy.gif
Here my code:
struct ContentView: View {
var body: some View {
GeometryReader { fullView in
ZStack {
ScrollView() {
Text("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.")
}
LinearGradient(gradient: Gradient(colors: [.white, .clear,.clear, .white]), startPoint: .top, endPoint: .bottom)
.mask(
Rectangle()
)
}
}
}
}
Did you guys have such need?
Thanks and Regards,
Max
Yeah I actually wanted to do this once, but I never did. However, looking at your code you could probably do something like:
//useful for testing large text blocks
func * (lhs: Text, rhs: Int) -> Text {
var out = lhs
for _ in 0..<rhs { out = out + lhs }
return out
}
struct ContentView: View {
var body: some View {
//first geometry reader, used to determine the _size_ of the gradient
GeometryReader { fullView in
ScrollView() {
ZStack{
Text("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt.") * 60
//second geometry reader, used to determin the _offset_ of the gradient
GeometryReader{ geo in
LinearGradient(gradient:
Gradient(colors: [
.white,
.clear,
.clear,
.white
]), startPoint: .top, endPoint: .bottom)
.mask( Rectangle() )
.frame(width: fullView.size.width, height: fullView.size.height)
.offset(x: 0, y: -geo.frame(in: .global).origin.y)
}
}
}
}
.edgesIgnoringSafeArea(.all)
}
}
What's going on here is we are setting the size of the linear gradient using the fullview Geometry Reader, and we are setting the offset based on the frame of the geo Geometry Reader, the locations of both geometry readers in the view "hierarchy" is important.
Also, I wanted to note, that your gradient looks kind of dark in some places...
By looking at the code, this probably isn't what you intended to do. It's happening because Color.clear is actually an rgb value of 0,0,0 with an alpha of 0. So as the color is interpolated along it transitions to black or grey.
You could fix it by replacing .clear with:
.init(UIColor(red: 1, green: 1, blue: 1, alpha: 0))
Here is a screenshot where I set the top color to .init(UIColor(red: 1, green: 1, blue: 1, alpha: 0)) and the bottom color to.clear:
I need a Regex which matches the closed [A-Za-z_0-9]*\.xml before ERROR.
The following input should match match_me.xml
ERROR should be determined.
The first [A-Za-z_0-9]*\.xml before ERROR should be matched. -> match_me.xml
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo
match_not_me.xml
dolores et ea rebum.
match_me.xml
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna
ERROR
aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
You can use awk for this:
awk 'p && /ERROR/{print p; p==""} /^[A-Za-z_0-9]*\.xml$/{p=$0}' file
match_me.xml
When a line matches our pattern we store that line in variable p
When p is set and we encounter ERROR we print p and reset it to blank.
grep (PCRE) solution:
grep -Poz 'ERROR[\s\S]+?\s\K[A-Za-z_0-9]+\.xml' <(tac file) && echo
tac file - concatenate lines of the file in reverse order
[\s\S]+? - matches any character in "non-greedy" manner
\K - ignoring previous match
The output:
match_me.xml
I need a regular expression to validate a large text (max 2000 characters), it should work as follows:
Say n=20
If there is any word in the text with letters greater than n, it will not validate the whole text.
If there is an email in the text say emailaddress#emailserver.com, then it should ignore that.
If the email address is like 123456789012345678901#email.com (as in this case n > 20), it will not validate the whole text.
I'm using ^(?!.*\S{10}).*$ as of now but it does not validate the email.
I racked my brain for a good ten minutes with no luck, I think it must have been some syntax I have yet to learn.
All suggestions would be greatly appreciated, many thanks!
I would use the following pattern:
\b[^\s#]{30,}\b
This pattern does not allow a word to be longer than 30 characters
It will handle e-mail addresses as two words (one word before and one word after the #.
var n = 30;
// doubly escaped slashes and global search (g)
var regex = new RegExp('\\b[^\\s#]{' + n + ',}\\b', 'g');
var text = document.getElementById('text').innerHTML;
var match = text.match(regex);
if(match) {
console.log("Document contains a value with over " + n + " characters.");
console.log(match);
}
else {
console.log("Document does not contain a value with over " + n + " characters.");
}
<div id="text">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos
et accusametjustoduodoloreseterebumstet#clita.kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla
facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet,
</div>
Is it possible to apply a regex in procmail that filters for specific word patterns.
For example I could do this with a normal regex:
/(?=.*dolor)(?=.*ipsum)(?=.*sit)/s
This would produce a match with the following text. Where this wouldn't:
/(?=.*money)(?=.*ipsum)(?=.*sit)/s
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea
rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem
ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur
sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et
dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam
et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea
takimata sanctus est Lorem ipsum dolor sit amet.
I would want this to adapt for procmail use. And even extend it so instead of just searching for "money" it would also match on "mOney", "möney", "móney" and so on.
Is it possible?
If so, how?
Yes, it is possible. Let me show you how.
Your regex checks if the words dolor, ispum and sit appearing in random order somewhere within the text. The following procmail recipe does the same:
:0 B
* -2^0
* 1^0 \<dorum\>
* 1^0 \<ipsum\>
* 1^0 \<sit\>
action_dorum_ipsum_sit
The first condition contains an empty regular expression which, because it always matches, is used to give your score a negative offset. A match of each of the next rules will increase that score by one (regardless how often each word occurs). At the end, the score will only be positive (and therefore trigger the action) if the text contains all 3 words at least once.
To add more keywords, you could either add more rules (and decrease the negative offset accordingly) or extend an existing rule, e.g. like this
* 1^0 \<(mOney|möney|móney)\>