Search in sources :

Example 1 with NumberListConverter

use of org.jbehave.core.steps.ParameterConverters.NumberListConverter in project jbehave-core by jbehave.

the class ParameterConvertersBehaviour method shouldConvertCommaSeparatedValuesOfSpecificNumberTypes.

@SuppressWarnings("unchecked")
@Test
public void shouldConvertCommaSeparatedValuesOfSpecificNumberTypes() {
    ParameterConverter converter = new NumberListConverter();
    Type doubleType = new TypeLiteral<List<Double>>() {
    }.getType();
    List<Double> doubles = (List<Double>) converter.convertValue("3, 0.5, 0.0, 8.00, " + NAN + ", " + POSITIVE_INFINITY, doubleType);
    assertThatCollectionIs(doubles, 3.0, 0.5, 0.0, 8.0, Double.NaN, Double.POSITIVE_INFINITY);
    Type floatType = new TypeLiteral<List<Float>>() {
    }.getType();
    List<Float> floats = (List<Float>) converter.convertValue("3, 0.5, 0.0, 8.00, " + NAN + ", " + NEGATIVE_INFINITY, floatType);
    assertThatCollectionIs(floats, 3.0f, 0.5f, 0.0f, 8.0f, Float.NaN, Float.NEGATIVE_INFINITY);
    Type longType = new TypeLiteral<List<Long>>() {
    }.getType();
    List<Long> longs = (List<Long>) converter.convertValue("3, 0, 8", longType);
    assertThatCollectionIs(longs, 3L, 0L, 8L);
    Type intType = new TypeLiteral<List<Integer>>() {
    }.getType();
    List<Integer> ints = (List<Integer>) converter.convertValue("3, 0, 8", intType);
    assertThatCollectionIs(ints, 3, 0, 8);
}
Also used : AbstractParameterConverter(org.jbehave.core.steps.ParameterConverters.AbstractParameterConverter) ParameterConverter(org.jbehave.core.steps.ParameterConverters.ParameterConverter) NumberListConverter(org.jbehave.core.steps.ParameterConverters.NumberListConverter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) Type(java.lang.reflect.Type) AtomicLong(java.util.concurrent.atomic.AtomicLong) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 2 with NumberListConverter

use of org.jbehave.core.steps.ParameterConverters.NumberListConverter in project jbehave-core by jbehave.

the class ParameterConvertersBehaviour method shouldConvertCommaSeparatedValuesToListOfNumbersWithDefaultFormat.

@Test
public void shouldConvertCommaSeparatedValuesToListOfNumbersWithDefaultFormat() {
    ParameterConverter<List<Number>> converter = new NumberListConverter();
    Type listOfNumbers = new TypeLiteral<List<Number>>() {
    }.getType();
    Type setOfNumbers = new TypeLiteral<Set<Number>>() {
    }.getType();
    assertThat(converter.accept(listOfNumbers), is(true));
    assertThat(converter.accept(setOfNumbers), is(false));
    List<Number> list = converter.convertValue("3, 0.5, 6.1f, 8.00", listOfNumbers);
    assertThatCollectionIs(list, 3L, 0.5, 6.1, 8L);
}
Also used : Type(java.lang.reflect.Type) Arrays.asList(java.util.Arrays.asList) NumberListConverter(org.jbehave.core.steps.ParameterConverters.NumberListConverter) Test(org.junit.Test)

Example 3 with NumberListConverter

use of org.jbehave.core.steps.ParameterConverters.NumberListConverter in project jbehave-core by jbehave.

the class ParameterConvertersBehaviour method shouldConvertCommaSeparatedValuesToListOfNumbersWithCustomFormat.

@Test
public void shouldConvertCommaSeparatedValuesToListOfNumbersWithCustomFormat() {
    ParameterConverter<List<Number>> converter = new NumberListConverter(new DecimalFormat("#,####"), " ");
    Type type = new TypeLiteral<List<Number>>() {
    }.getType();
    List<Number> list = converter.convertValue("3,000 0.5 6.1f 8.00", type);
    assertThatCollectionIs(list, 3000L, 0.5, 6.1, 8L);
}
Also used : Type(java.lang.reflect.Type) DecimalFormat(java.text.DecimalFormat) Arrays.asList(java.util.Arrays.asList) NumberListConverter(org.jbehave.core.steps.ParameterConverters.NumberListConverter) Test(org.junit.Test)

Example 4 with NumberListConverter

use of org.jbehave.core.steps.ParameterConverters.NumberListConverter in project jbehave-core by jbehave.

the class ParameterConvertersBehaviour method shouldFailToConvertCommaSeparatedValuesOfInvalidNumbers.

@Test
public void shouldFailToConvertCommaSeparatedValuesOfInvalidNumbers() {
    expectedException.expect(ParameterConvertionFailed.class);
    new NumberListConverter().convertValue("3x, x.5", new TypeLiteral<List<Number>>() {
    }.getType());
}
Also used : TypeLiteral(org.apache.commons.lang3.reflect.TypeLiteral) NumberListConverter(org.jbehave.core.steps.ParameterConverters.NumberListConverter) Test(org.junit.Test)

Aggregations

NumberListConverter (org.jbehave.core.steps.ParameterConverters.NumberListConverter)4 Test (org.junit.Test)4 Type (java.lang.reflect.Type)3 Arrays.asList (java.util.Arrays.asList)3 BigInteger (java.math.BigInteger)1 DecimalFormat (java.text.DecimalFormat)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 TypeLiteral (org.apache.commons.lang3.reflect.TypeLiteral)1 AbstractParameterConverter (org.jbehave.core.steps.ParameterConverters.AbstractParameterConverter)1 ParameterConverter (org.jbehave.core.steps.ParameterConverters.ParameterConverter)1