Skip to content

Commit aab7a87

Browse files
committed
Fix logical output; add better JsRenderCell tests
1 parent 1b67723 commit aab7a87

File tree

4 files changed

+140
-19
lines changed

4 files changed

+140
-19
lines changed

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
"dotenv",
1313
"endregion",
1414
"Fuzzysort",
15+
"gramm",
16+
"grammarly",
17+
"Hasher",
1518
"healthcheck",
1619
"healthchecks",
1720
"hljs",

quadratic-core/src/controller/send_render.rs

+33-16
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,11 @@ impl GridController {
173173
mod test {
174174
use crate::{
175175
controller::{transaction_types::JsCodeResult, GridController},
176-
grid::{js_types::JsHtmlOutput, RenderSize, SheetId},
177-
wasm_bindings::js::expect_js_call,
176+
grid::{
177+
js_types::{JsHtmlOutput, JsRenderCell},
178+
RenderSize, SheetId,
179+
},
180+
wasm_bindings::js::{expect_js_call, hash_test},
178181
};
179182
use serial_test::serial;
180183

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

188191
gc.set_cell_value((0, 0, sheet_id).into(), "test 1".to_string(), None);
192+
let result = vec![JsRenderCell {
193+
x: 0,
194+
y: 0,
195+
language: None,
196+
value: "test 1".to_string(),
197+
special: None,
198+
align: None,
199+
wrap: None,
200+
bold: None,
201+
italic: None,
202+
text_color: None,
203+
}];
204+
let result = serde_json::to_string(&result).unwrap();
189205
expect_js_call(
190206
"jsRenderCellSheets",
191-
format!(
192-
"{},{},{}",
193-
sheet_id,
194-
0,
195-
0,
196-
//r#"[{"x":0,"y":0,"value":"test 1","special":null}]"#
197-
),
207+
format!("{},{},{},{}", sheet_id, 0, 0, hash_test(&result)),
198208
true,
199209
);
200210

201211
gc.set_cell_value((100, 100, sheet_id).into(), "test 2".to_string(), None);
212+
let result = vec![JsRenderCell {
213+
x: 100,
214+
y: 100,
215+
language: None,
216+
value: "test 2".to_string(),
217+
special: None,
218+
align: None,
219+
wrap: None,
220+
bold: None,
221+
italic: None,
222+
text_color: None,
223+
}];
224+
let result = serde_json::to_string(&result).unwrap();
202225
expect_js_call(
203226
"jsRenderCellSheets",
204-
format!(
205-
"{},{},{}",
206-
sheet_id,
207-
6,
208-
3,
209-
//r#"[{"x":100,"y":100,"value":"test 2","special":null}]"#
210-
),
227+
format!("{},{},{},{}", sheet_id, 6, 3, hash_test(&result)),
211228
true,
212229
);
213230
}

quadratic-core/src/grid/sheet/rendering.rs

+77
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ impl Sheet {
7575
} else {
7676
None
7777
};
78+
if matches!(value, CellValue::Logical(_)) {
79+
let special = match value {
80+
CellValue::Logical(true) => Some(JsRenderCellSpecial::True),
81+
CellValue::Logical(false) => Some(JsRenderCellSpecial::False),
82+
_ => None,
83+
};
84+
return JsRenderCell {
85+
x,
86+
y,
87+
value: "".to_string(),
88+
language,
89+
align,
90+
wrap: None,
91+
bold: None,
92+
italic: None,
93+
text_color: None,
94+
special,
95+
};
96+
}
7897
JsRenderCell {
7998
x,
8099
y,
@@ -434,6 +453,7 @@ mod tests {
434453
js_types::{JsHtmlOutput, JsRenderCell, JsRenderCellSpecial, JsRenderCodeCell},
435454
Bold, CellAlign, CodeCellLanguage, CodeRun, CodeRunResult, Italic, RenderSize, Sheet,
436455
},
456+
wasm_bindings::js::{expect_js_call, hash_test},
437457
CellValue, CodeCellValue, Pos, Rect, RunError, RunErrorMsg, SheetPos, Value,
438458
};
439459

@@ -845,4 +865,61 @@ mod tests {
845865
})
846866
);
847867
}
868+
869+
#[test]
870+
fn render_bool_on_code_run() {
871+
let mut gc = GridController::test();
872+
let sheet_id = gc.sheet_ids()[0];
873+
874+
gc.set_code_cell(
875+
(0, 0, sheet_id).into(),
876+
CodeCellLanguage::Formula,
877+
"{TRUE(), FALSE(), TRUE()}".into(),
878+
None,
879+
);
880+
let cells = vec![
881+
JsRenderCell {
882+
x: 0,
883+
y: 0,
884+
value: "".to_string(),
885+
language: Some(CodeCellLanguage::Formula),
886+
align: None,
887+
wrap: None,
888+
bold: None,
889+
italic: None,
890+
text_color: None,
891+
special: Some(JsRenderCellSpecial::True),
892+
},
893+
JsRenderCell {
894+
x: 1,
895+
y: 0,
896+
value: "".to_string(),
897+
language: None,
898+
align: None,
899+
wrap: None,
900+
bold: None,
901+
italic: None,
902+
text_color: None,
903+
special: Some(JsRenderCellSpecial::False),
904+
},
905+
JsRenderCell {
906+
x: 2,
907+
y: 0,
908+
value: "".to_string(),
909+
language: None,
910+
align: None,
911+
wrap: None,
912+
bold: None,
913+
italic: None,
914+
text_color: None,
915+
special: Some(JsRenderCellSpecial::True),
916+
},
917+
];
918+
let cells_string = serde_json::to_string(&cells).unwrap();
919+
expect_js_call(
920+
"jsRenderCellSheets",
921+
format!("{},{},{},{}", sheet_id, 0, 0, hash_test(&cells_string)),
922+
true,
923+
);
924+
}
848925
}

quadratic-core/src/wasm_bindings/js.rs

+27-3
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,42 @@ pub fn jsRunPython(
194194
JsValue::NULL
195195
}
196196

197+
#[cfg(test)]
198+
#[allow(non_snake_case)]
199+
pub fn jsRunJavascript(
200+
transactionId: String,
201+
x: i32,
202+
y: i32,
203+
sheet_id: String,
204+
code: String,
205+
) -> JsValue {
206+
TEST_ARRAY.lock().unwrap().push(TestFunction::new(
207+
"jsRunJavascript",
208+
format!("{},{},{},{},{}", transactionId, x, y, sheet_id, code),
209+
));
210+
JsValue::NULL
211+
}
212+
213+
#[cfg(test)]
214+
pub fn hash_test<T: std::hash::Hash>(value: &T) -> u64 {
215+
use std::hash::{DefaultHasher, Hasher};
216+
let mut hasher = DefaultHasher::new();
217+
value.hash(&mut hasher);
218+
hasher.finish()
219+
}
220+
197221
#[cfg(test)]
198222
#[allow(non_snake_case)]
199223
pub fn jsRenderCellSheets(
200224
sheet_id: String,
201225
hash_x: i64,
202226
hash_y: i64,
203-
_cells: String, /*Vec<JsRenderCell>*/
227+
cells: String, /*Vec<JsRenderCell>*/
204228
) {
205-
// cells gets too large to store in the test array
229+
// we use a hash of cells to avoid storing too large test data
206230
TEST_ARRAY.lock().unwrap().push(TestFunction::new(
207231
"jsRenderCellSheets",
208-
format!("{},{},{}", sheet_id, hash_x, hash_y),
232+
format!("{},{},{},{}", sheet_id, hash_x, hash_y, hash_test(&cells)),
209233
));
210234
}
211235

0 commit comments

Comments
 (0)