1 #ifndef READ_WARNINGS_H_
2 #define READ_WARNINGS_H_
3 
4 #include "cpp11/data_frame.hpp"
5 #include "cpp11/sexp.hpp"
6 #include "cpp11/strings.hpp"
7 #include <string>
8 #include <vector>
9 
10 class Warnings {
11   std::vector<int> row_, col_;
12   std::vector<std::string> expected_, actual_;
13 
14 public:
Warnings()15   Warnings() {}
16 
17   // row and col should be zero-indexed. addWarning converts into one-indexed
addWarning(int row,int col,const std::string & expected,const std::string & actual)18   void addWarning(
19       int row,
20       int col,
21       const std::string& expected,
22       const std::string& actual) {
23     row_.push_back(row == -1 ? NA_INTEGER : row + 1);
24     col_.push_back(col == -1 ? NA_INTEGER : col + 1);
25     expected_.push_back(expected);
26     actual_.push_back(actual);
27   }
28 
addAsAttribute(cpp11::sexp x)29   cpp11::sexp addAsAttribute(cpp11::sexp x) {
30     if (size() == 0)
31       return x;
32 
33     x.attr("problems") = asDataFrame();
34     return x;
35   }
36 
size()37   size_t size() { return row_.size(); }
38 
clear()39   void clear() {
40     row_.clear();
41     col_.clear();
42     expected_.clear();
43     actual_.clear();
44   }
45 
asDataFrame()46   cpp11::data_frame asDataFrame() {
47     using namespace cpp11::literals;
48 
49     cpp11::writable::data_frame out(
50         {"row"_nm = row_,
51          "col"_nm = col_,
52          "expected"_nm = expected_,
53          "actual"_nm = actual_});
54     out.attr("class") = {"tbl_df", "tbl", "data.frame"};
55 
56     return static_cast<SEXP>(out);
57   }
58 };
59 
60 #endif
61