Search code examples
fantomafbedsheetfancordion

How to refer to a row index in a Fancordion fixture table?


Using tables in Fancordion v1.0.4, how can I use the row index in a column command to validate its value.

For example if my Fixture is:

class MyFixture : FixtureTest {

    [Str:Obj?][] getCountries() {
         return [["code":"AU", "name":"Australia"], ["code":"NZ", "name":"New Zealand"], ["code":"UK", "name":"United Kingdom"]]
    }
}

And the spec is:

table:
col[0]+verifyEq:getCountries[#ROW]["code"]
col[1]+verifyEq:getCountries[#ROW]["name"]

Country Code    Country Name
------------    ------------
AU              Australia
NZ              New Zealand
UK              United Kingdom

What should I use instead of the #ROW placeholder in the specification example above?

Is there a better way to write this specification and fixture? e.g. is it better to create a method in the fixture to retrieve each individual map in the list instead of the full list?


Solution

  • As you noted there is no #ROW macro (but it may be a nice addition!) so currently you'll need to change either the assertions, or the data structure.

    Change the assertions:

    Here we change the assertions slightly and introduce our own row field that gets incremented at the end of every row iteration:

    **  Hi!
    ** 
    **   table:
    **   col[0]+verifyEq:getCountries[#FIXTURE.row]["code"]
    **   col[1]+verifyEq:getCountries[#FIXTURE.row]["name"]
    **   row+exe:row++
    **
    **   Country Code    Country Name
    **   ------------    ------------
    **   AU              Australia
    **   NZ              New Zealand
    **   UK              United Kingdom
    ** 
    class CountryTest : FixtureTest {
        Int row := 0
    
        [Str:Obj?][] getCountries() {
             return [["code":"AU", "name":"Australia"], ["code":"NZ", "name":"New Zealand"], ["code":"UK", "name":"United Kingdom"]]
        }
    }
    

    Change the data structure:

    A better way would probably be to change the data being tested into a list of lists. For that has the added advantage of failing when the test data is too much or too little.

    **  Hi!
    ** 
    **   table:
    **   verifyRows:getCountries()
    **
    **   Country Code    Country Name
    **   ------------    ------------
    **   AU              Australia
    **   NZ              New Zealand
    **   UK              United Kingdom
    ** 
    class CountryTest : FixtureTest {
    
        Str[][] getCountries() {
            countries := [["code":"AU", "name":"Australia"], ["code":"NZ", "name":"New Zealand"], ["code":"UKs", "name":"United Kingdom"]]
            return countries.map { it.vals }
        }
    }