Skip to content

Commit

Permalink
Test and Fix for comparing long quote expressions (rstudio#34)
Browse files Browse the repository at this point in the history
* tdd for bugfix

* find the other user_code == solution_code "bug"

* use more generic test case w/o packages

* user rlang::sim() instead of quote
  • Loading branch information
chendaniely authored and schloerke committed Jul 15, 2019
1 parent 2ac1820 commit e6ad96f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
^codecov\.yml$
^man-roxygen$
^\.lintr$
^\.vscode$
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.vscode/

# History files
.Rhistory
.Rapp.history
Expand Down
2 changes: 1 addition & 1 deletion R/check_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ code_is_same <- function(user = NULL, solution = NULL) {
solution_code <- rlang::get_expr(solution)

# Correct answers are all alike
if (suppressWarnings(user_code == solution_code)) {
if (identical(user_code, solution_code)) {
return(graded(correct = TRUE, message = NULL))
}

Expand Down
3 changes: 2 additions & 1 deletion R/detect_mistakes.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ detect_mistakes <- function(user,

# Does the user code not match the solution code?
if (length(user[[i]]) != length(solution[[i]]) ||
user[[i]] != solution[[i]]) {
!identical(user[[i]], solution[[i]])
) {
return(isolate_mismatch(user, solution, i))
}
}
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test_check_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ expect_correct <- function(x) {
expect_s3_class(x, "grader_graded")
expect_true(x$correct)
}

expect_wrong <- function(x) {
expect_s3_class(x, "grader_graded")
expect_false(x$correct)
}

expect_message <- function(x, message) {
expect_s3_class(x, "grader_graded")
expect_true(!x$correct)
Expand Down Expand Up @@ -197,3 +203,20 @@ test_that("Spot differences when pipes are involved", {
expect_correct(check_code(grader_args = list(user_quo = pipe3, solution_quo = pipe3)))

})

test_that("Spots differences in long calls", {
# original discussion here:
# https://github.com/rstudio-education/grader/issues/28

user <- rlang::sym("tidyr::gather(key = key, value = value, new_sp_m014:newrel_f65, na.rm = TRUE)") # nolint
solution <- rlang::sym("tidyr::gather(key = key, value = value, new_sp_m014:newrel_f65, na.rm = FALSE)") # nolint
expect_wrong(
check_code(grader_args = list(user_quo = user, solution_quo = solution))
)

user <- rlang::sym("tidyr::gather(key = key, value = value, new_sp_m014:newrel_f65, na.rm = TRUE)") # nolint
solution <- rlang::sym("tidyr::gather(key = key, value = value, new_sp_m014:newrel_f65, na.rm = TRUE)") # nolint
expect_correct(
check_code(grader_args = list(user_quo = user, solution_quo = solution))
)
})

0 comments on commit e6ad96f

Please sign in to comment.