tests/testthat/test_r_parser.R

context("test R parser")
test_that("get_refs_in_script", {
  # Test the handling of NULL call name from the :: operator.
  refs <- get_refs_in_script("dplyr::mutate_if(df1, bit64::is.integer64, as.integer)", after_pipe = FALSE)
  expect_equal(refs, c('df1'))
  # Test the handling of NULL call name from the :: operator, after_pipe case.
  refs <- get_refs_in_script("dplyr::mutate_if(bit64::is.integer64, as.integer)", after_pipe = TRUE)
  expect_equal(refs, NULL)
  refs <- get_refs_in_script("select(cyl)")
  expect_equal(refs, NULL)
  refs <- get_refs_in_script("mutate(cyl2 = cyl)")
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('mutate(cyl2 = !!external[["cyl"]])')
  expect_equal(refs, c('external'))
  refs <- get_refs_in_script("library(MASS)", after_pipe = FALSE) # library()
  expect_equal(refs, NULL)
  refs <- get_refs_in_script("library(MASS)", after_pipe = TRUE) # library() in after-pipe expression.
  expect_equal(refs, NULL)
  refs <- get_refs_in_script("cyl1 <- external1\ncyl2 <- external2", after_pipe = FALSE) # multi-expressions, non-after-pipe.
  expect_equal(refs, c('external1', 'external2')) # cyl1, cyl2 should not be picked up as references.
  refs <- get_refs_in_script("cyl1 = external1\ncyl2 = external2", after_pipe = FALSE) # multi-expressions, non-after-pipe.
  expect_equal(refs, c('external1', 'external2')) # cyl1, cyl2 should not be picked up as references.
  refs <- get_refs_in_script("mutate(cyl2 = \nexternal$cyl)") # multi-line
  expect_equal(refs, c('external'))
  refs <- get_refs_in_script("df1 %>% mutate(a1 = x1)\ndf2 %>% select(a2)", after_pipe = FALSE) # multi-expressions, non-after-pipe case 2.
  expect_equal(refs, c('df1', 'df2'))
  refs <- get_refs_in_script("mutate(df1, a1 = x1)\nselect(df2, a2)", after_pipe = FALSE) # multi-expressions, non-after-pipe case 3.
  expect_equal(refs, c('df1', 'df2'))
  refs <- get_refs_in_script("mutate(cyl2 = \u5916\u90e8$cyl)") # "external" in Japanese.
  expect_equal(refs, c('\u5916\u90e8'))
  refs <- get_refs_in_script("mutate(cyl2 = `\u5916\u90e8`$cyl)") # With backticks.
  expect_equal(refs, c('\u5916\u90e8'))
  refs <- get_refs_in_script("mutate(") # With parse error.
  expect_equal(refs, NULL)
  refs <- get_refs_in_script("") # With parse error (empty input).
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('build_lm(y~x+z)')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('build_lr(y~x+z)')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('top_n(10, y)')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('distinct(y, color, .keep_all = TRUE) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('get_dupes(color, country) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('drop_na(x, y) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('fill(x, y, .direction = "down") ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('exp_balance(b) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('one_hot(color) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_cor(x, y) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_dist(y, x) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_cosine_sim.kv(color, country) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_survfit(x, b) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_market_impact(time, value, market, target_market="US") ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_cmdscale(pair.name.x, pair.name.y, value) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_svd(y, x, b) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_prophet(created_at, , 2, time_unit = \"day\") ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_prophet(created_at, , 2, time_unit = \"day\", holidays = `domo`) ')
  expect_equal(refs, c('domo'))
  refs <- get_refs_in_script('exp_ts_cluster(created_at, , screen_name, time_unit = \"day\", na_fill_type = c(\"extend\", \"previous\", \"extend\"), distance = \"dtw2\", centroid = \"dba\", normalize = \"center_and_scale\") ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_anomaly_detection(created_at, time_unit = "day") ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('exp_bayes_ab(ab, xy, NULL, type = "summary") ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('prediction(data="new_data", data_frame=domo) ')
  expect_equal(refs, c('domo'))
  refs <- get_refs_in_script('prediction_binary(data="new_data", data_frame=`domo`) ')
  expect_equal(refs, c('domo'))
  refs <- get_refs_in_script('evaluate_regression(predicted_value, y) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('evaluate_binary(predicted_probability, b, threshold = "f_score") ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('evaluate_multi(predicted_label, color) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_roc(predicted_probability, b) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('build_multinom(color ~ x + country, test_rate = 0.3) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_t.test(x) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_var.test(x) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_chisq.test(x) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('build_lm(x) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('build_lr(x) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('build_multinom(x) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('build_glm(x) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('build_model(x) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('build_coxph(x) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_tokenize(x) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_ngram(x) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('do_tf_idf(x) ')
  expect_equal(refs, NULL)
  refs <- get_refs_in_script('pair_count(x) ')
  expect_equal(refs, NULL)
})
exploratory-io/exploratory_func documentation built on April 23, 2024, 9:15 p.m.