Skip to content

Commit 92c61b0

Browse files
committed
fix one test
1 parent b1e7b2f commit 92c61b0

3 files changed

Lines changed: 32 additions & 9 deletions

File tree

‎rfc9535/src/tests.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,20 @@ fn space_test() -> Queried<()> {
284284

285285
Ok(())
286286
}
287+
#[test]
288+
fn neg_idx() -> Queried<()> {
289+
let json = json!([
290+
"first",
291+
"second"
292+
]);
293+
294+
let vec = json.query_with_path("$[-2]")?;
295+
assert_eq!(
296+
vec,
297+
vec![
298+
(&json!("first"), "$[0]".to_string()).into(),
299+
]
300+
);
301+
302+
Ok(())
303+
}

‎rfc9535/test_suite/results.csv‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
Total; Passed; Failed; Date
2-
687; 650; 27; 2025-03-16 11:36:02
3-
687; 650; 27; 2025-03-16 11:40:04
42
687; 650; 26; 2025-03-16 11:50:00
53
687; 650; 25; 2025-03-16 11:51:32
64
687; 650; 25; 2025-03-16 11:54:46
@@ -9,3 +7,5 @@ Total; Passed; Failed; Date
97
687; 650; 25; 2025-03-16 11:56:18
108
687; 650; 25; 2025-03-16 11:56:31
119
687; 650; 25; 2025-03-16 11:58:52
10+
687; 650; 25; 2025-03-16 13:02:53
11+
687; 651; 24; 2025-03-16 19:37:57

‎src/query/selector.rs‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,21 @@ pub fn process_index<'a, T: Queryable>(
190190
inner
191191
.as_array()
192192
.map(|array| {
193-
if (idx.abs() as usize) < array.len() {
194-
let i = if *idx < 0 {
195-
array.len() - idx.abs() as usize
193+
if *idx >= 0 {
194+
if *idx >= array.len() as i64 {
195+
Data::Nothing
196196
} else {
197-
*idx as usize
198-
};
199-
Data::new_ref(Pointer::idx(&array[i], path, i))
197+
let i = *idx as usize;
198+
Data::new_ref(Pointer::idx(&array[i], path, i))
199+
}
200200
} else {
201-
Data::Nothing
201+
let abs_idx = idx.abs() as usize;
202+
if abs_idx > array.len() {
203+
Data::Nothing
204+
} else {
205+
let i = array.len() - abs_idx;
206+
Data::new_ref(Pointer::idx(&array[i], path, i))
207+
}
202208
}
203209
})
204210
.unwrap_or_default()

0 commit comments

Comments
 (0)