Skip to content

Commit

Permalink
tests: luau qsv_writejson tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jqnatividad committed Dec 25, 2024
1 parent 18cc63e commit ea3592f
Showing 1 changed file with 37 additions and 60 deletions.
97 changes: 37 additions & 60 deletions tests/test_luau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2951,7 +2951,8 @@ fn luau_writejson_simple() {
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("result")
.arg(r#"
.arg(
r#"
BEGIN {
-- Create a table to store
local data = {
Expand All @@ -2968,86 +2969,61 @@ BEGIN {
}!
return "ok"
"#)
"#,
)
.arg("data.csv");

wrk.assert_success(&mut cmd);

// Verify the compact JSON output
let json_content = std::fs::read_to_string(wrk.path("output.json")).unwrap();
let json: serde_json::Value = serde_json::from_str(&json_content).unwrap();

assert_eq!(json!("{\"numbers\":[13,24,72,7],\"nested\":{\"key\":\"value\",\"array\":[1,2,3]},\"letters\":[\"a\",\"b\",\"c\",\"d\"]}"), json);
assert_eq!(
json_content,
r#"{"numbers":[13,24,72,7],"nested":{"key":"value","array":[1,2,3]},"letters":["a","b","c","d"]}"#
);

// Verify the pretty JSON output has newlines and indentation
let pretty_content = std::fs::read_to_string(wrk.path("output_pretty.json")).unwrap();
let expected = r#""{\"numbers\":[13,24,72,7],\"nested\":{\"key\":\"value\",\"array\":[1,2,3]},\"letters\":[\"a\",\"b\",\"c\",\"d\"]}""#;

assert_eq!(pretty_content, expected);

// The actual content should be the same when parsed
let pretty_json: serde_json::Value = serde_json::from_str(&pretty_content).unwrap();
assert_eq!(json, pretty_json);
}

#[test]
fn luau_writejson_error_cases() {
let wrk = Workdir::new("luau_writejson_errors");
wrk.create(
"data.csv",
vec![
svec!["letter", "number"],
svec!["a", "13"],
],
);

// Test invalid filename
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("result")
.arg(r#"
BEGIN {
qsv_writejson({test = "value"}, "", false)
}!
return "should not get here"
"#)
.arg("data.csv");

wrk.assert_err(&mut cmd);

// Test writing to a directory that doesn't exist
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("result")
.arg(r#"
BEGIN {
qsv_writejson({test = "value"}, "nonexistent/dir/file.json", false)
}!
return "should not get here"
"#)
.arg("data.csv");
let expected = r#"{
"numbers": [
13,
24,
72,
7
],
"nested": {
"key": "value",
"array": [
1,
2,
3
]
},
"letters": [
"a",
"b",
"c",
"d"
]
}"#;

wrk.assert_err(&mut cmd);
assert_eq!(pretty_content, expected);
}

#[test]
fn luau_writejson_special_values() {
let wrk = Workdir::new("luau_writejson_special");
wrk.create(
"data.csv",
vec![
svec!["letter", "number"],
svec!["a", "13"],
],
vec![svec!["letter", "number"], svec!["a", "13"]],
);

// Test writing various Lua types
let mut cmd = wrk.command("luau");
cmd.arg("map")
.arg("result")
.arg(r#"
.arg(
r#"
BEGIN {
local data = {
null_value = nil,
Expand All @@ -3063,15 +3039,16 @@ BEGIN {
}!
return "ok"
"#)
"#,
)
.arg("data.csv");

wrk.assert_success(&mut cmd);

// Verify the JSON output
let json_content = std::fs::read_to_string(wrk.path("special.json")).unwrap();
let json: serde_json::Value = serde_json::from_str(&json_content).unwrap();

assert!(json["null_value"].is_null());
assert_eq!(json["boolean_true"], json!(true));
assert_eq!(json["boolean_false"], json!(false));
Expand Down

0 comments on commit ea3592f

Please sign in to comment.