String Values with Multiple Spaces

What is the most readable way to check a variable equals a string, which contains multiple spaces.

For example if I have a text file (/tmp/a.txt) that contains the following text I wish to test:

String with    4 spaces

I can use the strip_spaces and collapse_spaces arguments of Builtin.Should Be Equal to test that it contains the correct words:

*** Settings ***
Library    OperatingSystem

*** Test Cases ***
Check String With Spaces
    ${a} =    Get File    /tmp/a.txt
    Log    ${a}    console=True
    Should Be Equal    ${a}    String with 4 spaces    strip_spaces=True    collapse_spaces=True

But say my tests are space sensitive, I want any text in ${a} that has more or less than 4 spaces to cause a failure. For example I need to check that the space padding of a date value is correct.

I could store the String with 4 spaces in a text file as well and call it something like a.txt.expected.

*** Test Cases ***
Check String With Spaces
    ${a} =    Get File    /tmp/a.txt
    ${expected} =    Get File    a.txt.expected
    Should Be Equal    ${a}    ${expected}

But my issue with that is it makes the test less readable because you then need to go and look in another file to see what the expected value is. If I have lots of strings to test that’s lots of other files to go and look in.

Similarly I could implement a Python robot library keyword like:

text_contains_expected_value(text: str):
    assert text == "String with    4 spaces"

My robot test then would look like:

*** Test Cases ***
Check String With Spaces
    ${a} =    Get File    /tmp/a.txt
    Log    ${a}    console=True
    Text Contains Expected Value    ${a}

But that has the same issue. A casual reader of the .robot file needs to go and look in another file to see what the expected value is.

ChatGPT tells me I can use a string enclosed in triple quotes like so:

*** Test Cases ***
Check String With Spaces
    ${a} =    Get File    /tmp/a.txt
    Log    ${a}    console=True
    Should Be Equal    ${a}    """String with    4 spaces"""

But that gives me """String with as the first argument and 4 spaces""" as the second argument, resulting in an error.

Hi Kim,

From the documentation for Space separated format${SPACE} is probably what you are after

This String with ${SPACE}${SPACE} 4 spaces should do what you need.

Dave.

1 Like

Thanks Dave. Yes I saw the ${SPACE} variable. I think that makes the tests even less readable if there are more than a few spaces.

For example some of my tests are plain text reports formatted with 80 characters width and right justified header and footer values.

I suppose I was hoping for some kind of magic escape sequence like triple quotes or double braces or the heredoc style << END syntax that I could wrap the string in.

Hi Kim,

well there are a couple of other options: Space and empty variables

Maybe one of these is better?

String with${SPACE * 4}4 spaces
String with\ \ \ \ 4 spaces

Dave.

1 Like

Thanks Dave. Those options are easier on the eye.

I also noticed in the docs for the pipe-separated format that it preserves spaces, and also that you can mix and match space separated format and pipe separated format in the one .robot file. I’d never really paid much attention to the pipe format before.

So for example:

*** Test Cases ***
Check String With Strip and Collapse Spaces
    ${a} =    Get File    /tmp/a.txt
    Should Be Equal    ${a}    String with 4 spaces    collapse_spaces=True

Check String With SPACE Variable
    ${a} =    Get File    /tmp/a.txt
    Should Be Equal    ${a}    String with${SPACE}${SPACE}${SPACE} 4 spaces

| Pipe Separated Format |
|                       | ${a} =          | Get File | /tmp/a.txt              |
|                       | Should Be Equal | ${a}     | String with    4 spaces |

Partial Pipe Separated Format
    ${a} =    Get File    /tmp/a.txt
| | Should Be Equal | ${a} | String with    4 spaces

I think using the Partial Pipe Separated Format, just on the lines where you need to is going to work for me in most cases. But I suppose this would not work if the string being tested also contains pipe characters (which mine often do), in which case you could escape them with \|.

I think I’ll have to settle on storing them in another file.

1 Like

Hi Kim,

Another option might be to use a Variable file, these can be robot, python, yaml etc and store the data with extra spaces, pipes etc in there and just use it’s variable in the test case (maybe with a comment of what the variable contains)

Dave.

1 Like

and I think that is precisely what I will do. Thanks for your help. :raised_hands:

1 Like