101 lines
4.2 KiB
C++
101 lines
4.2 KiB
C++
#include "tests/test_framework.hpp"
|
|
|
|
#include "engine/io_csv.hpp"
|
|
#include "engine/io_native.hpp"
|
|
#include "engine/workbook.hpp"
|
|
|
|
#include <string>
|
|
|
|
using namespace htab;
|
|
|
|
TEST_CASE("Phase4: RFC 4180 CSV import/export round-trip and spills") {
|
|
const std::string csv_in =
|
|
"\xEF\xBB\xBF"
|
|
"Name,Value,Note\r\n"
|
|
"\"Smith, John\",10,\"He said \"\"Hi\"\"\"\r\n"
|
|
"\"Line1\nLine2\",25,TRUE\r\n"
|
|
"Total,=SUM(B2:B3),\"=IF(B4>30, \"\"Big\"\", \"\"Small\"\")\"\r\n";
|
|
|
|
Workbook wb;
|
|
std::string err;
|
|
CHECK(import_csv_string(wb, 0, csv_in, &err));
|
|
CHECK_EQ(wb.get_scalar_value(0, 1, 0).as_string(), std::string("Smith, John"));
|
|
CHECK_NEAR(wb.get_scalar_value(0, 1, 1).as_number(), 10.0, 1e-12);
|
|
CHECK_EQ(wb.get_scalar_value(0, 1, 2).as_string(), std::string("He said \"Hi\""));
|
|
CHECK_EQ(wb.get_scalar_value(0, 2, 0).as_string(), std::string("Line1\nLine2"));
|
|
CHECK(wb.get_scalar_value(0, 2, 2).as_bool());
|
|
CHECK_NEAR(wb.get_scalar_value(0, 3, 1).as_number(), 35.0, 1e-12);
|
|
CHECK_EQ(wb.get_scalar_value(0, 3, 2).as_string(), std::string("Big"));
|
|
|
|
// Add a FILTER spill in column E (index 4)
|
|
wb.set_cell_input(0, 0, 4, "=FILTER(A2:B3, B2:B3>15)");
|
|
CHECK_EQ(wb.get_scalar_value(0, 0, 4).as_string(), std::string("Line1\nLine2"));
|
|
CHECK_NEAR(wb.get_scalar_value(0, 0, 5).as_number(), 25.0, 1e-12);
|
|
|
|
// Export evaluated values (default)
|
|
std::string csv_vals = export_csv_string(wb, 0, false);
|
|
Workbook wb2;
|
|
CHECK(import_csv_string(wb2, 0, csv_vals, &err));
|
|
CHECK_EQ(wb2.get_scalar_value(0, 1, 0).as_string(), std::string("Smith, John"));
|
|
CHECK_EQ(wb2.get_scalar_value(0, 1, 2).as_string(), std::string("He said \"Hi\""));
|
|
CHECK_EQ(wb2.get_scalar_value(0, 2, 0).as_string(), std::string("Line1\nLine2"));
|
|
CHECK_NEAR(wb2.get_scalar_value(0, 3, 1).as_number(), 35.0, 1e-12);
|
|
CHECK_EQ(wb2.get_scalar_value(0, 3, 2).as_string(), std::string("Big"));
|
|
CHECK_NEAR(wb2.get_scalar_value(0, 0, 5).as_number(), 25.0, 1e-12);
|
|
|
|
// Export raw formulas and re-import -> formula & spill intact
|
|
std::string csv_forms = export_csv_string(wb, 0, true);
|
|
Workbook wb3;
|
|
CHECK(import_csv_string(wb3, 0, csv_forms, &err));
|
|
CHECK_EQ(wb3.get_cell(0, 3, 1)->raw_input, std::string("=SUM(B2:B3)"));
|
|
CHECK_NEAR(wb3.get_scalar_value(0, 3, 1).as_number(), 35.0, 1e-12);
|
|
CHECK_EQ(wb3.get_cell(0, 0, 4)->raw_input, std::string("=FILTER(A2:B3, B2:B3>15)"));
|
|
CHECK_NEAR(wb3.get_scalar_value(0, 0, 5).as_number(), 25.0, 1e-12);
|
|
|
|
// Reject malformed CSV
|
|
Workbook bad_wb;
|
|
CHECK(!import_csv_string(bad_wb, 0, "\"unterminated", &err));
|
|
CHECK(!err.empty());
|
|
}
|
|
|
|
TEST_CASE("Phase4: Native workbook JSON serialization round-trip") {
|
|
Workbook wb;
|
|
wb.rename_sheet(0, "Summary");
|
|
int s2 = wb.add_sheet("Raw Data");
|
|
wb.set_active_sheet(1);
|
|
|
|
wb.set_col_width(0, 0, 120);
|
|
wb.set_row_height(0, 1, 28);
|
|
wb.set_cell_input(0, 0, 0, "Total:");
|
|
wb.set_cell_input(0, 0, 1, "=SUM('Raw Data'!B1:B3)");
|
|
wb.set_cell_format(0, 0, 1, "$#,##0.00");
|
|
wb.set_cell_input(0, 1, 0, "=FILTER('Raw Data'!A1:B3, 'Raw Data'!B1:B3>=20)");
|
|
|
|
wb.set_cell_input(s2, 0, 0, "Alpha"); wb.set_cell_input(s2, 0, 1, "10");
|
|
wb.set_cell_input(s2, 1, 0, "Beta"); wb.set_cell_input(s2, 1, 1, "20");
|
|
wb.set_cell_input(s2, 2, 0, "Gamma"); wb.set_cell_input(s2, 2, 1, "30");
|
|
|
|
std::string json = save_native_string(wb);
|
|
|
|
Workbook loaded;
|
|
std::string err;
|
|
CHECK(load_native_string(loaded, json, &err));
|
|
CHECK_EQ(loaded.sheet_count(), 2);
|
|
CHECK_EQ(loaded.active_sheet(), 1);
|
|
CHECK_EQ(loaded.sheet(0).name, std::string("Summary"));
|
|
CHECK_EQ(loaded.sheet(1).name, std::string("Raw Data"));
|
|
CHECK_EQ(loaded.get_col_width(0, 0, 80), 120);
|
|
CHECK_EQ(loaded.get_row_height(0, 1, 20), 28);
|
|
CHECK_EQ(loaded.get_cell(0, 0, 1)->raw_input, std::string("=SUM('Raw Data'!B1:B3)"));
|
|
CHECK_EQ(loaded.get_formatted_value(0, 0, 1), std::string("$60.00"));
|
|
CHECK_EQ(loaded.get_scalar_value(0, 1, 0).as_string(), std::string("Beta"));
|
|
CHECK_NEAR(loaded.get_scalar_value(0, 1, 1).as_number(), 20.0, 1e-12);
|
|
CHECK_EQ(loaded.get_scalar_value(0, 2, 0).as_string(), std::string("Gamma"));
|
|
CHECK_NEAR(loaded.get_scalar_value(0, 2, 1).as_number(), 30.0, 1e-12);
|
|
|
|
// Corrupted JSON rejected cleanly
|
|
Workbook bad;
|
|
CHECK(!load_native_string(bad, "{bad json", &err));
|
|
CHECK(!err.empty());
|
|
}
|