Skip to content

Commit

Permalink
Test is_complete_request() (#37)
Browse files Browse the repository at this point in the history
* test for complete and incomplete code samples

* using |> and _

* expose is_complete_request()

* test invalid code samples
  • Loading branch information
romainfrancois authored Nov 14, 2023
1 parent c8a6ac9 commit 51169dd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions share/jupyter/kernels/xr/resources/routines.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ kernel_info_request <- function() {
clear_output <- function(wait = FALSE) {
invisible(.Call("xeusr_clear_output", isTRUE(wait), PACKAGE = "(embedding)"))
}

is_complete_request <- function(code) {
.Call("xeusr_is_complete_request", code, PACKAGE = "(embedding)")
}
10 changes: 10 additions & 0 deletions src/routines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ SEXP clear_output(SEXP wait_) {
return R_NilValue;
}

SEXP is_complete_request(SEXP code_) {
std::string code = CHAR(STRING_ELT(code_, 0));
auto is_complete = xeus_r::get_interpreter()->is_complete_request(code);

SEXP out = PROTECT(Rf_mkString(is_complete.dump(4).c_str()));
Rf_classgets(out, Rf_mkString("json"));
UNPROTECT(1);
return out;
}

}

Expand All @@ -95,6 +104,7 @@ void register_r_routines() {
{"xeusr_display_data" , (DL_FUNC) &routines::display_data , 2},
{"xeusr_update_display_data" , (DL_FUNC) &routines::update_display_data , 2},
{"xeusr_clear_output" , (DL_FUNC) &routines::clear_output , 1},
{"xeusr_is_complete_request" , (DL_FUNC) &routines::is_complete_request , 1},

{NULL, NULL, 0}
};
Expand Down
1 change: 1 addition & 0 deletions src/xinterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ nl::json interpreter::is_complete_request_impl(const std::string& code)
return xeus::create_is_complete_reply("incomplete", "");

case PARSE_ERROR:
Rprintf("invalid");
return xeus::create_is_complete_reply("invalid", "");
}
}
Expand Down
7 changes: 4 additions & 3 deletions test/test_xr_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ def _execute_code(self, code, tests=True, silent=False, store_history=True):
code_hello_world = "cat('hello, world')"
# code_page_something = "?cat"
completion_samples = [{"text": "H", "matches": {"Hello", "Hey", "Howdy"}}]
#complete_code_samples = ["hello, world"]
code_execute_result = [{"code": "6*7", "result": "[1] 42"}]
#incomplete_code_samples = ["incomplete"]
#invalid_code_samples = ["invalid"]
#code_inspect_sample = "print"

complete_code_samples = ["fun()", "1 + 2", "a %>% b", "a |> b()", "a |> b(c = _)"]
incomplete_code_samples = ["fun(", "1 + "]
invalid_code_samples = ["fun())"]

def test_stdout(self):
self.flush_channels()
reply, output_msgs = self.execute_helper(code="cat('hello, world')")
Expand Down

0 comments on commit 51169dd

Please sign in to comment.