@@ -40,3 +40,76 @@ test_that("Write function test for dataset input", {
4040
4141 expect_equal(y , z )
4242})
43+
44+ test_that(" write_docx creates DOCX file from RTF encoding" , {
45+ skip_on_os(" windows" ) # rtf_convert_format only supports Unix/Linux/macOS
46+ skip_if_not(
47+ nzchar(Sys.which(" libreoffice" )) ||
48+ nzchar(Sys.which(" libreoffice24.8" )) ||
49+ nzchar(Sys.which(" libreoffice7.6" )),
50+ " LibreOffice is not installed"
51+ )
52+
53+ # Create RTF encoding
54+ rtf <- head(iris ) | >
55+ rtf_body() | >
56+ rtf_encode()
57+
58+ # Write to DOCX
59+ docx_file <- file.path(tempdir(), " test_table.docx" )
60+ result <- write_docx(rtf , docx_file )
61+
62+ # Verify file was created
63+ expect_true(file.exists(docx_file ))
64+ expect_equal(result , docx_file )
65+
66+ # Clean up
67+ unlink(docx_file )
68+ })
69+
70+ test_that(" write_docx creates output directory if needed" , {
71+ skip_on_os(" windows" ) # rtf_convert_format only supports Unix/Linux/macOS
72+ skip_if_not(
73+ nzchar(Sys.which(" libreoffice" )) ||
74+ nzchar(Sys.which(" libreoffice24.8" )) ||
75+ nzchar(Sys.which(" libreoffice7.6" )),
76+ " LibreOffice is not installed"
77+ )
78+
79+ # Create RTF encoding
80+ rtf <- head(cars ) | >
81+ rtf_body() | >
82+ rtf_encode()
83+
84+ # Create nested path
85+ nested_path <- file.path(tempdir(), " test_output_dir" , " nested" , " test.docx" )
86+
87+ # Write to DOCX
88+ result <- write_docx(rtf , nested_path )
89+
90+ # Verify directory and file were created
91+ expect_true(dir.exists(dirname(nested_path )))
92+ expect_true(file.exists(nested_path ))
93+
94+ # Clean up
95+ unlink(file.path(tempdir(), " test_output_dir" ), recursive = TRUE )
96+ })
97+
98+ test_that(" write_docx fails gracefully when LibreOffice is not available" , {
99+ skip_on_os(" windows" ) # rtf_convert_format only supports Unix/Linux/macOS
100+ skip_if(
101+ nzchar(Sys.which(" libreoffice" )) ||
102+ nzchar(Sys.which(" libreoffice24.8" )) ||
103+ nzchar(Sys.which(" libreoffice7.6" )),
104+ " Skip if LibreOffice IS installed"
105+ )
106+
107+ rtf <- head(iris ) | >
108+ rtf_body() | >
109+ rtf_encode()
110+
111+ expect_error(
112+ write_docx(rtf , file.path(tempdir(), " test.docx" )),
113+ " Can't find libreoffice"
114+ )
115+ })
0 commit comments