1
1
context(" check grid grob" )
2
2
3
- skip_on_cran()
4
- skip_if_not_installed(" doconv" )
5
- skip_if_not(doconv :: msoffice_available())
6
- library(doconv )
7
3
library(officer )
8
4
library(gdtools )
5
+
9
6
register_liberationsans()
10
7
11
8
init_flextable_defaults()
12
- set_flextable_defaults(font.family = " Liberation Sans" )
9
+
10
+ set_flextable_defaults(
11
+ font.family = " Liberation Sans" ,
12
+ border.color = " #333333" )
13
+
14
+
15
+ test_that(" png is created" , {
16
+ ft <- as_flextable(iris )
17
+ file <- tempfile(fileext = " .png" )
18
+ try(invisible (save_as_image(x = ft , path = file , res = 150 )),
19
+ silent = TRUE )
20
+ expect_true(file.exists(file ))
21
+ expect_gt(file.info(file )$ size , 20000 )
22
+ })
13
23
14
24
test_that(" merged borders" , {
15
25
local_edition(3 )
@@ -24,12 +34,36 @@ test_that("merged borders", {
24
34
border = fp_border(color = " red" )
25
35
)
26
36
27
- path <- save_as_image(ft , path = tempfile(fileext = " .png" ), res = 150 )
28
- expect_snapshot_doc(name = " vmerged-borders" , x = path , engine = " testthat" )
37
+ gr <- gen_grob(ft )
38
+
39
+ expect_length(gr $ children , 10 )
40
+
41
+ expect_equal(gr $ children [[3 ]]$ children $ borders $ children [[1 ]]$ gp $ col , " red" )
42
+
43
+ expect_length(gr $ children [[1 ]]$ children $ borders $ children , 2 )
44
+ expect_equal(gr $ children [[1 ]]$ children $ borders $ children [[1 ]]$ gp $ col , " #333333" )
45
+ expect_equal(gr $ children [[1 ]]$ children $ borders $ children [[1 ]]$ x0 , grid :: unit(0 , " npc" ))
46
+ expect_equal(gr $ children [[1 ]]$ children $ borders $ children [[1 ]]$ x1 , grid :: unit(1 , " npc" ))
47
+ expect_equal(gr $ children [[1 ]]$ children $ borders $ children [[1 ]]$ y0 , grid :: unit(1 , " npc" ))
48
+ expect_equal(gr $ children [[1 ]]$ children $ borders $ children [[1 ]]$ y1 , grid :: unit(1 , " npc" ))
49
+
50
+ expect_equal(gr $ children [[1 ]]$ children $ borders $ children [[2 ]]$ gp $ col , " #333333" )
51
+ expect_equal(gr $ children [[1 ]]$ children $ borders $ children [[2 ]]$ x0 , grid :: unit(0 , " npc" ))
52
+ expect_equal(gr $ children [[1 ]]$ children $ borders $ children [[2 ]]$ x1 , grid :: unit(1 , " npc" ))
53
+ expect_equal(gr $ children [[1 ]]$ children $ borders $ children [[2 ]]$ y0 , grid :: unit(0 , " npc" ))
54
+ expect_equal(gr $ children [[1 ]]$ children $ borders $ children [[2 ]]$ y1 , grid :: unit(0 , " npc" ))
55
+
56
+ expect_length(gr $ children [[10 ]]$ children $ borders $ children , 1 )
57
+
58
+ expect_equal(gr $ children [[10 ]]$ children $ borders $ children [[1 ]]$ gp $ col , " #333333" )
59
+ expect_equal(gr $ children [[10 ]]$ children $ borders $ children [[1 ]]$ x0 , grid :: unit(0 , " npc" ))
60
+ expect_equal(gr $ children [[10 ]]$ children $ borders $ children [[1 ]]$ x1 , grid :: unit(1 , " npc" ))
61
+ expect_equal(gr $ children [[10 ]]$ children $ borders $ children [[1 ]]$ y0 , grid :: unit(0 , " npc" ))
62
+ expect_equal(gr $ children [[10 ]]$ children $ borders $ children [[1 ]]$ y1 , grid :: unit(0 , " npc" ))
63
+
29
64
})
30
65
31
66
test_that(" text wrapping" , {
32
- local_edition(3 )
33
67
34
68
text <- " Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
35
69
source1 <- " DATA_SOURCE_A.COURSE_TITLE\n DATA_SOURCE_A.SUBJECT_DESCR\n DATA_SOURCE_A.CATALOG_NUMBER"
@@ -45,6 +79,52 @@ test_that("text wrapping", {
45
79
ft <- flextable(temp_dat )
46
80
ft <- merge_h(ft , part = " body" )
47
81
48
- path <- save_as_image(ft , path = tempfile(fileext = " .png" ), res = 150 )
49
- expect_snapshot_doc(name = " long-text-wrapping" , x = path , engine = " testthat" )
82
+ gr <- gen_grob(ft , fit = " fixed" )
83
+
84
+ expect_length(gr $ children , 9 )
85
+ expect_equal(gr $ children [[5 ]]$ children $ contents $ ftgrobs [[1 ]]$ label , source1 )
86
+ expect_equal(gr $ children [[6 ]]$ children $ contents $ ftgrobs [[1 ]]$ label , source2 )
87
+
88
+ # check wrap on 3 rows
89
+ expect_length(gr $ children [[5 ]]$ children $ contents $ children , 3 )
90
+ expect_length(gr $ children [[6 ]]$ children $ contents $ children , 3 )
91
+ expect_equal(gr $ children [[8 ]]$ children $ contents $ ftgrobs [[1 ]]$ label , " Notes" )
92
+ expect_length(gr $ children [[8 ]]$ children $ contents $ children , 1 )
93
+ expect_equal(gr $ children [[9 ]]$ children $ contents $ ftgrobs [[1 ]]$ label , text )
94
+ # check wrap on 3 rows
95
+ expect_length(gr $ children [[9 ]]$ children $ contents $ children , 3 )
96
+
97
+ # check that height and width are greater than those of smaller cells
98
+ expect_gt(gr $ children $ cell_2_2 $ children $ contents $ ftpar $ height ,
99
+ gr $ children $ cell_1_2 $ children $ contents $ ftpar $ height
100
+ )
101
+ expect_gt(gr $ children $ cell_2_2 $ children $ contents $ ftpar $ width ,
102
+ gr $ children $ cell_2_1 $ children $ contents $ ftpar $ width
103
+ )
104
+ })
105
+
106
+ test_that(" grid with raster" , {
107
+ skip_if_not_installed(" magick" )
108
+
109
+ img.file <- file.path(
110
+ R.home(" doc" ),
111
+ " html" , " logo.jpg"
112
+ )
113
+ myft <- flextable(head(iris ))
114
+ myft <- prepend_chunks(
115
+ x = myft ,
116
+ i = 1 : 2 , j = 1 ,
117
+ as_image(src = img.file ),
118
+ part = " body"
119
+ )
120
+ ft <- autofit(myft )
121
+
122
+ gr <- gen_grob(ft )
123
+
124
+ expect_s3_class(gr $ children [[6 ]]$ children $ contents $ ftgrobs [[1 ]], " rastergrob" )
125
+ expect_s3_class(gr $ children [[6 ]]$ children $ contents $ ftgrobs [[2 ]], " text" )
126
+ expect_s3_class(gr $ children [[11 ]]$ children $ contents $ ftgrobs [[1 ]], " rastergrob" )
127
+ expect_s3_class(gr $ children [[11 ]]$ children $ contents $ ftgrobs [[2 ]], " text" )
128
+ expect_s3_class(gr $ children [[12 ]]$ children $ contents $ ftgrobs [[1 ]], " text" )
50
129
})
130
+
0 commit comments