Junit : how could I use assertion to check String >0 - junit4

I have a String and I would like to check that this string is > 0
using assert
trying :
using assertFalse I can't set 2 strings : my value and "0" .
Is there another way to say : assertThat "myString"> "0" ?
my purpose :
myTable.getValue =>this return a string : "20"
I would like to check that this string is > or different from "0"
thanks

You really want to do the comparison as a number, for safety. So you want something like
assertTrue(Integer.parseInt(myTable.getValue()) > 0)

Additionally, with assertThat style you may want to use:
assertThat(myTable.length()).isGreaterThan(0);
you will need to import org.assertj.core.api.Assertions.assertThat static methods. I'd prefer this method as enhance test readability.

Related

How to check if int is greater than 1 and make word plural or singular in Flutter?

I would check if the int is greater than 1 and make the word plural or singular. I mean something like the following example:
If the apple variable is 1, it will return:
1 apple
Else:
2 apples
Here's the code of how I do this so far:
"$totalApples ${totalApples > 1 ? "apples" : "apple"}"
But, I think there should be some function or package or other way to solve this problem, right? I wish to use a short code instead of this long code.
How to check if int is greater than 1 and make word plural or singular? I would appreciate any help. Thank you in advance!
yes, there is a plural method in the intl package, check it from here.
the Getx package provide same functionality with a plural method.
However, if you want just to make a simple-call method to get the plural, you can use Dart extension methods, like this:
extension PluralExtension on int {
String plural(String singularWord, [String pluralLetters = "s"]) {
return this > 1 ? "$this $singularWord$pluralLetters" : "$this $singularWord";
}
}
print(5.plural("apple")); // 5 apples
print(0.plural("apple")); // 0 apple
print(1.plural("apple")); // 1 apple
print(10.plural("tomato", "es")); // 1 tomatoes
print(1.plural("tomato", "es")); // 1 tomato
In this example, I added the a custom plural method on the int type, so you can call it simply on anything that's a int.

Vue Conditional Class Binding

I am trying to dynamically render class based off actionTypeCreate. This is a method that simply returns a boolean value based off the prop actionType that is passed. I am triggering this method on the mounted hook and confirmed it is returning properly.
Now I am trying to return the class value of 'col-md-4' if actionTypeCreate. If not actionTypeCreate I want to return the class 'col-md-6'.
This is what I have but it is not working:
:class="{toggleActionType : 'col-md-4' ? 'col-md-6'}"
I tried to reference this existing question, but I did not get it.
You can do it as follows:
:class="{'col-md-4' : toggleActionType , 'col-md-6' : !toggleActionType }"
According to the Vue documentation itself you can do it in two ways. First, you can use Array Syntax and this is broadly used to apply a list of classes.
Array Syntax
:class="[toggleActionType ? 'col-md-4' : 'col-md-6']"
Or you can do it as normal by Object Syntax but it does not accept ternary operations, so you have to do it this way:
Object Syntax
:class="{'col-md-4' : toggleActionType , 'col-md-6' : !toggleActionType}"
Try this:
:class="[toggleActionType : 'col-md-4' ? 'col-md-6']"

Codeeffect Rule Engine - How to define rules to check length of string

I have to define a rule like <> length is < 5 or >5 AND not equal to 'N/A'.
I am not able to get Len() function in the list of operators for the string data type.
Please suggest if any field level attributes needs to define.
In your source class define a method like this:
public int Length(string str)
{
return string.IsNullOrEmpty(str) ? 0 : str.Length;
}
Then run your project, open the rule editor and create your rule like this:
Check if YourString has any value and Length(YourString) is greater than [5]
Details on in-rule methods can be found here

Populating the list in g.select

I need to programmatically load a list.
Instead of:
<g:select
name="cars"
from="${Car.list()}"
value="${person?.cars*.id}"
optionKey="id"
multiple="true" />
I would like to do it this because, the list is not always coming from the same source
g.select(name : searchfield.fieldName,
class : "fillWidth searchfield",
multiple : "true",
from : ${ searchfield.fieldFrom },
optionKey : searchfield.fieldKey,
optionValue : searchfield.fieldValue)
The from does not load. with the list, I get an error message:
No signature of method: sample.SearchTagLib.$() is applicable for argument types: (sample.SearchTagLib$_getSelectField_closure5) values: [sample.SearchTagLib$_getSelectField_closure5#1187b50] Possible solutions: is(java.lang.Object), any(), use([Ljava.lang.Object;), any(groovy.lang.Closure), wait(), grep()
You don't need the ${} in the from option
g.select(name : searchfield.fieldName,
class : "fillWidth searchfield",
multiple : "true",
from : searchfield.fieldFrom,
optionKey : searchfield.fieldKey,
optionValue : searchfield.fieldValue)
In Groovy code ${} is a way to put Groovy expressions inside double quoted GStrings, if you're not in a GString you can just use the expression directly without wrapping it in ${}.
Edit from your comment
The fieldFrom at this point is a string which would get its value from a database. So the value in the DB is "Car.list()" which in the prototype I need to convert to a bound able or execute-able line of code.
It's not generally recommended to allow your app to execute arbitrary snippets of Groovy code provided by users (for obvious security reasons). As long as the code snippets come from a secure source such as a trusted admin user then fair enough, it is possible using GroovyShell
def from = new GroovyShell().evaluate(searchfield.fieldFrom)
but this is likely to be rather inefficient, creating a new classloader and parsing and compiling a whole Groovy script class every time. If the fieldFrom values are intended to always be pulling something from the database (i.e. they'll always be something like Car.list() or Vehicle.findAllByNumberOfWheelsGreaterThan(2), rather than arbitrary Groovy like [1,2,3]) then it might be better to store HQL expressions in fieldFrom and run them using executeQuery
def from = AnyDomainClass.executeQuery(searchfield.fieldFrom)
(executeQuery is a static GORM method, you need to call it on a specific domain class but it can return results of any type). The HQL equivalent of Car.list() would be "from Car", the equivalent of Vehicle.findAllByNumberOfWheelsGreaterThan(2) would be "from Vehicle where numberOfWheels > 2", etc.
I think you need to use strings as the attribute name:
g.select('name' : searchfield.fieldName,
'class' : "fillWidth searchfield",
'multiple' : "true",
'from' : ${ searchfield.fieldFrom },
'optionKey' : searchfield.fieldKey,
'optionValue' : searchfield.fieldValue)

Converting boolean values to text in mvc

I was wondering what is the best way to convert a boolean value retrieved from the database to text for use in a display-field or textbox (e.g. true or false to Male or Female). Any suggestions?
make the conversion in the view - IMHO this is not against MVC.
If you want to go hardcore with your design create a new class/struct "Gender" with static creation methods/properties "Male", "Female", overload the ToString accordingly and cast-operators to bool or a constructor with bool and a ToBool function.
#((bool)item.BooleanColumn ? "Yes" : "No")