Skip to content

Commit

Permalink
cxGet unit tests were fixed to match new cxGet. on failure, we get an…
Browse files Browse the repository at this point in the history
… error, not false
  • Loading branch information
reginaldford committed Jun 30, 2024
1 parent 738f387 commit a25939c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
25 changes: 16 additions & 9 deletions src/main/engine/sm_ast_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,12 @@ sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *sf) {
if (key->my_type == SM_ERR_TYPE)
return (sm_object *)key;
char *result = getenv(&key->content);
if (!result)
return (sm_object *)sms_false;
if (!result) {
sm_symbol *title = sm_new_symbol("osGetEnvFailed", 14);
sm_string *message =
sm_new_fstring_at(sms_heap, "Failed to get environment variable: %s", &key->content);
return (sm_object *)sm_new_error_from_expr(title, message, sme, NULL);
}
return (sm_object *)sm_new_string(strlen(result), result);
break;
}
Expand Down Expand Up @@ -251,7 +255,9 @@ sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *sf) {
char *path_cstr = &path->content;
if (chdir(path_cstr) == 0)
return (sm_object *)sms_true;
return (sm_object *)sms_false;
sm_symbol *title = sm_new_symbol("cdFailed", 8);
sm_string *message = sm_new_fstring_at(sms_heap, "Failed to change directory to %s");
return (sm_object *)sm_new_error_from_expr(title, message, sme, NULL);
break;
}
case SM_DATE_STR_EXPR: {
Expand Down Expand Up @@ -361,7 +367,7 @@ sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *sf) {
sm_object *value = (sm_object *)sm_engine_eval(sm_expr_get_arg(sme, 1), current_cx, sf);
// If an error occurred, it is stored in the mapping
sm_cx_let(current_cx, sym, value);
return (sm_object *)sms_true;
return value;
}
case SM_CX_SETPARENT_EXPR: {
sm_cx *cx = (sm_cx *)eager_type_check(sme, 0, SM_CX_TYPE, current_cx, sf);
Expand All @@ -382,9 +388,8 @@ sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *sf) {
if (sym->my_type == SM_ERR_TYPE)
return (sm_object *)sym;
sm_object *value = (sm_object *)sm_engine_eval(sm_expr_get_arg(sme, 2), current_cx, sf);
if (sm_cx_let(cx, sym, value))
return (sm_object *)sms_true;
return (sm_object *)sms_false;
sm_cx_let(cx, sym, value);
return value;
}
case SM_CX_GET_EXPR: {
sm_cx *cx = (sm_cx *)eager_type_check(sme, 0, SM_CX_TYPE, current_cx, sf);
Expand All @@ -396,8 +401,10 @@ sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *sf) {
sm_object *result = sm_cx_get(cx, sym);
if (result)
return result;
else
return (sm_object *)sms_false;
sm_symbol *title = sm_new_symbol("cxGetFailed", 11);
sm_string *message =
sm_new_fstring_at(sms_heap, "cxGet did not find %s in this context.", &sym->name->content);
return (sm_object *)sm_new_error_from_expr(title, message, sme, NULL);
}
case SM_CX_HAS_EXPR: {
sm_cx *cx = (sm_cx *)eager_type_check(sme, 0, SM_CX_TYPE, current_cx, sf);
Expand Down
8 changes: 4 additions & 4 deletions test_zone/cx/0.sms
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
[cxKeys(cx1),[ ex1, ex2 ], "cxKeys"],
[cxValues(cx1),[ "info", "info2" ], "cxValues"],
[cxGet(cx1,:ex2),"info2", "cxGet"],
[cxGet(cx1,:true),false, "cxGet true should fail"],
[isErr(cxGet(cx1,:true)),true, "cxGet true should fail"],
[cxGetFar(cx1,:true),true, "cxGetFar true should succeed"],
[cxGetFar(cx1,:PI),3.141592653589793, "cxGetFar PI"],
[cxSize(cx1),2, "cxSize is 2"],
[cxClear(cx1),true, "cxClear"],
[cxSize(cx1),0, "cxSize is 0"],
[cxKeys(cx1),[], "cxKeys is []"],
[cxValues(cx1),[], "cxValues is []"],
[cxLet(cx1,:a,4),true, "cxLet a 4"],
[cxLet(cx1,:a,4),4, "cxLet a 4"],
[cxGet(cx1,:a),4, "cxGet a"],
[cxGetFar(cx1,:a),4, "cxGetFar a"],
[cxRm(cx1,:a),true, "cxRm a"],
[cxGet(cx1,:a),false, "cxGet a"],
[isErr(cxGet(cx1,:a)),true, "cxGet a should fail"],
[cxGetFar(cx1,:a),false, "cxGetFar a"],
[cxRm(parent(parent(self)),:false),true, "cxRm False"],
[cxGetFar(self,:false),false, "cxGetFar False"],
[cxLet(parent(self),:false,:false),true, "cxLet :false :false"],
[cxLet(parent(self),:false,:false),false, "cxLet :false :false"],
[cxGetFar(self,:false),false, "cxLet :false :false"],
[cxGetFar(self,:false) is false,true, "Same false instance."]
];
Expand Down

0 comments on commit a25939c

Please sign in to comment.