Skip to content

Commit d133163

Browse files
committed
Increase test coverage pre-release
1 parent 737ae2a commit d133163

5 files changed

Lines changed: 181 additions & 0 deletions

File tree

src/args2cel_test.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,35 @@ fn test_bool() {
4343
assert!(matches!(vars.get("x").unwrap(), CelValue::Bool(true)));
4444
}
4545

46+
#[test]
47+
fn test_alias_types() {
48+
let args = vec![
49+
("x".to_string(), "i64".to_string(), "42".to_string()),
50+
("y".to_string(), "u64".to_string(), "7".to_string()),
51+
("z".to_string(), "double".to_string(), "2.5".to_string()),
52+
("w".to_string(), "boolean".to_string(), "false".to_string()),
53+
("s".to_string(), "str".to_string(), "hello".to_string()),
54+
];
55+
let vars = args_to_cel_variables(&args).unwrap();
56+
57+
assert!(matches!(vars.get("x").unwrap(), CelValue::Int(42)));
58+
assert!(matches!(vars.get("y").unwrap(), CelValue::UInt(7)));
59+
assert!(matches!(vars.get("z").unwrap(), CelValue::Float(f) if (*f - 2.5).abs() < 0.001));
60+
assert!(matches!(vars.get("w").unwrap(), CelValue::Bool(false)));
61+
assert!(matches!(vars.get("s").unwrap(), CelValue::String(s) if s.as_str() == "hello"));
62+
}
63+
64+
#[test]
65+
fn test_duplicate_args_last_wins() {
66+
let args = vec![
67+
("x".to_string(), "int".to_string(), "1".to_string()),
68+
("x".to_string(), "int".to_string(), "2".to_string()),
69+
];
70+
let vars = args_to_cel_variables(&args).unwrap();
71+
assert_eq!(vars.len(), 1);
72+
assert!(matches!(vars.get("x").unwrap(), CelValue::Int(2)));
73+
}
74+
4675
#[test]
4776
fn test_multiple_args() {
4877
let args = vec![

src/input_handler_test.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ fn test_handle_json_with_args() {
5454
assert!(is_truthy);
5555
}
5656

57+
#[test]
58+
fn test_handle_json_input_overrides_arg_with_same_name() {
59+
let program = Program::compile("this.value").unwrap();
60+
let mut args = BTreeMap::new();
61+
args.insert("this".to_string(), CelValue::Int(999));
62+
let json = r#"{"value": 50}"#;
63+
let params = default_params();
64+
65+
let (output, is_truthy) = handle_json(&program, &args, &params, Some(json)).unwrap();
66+
67+
assert!(output.contains("50"));
68+
assert!(is_truthy);
69+
}
70+
5771
#[test]
5872
fn test_handle_json_args_and_json() {
5973
let program = Program::compile("x + this.value").unwrap();
@@ -114,6 +128,36 @@ fn test_handle_json_truthiness_empty_string() {
114128
assert!(!is_truthy);
115129
}
116130

131+
#[test]
132+
fn test_handle_json_raw_output_for_string() {
133+
let program = Program::compile(r#""hello""#).unwrap();
134+
let args = BTreeMap::new();
135+
let mut params = default_params();
136+
params.raw_output = true;
137+
138+
let (output, is_truthy) = handle_json(&program, &args, &params, None).unwrap();
139+
140+
assert_eq!(output, "hello");
141+
assert!(is_truthy);
142+
}
143+
144+
#[test]
145+
fn test_handle_json_sorted_pretty_output() {
146+
let program = Program::compile(r#"{"z": {"b": 2, "a": 1}, "a": 0}"#).unwrap();
147+
let args = BTreeMap::new();
148+
let mut params = default_params();
149+
params.sort_keys = true;
150+
params.pretty_print = true;
151+
152+
let (output, is_truthy) = handle_json(&program, &args, &params, None).unwrap();
153+
154+
assert_eq!(
155+
output,
156+
"{\n \"a\": 0,\n \"z\": {\n \"a\": 1,\n \"b\": 2\n }\n}"
157+
);
158+
assert!(is_truthy);
159+
}
160+
117161
#[test]
118162
fn test_handle_json_invalid_json() {
119163
let program = Program::compile("this.x").unwrap();
@@ -239,3 +283,20 @@ fn test_handle_buffer_skip_empty_lines() {
239283
assert!(results[1].0.contains("2"));
240284
assert!(results[2].0.contains("3"));
241285
}
286+
287+
#[test]
288+
fn test_handle_buffer_custom_root_var() {
289+
let program = Program::compile("request.x").unwrap();
290+
let args = BTreeMap::new();
291+
let input = r#"{"x": 42}"#;
292+
let cursor = Cursor::new(input.as_bytes());
293+
let reader = BufReader::new(cursor);
294+
let mut params = default_params();
295+
params.root_var = "request".to_string();
296+
297+
let results = handle_buffer(&program, &args, &params, reader).unwrap();
298+
299+
assert_eq!(results.len(), 1);
300+
assert_eq!(results[0].0, "42");
301+
assert!(results[0].1);
302+
}

src/json2cel_test.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ fn test_slurp_parallel() {
169169
}
170170
}
171171

172+
#[test]
173+
fn test_slurp_empty_input() {
174+
let vars =
175+
json_to_cel_variables("", ROOT_VAR, InputFormat::SlurpJson, DEFAULT_PARALLELISM).unwrap();
176+
177+
if let CelValue::List(list) = vars.get("this").unwrap() {
178+
assert!(list.is_empty());
179+
} else {
180+
panic!("Expected list");
181+
}
182+
}
183+
172184
#[test]
173185
#[cfg(feature = "from-toml")]
174186
fn test_toml_format() {
@@ -245,6 +257,24 @@ fn test_yaml_format_disabled() {
245257
assert!(vars.is_err());
246258
}
247259

260+
#[test]
261+
#[cfg(feature = "from-yaml")]
262+
fn test_yaml_multi_document_format() {
263+
let yaml_input = r#"name: first
264+
---
265+
name: second
266+
"#;
267+
268+
let vars = json_to_cel_variables(yaml_input, ROOT_VAR, InputFormat::Yaml, DEFAULT_PARALLELISM)
269+
.unwrap();
270+
271+
if let CelValue::List(list) = vars.get("this").unwrap() {
272+
assert_eq!(list.len(), 2);
273+
} else {
274+
panic!("Expected list");
275+
}
276+
}
277+
248278
#[test]
249279
#[cfg(feature = "greppable")]
250280
fn test_gron_format() {

src/ungron_test.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,24 @@ json.nothing = null;"#;
8080

8181
assert_eq!(result, expected);
8282
}
83+
84+
#[test]
85+
fn test_empty_container_assignments_do_not_overwrite_existing_values() {
86+
let input = r#"json = {};
87+
json.user = {};
88+
json.user.name = "John";
89+
json.user = {};
90+
json.items = [];
91+
json.items[0] = "first";
92+
json.items = [];"#;
93+
94+
let result = gron_to_json(input).unwrap();
95+
let expected: JsonValue = serde_json::json!({
96+
"user": {
97+
"name": "John"
98+
},
99+
"items": ["first"]
100+
});
101+
102+
assert_eq!(result, expected);
103+
}

tests/golden.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,18 @@ test!(
472472
"20"
473473
);
474474

475+
test!(
476+
rename_root_variable_overrides_same_named_arg,
477+
&[
478+
"--root-var=request",
479+
"--arg",
480+
"request:int=99",
481+
"request.value"
482+
],
483+
r#"{"value":42}"#,
484+
"42"
485+
);
486+
475487
// Multi-line JSON5 (with trailing comma and comment)
476488
test!(
477489
multiline_json5_object,
@@ -692,6 +704,16 @@ value: third
692704
"[\"first\",\"second\",\"third\"]"
693705
);
694706

707+
#[cfg(feature = "from-yaml")]
708+
test!(
709+
yaml_multi_document_single_doc_stays_scalar,
710+
&["--from-yaml", "this.name"],
711+
r#"name: Alice
712+
age: 30
713+
"#,
714+
"\"Alice\""
715+
);
716+
695717
// Void test
696718
test!(void_mode, &["--void", "-n", "2 + 2"], "", "");
697719

@@ -857,6 +879,17 @@ json.a.b.c.d[0].e = "deep";
857879
r#"{"a":{"b":{"c":{"d":[{"e":"deep"}]}}}}"#
858880
);
859881

882+
#[cfg(feature = "greppable")]
883+
test!(
884+
greppable_round_trip_special_keys,
885+
&["--from-gron", "-S", "this"],
886+
r#"json = {};
887+
json["key with spaces"] = "value";
888+
json["key-with-dashes"] = "other";
889+
"#,
890+
r#"{"key with spaces":"value","key-with-dashes":"other"}"#
891+
);
892+
860893
// Slice extension tests
861894
test!(
862895
slice_basic,
@@ -886,6 +919,13 @@ test!(
886919
"[3]"
887920
);
888921

922+
test!(
923+
slice_empty_result,
924+
&["this.items.slice(3, 3)"],
925+
r#"{"items":[1,2,3,4]}"#,
926+
"[]"
927+
);
928+
889929
test!(
890930
slice_negative_to_positive,
891931
&["this.items.slice(-5, 4)"],

0 commit comments

Comments
 (0)