Skip to content

Commit

Permalink
Merge pull request #1309 from quadratichq/fix-bool-code-output
Browse files Browse the repository at this point in the history
Fix logical output for code cells
  • Loading branch information
davidkircos committed Apr 24, 2024
2 parents 1b67723 + aab7a87 commit f4a7374
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"dotenv",
"endregion",
"Fuzzysort",
"gramm",
"grammarly",
"Hasher",
"healthcheck",
"healthchecks",
"hljs",
Expand Down
49 changes: 33 additions & 16 deletions quadratic-core/src/controller/send_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,11 @@ impl GridController {
mod test {
use crate::{
controller::{transaction_types::JsCodeResult, GridController},
grid::{js_types::JsHtmlOutput, RenderSize, SheetId},
wasm_bindings::js::expect_js_call,
grid::{
js_types::{JsHtmlOutput, JsRenderCell},
RenderSize, SheetId,
},
wasm_bindings::js::{expect_js_call, hash_test},
};
use serial_test::serial;

Expand All @@ -186,28 +189,42 @@ mod test {
gc.sheet_mut(gc.sheet_ids()[0]).id = sheet_id;

gc.set_cell_value((0, 0, sheet_id).into(), "test 1".to_string(), None);
let result = vec![JsRenderCell {
x: 0,
y: 0,
language: None,
value: "test 1".to_string(),
special: None,
align: None,
wrap: None,
bold: None,
italic: None,
text_color: None,
}];
let result = serde_json::to_string(&result).unwrap();
expect_js_call(
"jsRenderCellSheets",
format!(
"{},{},{}",
sheet_id,
0,
0,
//r#"[{"x":0,"y":0,"value":"test 1","special":null}]"#
),
format!("{},{},{},{}", sheet_id, 0, 0, hash_test(&result)),
true,
);

gc.set_cell_value((100, 100, sheet_id).into(), "test 2".to_string(), None);
let result = vec![JsRenderCell {
x: 100,
y: 100,
language: None,
value: "test 2".to_string(),
special: None,
align: None,
wrap: None,
bold: None,
italic: None,
text_color: None,
}];
let result = serde_json::to_string(&result).unwrap();
expect_js_call(
"jsRenderCellSheets",
format!(
"{},{},{}",
sheet_id,
6,
3,
//r#"[{"x":100,"y":100,"value":"test 2","special":null}]"#
),
format!("{},{},{},{}", sheet_id, 6, 3, hash_test(&result)),
true,
);
}
Expand Down
77 changes: 77 additions & 0 deletions quadratic-core/src/grid/sheet/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ impl Sheet {
} else {
None
};
if matches!(value, CellValue::Logical(_)) {
let special = match value {
CellValue::Logical(true) => Some(JsRenderCellSpecial::True),
CellValue::Logical(false) => Some(JsRenderCellSpecial::False),
_ => None,
};
return JsRenderCell {
x,
y,
value: "".to_string(),
language,
align,
wrap: None,
bold: None,
italic: None,
text_color: None,
special,
};
}
JsRenderCell {
x,
y,
Expand Down Expand Up @@ -434,6 +453,7 @@ mod tests {
js_types::{JsHtmlOutput, JsRenderCell, JsRenderCellSpecial, JsRenderCodeCell},
Bold, CellAlign, CodeCellLanguage, CodeRun, CodeRunResult, Italic, RenderSize, Sheet,
},
wasm_bindings::js::{expect_js_call, hash_test},
CellValue, CodeCellValue, Pos, Rect, RunError, RunErrorMsg, SheetPos, Value,
};

Expand Down Expand Up @@ -845,4 +865,61 @@ mod tests {
})
);
}

#[test]
fn render_bool_on_code_run() {
let mut gc = GridController::test();
let sheet_id = gc.sheet_ids()[0];

gc.set_code_cell(
(0, 0, sheet_id).into(),
CodeCellLanguage::Formula,
"{TRUE(), FALSE(), TRUE()}".into(),
None,
);
let cells = vec![
JsRenderCell {
x: 0,
y: 0,
value: "".to_string(),
language: Some(CodeCellLanguage::Formula),
align: None,
wrap: None,
bold: None,
italic: None,
text_color: None,
special: Some(JsRenderCellSpecial::True),
},
JsRenderCell {
x: 1,
y: 0,
value: "".to_string(),
language: None,
align: None,
wrap: None,
bold: None,
italic: None,
text_color: None,
special: Some(JsRenderCellSpecial::False),
},
JsRenderCell {
x: 2,
y: 0,
value: "".to_string(),
language: None,
align: None,
wrap: None,
bold: None,
italic: None,
text_color: None,
special: Some(JsRenderCellSpecial::True),
},
];
let cells_string = serde_json::to_string(&cells).unwrap();
expect_js_call(
"jsRenderCellSheets",
format!("{},{},{},{}", sheet_id, 0, 0, hash_test(&cells_string)),
true,
);
}
}
30 changes: 27 additions & 3 deletions quadratic-core/src/wasm_bindings/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,42 @@ pub fn jsRunPython(
JsValue::NULL
}

#[cfg(test)]
#[allow(non_snake_case)]
pub fn jsRunJavascript(
transactionId: String,
x: i32,
y: i32,
sheet_id: String,
code: String,
) -> JsValue {
TEST_ARRAY.lock().unwrap().push(TestFunction::new(
"jsRunJavascript",
format!("{},{},{},{},{}", transactionId, x, y, sheet_id, code),
));
JsValue::NULL
}

#[cfg(test)]
pub fn hash_test<T: std::hash::Hash>(value: &T) -> u64 {
use std::hash::{DefaultHasher, Hasher};
let mut hasher = DefaultHasher::new();
value.hash(&mut hasher);
hasher.finish()
}

#[cfg(test)]
#[allow(non_snake_case)]
pub fn jsRenderCellSheets(
sheet_id: String,
hash_x: i64,
hash_y: i64,
_cells: String, /*Vec<JsRenderCell>*/
cells: String, /*Vec<JsRenderCell>*/
) {
// cells gets too large to store in the test array
// we use a hash of cells to avoid storing too large test data
TEST_ARRAY.lock().unwrap().push(TestFunction::new(
"jsRenderCellSheets",
format!("{},{},{}", sheet_id, hash_x, hash_y),
format!("{},{},{},{}", sheet_id, hash_x, hash_y, hash_test(&cells)),
));
}

Expand Down

0 comments on commit f4a7374

Please sign in to comment.