| Marc Kupietz | 7cf697f | 2026-09-05 10:47:53 +0200 | [diff] [blame] | 1 | # Snippets come in several shapes, and matching them as a whole used to require |
| 2 | # one particular one, silently dropping the rest (issue #14). |
| 3 | |
| 4 | snippetOf <- function(left, match, right) { |
| 5 | paste0( |
| 6 | '<span class="context-left">', left, '</span>', |
| 7 | '<span class="match">', match, '</span>', |
| 8 | '<span class="context-right">', right, '</span>' |
| 9 | ) |
| 10 | } |
| 11 | |
| 12 | freqOf <- function(snippet, ...) { |
| 13 | RKorAPClient:::snippet2FreqTable( |
| 14 | snippet, |
| 15 | oldTable = dplyr::tibble(word = character(0), frequency = numeric(0)), |
| 16 | verbose = FALSE, |
| 17 | ... |
| 18 | ) |
| 19 | } |
| 20 | |
| 21 | test_that("the words of an ordinary snippet are counted", { |
| 22 | result <- freqOf(snippetOf("der grosse alte Baum ", "<mark>steht</mark>", " im tiefen dunklen Wald")) |
| 23 | expect_true(all(c("Baum", "Wald") %in% result$word)) |
| 24 | }) |
| 25 | |
| 26 | test_that("a snippet cut at the sentence boundary is counted", { |
| 27 | # <span class="cutted"> appears inside the match of contains(<base/s=s>, ...) |
| 28 | # queries, which used to make the whole snippet be dropped |
| 29 | cut <- snippetOf( |
| 30 | 'ein linker Kontext hier ', |
| 31 | '<mark>Treffer</mark><span class="cutted"></span>', |
| 32 | ' und rechts weiter' |
| 33 | ) |
| 34 | result <- freqOf(cut) |
| 35 | expect_true(all(c("Kontext", "rechts") %in% result$word)) |
| 36 | }) |
| 37 | |
| 38 | test_that("a snippet with an empty context is counted on its other side", { |
| 39 | # a match filling the whole sentence leaves one context span empty |
| 40 | result <- freqOf(snippetOf("", "<mark>Treffer</mark>", " nur rechts etwas")) |
| 41 | expect_true("rechts" %in% result$word) |
| 42 | |
| 43 | result <- freqOf(snippetOf("nur links etwas ", "<mark>Treffer</mark>", "")) |
| 44 | expect_true("links" %in% result$word) |
| 45 | }) |
| 46 | |
| 47 | test_that("the more markers are not counted as words", { |
| 48 | result <- freqOf(snippetOf( |
| 49 | '<span class="more"></span>links davon ', |
| 50 | "<mark>Treffer</mark>", |
| 51 | ' rechts davon<span class="more"></span>' |
| 52 | )) |
| 53 | expect_true(all(c("links", "rechts") %in% result$word)) |
| 54 | expect_false(any(grepl("span|more|class", result$word))) |
| 55 | }) |
| 56 | |
| 57 | test_that("both contexts empty yields no words rather than an error", { |
| 58 | expect_equal(nrow(freqOf(snippetOf("", "<mark>Treffer</mark>", ""))), 0) |
| 59 | }) |
| 60 | |
| 61 | test_that("a snippet without the expected spans is skipped", { |
| 62 | expect_equal(nrow(freqOf("<span class=\"nothing\">kein KWIC</span>")), 0) |
| 63 | }) |
| 64 | |
| 65 | test_that("findExample survives a fetch that returned no snippet column", { |
| 66 | # after a failed request collectedMatches has no snippet column at all, so |
| 67 | # that the example was character(0) and the assignment aborted with |
| 68 | # "replacement has length zero" |
| 69 | kco <- methods::new( |
| 70 | "KorAPConnection", |
| 71 | apiUrl = "https://example.invalid/", |
| 72 | KorAPUrl = "https://example.invalid/", |
| 73 | authorizationSupported = FALSE, |
| 74 | verbose = FALSE |
| 75 | ) |
| 76 | |
| 77 | testthat::local_mocked_bindings( |
| 78 | corpusQuery = function(...) methods::new("KorAPQuery", korapConnection = kco, totalResults = 1), |
| 79 | fetchNext = function(q, ...) q, |
| 80 | .package = "RKorAPClient" |
| 81 | ) |
| 82 | |
| 83 | expect_equal(RKorAPClient:::findExample(kco, query = "irgendwas"), "") |
| 84 | }) |