Search code examples
fitnessefitsharp

Fitnesse Trim String data before comparison


I wanted to ask if there is a way to trim String data before comparing it with the data in the table. For example, if we have

|MyCompareClass|
|getString?    |
|string1       |

And the result of getString() will be "string1 ". I want the comparison to be green and not Expected "string1". I'm looking for a way to do it without modifying the MyCompareClass source code. Any ideas?


Solution

  • You can write a custom string compare operator class:

    public class MyCustomCompare: CellOperator, CompareOperator<Cell> {
        public bool CanCompare(TypedValue actual, Tree<Cell> expected) {
            return actual.Type == typeof(string);
        }
    
        public bool Compare(TypedValue actual, Tree<Cell> expected) {
            return actual.Value.ToString().Trim() == expected.Value.Text;
        }
    }
    

    Then register your class with the Fitnesse configure fixture:

    |configure|processor|add operator|MyCustomCompare|