Skip to content

Commit

Permalink
Merge pull request #716 from rundel/issue701
Browse files Browse the repository at this point in the history
Handle non finite edge cases with pluralization()
  • Loading branch information
gaborcsardi authored Aug 28, 2024
2 parents 8d707ba + b53d79c commit b14d61b
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 5 deletions.
17 changes: 12 additions & 5 deletions R/pluralize.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ NULL
make_quantity <- function(object) {
val <- if (is.numeric(object)) {
stopifnot(length(object) == 1)
as.integer(object)

if (is.finite(object))
as.integer(object)
else
object
} else {
length(object)
}
Expand Down Expand Up @@ -87,13 +91,16 @@ process_plural <- function(qty, code) {
parts <- strsplit(str_tail(code), "/", fixed = TRUE)[[1]]
if (last_character(code) == "/") parts <- c(parts, "")
if (length(parts) == 1) {
if (qty != 1) parts[1] else ""
if (is.finite(qty) & qty == 1) "" else parts[1]
} else if (length(parts) == 2) {
if (qty == 1) parts[1] else parts[2]
if (is.finite(qty) & qty == 1)
parts[1]
else
parts[2]
} else if (length(parts) == 3) {
if (qty == 0) {
if (is.finite(qty) & qty == 0) {
parts[1]
} else if (qty == 1) {
} else if (is.finite(qty) & qty == 1) {
parts[2]
} else {
parts[3]
Expand Down
93 changes: 93 additions & 0 deletions tests/testthat/_snaps/pluralization.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,96 @@
Output
9 word

# issue 701

Code
print(pluralize("{NA} file{?s} expected"))
Output
NA file expected
Code
print(pluralize("{NA_character_} file{?s} expected"))
Output
NA file expected
Code
print(pluralize("{NA_real_} file{?s} expected"))
Output
NA files expected
Code
print(pluralize("{NA_integer_} file{?s} expected"))
Output
NA files expected
Code
print(pluralize("{NaN} file{?s} expected"))
Output
NaN files expected
Code
print(pluralize("{Inf} file{?s} expected"))
Output
Inf files expected
Code
print(pluralize("{-Inf} file{?s} expected"))
Output
-Inf files expected

---

Code
print(pluralize("Found {NA} director{?y/ies}."))
Output
Found NA directory.
Code
print(pluralize("Found {NA_character_} director{?y/ies}."))
Output
Found NA directory.
Code
print(pluralize("Found {NA_real_} director{?y/ies}."))
Output
Found NA directories.
Code
print(pluralize("Found {NA_integer_} director{?y/ies}."))
Output
Found NA directories.
Code
print(pluralize("Found {NaN} director{?y/ies}."))
Output
Found NaN directories.
Code
print(pluralize("Found {Inf} director{?y/ies}."))
Output
Found Inf directories.
Code
print(pluralize("Found {-Inf} director{?y/ies}."))
Output
Found -Inf directories.

---

Code
print(pluralize("Will remove {?no/the/the} {NA} package{?s}."))
Output
Will remove the NA package.
Code
print(pluralize("Will remove {?no/the/the} {NA_character_} package{?s}."))
Output
Will remove the NA package.
Code
print(pluralize("Will remove {?no/the/the} {NA_real_} package{?s}."))
Output
Will remove the NA packages.
Code
print(pluralize("Will remove {?no/the/the} {NA_integer_} package{?s}."))
Output
Will remove the NA packages.
Code
print(pluralize("Will remove {?no/the/the} {NaN} package{?s}."))
Output
Will remove the NaN packages.
Code
print(pluralize("Will remove {?no/the/the} {Inf} package{?s}."))
Output
Will remove the Inf packages.
Code
print(pluralize("Will remove {?no/the/the} {-Inf} package{?s}."))
Output
Will remove the -Inf packages.

41 changes: 41 additions & 0 deletions tests/testthat/test-pluralization.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,44 @@ test_that("issue 158", {
print(pluralize("{9} word{?A/B/}"))
})
})

test_that("Edge cases for pluralize() (#701)", {
expect_snapshot({
# Should not be pluralized
print(pluralize("{NA} file{?s} expected"))
print(pluralize("{NA_character_} file{?s} expected"))

# Should be pluralized
print(pluralize("{NA_real_} file{?s} expected"))
print(pluralize("{NA_integer_} file{?s} expected"))
print(pluralize("{NaN} file{?s} expected"))
print(pluralize("{Inf} file{?s} expected"))
print(pluralize("{-Inf} file{?s} expected"))
})

expect_snapshot({
# Should not be pluralized
print(pluralize("Found {NA} director{?y/ies}."))
print(pluralize("Found {NA_character_} director{?y/ies}."))

# Should be pluralized
print(pluralize("Found {NA_real_} director{?y/ies}."))
print(pluralize("Found {NA_integer_} director{?y/ies}."))
print(pluralize("Found {NaN} director{?y/ies}."))
print(pluralize("Found {Inf} director{?y/ies}."))
print(pluralize("Found {-Inf} director{?y/ies}."))
})

expect_snapshot({
# Should not be pluralized
print(pluralize("Will remove {?no/the/the} {NA} package{?s}."))
print(pluralize("Will remove {?no/the/the} {NA_character_} package{?s}."))

# Should be pluralized
print(pluralize("Will remove {?no/the/the} {NA_real_} package{?s}."))
print(pluralize("Will remove {?no/the/the} {NA_integer_} package{?s}."))
print(pluralize("Will remove {?no/the/the} {NaN} package{?s}."))
print(pluralize("Will remove {?no/the/the} {Inf} package{?s}."))
print(pluralize("Will remove {?no/the/the} {-Inf} package{?s}."))
})
})

0 comments on commit b14d61b

Please sign in to comment.