@@ -3,9 +3,8 @@ enable_vdiffr <- grepl("true", Sys.getenv("VDIFFR"), fixed = TRUE)
3
3
4
4
message(" Visual testing is " , if (! enable_vdiffr ) " not " , " enabled." )
5
5
6
- # start up the image server and let vdiffr's svg writing method know about it
6
+ # start up the orca image server
7
7
if (enable_vdiffr ) {
8
-
9
8
# try 20 random ports
10
9
for (vdiff_port_tries in 1 : 20 ) {
11
10
port <- floor(runif(1 , 3001 , 8000 ))
@@ -18,52 +17,66 @@ if (enable_vdiffr) {
18
17
})
19
18
if (success ) break
20
19
}
21
-
22
- # define logic for writing svg in vdiffr
23
- write_plotly_svg <- function (p , file , title ) {
24
- # before exporting, specify trace[i].uid so resulting svg is deterministic
25
- # https://github.com/plotly/orca/issues/133
26
- p <- plotly_build(p )
27
- uid_data <- paste0(" -vdiffr-plotly-" , seq_along(p $ x $ data ))
28
- p $ x $ data <- Map(function (tr , id ) { tr $ uid <- id ; tr }, p $ x $ data , uid_data )
29
-
30
- # write svg to disk
31
- # NOTE TO SELF: yes, it would be great to use `orca_serve()` here, but it gives
32
- # slightly different results from `orca()` (ordering of attributes are different)
33
- # and `orca_serve()` doesn't seem to run reliably everywhere
34
- owd <- setwd(dirname(file ))
35
- on.exit(setwd(owd ))
36
- # NOTE: the dimensions here should match the server args part of xvfb-run
37
- orcaServer $ export(p , file = basename(file ), width = 640 , height = 480 )
38
-
39
- # strip out non-deterministic fullLayout.uid
40
- # TODO: if and when plotly provides an API to pre-specify, use it!
41
- svg_txt <- readLines(file , warn = FALSE )
42
- strextract <- function (str , pattern ) regmatches(str , regexpr(pattern , str ))
43
- def <- strextract(svg_txt , ' defs id=\\ "defs-[[:alnum:]]+\\ "' )
44
- uid <- sub(" defs-" , " " , strextract(def , " defs-[[:alnum:]]+" ))
45
- svg_txt <- gsub(uid , " " , svg_txt , fixed = TRUE )
46
- writeLines(svg_txt , file )
47
- }
48
-
49
20
}
50
21
51
22
expect_doppelganger <- function (p , name , ... ) {
52
23
53
- if (enable_vdiffr ) {
54
- # some plots have random characteristics, so make sure we always have the same seed,
55
- # otherwise comparing svg produces false positives
56
- set.seed(555 )
57
- if (ggplot2 :: is.ggplot(p )) p <- ggplotly(p )
58
- vdiffr :: expect_doppelganger(name , p , ... , writer = write_plotly_svg )
59
- } else {
60
- invisible (NULL )
24
+ if (! enable_vdiffr ) {
25
+ return (invisible (NULL ))
61
26
}
62
27
28
+ testthat :: local_edition(3 )
29
+
30
+ # some plots have random characteristics, so make sure we always have the same seed,
31
+ # otherwise comparing svg produces false positives
32
+ set.seed(555 )
33
+
34
+ name <- str_standardise(name )
35
+ file <- paste0(name , " .svg" )
36
+ path <- tempfile(file , fileext = " .svg" )
37
+ write_plotly_svg(p , path )
38
+ testthat :: expect_snapshot_file(path = path , name = file , cran = FALSE )
63
39
}
64
40
65
41
# run visual test and return 'built' data/layout
66
42
expect_doppelganger_built <- function (p , name , ... ) {
67
43
expect_doppelganger(p , name , ... )
68
44
plotly_build(p )$ x [c(" data" , " layout" )]
69
45
}
46
+
47
+
48
+ # define logic for writing svg
49
+ write_plotly_svg <- function (p , file ) {
50
+ # before exporting, specify trace[i].uid so resulting svg is deterministic
51
+ # https://github.com/plotly/orca/issues/133
52
+ p <- plotly_build(p )
53
+ uid_data <- paste0(" -vdiffr-plotly-" , seq_along(p $ x $ data ))
54
+ p $ x $ data <- Map(function (tr , id ) { tr $ uid <- id ; tr }, p $ x $ data , uid_data )
55
+
56
+ # write svg to disk
57
+ # NOTE TO SELF: yes, it would be great to use `orca_serve()` here, but it gives
58
+ # slightly different results from `orca()` (ordering of attributes are different)
59
+ # and `orca_serve()` doesn't seem to run reliably everywhere
60
+ owd <- setwd(dirname(file ))
61
+ on.exit(setwd(owd ))
62
+ # NOTE: the dimensions here should match the server args part of xvfb-run
63
+ orcaServer $ export(p , file = basename(file ), width = 640 , height = 480 )
64
+
65
+ # strip out non-deterministic fullLayout.uid
66
+ # TODO: if and when plotly provides an API to pre-specify, use it!
67
+ svg_txt <- readLines(file , warn = FALSE )
68
+ strextract <- function (str , pattern ) regmatches(str , regexpr(pattern , str ))
69
+ def <- strextract(svg_txt , ' defs id=\\ "defs-[[:alnum:]]+\\ "' )
70
+ uid <- sub(" defs-" , " " , strextract(def , " defs-[[:alnum:]]+" ))
71
+ svg_txt <- gsub(uid , " " , svg_txt , fixed = TRUE )
72
+ writeLines(svg_txt , file )
73
+ }
74
+
75
+ # copied from vdiffr
76
+ str_standardise <- function (s , sep = " -" ) {
77
+ stopifnot(rlang :: is_scalar_character(s ))
78
+ s <- gsub(" [^a-z0-9]" , sep , tolower(s ))
79
+ s <- gsub(paste0(sep , sep , " +" ), sep , s )
80
+ s <- gsub(paste0(" ^" , sep , " |" , sep , " $" ), " " , s )
81
+ s
82
+ }
0 commit comments