Skip to content

Commit

Permalink
tests-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
trobro committed Jul 23, 2024
1 parent 2e23689 commit 742c5fb
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 14 deletions.
2 changes: 1 addition & 1 deletion hjson/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ default = ["preserve_order"]
[dependencies]
serde = "1.0"
num-traits = "0.2"
regex = "1.0"
regex = "1.10"
lazy_static = "1"
linked-hash-map = { version = "0.5", optional = true }
25 changes: 22 additions & 3 deletions hjson/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,20 @@ where
}
}
if is_eol {
let pos = self.rdr.pos();
// remove any whitespace at the end (ignored in quoteless strings)
return visitor.visit_str(str::from_utf8(&self.str_buf).unwrap().trim());
return visitor.visit_str(
str::from_utf8(&self.str_buf)
.map_err(|_| {
let reader_error = super::Error::Syntax(
super::ErrorCode::EOFWhileParsingString,
pos.0,
pos.1,
);
reader_error
})?
.trim(),
);
}
}
self.str_buf.push(ch);
Expand Down Expand Up @@ -345,7 +357,9 @@ where

// we are at ''' +1 - get indent
let (_, col) = self.rdr.pos();
let indent = col - 4;

// Fallback machanism if col is improper
let indent = if col >= 4 { col - 4 } else { 0 };

// skip white/to (newline)
while self.ml_skip_white()? {}
Expand Down Expand Up @@ -813,5 +827,10 @@ pub fn from_str<T>(s: &str) -> Result<T>
where
T: de::DeserializeOwned,
{
from_slice(s.as_bytes())
if s.chars().last().is_some_and(|x| x.is_whitespace()) {
from_slice(s.as_bytes())
} else {
let s = format!("{s}\n");
from_slice(s.as_bytes())
}
}
21 changes: 17 additions & 4 deletions hjson/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ where
}

pub fn eat_char(&mut self) -> u8 {
self.ch.remove(0)
if !self.ch.is_empty() {
self.ch.remove(0)
// Fallback mechanism for consuming a character.
// Sets ch to NULL termination
} else { 0 }
}

pub fn uneat_char(&mut self, ch: u8) {
Expand Down Expand Up @@ -207,12 +211,21 @@ impl<Iter: Iterator<Item = u8>> ParseNumber<Iter> {
}

if is_float {
Ok(Number::F64(res.parse::<f64>().unwrap()))
Ok(Number::F64(res.parse::<f64>().map_err(|_| {
let pos = self.rdr.pos();
Error::Syntax(ErrorCode::InvalidNumber, pos.0, pos.1)
})?))
} else {
if res.starts_with("-") {
Ok(Number::I64(res.parse::<i64>().unwrap()))
Ok(Number::I64(res.parse::<i64>().map_err(|_| {
let pos = self.rdr.pos();
Error::Syntax(ErrorCode::InvalidNumber, pos.0, pos.1)
})?))
} else {
Ok(Number::U64(res.parse::<u64>().unwrap()))
Ok(Number::U64(res.parse::<u64>().map_err(|_| {
let pos = self.rdr.pos();
Error::Syntax(ErrorCode::InvalidNumber, pos.0, pos.1)
})?))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion hjson/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ impl<'de, 'a> de::MapAccess<'de> for MapDeserializer {
/// ```rust
/// use serde_hjson::to_value;
/// let val = to_value("foo");
/// assert_eq!(val.as_str(), Some("foo"))
/// assert_eq!(val.unwrap().as_str(), Some("foo"))
/// ```
pub fn to_value<T: ?Sized>(value: &T) -> Result<Value>
where
Expand Down
1 change: 1 addition & 0 deletions hjson_tests/assets/simplenumber_result.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123
1 change: 1 addition & 0 deletions hjson_tests/assets/simplenumber_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123
1 change: 1 addition & 0 deletions hjson_tests/assets/simplenumber_test.hjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123
59 changes: 54 additions & 5 deletions hjson_tests/tests/test_hjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ extern crate serde_hjson;
extern crate serde_json;

use regex::Regex;
use serde_hjson::Value;
use std::borrow::Cow;
use std::fs;
use std::io;
use std::path::Path;
use serde_hjson::{Map, Value};
use serde_hjson::error::Result;

pub const TRIM_ENDLINE: bool = true;


fn get_test_content(name: &str) -> io::Result<String> {
let mut p = format!("./assets/{}_test.hjson", name);
Expand All @@ -24,19 +28,22 @@ fn get_result_content(name: &str) -> io::Result<(String, String)> {
}

macro_rules! run_test {
// {{ is a workaround for rust stable
($v: ident, $list: expr, $fix: expr) => {{
run_test!($v, $list, $fix, false);
}};
// {{ is a workaround for rust stable
($v: ident, $list: expr, $fix: expr, $trim_endline: expr) => {{
let name = stringify!($v);
$list.push(format!("{}_test", name));
println!("- running {}", name);
let should_fail = name.starts_with("fail");
let test_content = get_test_content(name).unwrap();
let test_content = get_test_content(name).expect("Could not read test content");
let data: serde_hjson::Result<Value> = serde_hjson::from_str(&test_content);
assert!(should_fail == data.is_err());

if !should_fail {
let udata = data.unwrap();
let (rjson, rhjson) = get_result_content(name).unwrap();
let (rjson, rhjson) = get_result_content(name).expect("Could not read result content");
let actual_hjson = serde_hjson::to_string(&udata).unwrap();
let actual_json = serde_json::to_string_pretty(&udata).unwrap();
let actual_json = $fix(&actual_json);
Expand All @@ -52,7 +59,10 @@ macro_rules! run_test {
name, rjson, actual_json
);
}
assert!(rhjson == actual_hjson && rjson == actual_json);
if $trim_endline { assert!(rhjson.trim_end() == actual_hjson && rjson.trim_end() == actual_json); }
else {
assert!(rhjson == actual_hjson && rjson == actual_json);
}
}
}};
}
Expand Down Expand Up @@ -155,6 +165,7 @@ fn test_hjson() {
run_test!(stringify1, done, std_fix);
run_test!(strings, done, std_fix);
run_test!(trail, done, std_fix);
run_test!(simplenumber, done, std_fix, TRIM_ENDLINE);

// check if we include all assets
let paths = fs::read_dir("./assets/").unwrap();
Expand All @@ -176,3 +187,41 @@ fn test_hjson() {
assert!(false);
}
}

#[test]
pub fn parse_int_error() {
let data: Vec<u8> = vec![47, 97, 47, 65, 58, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 0, 0, 0, 54, 35, 54, 54, 54, 54, 54, 54, 54, 44, 35, 58, 45, 85, 85, 85, 35, 116, 45, 35, 35, 58, 47];

let sample: Result<Map<String, Value>> = serde_hjson::from_slice(&data);
assert!(sample.is_err())
}

#[test]
pub fn removal_index() {
let data: Vec<u8> = vec![47, 42, 44, 45];

let sample: Result<Map<String, Value>> = serde_hjson::from_slice(&data);
assert!(sample.is_err())
}

#[test]
pub fn subtract_overflow() {
let data: Vec<u8> = vec![39, 39, 39];

let sample: Result<Map<String, Value>> = serde_hjson::from_slice(&data);
assert!(sample.is_err())
}

#[test]
pub fn invalid_utf8() {
let data: Vec<u8> = vec![155];

let sample: Result<Map<String, Value>> = serde_hjson::from_slice(&data);
assert!(sample.is_err())
}

#[test]
pub fn integer_type() {
let json: Value = serde_hjson::from_str("123").unwrap();
assert!(json.is_number())
}

0 comments on commit 742c5fb

Please sign in to comment.