Skip to content

Commit

Permalink
Add additional tests for weighted functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lewinfox committed Oct 2, 2023
1 parent 5793e8f commit f5f109f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Suggests:
knitr,
pkgdown,
rmarkdown,
stringi,
styler,
testthat
VignetteBuilder:
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-weighted.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,26 @@ test_that("`lev_weighted_token_ratio()` handles zero-weighted tokens correctly",
lev_weighted_token_ratio("bill", "jill")
)
})

test_that("weighted functions score identical strings as 1", {
expect_equal(lev_weighted_token_ratio("abc def", "abc def"), 1)
expect_equal(lev_weighted_token_set_ratio("abc def", "abc def"), 1)
expect_equal(lev_weighted_token_set_ratio("abc def", "abc def"), 1)
})

test_that("weighted functions score entirely dissimilar strings as 0", {
expect_equal(lev_weighted_token_ratio("abc def", "ghi jkl"), 0)
expect_equal(lev_weighted_token_set_ratio("abc def", "ghi jkl"), 0)
expect_equal(lev_weighted_token_set_ratio("abc def", "ghi jkl"), 0)
})

test_that("weighted function outputs are always between 0 and 1", {
between_0_and_1 <- function(x) x >= 0 && x <= 1
for (i in seq(1, 50)) {
random_para_a <- stringi::stri_rand_lipsum(1)
random_para_b <- stringi::stri_rand_lipsum(1)
for (fn in c(lev_weighted_token_ratio, lev_weighted_token_sort_ratio, lev_weighted_token_set_ratio)) {
expect_true(between_0_and_1(fn(random_para_a, random_para_b)))
}
}
})

0 comments on commit f5f109f

Please sign in to comment.