vendredi 5 novembre 2021

How to hard code or declare the content of a file in std::string

I have the following content in a CSV file

Column1, Column2, Column3, Column4, Column5 
something, false, somemore, 1.010000, 1.020000

Above CSV file content is returned as std::string from a method of my library.

std::string GetCsvContent() {
    // Create csv content
    return csv_content;
}

Now I want to unit test GetCsvContent. I know what it will return for a certain input. But to be able to test I need to predeclare the CSV content in a std::string. Something like below.

std::string expected_csv_content = "Column1, Column2, Column3, Column4, Column5\nsomething, false, somemore, 1.010000, 1.020000";
EXPECT_EQ(expected_csv_content, my_library_object.GetCsvContent());

But above way of declaring expected_csv_content is not correct because it is not exactly matching the format of following data.

Column1, Column2, Column3, Column4, Column5 
something, false, somemore, 1.010000, 1.020000

Is there a way to declare in R notation like below?

const std::string expected_csv_content = R"(
    Column1, Column2, Column3, Column4, Column5
    something, false, somemore, 1.010000, 1.020000
)";

Question:
My question is how to I hard code or declare the content of a CSV or any small file in a std::string for comparison if I want to?

Aucun commentaire:

Enregistrer un commentaire