File: test-split_by.R

package info (click to toggle)
r-cran-bookdown 0.46%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,972 kB
  • sloc: javascript: 11,343; makefile: 21; sh: 20
file content (176 lines) | stat: -rw-r--r-- 6,443 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
test_that("invalid split_by produces error", {
  skip_on_cran()
  skip_if_not_pandoc()
  skip_if_not_installed("xml2")

  rmd <- local_rmd_file(
    c("---", "title: test split_by as numeric", "---", "",
      "# CHAPTER 1", "## SECTION 1", "### SUBSECTION 1",
      "#### SUBSUBSECTION 1",
      "", "# CHAPTER 2", "## SECTION 2",
      "##### LEVEL 5", "###### LEVEL 6"
    )
  )
  for (split_by in list(letters, 8, 'numbers+1', -1)){
    expect_error(local_render_book(rmd, output_format = gitbook(split_by = split_by)))
  }
})

test_that("gitbook() correctly splits with a specified level", {

  skip_on_cran()
  skip_if_not_pandoc()
  skip_if_not_installed("xml2")

  rmd <- local_rmd_file(
    c("---", "title: test split_by as numeric", "---", "",
      "# CHAPTER 1", "## SECTION 1", "### SUBSECTION 1",
      "#### SUBSUBSECTION 1",
      "", "# CHAPTER 2", "## SECTION 2",
      "##### LEVEL 5", "###### LEVEL 6"
    )
  )
  expected <- list(
    "0" = list(
      "toc-numbers" = c("1", "2"),
      "html-names-default" = c("", ""),
      "html-names-number" = c("", "")
    ),
    "1" = list(
      "toc-numbers" = c("1", "2"),
      "html-names-default" = c("chapter-1.html", "chapter-2.html"),
      "html-names-number" =  c("1-chapter-1.html", "2-chapter-2.html")
    ),
    "2" = list(
      "toc-numbers" = c("1", "1.1", "2", "2.1"),
      "html-names-default" = c("chapter-1.html", "section-1.html",
                               "chapter-2.html", "section-2.html"),
      "html-names-number" =  c("1-chapter-1.html", "1-1-section-1.html",
                               "2-chapter-2.html", "2-1-section-2.html")
    ),
    "3" = list(
      "toc-numbers" = c("1", "1.1", "1.1.1", "2", "2.1"),
      "html-names-default" = c("chapter-1.html", "section-1.html",
                               "subsection-1.html",
                               "chapter-2.html", "section-2.html"),
      "html-names-number" =  c("1-chapter-1.html", "1-1-section-1.html",
                               "1-1-1-subsection-1.html",
                               "2-chapter-2.html", "2-1-section-2.html")
    ),
    "4" = list(
      "toc-numbers" = c("1", "1.1", "1.1.1", "1.1.1.1", "2", "2.1"),
      "html-names-default" = c("chapter-1.html", "section-1.html",
                               "subsection-1.html", "subsubsection-1.html",
                               "chapter-2.html", "section-2.html"),
      "html-names-number" =  c("1-chapter-1.html", "1-1-section-1.html",
                               "1-1-1-subsection-1.html", "1-1-1-1-subsubsection-1.html",
                               "2-chapter-2.html", "2-1-section-2.html")
    ),
    "5" = list(
      "toc-numbers" = c("1", "1.1", "1.1.1", "1.1.1.1", "2", "2.1", "2.1.0.0.1"),
      "html-names-default" = c("chapter-1.html", "section-1.html",
                               "subsection-1.html", "subsubsection-1.html",
                               "chapter-2.html", "section-2.html",
                               "level-5.html"),
      "html-names-number" =  c("1-chapter-1.html", "1-1-section-1.html",
                               "1-1-1-subsection-1.html", "1-1-1-1-subsubsection-1.html",
                               "2-chapter-2.html", "2-1-section-2.html",
                               "2-1-1-1-1-level-5.html")
    ),
    "6" = list(
      "toc-numbers" = c("1", "1.1", "1.1.1", "1.1.1.1", "2", "2.1", "2.1.0.0.1", "2.1.0.0.1.1"),
      "html-names-default" = c("chapter-1.html", "section-1.html",
                               "subsection-1.html", "subsubsection-1.html",
                               "chapter-2.html", "section-2.html",
                               "level-5.html", "level-6.html"),
      "html-names-number" =  c("1-chapter-1.html", "1-1-section-1.html",
                               "1-1-1-subsection-1.html", "1-1-1-1-subsubsection-1.html",
                               "2-chapter-2.html", "2-1-section-2.html",
                               "2-1-0-0-1-level-5.html", "2-1-0-0-1-1-level-6.html")
    )
  )

  for (split_by in names(expected)) {
    for (toc_depth in seq_len(as.numeric(split_by))) {
      for (with_num in ""){
        html_path <- local_render_book(
          rmd,
          output_format = gitbook(split_by = paste0(split_by, with_num), toc_depth = toc_depth)
        )
        TOC <- xml2::xml_find_all(xml2::read_html(html_path), "//div[@class='book-summary']/nav/ul//li")
        validate_html(html_path)
        expect_equal(
          xml2::xml_attr(TOC, "data-level"),
          intersect(
            expected[[split_by]][["toc-numbers"]],
            expected[[as.character(toc_depth)]][["toc-numbers"]]
          )
        )
        expect_equal(
          xml2::xml_attr(TOC, "data-path"),
          intersect(
            expected[[split_by]][["html-names-default"]],
            expected[[as.character(toc_depth)]][["html-names-default"]]
          )
        )
      }
    }
  }

})

test_that("gitbook() split_by equivalents produce equivalent output", {

  skip_on_cran()
  skip_if_not_pandoc()
  skip_if_not_installed("xml2")

  equivalents <- list(
    #list('none', '0', 0),
    list('chapter', '1', 1),
    list('section', '2', 2)
  )

  rmd <- local_rmd_file(
    c("---", "title: test split_by section", "---", "",
      "# CHAPTER 1", "## SECTION 1", "### SUBSECTION 1",
      "# CHAPTER 2", "## SECTION 2")
  )


  for (i in seq_along(equivalents)) {
    for(j in seq_along(equivalents[[i]])){
      html_path <- local_render_book(
        rmd,
        output_format = gitbook(split_by = equivalents[[i]][[j]], toc_depth = 1)
      )
      validate_html(html_path)
      # using the first content as a baseline, check if each subsequent content matches it
      if(j == 1) content_baseline <- xml2::read_html(html_path) else
        expect_equal(content_baseline, xml2::read_html(html_path))
    }
  }
})

test_that("non-sequential headings produces valid html", {
  skip_on_cran()
  skip_if_not_pandoc()
  skip_if_not_installed("xml2")

  rmd <- local_rmd_file(
    c("---", "title: test split_by as numeric", "---", "",
      "# CHAPTER 1", "## SECTION 1", "### SUBSECTION 1",
      "", "# CHAPTER 2", "## SECTION 2",
      "##### LEVEL 5-1", "###### LEVEL 6-1",
      "# CHAPTER 3",
      "#### SUBSUBSECTION 1",
      "# CHAPTER 3"
    )
  )
  for (split_by in 0:6){
    validate_html(local_render_book(
      rmd,
      output_format = gitbook(split_by = split_by)
    ))
  }
})