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

Add table value accessor methods #3

Open
apoorvam opened this issue Oct 4, 2016 · 1 comment
Open

Add table value accessor methods #3

apoorvam opened this issue Oct 4, 2016 · 1 comment

Comments

@apoorvam
Copy link
Member

apoorvam commented Oct 4, 2016

Table type can have value accessor methods by column name.

Currently table values can be accessed using tbl.Rows[0].Cells[0] to fetch first element of first row.
This can be enhanced to access value using column name as: tbl.getColumn("columnName").get(0) so that its more explicit and avoids index out of range exceptions.

Better way?

@ferdypruis
Copy link

ferdypruis commented Aug 2, 2021

For anyone looking.
I map a Table to a slice of maps, where each row becomes a slice element with the column header being the index in the map, using this;

// TableParameters can be used when a table of parameters is used with a step.
// The function provides a map per row of which the index is the name of the cell from the header.
// https://docs.gauge.org/writing-specifications.html?os=linux&language=python&ide=vscode#table-parameters
func TableParameters(tbl *m.Table) []map[string]string {
	rows := make([]map[string]string, len(tbl.Rows))
	for i, r := range tbl.Rows {
		cells := make(map[string]string, len(r.Cells))
		for col, cell := range r.Cells {
			cells[tbl.Headers.Cells[col]] = cell
		}
		rows[i] = cells
	}

	return rows
}

Having for example this specification;

* Almost all words have vowels 

   |Word  |Vowel Count|
   |------|-----------|
   |Gauge |3          |
   |Mingle|2          |
   |Snap  |1          |
   |GoCD  |1          |
   |Rhythm|0          |

You could do this in your implementation;

var _ = gauge.Step("Almost all words have vowels <table>", func(tbl *m.Table) {
	for _, row := range TableParameters(tbl) {
		word := row[`Word`]
		expectedCount, err := strconv.Atoi(row[`Vowel Count`])
	
[...]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants