Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement multiple Configurers for DataTables #136

Open
kathyrollo opened this issue Oct 21, 2018 · 0 comments
Open

Implement multiple Configurers for DataTables #136

kathyrollo opened this issue Oct 21, 2018 · 0 comments
Assignees
Labels
dependency fix This requires a fix for a dependency enhancement New feature or request
Milestone

Comments

@kathyrollo
Copy link
Owner

kathyrollo commented Oct 21, 2018

Implement TableConfigurer and TableEntryConfigurer for different DataTable types.

Currently not supported:
cucumber/cucumber-jvm#1426

TableConfigurer.java:

package project.datatable;

import java.util.Locale;

import cucumber.api.TypeRegistry;
import cucumber.api.TypeRegistryConfigurer;
import io.cucumber.datatable.DataTable;
import io.cucumber.datatable.DataTableType;
import io.cucumber.datatable.TableTransformer;
import project.domain.Transaction;

/**
 * Maps DataTable with label column in feature file to a single object of
 * Type{@literal <T>}. Left column is field name, right column is value.
 */
public class TableConfigurer implements TypeRegistryConfigurer {

	@Override
	public Locale locale() {
		return Locale.ENGLISH;
	}

	@Override
	public void configureTypeRegistry(TypeRegistry registry) {

		registry.defineDataTableType(new DataTableType(Transaction.class, new TableTransformer<Transaction>() {
			@Override
			public Transaction transform(DataTable dataTable) throws Throwable {
				return new Transaction(dataTable.asMaps().get(0));
			}
		}));

	}

}

TableEntryConfigurer.java:

package project.datatable;

import java.util.Locale;
import java.util.Map;

import cucumber.api.TypeRegistry;
import cucumber.api.TypeRegistryConfigurer;
import io.cucumber.datatable.DataTableType;
import io.cucumber.datatable.TableEntryTransformer;
import project.domain.Transaction;

/**
 * Maps DataTable with header row in feature file to multiple objects of
 * Type{@literal <T>}. Each row below the header is an object.
 */
public class TableEntryConfigurer implements TypeRegistryConfigurer {

	@Override
	public Locale locale() {
		return Locale.ENGLISH;
	}

	@Override
	public void configureTypeRegistry(TypeRegistry registry) {

		registry.defineDataTableType(new DataTableType(Transaction.class, new TableEntryTransformer<Transaction>() {
			@Override
			public Transaction transform(Map<String, String> map) {
				return new Transaction(map.get("name"), map.get("amount"), map.get("frequency"), map.get("month"));
			}
		}));

	}

}

@kathyrollo kathyrollo added enhancement New feature or request dependency fix This requires a fix for a dependency labels Oct 21, 2018
@kathyrollo kathyrollo self-assigned this Oct 21, 2018
@kathyrollo kathyrollo added this to the version 4 milestone Dec 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependency fix This requires a fix for a dependency enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant