Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle non finite edge cases with pluralization() #716

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
gaborcsardi marked this conversation as resolved.
Show resolved Hide resolved
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.
gaborcsardi marked this conversation as resolved.
Show resolved Hide resolved
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("issue 701", {
rundel marked this conversation as resolved.
Show resolved Hide resolved
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}."))
})
})
Loading