|
1 | | -test_that("Detect RStudio", { |
2 | | - # Obtain value |
3 | | - before = Sys.getenv("RSTUDIO") |
| 1 | +# Tests for Utility Functions |
4 | 2 |
|
5 | | - # Modify check environment variable |
6 | | - Sys.setenv("RSTUDIO" = 1) |
7 | | - expect_true(is_rstudio(), info = "Within RStudio") |
| 3 | +## Test is_rstudio() ---- |
| 4 | +test_that("is_rstudio(): detects RStudio environment correctly", { |
| 5 | + # Save original environment variable |
| 6 | + original_rstudio <- Sys.getenv("RSTUDIO") |
8 | 7 |
|
9 | | - Sys.setenv("RSTUDIO" = 0) |
10 | | - expect_false(is_rstudio(), info = "Not in RStudio") |
| 8 | + # Test when in RStudio |
| 9 | + Sys.setenv("RSTUDIO" = "1") |
| 10 | + expect_true(is_rstudio(), info = "Should detect RStudio when RSTUDIO=1") |
11 | 11 |
|
12 | | - # Restore |
13 | | - Sys.setenv("RSTUDIO" = before) |
| 12 | + # Test when not in RStudio |
| 13 | + Sys.setenv("RSTUDIO" = "0") |
| 14 | + expect_false(is_rstudio(), info = "Should not detect RStudio when RSTUDIO=0") |
| 15 | + |
| 16 | + # Test with empty environment variable |
| 17 | + Sys.setenv("RSTUDIO" = "") |
| 18 | + expect_false(is_rstudio(), info = "Should not detect RStudio when RSTUDIO is empty") |
| 19 | + |
| 20 | + # Restore original environment |
| 21 | + Sys.setenv("RSTUDIO" = original_rstudio) |
| 22 | +}) |
| 23 | + |
| 24 | + |
| 25 | +## Test valid_query() ---- |
| 26 | +test_that("valid_query validates queries correctly", { |
| 27 | + # Valid queries |
| 28 | + expect_true(valid_query("valid query")) |
| 29 | + expect_true(valid_query("a")) |
| 30 | + expect_true(valid_query("query with spaces and numbers 123")) |
| 31 | + expect_true(valid_query("special chars: !@#$%^&*()")) |
| 32 | + |
| 33 | + # Invalid queries |
| 34 | + expect_false(valid_query("")) |
| 35 | + expect_false(valid_query(NULL)) |
| 36 | + expect_false(valid_query(c("multiple", "elements"))) |
| 37 | + expect_false(valid_query(character(0))) |
| 38 | + expect_false(valid_query(NA_character_)) |
| 39 | + expect_false(valid_query(123)) # Not a character |
| 40 | + |
| 41 | + # Test missing argument |
| 42 | + expect_false(valid_query()) |
| 43 | +}) |
| 44 | + |
| 45 | +## Test encode_url() ---- |
| 46 | +test_that("encode_url() creates properly formatted URLs", { |
| 47 | + base_url <- "https://example.com/search?q=" |
| 48 | + |
| 49 | + # Test basic encoding |
| 50 | + result <- encode_url(base_url, "simple query") |
| 51 | + expect_equal(result, "https://example.com/search?q=simple%20query") |
| 52 | + |
| 53 | + # Test with special characters |
| 54 | + result <- encode_url(base_url, "query with & and + symbols") |
| 55 | + expect_true(grepl("%26", result)) # & becomes %26 |
| 56 | + expect_true(grepl("%2B", result)) # + becomes %2B |
| 57 | + expect_true(grepl("%20", result)) # space becomes %20 |
| 58 | + |
| 59 | + # Test with encoded query suffix |
| 60 | + result <- encode_url(base_url, "query", "&type=test") |
| 61 | + expect_equal(result, "https://example.com/search?q=query&type=test") |
| 62 | + |
| 63 | + # Test with empty query |
| 64 | + result <- encode_url(base_url, "") |
| 65 | + expect_equal(result, "https://example.com/search?q=") |
| 66 | + |
| 67 | + # Test with complex characters |
| 68 | + result <- encode_url(base_url, "R [language] test") |
| 69 | + expect_true(grepl("%5B", result)) # [ becomes %5B |
| 70 | + expect_true(grepl("%5D", result)) # ] becomes %5D |
| 71 | +}) |
| 72 | + |
| 73 | +# Test append_search_term_suffix() ---- |
| 74 | +test_that("append_search_term_suffix(): works correctly", { |
| 75 | + # Test with rlang = TRUE (default behavior) |
| 76 | + result <- append_search_term_suffix("test query", rlang = TRUE, suffix = "r programming") |
| 77 | + expect_equal(result, "test query r programming") |
| 78 | + |
| 79 | + # Test with rlang = FALSE |
| 80 | + result <- append_search_term_suffix("test query", rlang = FALSE, suffix = "r programming") |
| 81 | + expect_equal(result, "test query") |
| 82 | + |
| 83 | + # Test with NULL suffix |
| 84 | + result <- append_search_term_suffix("test query", rlang = TRUE, suffix = NULL) |
| 85 | + expect_equal(result, "test query") |
| 86 | + |
| 87 | + # Test with empty suffix |
| 88 | + result <- append_search_term_suffix("test query", rlang = TRUE, suffix = "") |
| 89 | + expect_equal(result, "test query") |
| 90 | + |
| 91 | + # Test with custom suffix |
| 92 | + result <- append_search_term_suffix("test", rlang = TRUE, suffix = "custom") |
| 93 | + expect_equal(result, "test custom") |
| 94 | + |
| 95 | + # Test edge cases |
| 96 | + result <- append_search_term_suffix("", rlang = TRUE, suffix = "r programming") |
| 97 | + expect_equal(result, " r programming") |
| 98 | + |
| 99 | + result <- append_search_term_suffix("test", rlang = FALSE, suffix = "ignored") |
| 100 | + expect_equal(result, "test") |
| 101 | +}) |
| 102 | + |
| 103 | +# Test browse_url() ---- |
| 104 | +test_that("browse_url(): generates correct URLs without opening browser", { |
| 105 | + base_url <- "https://example.com/search?q=" |
| 106 | + query <- "test query" |
| 107 | + |
| 108 | + # Test basic URL generation (open_browser = FALSE) |
| 109 | + expect_message( |
| 110 | + result <- browse_url(base_url, query, open_browser = FALSE), |
| 111 | + "Please type into your browser" |
| 112 | + ) |
| 113 | + expect_equal(result, "https://example.com/search?q=test%20query") |
| 114 | + |
| 115 | + # Test with encoded suffix |
| 116 | + expect_message( |
| 117 | + result <- browse_url(base_url, query, "&type=issues", open_browser = FALSE), |
| 118 | + "Please type into your browser" |
| 119 | + ) |
| 120 | + expect_equal(result, "https://example.com/search?q=test%20query&type=issues") |
| 121 | + |
| 122 | + # Test with special characters |
| 123 | + special_query <- "R [language] & symbols" |
| 124 | + expect_message( |
| 125 | + result <- browse_url(base_url, special_query, open_browser = FALSE), |
| 126 | + "Please type into your browser" |
| 127 | + ) |
| 128 | + expect_true(grepl("%5B", result)) # [ is encoded |
| 129 | + expect_true(grepl("%5D", result)) # ] is encoded |
| 130 | + expect_true(grepl("%26", result)) # & is encoded |
14 | 131 | }) |
0 commit comments