2

I have the following yaml script that I have tested here: online yaml parser

testcases:
- testcase:
    desc: 'TEST TC1'
    requirement-ref: Doors-10.1.1.0
    given:
      text: 'A UUT, TEST2 and TEST are connected'
      devices:
      - Device:
          Type: UUT
          Status: Connected
      - Device:
          Type: TEST
          Status: Connected
      - Device:
          Type: TEST2
          Status: Connected

- testcase: # next test case...
    desc: 'TEST TC2'

Then I have the following code:

#include <iostream>
#include <unistd.h>
#include <ctime>
#include "yaml-cpp/yaml.h"

using namespace std;

void load_config()
{
    cout << "loading yaml file\n";

    YAML::Node testfile = YAML::LoadFile("BDD-test-case.yaml");
    if (testfile["testcases"])
    {
        cout << "found testfile[\"testcases\"] - size: " << testfile["testcases"].size() << "\n";
        YAML::Node testcases = testfile["testcases"];

        for (int i = 0; i < testcases.size(); i++)
        {
            YAML::Node testcase = testcases[0];
            cout << "testcase " << i << ":\n";
            cout << "\tsize:" << testcase.size() << "\n";
            cout << "\tIsNull:" << testcase.IsNull() << "\n";

            YAML::Node desc = testcase["desc"];
            if (desc)
            {
                std::string desc_str = desc.as<std::string>();
                cout << "desc: " << desc_str.c_str() << "\n";
            }
        }
    }
    cout << "yaml - done\n";
}

int main(int argc, char *argv[])
{
    long double lastTimeMS = time(0)*1000;
    long double CurrTimeMS = time(0)*1000;
    int localPort = 31010;
    int remotePort = 31011;
    int procId = 1;

    load_config();

    cout << "Hello World!" << endl;
    return 0;
}

I get the following output:

loading yaml file
found testfile["testcases"] - size: 2
testcase 0:
        size:1
        IsNull:0
testcase 1:
        size:1
        IsNull:0
yaml - done
Hello World!

I am trying to parse through each testcase and print out the value of "desc" field.

As you can see from the debug I am able to find the top level node testcases. I check its size and it is 2 - which is correct. Then I loop through the testcases and store each as a new node testcase = testcases[i]. So far so good. However now I want to see what is inside each testcase - I should find a tsetcase["desc"]... but I don't see that. Infact testcase seems to be null...

What am I doing wrong here?

1 Answer 1

2

When you look up:

YAML::Node desc = testcase["desc"];

You're trying to look for the desc key in the following node:

testcase:
  desc: 'TEST TC1'
  requirement-ref: Doors-10.1.1.0
  given:
    text: 'A UUT, TEST2 and TEST are connected'
    devices:
    - Device:
        Type: UUT
        Status: Connected
    - Device:
        Type: TEST
        Status: Connected
    - Device:
        Type: TEST2
        Status: Connected

But this node has a top-level map key testcase. You probably didn't intend this; I think if you simply remove that key, then your doc will look like you're intending.

Update

The fixed YAML (removed the extra level of 'tags' that are not intended):

testcases:
-   desc: 'TEST TC1'
    requirement-ref: Doors-10.1.1.0
    given:
      text: 'A UUT, TEST2 and TEST are connected'
      devices:
      -   Type: UUT
          Status: Connected
      -   Type: TEST
          Status: Connected
      -   Type: TEST2
          Status: Connected

-   desc: 'TEST TC2'
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for that; I am not quite sure what you mean... To clarify, I want "desc" to be a sub-element of "testcase". So a "testcase" is defined as something that has a description "desc", a requirement reference "requirement-ref" and an object called "given" which will itself contain sub-elements. I looked at the JSON output of what I wrote in YAML and I am fairly sure it is what I want.... but the issue is that "testcase" appears to be null :(
The path to "desc" in your document is ["testcases"][0]["testcase"]["desc"]. You're trying to read it as ["testcases"][0]["desc"].
Ahhh....I see. So maybe my yaml is wrong as you say - the node "testcase" seems not required, but then I end up with a line that only has a - dash in it... seems odd. +1 for now, I will test it when I get to work tomorrow and upvote the answer too, thanks! :)
Yes that worked nicely thanks :) ... added an update on your answer to show the fixed yaml code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.