ODF_TEST/tests/basic_test.cpp

119 lines
2.4 KiB
C++

/**
* @file basic_test.cpp
* @brief 使用标准库 assert 对 ODF 核心类进行基本单元测试。
*/
#include "app.hpp"
#include <cassert>
#include <sstream>
#include <string>
/**
* @brief 测试 TextRun 的构造和基本访问器。
*/
static void test_text_run() {
using namespace odf;
TextRun plain("hello");
assert(plain.text() == "hello");
assert(!plain.is_bold());
assert(!plain.is_italic());
TextRun bold("bold", true, false);
assert(bold.text() == "bold");
assert(bold.is_bold());
assert(!bold.is_italic());
TextRun italic("italic", false, true);
assert(italic.text() == "italic");
assert(!italic.is_bold());
assert(italic.is_italic());
TextRun both("both", true, true);
assert(both.text() == "both");
assert(both.is_bold());
assert(both.is_italic());
}
/**
* @brief 测试 TextRun::write 输出格式。
*/
static void test_text_run_write() {
using namespace odf;
std::ostringstream oss;
TextRun("plain").write(oss);
assert(oss.str() == "plain");
oss.str("");
TextRun("b", true, false).write(oss);
assert(oss.str() == "*b*");
oss.str("");
TextRun("i", false, true).write(oss);
assert(oss.str() == "_i_");
oss.str("");
TextRun("bi", true, true).write(oss);
assert(oss.str() == "***bi***");
}
/**
* @brief 测试 Paragraph 的 add_run 和 write。
*/
static void test_paragraph() {
using namespace odf;
Paragraph p;
p.add_run(TextRun("A "));
p.add_run(TextRun("B", true, false));
assert(p.runs().size() == 2);
std::ostringstream oss;
p.write(oss);
assert(oss.str() == "A *B*\n");
}
/**
* @brief 测试 Document 的 add_paragraph、write 和 to_string。
*/
static void test_document() {
using namespace odf;
Document doc;
assert(doc.paragraphs().empty());
{
Paragraph p;
p.add_run(TextRun("Line1"));
doc.add_paragraph(std::move(p));
}
{
Paragraph p;
p.add_run(TextRun("Line2"));
doc.add_paragraph(std::move(p));
}
assert(doc.paragraphs().size() == 2);
std::ostringstream oss;
doc.write(oss);
assert(oss.str() == "Line1\nLine2\n");
// to_string 应与 write 结果一致
assert(doc.to_string() == "Line1\nLine2\n");
}
/**
* @brief 主测试入口。
*/
int main() {
test_text_run();
test_text_run_write();
test_paragraph();
test_document();
return 0;
}