I have that kind of test which works:
Feature: TestAddition
Scenario Outline: "Addition"
Given A is <A> and B is <B>
Then A + B is <result>
Examples:
| A | B | result |
| 3 | 4 | 7 |
| 2 | 5 | 7 |
| 1 | 4 | 5 |
And thats the glue code:
package featuresAdditions;
import org.junit.Assert;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import math.AdditionEngine;
public class step {
private AdditionEngine testAdditionEngine;
private double resultAddition;
#Given("^A is (\\d+) and B is (\\d+)$")
public void addition(int arg1, int arg2) throws Throwable {
testAdditionEngine = new AdditionEngine();
resultAddition = testAdditionEngine.calculateAdditionAmount(arg1, arg2);
}
#Then("^A + B is (.)$")
public void addition(double arg1) throws Throwable {
Assert.assertEquals(arg1, resultAddition, 0.01);
}
}
However I would like to know how to create an invalid table example [where ?? means I do not know what to put in the below table]
Examples:
| A | B | result |
| "é3-3" | 5 | ?? |
| "é3-3" | "aB" | ?? |
This should give a java.lang.NumberFormatException
In pure jUnit I would do something like the code below which works like a charm [with #Test(expected = NumberFormatException.class)]. However, I have to use Cucumber... Someone can tell me how to perform such a test with Cucubmer?
public class test {
AdditionEngine testAdditionEngine = new AdditionEngine();
#Test(expected = NumberFormatException.class)
public void test() {
testAdditionEngine.calculateAdditionAmount("é3-3", 5);
}
}
Scenario Outline: "Invalid Addition"
Given A is <A> and B is <B>
Then A + B is <result>
Examples:
| A | B | result |
| "é3-3" | 5 | java.lang.NumberFormatException |
| "é3-3" | "aB" | java.lang.NumberFormatException |
Change the stepdefinition to take a String as an argument instead of Integer.
private Exception excep;
#Given("^A is (.*?) and B is (.*?)$")
public void addValid(String arg1, String arg2) {
try {
testAdditionEngine = new AdditionEngine();
testAdditionEngine.calculateAdditionAmount(arg1, arg2);
} catch (NumberFormatException e) {
excep = e;
}
};
#Then("^A \\+ B is (.*?)$")
public void validResult(String arg1){
assertEquals(arg1, excep.getClass().getName());
};
You will get an ambiguous step message if you are on Cucumber 2 and above. This will be because the valid scenariooutline will match the integer and string stepdefinitions. Change either one of the scenario statements.
Related
Not getting the input request properly if the given input string contains space in-between in cucumber data table. Also, I did delete the trailing and leading space to ensure there are no hidden illegal characters but still received the inputs incorrectly in java code. Any advice would certainly help.
Feature:Foo
#foo
Scenario Outline: sample run
Then send request <aoo> <boo> <coo> <doo>
Examples:
| aoo | boo | coo | doo |
| 200 | xyx | Do not disturb | true |
#Then("^send request (.*) (.*) (.*) (.*)$")
public void send_request(String aoo, String boo, String coo, String doo) throws Throwable {
System.out.println("aoo " + aoo);
System.out.println("boo " + boo);
System.out.println("coo " + coo);
System.out.println("doo " + doo);
}
Expected Output:-
aoo 200
boo xyx
coo Do not disturb
doo true
Actual Output:-
aoo 200 xyz Do
boo not
coo disturb
doo true
(.*) is greedy and matches any character. You have to limit them. If possible try to use other data types. aoo -> int, doo -> boolean
#foo
Scenario Outline: sample run
Then send request "<aoo>" "<boo>" "<coo>" "<doo>"
Examples:
| aoo | boo | coo | doo |
| 200 | xyx | Do not disturb | true |
#Then("^send request \"(.*)\" \"(.*)\" \"(.*)\" \"(.*)\"$")
public void send_request(String aoo, String boo, String coo, String doo) throws Throwable {
// omitted code
}
im having 2 list of different variable, so i want to compare and update the 'Check' value from list 2 if the 'Brand' from list 2 is found in list 1
-------------------- --------------------
| Name | Brand | | Brand | Check |
-------------------- --------------------
| vga x | Asus | | MSI | X |
| vga b | Asus | | ASUS | - |
| mobo x | MSI | | KINGSTON | - |
| memory | Kingston| | SAMSUNG | - |
-------------------- --------------------
so usually i just did
for(x in list1){
for(y in list2){
if(y.brand == x.brand){
y.check == true
}
}
}
is there any simple solution for that?
Since you're mutating the objects, it doesn't really get any cleaner than what you have. It can be done using any like this, but in my opinion is not any clearer to read:
list2.forEach { bar ->
bar.check = bar.check || list1.any { it.brand == bar.brand }
}
The above is slightly more efficient than what you have since it inverts the iteration of the two lists so you don't have to check every element of list1 unless it's necessary. The same could be done with yours like this:
for(x in list2){
for(y in list1){
if(y.brand == x.brand){
x.check = true
break
}
}
}
data class Item(val name: String, val brand: String)
fun main() {
val list1 = listOf(
Item("vga_x", "Asus"),
Item("vga_b", "Asus"),
Item("mobo_x", "MSI"),
Item("memory", "Kingston")
)
val list2 = listOf(
Item("", "MSI"),
Item("", "ASUS"),
Item("", "KINGSTON"),
Item("", "SAMSUNG")
)
// Get intersections
val intersections = list1.map{it.brand}.intersect(list2.map{it.brand})
println(intersections)
// Returns => [MSI]
// Has any intersections
val intersected = list1.map{it.brand}.any { it in list2.map{it.brand} }
println(intersected)
// Returns ==> true
}
UPDATE: I just see that this isn't a solution for your problem. But I'll leave it here.
I have input as below from file reading,
1|VegemiteScroll|VS5|3|6.99
2|VegemiteScroll|VS5|5|8.99
3|BlueberryMuffin|MB11|2|9.95
4|BlueberryMuffin|MB11|5|16.95
5|BlueberryMuffin|MB11|8|24.95
6|Croissant|CF|3|5.95
7|Croissant|CF|5|9.95
I wanted to put it in Hashmap using group by as below, Let's consider #1,#2 lines.
Map obj = new HashMap();
obj.put(3,6.99);
obj.put(5,8.99);
List<Map> list = new ArrayList<>();
list.add(obj)
Map<String, List<Map>> map = new HashMap();
map.put("VS5", list);
This is for 1st case, and for second and third case (# 3 to #8),
map.put("MB11", list);
The list contains List of Map as above. Could you please me to put solve this using java8 streams. Thanks in advance!
Here is a solution using Java 8 Streams, groupingBy and other "fancy stuff". For the sake of simplicity, I've assumed that the input is already provided as a String.
private static final int EXPECTED_LINE_ELEMENTS = 5;
private static final int LINE_KEY_INDEX = 2;
private static final int DATA_KEY_INDEX = 3;
private static final int DATA_VALUE_INDEX = 4;
private static Map<String, List<Map<Integer, Float>>> convert(final String input) {
return Stream.of(input.split("\\n")) // split input by new line character
.filter(line -> !line.trim().isEmpty()) // filter out empty lines
.map(line -> line.split("\\|")) // split each line by '|' character
.filter(line -> line.length == EXPECTED_LINE_ELEMENTS) // filter out potential elements containing undesired number of columns
.collect(Collectors.groupingBy(line -> line[LINE_KEY_INDEX])) // convert to map using 3rd column as a key and a list of corresponding lines as values
.entrySet()
.stream()
.collect(
Collectors.toMap(
Map.Entry::getKey, // keep the same key
stringListEntry -> Collections.singletonList(convertLinesToMap(stringListEntry.getValue())) // convert list of lines to a singleton list containing a map
)
);
}
private static Map<Integer, Float> convertLinesToMap(final List<String[]> lines) {
return lines.stream()
.collect(Collectors.toMap(
line -> Integer.valueOf(line[DATA_KEY_INDEX].trim()), // use 4th column as key of the map (mapped to Integer)
line -> Float.valueOf(line[DATA_VALUE_INDEX].trim())) // use 5th column as key of the map (mapped to Float)
);
}
Then, the following:
System.out.println(convert(input));
Should print this:
{ CF =[{3=5.95, 5=9.95, 9=16.99}], VS5 =[{3=6.99, 5=8.99}], MB11 =[{2=9.95, 5=16.95, 8=24.95}]}
PS. As you've written:
Map obj = new HashMap();
obj.put(3,6.99);
obj.put(5,8.99);
I've assumed that you want to have Integers as keys and Floats as values. If this is not the case, you can just update appropriate fragments of convertLinesToMap method.
Below is the code which serves purpose. It is not optimised fully. But it is working.
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/**
*
*/
/**
* #author KishorRaskar
*
*/
public class Main {
/**
* #param args
* #throws Exception
*/
public static void main(String[] args) throws Exception {
// pass the path to the file as a parameter
File file =
new File("C:\\Data\\FAB\\WorkSpace\\Test\\MyCode\\test.txt");
Scanner sc = new Scanner(file);
List<HashMap> mapList = new ArrayList<>();
HashMap<String, String> dataMap = null;
HashMap<String, List<HashMap<String, String>>> dataMapList = new HashMap<>();
while (sc.hasNextLine()) {
String line = sc.nextLine();
if(null == line || line.isEmpty()) {
continue;
}
String[] dataArray = line.split("\\|");
//System.out.println(Arrays.toString(dataArray));
String dataMapKey = dataArray[3].trim();
String dataMapValue = dataArray[4].trim();
String dataMapListKey = dataArray[2].trim();
if(!dataMapList.containsKey(dataMapListKey)) {
dataMapList.put(dataMapListKey, new ArrayList<>());
dataMapList.get(dataMapListKey).add(new HashMap<>());
}
dataMapList.get(dataMapListKey).get(0).put(dataMapKey, dataMapValue);
//System.out.println(line);
}
System.out.println("###############################");
System.out.println(dataMapList);
System.out.println("###############################");
}
}
Input : test.txt
1 | Vegemite Scroll| VS5 | 3 | 6.99
2 | Vegemite Scroll| VS5 | 5 | 8.99
3 | Blueberry Muffin| MB11 | 2 | 9.95
4 | Blueberry Muffin| MB11 | 5 | 16.95
5 | Blueberry Muffin| MB11 | 8 | 24.95
6 | Croissant| CF | 3 | 5.95
7 | Croissant| CF | 5 | 9.95
8 | Croissant| CF | 9 | 16.99
Output:
###############################
{CF=[{3=5.95, 5=9.95, 9=16.99}], MB11=[{2=9.95, 5=16.95, 8=24.95}], VS5=[{3=6.99, 5=8.99}]}
###############################
I am beginner in libgdx . When trying to make a game I get this error in Android Studio :
Error:(39, 16) Gradle: error: no suitable constructor found for
Stage(Viewport,SpriteBatch) constructor Stage.Stage() is not
applicable (actual and formal argument lists differ in length)
constructor Stage.Stage(StageStyle) is not applicable (actual and
formal argument lists differ in length)
-
public class Hud {
| public Stage stage;
| private Viewport viewport;
|
| private Integer worldTimer;
| private float timeCount;
| private Integer score;
|
| Label countdownLabel;
| Label scoreLabel;
| Label timeLabel;
| Label levelLabel;
| Label worldLabel;
| Label snakeLabel;
|
| public Hud(SpriteBatch sb) {
| | worldTimer = 300;
| | timeCount = 0;
| | score = 0;
| |
| | viewport = new FitViewport(Snake.V_WIDTH, Snake.V_HEIGHT, new OrthographicCamera());
| | stage = new Stage(viewport,sb);
| }
}
Here is the error:
stage = new Stage(viewport,sb);
I searched the internet some solution but I have not found anything. I'm a little lost.
Excuse me for my bad english :)
I hope you can help me . I will be grateful.
You have the wrong Stage class imported, it should be:
com.badlogic.gdx.scenes.scene2d.Stage
But you probably have this or some other package:
javafx.stage.Stage
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am getting a dataset using LINQ to SQL. I need to filter this dataset such that:
If a field with a null SourceName exists and there's at least one other record for this field with a non-null SourceName, then it should be removed.
If it is the only row for that 'Field', then it should remain in the list.
Here's an example data: Data consists of 3 columns: 'Field', 'SourceName' and 'Rate'
Field | SourceName | Rate
10 | s1 | 9
10 | null | null
11 | null | null
11 | s2 | 5
11 | s3 | 4
12 | null | null
13 | null | null
13 | s4 | 7
13 | s5 | 8
8 | s6 | 2
9 | s7 | 23
9 | s8 | 9
9 | s9 | 3
Output should look like:
Field | SourceName | Rate
10 | s1 | 9
11 | s2 | 5
11 | s3 | 4
12 | null | null // <- (remains since there's only
13 | s4 | 7 // 1 record for this 'Field')
13 | s5 | 8
8 | null | null
9 | s8 | 9
9 | s9 | 3
How do I filter it?
What you are trying to achieve is not trivial and can't be solved with just a .Where() clause. Your filter criteria depends on a condition that requires grouping, so you will have to .GroupBy() and then flatten that collection of collections using .SelectMany().
The following code satisfies your expected output using LINQ to Objects, and I don't see any reason for LINQ to SQL not to be able to translate it to SQL, haven't tried that tough.
//Group by the 'Field' field.
yourData.GroupBy(x => x.Field)
//Project the grouping to add a new 'IsUnique' field
.Select(g => new {
SourceAndRate = g,
IsUnique = g.Count() == 1,
})
//Flatten the collection using original items, plus IsUnique
.SelectMany(t => t.SourceAndRate, (t, i) => new {
Field = t.SourceAndRate.Key,
SourceName = i.SourceName,
Rate = i.Rate,
IsUnique = t.IsUnique
})
//Now we can do the business here; filter nulls except unique
.Where(x => x.SourceName != null || x.IsUnique);
Use Linq's built in 'Where' clause with a lambda continuation:
Simple static example of using lambda's and a simple POCO class to store the data in a list like yours:
using System;
using System.Collections.Generic;
using System.Linq;
namespace Simple
{
class Program
{
class Data
{
public string Field { get; set; }
public string SourceName { get; set; }
public string Rate { get; set; }
}
static List<Data> Create()
{
return new List<Data>
{
new Data {Field = "10", SourceName = null, Rate = null},
new Data {Field = "11", SourceName = null, Rate = null},
new Data {Field = "11", SourceName = "s2", Rate = "5"}
};
}
static void Main(string[] args)
{
var ls = Create();
Console.WriteLine("Show me my whole list: \n\n");
// write out everything
ls.ForEach(x => Console.WriteLine(x.Field + "\t" + x.SourceName + "\t" + x.Rate + "\n"));
Console.WriteLine("Show me only non nulls: \n\n");
// exclude some things
ls.Where(l => l.SourceName != null)
.ToList()
.ForEach(x => Console.WriteLine(x.Field + "\t" + x.SourceName + "\t" + x.Rate + "\n"));
Console.ReadLine();
}
}
}