|
| 1 | +use super::*; |
| 2 | +use serde_json::json; |
| 3 | + |
| 4 | +#[test] |
| 5 | +fn test_simple_object() { |
| 6 | + let value = json!({ |
| 7 | + "name": "Get Celq", |
| 8 | + "email": "get-celq@proton.me" |
| 9 | + }); |
| 10 | + |
| 11 | + let result = json_to_gron(&value); |
| 12 | + assert!(result.contains(r#"json = {};"#)); |
| 13 | + assert!(result.contains(r#"json.name = "Get Celq";"#)); |
| 14 | + assert!(result.contains(r#"json.email = "get-celq@proton.me";"#)); |
| 15 | +} |
| 16 | + |
| 17 | +#[test] |
| 18 | +fn test_nested_object() { |
| 19 | + let value = json!({ |
| 20 | + "commit": { |
| 21 | + "author": { |
| 22 | + "name": "Get Celq", |
| 23 | + "email": "get-celq@proton.me", |
| 24 | + "date": "2026-01-01T06:00:00Z" |
| 25 | + } |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + let result = json_to_gron(&value); |
| 30 | + assert!(result.contains(r#"json.commit.author = {};"#)); |
| 31 | + assert!(result.contains(r#"json.commit.author.name = "Get Celq";"#)); |
| 32 | + assert!(result.contains(r#"json.commit.author.email = "get-celq@proton.me";"#)); |
| 33 | + assert!(result.contains(r#"json.commit.author.date = "2026-01-01T06:00:00Z";"#)); |
| 34 | +} |
| 35 | + |
| 36 | +#[test] |
| 37 | +fn test_array() { |
| 38 | + let value = json!([ |
| 39 | + { |
| 40 | + "commit": { |
| 41 | + "author": { |
| 42 | + "name": "Get Celq" |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + ]); |
| 47 | + |
| 48 | + let result = json_to_gron(&value); |
| 49 | + assert!(result.contains("json = [];")); |
| 50 | + assert!(result.contains(r#"json[0].commit.author.name = "Get Celq";"#)); |
| 51 | +} |
| 52 | + |
| 53 | +#[test] |
| 54 | +fn test_special_keys() { |
| 55 | + let value = json!({ |
| 56 | + "x86_64-unknown-linux-musl": { |
| 57 | + "pkg-url": "some-value" |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + let result = json_to_gron(&value); |
| 62 | + // Keys with hyphens are not valid identifiers, so they need bracket notation |
| 63 | + assert!(result.contains(r#"json["x86_64-unknown-linux-musl"]["pkg-url"] = "some-value";"#)); |
| 64 | +} |
| 65 | + |
| 66 | +#[test] |
| 67 | +fn test_primitives() { |
| 68 | + let value = json!({ |
| 69 | + "null_val": null, |
| 70 | + "bool_val": true, |
| 71 | + "num_val": 42, |
| 72 | + "float_val": 3.14 |
| 73 | + }); |
| 74 | + |
| 75 | + let result = json_to_gron(&value); |
| 76 | + assert!(result.contains(r#"json.null_val = null;"#)); |
| 77 | + assert!(result.contains(r#"json.bool_val = true;"#)); |
| 78 | + assert!(result.contains(r#"json.num_val = 42;"#)); |
| 79 | + assert!(result.contains(r#"json.float_val = 3.14;"#)); |
| 80 | +} |
| 81 | + |
| 82 | +#[test] |
| 83 | +fn test_string_escaping() { |
| 84 | + let value = json!({ |
| 85 | + "with\"quote": "value with \"quote\"", |
| 86 | + "with\nnewline": "value\nwith\nnewline" |
| 87 | + }); |
| 88 | + |
| 89 | + let result = json_to_gron(&value); |
| 90 | + println!("DEBUG: {}", result); |
| 91 | + assert!(result.contains(r#"json["with\"quote"] = "value with \"quote\"";"#)); |
| 92 | + assert!(result.contains(r#"json["with\nnewline"] = "value\nwith\nnewline";"#)); |
| 93 | +} |
| 94 | + |
| 95 | +#[test] |
| 96 | +fn test_your_example() { |
| 97 | + let value = json!({ |
| 98 | + "package": { |
| 99 | + "metadata": { |
| 100 | + "binstall": { |
| 101 | + "overrides": { |
| 102 | + "x86_64-unknown-linux-musl": { |
| 103 | + "pkg-url": "{ repo }/releases/download/v{ version }/{ name }-linux-x86_64-musl{ archive-suffix }" |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + let result = json_to_gron(&value); |
| 112 | + // "pkg-url" and "x86_64-unknown-linux-musl" have hyphens, so they need brackets |
| 113 | + // but package, metadata, binstall, overrides are valid identifiers |
| 114 | + assert!(result.contains( |
| 115 | + r#"json.package.metadata.binstall.overrides["x86_64-unknown-linux-musl"]["pkg-url"] = "{ repo }/releases/download/v{ version }/{ name }-linux-x86_64-musl{ archive-suffix }";"# |
| 116 | + )); |
| 117 | +} |
| 118 | + |
| 119 | +#[test] |
| 120 | +fn test_mixed_notation() { |
| 121 | + let value = json!({ |
| 122 | + "valid_id": { |
| 123 | + "also-valid": "value", |
| 124 | + "nested": { |
| 125 | + "deep-key": "deep-value" |
| 126 | + } |
| 127 | + }, |
| 128 | + "123start": "invalid" |
| 129 | + }); |
| 130 | + |
| 131 | + let result = json_to_gron(&value); |
| 132 | + // valid_id is a valid identifier, uses dot |
| 133 | + assert!(result.contains(r#"json.valid_id = {};"#)); |
| 134 | + // also-valid has hyphen, needs brackets |
| 135 | + assert!(result.contains(r#"json.valid_id["also-valid"] = "value";"#)); |
| 136 | + // nested is valid, uses dot |
| 137 | + assert!(result.contains(r#"json.valid_id.nested = {};"#)); |
| 138 | + // deep-key has hyphen, needs brackets |
| 139 | + assert!(result.contains(r#"json.valid_id.nested["deep-key"] = "deep-value";"#)); |
| 140 | + // 123start starts with number but has letters, needs quoted brackets |
| 141 | + assert!(result.contains(r#"json["123start"] = "invalid";"#)); |
| 142 | +} |
| 143 | + |
| 144 | +#[test] |
| 145 | +fn test_array_with_objects() { |
| 146 | + let value = json!([ |
| 147 | + { |
| 148 | + "123": "starts with number", |
| 149 | + "validKey": "valid" |
| 150 | + } |
| 151 | + ]); |
| 152 | + |
| 153 | + let result = json_to_gron(&value); |
| 154 | + // Array index is unquoted |
| 155 | + assert!(result.contains(r#"json[0] = {};"#)); |
| 156 | + // Numeric string object key uses quoted bracket notation |
| 157 | + assert!(result.contains(r#"json[0]["123"] = "starts with number";"#)); |
| 158 | + // Valid identifier after array index uses dot |
| 159 | + assert!(result.contains(r#"json[0].validKey = "valid";"#)); |
| 160 | +} |
| 161 | + |
| 162 | +#[test] |
| 163 | +fn test_serde_saphyr_example() { |
| 164 | + let value = json!({ |
| 165 | + "dependencies": { |
| 166 | + "serde-saphyr": { |
| 167 | + "version": "=0.0.14" |
| 168 | + } |
| 169 | + } |
| 170 | + }); |
| 171 | + |
| 172 | + let result = json_to_gron(&value); |
| 173 | + // Should produce: json.dependencies["serde-saphyr"].version = "=0.0.14"; |
| 174 | + assert!(result.contains(r#"json.dependencies["serde-saphyr"].version = "=0.0.14";"#)); |
| 175 | +} |
| 176 | + |
| 177 | +#[test] |
| 178 | +fn test_features_array_example() { |
| 179 | + let value = json!({ |
| 180 | + "features": { |
| 181 | + "default": [ |
| 182 | + "from-yaml", |
| 183 | + "from-toml" |
| 184 | + ] |
| 185 | + } |
| 186 | + }); |
| 187 | + |
| 188 | + let result = json_to_gron(&value); |
| 189 | + // Array indices are unquoted |
| 190 | + assert!(result.contains(r#"json.features.default[0] = "from-yaml";"#)); |
| 191 | + assert!(result.contains(r#"json.features.default[1] = "from-toml";"#)); |
| 192 | +} |
0 commit comments