I'm using Fitnesse SliM and I want to check if the result of a fixture is the empty string. Leaving the result field in Fitnesse empty just results in an ignored test which is obviously not what I want. I could solve this by extending the fixture code, but I wonder if this can be handled within Fitnesse itself.
It seems that Slim implies an empty string as an ignore, at least for the QueryTable fixture: A cell that is left blank in the table will be filled in from the result and counted as ignored.
Even though this is not considered a good solution, if you really have to you could use a regular expression to test on an empty string by matching on
=~/^$/
Another option is using the null fixture driver, as seen in http://fitnesse.org/FitNesse.SuiteAcceptanceTests.SuiteSlimTests.SlimSymbolsCanBeBlankOrNull
passing the word 'blank' simulates a empty string.
like:
|Check|That the returned string is | blank |
In this case - when you need to check with SLIM usage, whether the result is an empty string, you can use markup variable. Just define it somewhere on your page with test, like:
!define blank {}
And then call it anywhere you want:
|check|SomeFixtureName|${blank}|
Related
I need to find a section of a string that includes the substring "print time:" and save it with the time it displays after the colon on the database.
What I've used until now is the downcase helper and the includes? helper to start the search but I'm not sure how to actually execute a search inside the string.
How can I find the section of the string so that I can save it afterwards?
Use regular expressions, which in Ruby can be written with the /…/ syntax and matched against using String#match.
log = "username: jsmith\nprint time: 08:02:41\npower level: 9001"
print_time = log.match(/print time:\s*([^\n]+)\s*\n/)[1]
p print_time # => "08:02:41"
The regex /print time:\s*([^\n]+?)\s*\n/ matches any text after “print time:”, on the same line, ignoring surrounding whitespace \s*, and saves it as the first capture group using the (). Then [1] selects the contents of that first capture group.
After extracting the print_time string, you can do whatever you need to with it. For example, if you had a Rails model PrintTime, you might save the extracted time to the database with PrintTime.create(extracted_time: print_time).
Below, is what returning on my server:
[test[]:[1, 2, 3, 4, 5], action: something, controller: myController]
And I can't println using
params.test.each{ println it}
How can I print on each element in test[]?
The issue you are having is that the key named test[] needs to be escaped.
Thus you should be able to do:
params."test[]".each { println it }
The reason for needing to use double or single quotes around the property name is due to the fact you have brackets in the name of the property. Usually brackets appearing in Groovy code would be seen as a collection and without an index or key within the brackets it's considered invalid syntax.
So, in this case since the name of the property/key within your params contains brackets you need to wrap it in quotes so Groovy understands it's just part of the name/key and not an attempt to access it as a collection (even if your property is actually a collection).
Honestly, I would fix the root of the issue, meaning don't name your property test[] but rather test since it's much cleaner and clearer as to what your property actually is.
I am just starting a very basic program in Grails (never used it before, but it seems to be very useful).
What I have so far is:
in X.groovy,
a String named parameters, with constraint of maximum length 50000 and a couple other strings and dates, etc.
in XController.groovy,
static scaffold = X;
It displays the scaffold UI (very handy!), and I can add parameter strings and the other objects associated with it.
My problem is that the parameters string is a long string with formatting that is pasted in by the user. When it is displayed on the browser, however, it does not retain any carriage returns.
What is the best way to go about this? I'm a very beginner at Grails and still have lots and lots of learning to do on this account. Thanks.
The problem is that the string is being displayed using HTML which doesn't parse \n into a new line by default. You need to wrap the text in <pre> (see: http://www.w3schools.com/tags/tag_pre.asp) or replace the \n with <br/> tags to display it correctly to the user.
Is there a way to get a list of sections defined in a layout file? For example, if I want to know what sections are defined in my Shared/_Layout.cshtml file is there a way to parse that Layout file so that I know what sections exist in the layout?
There's no built-in function I'm aware of because the name isn't necessarily known without executing the view.
You can probably just run a regular expression over your layouts, like
[^#]#RenderSection\(\s*"(?<name>[^"]+)"\s*\)
which accepts #RenderSection("foo") or #RenderSection( "foo" ), but skips ##RenderSection (the ## escaped #).
However, this assumes that the name of the section is passed in as a string literal. The view could also look like (not your typical situation, but possible):
#RenderSection(Model.SectionName)
In that case you're pretty much lost.
I'm trying to store regexes in a database but they're not working when used in a .sub(), even though the same regex works when used directly in .sub() as a string.
regex = Class.object.field // Class.object is an active record containing "\w*\s\/\s"
mystring = "first / second"
mystring.sub(/#{regex}/, '')
// => nil
mystring.sub(/\w*\s\/\s/, '')
// => second
Any insight appreciated!
Thanks,
Matt.
Editing to correct class/object terminology (thanks) & correcting my 2nd example as I had shown #{} wrapped around the working regex (cut & paste SNAFU).
To answer your question: It is not quite what kind of thing your Class.object is. If it's an ActiveRecord, it won't work.
Edit: You obviously found that the problem is Rails escaping the regexp.
An ActiveRecord cannot "contain" your regular expression directly; the regexp will be in one of the fields of your record. In which case you'd want to do something like regexp = Class.object.field_containing_the_regexp.
Even if that is not the case, I suspect that the problem is that your regexp is something other than a string. You can quickly test this by using
puts "My regexp: #{regexp}"
The string that you will see in the output will be the one that is used for the regexp.
A String is not a Regexp. You have to create a Regexp object first.
regex = Regexp.new("\w*\s\/\s")
Turns out my regexp didn't cater for all cases - \w didn't account for symbols. After checking in rails console, and seeing the screwey escaping I was alreasdy half-way down the wrong track.
Thanks for the help.