Skip to content

Commit

Permalink
Fix test.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Mar 4, 2024
1 parent 5975d97 commit 34bede9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Enhancements
* Array/BLOB/string iterators are defined also within the `BasicIteratorPackage` in addition to the regular array/BLOB/string packages.
* `LexError::Runtime` is added for use with `Engine::on_parse_token`.
* Shared values under `sync` are now handled more elegantly -- instead of deadlocking and hanging indefinitely, it spins for a number of tries (waiting one second between each), then errors out.
* `parse_json` is also available without the `metadata` or `serde` feature -- it uses `Engine::parse_json` to parse the JSON text.


Version 1.17.2
Expand Down
40 changes: 22 additions & 18 deletions tests/parse_json.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use rhai::{Engine, LexError, ParseErrorType, Scope, INT};
use rhai::{Dynamic, Engine, EvalAltResult, LexError, ParseErrorType, Position, Scope, INT};

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,no_time,no_function,no_float,no_position,no_inde...

unused imports: `Dynamic`, `Engine`, `EvalAltResult`, `INT`, `LexError`, `ParseErrorType`, `Position`, `Scope`

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (stable, macos-latest, false)

unused imports: `Dynamic`, `EvalAltResult`, `INT`, `LexError`, `ParseErrorType`, `Position`

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (beta, ubuntu-latest, false, --features unstable)

unused imports: `Dynamic`, `EvalAltResult`, `INT`, `LexError`, `ParseErrorType`, `Position`

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,no_object,serde,metadata,internals,debugging, st...

unused imports: `INT`, `LexError`, `ParseErrorType`

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,no_module,serde,metadata,internals,debugging, st...

unused imports: `Dynamic`, `EvalAltResult`, `INT`, `LexError`, `ParseErrorType`, `Position`

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,no_float,serde,metadata,internals,debugging, sta...

unused imports: `Dynamic`, `EvalAltResult`, `INT`, `LexError`, `ParseErrorType`, `Position`

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,f32_float,serde,metadata,internals,debugging, st...

unused imports: `Dynamic`, `EvalAltResult`, `INT`, `LexError`, `ParseErrorType`, `Position`

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,no_optimize,serde,metadata,internals,debugging, ...

unused imports: `Dynamic`, `EvalAltResult`, `INT`, `LexError`, `ParseErrorType`, `Position`

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --tests --features testing-environ,only_i32,serde,metadata,internals,debugg...

unused imports: `Dynamic`, `EvalAltResult`, `INT`, `LexError`, `ParseErrorType`, `Position`

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,serde, stable, false)

unused imports: `Dynamic`, `EvalAltResult`, `INT`, `LexError`, `ParseErrorType`, `Position`

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,no_float,decimal, stable, false)

unused imports: `Dynamic`, `EvalAltResult`, `INT`, `LexError`, `ParseErrorType`, `Position`

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,internals, stable, false)

unused imports: `Dynamic`, `EvalAltResult`, `INT`, `LexError`, `ParseErrorType`, `Position`

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (stable, windows-latest, false)

unused imports: `Dynamic`, `EvalAltResult`, `INT`, `LexError`, `ParseErrorType`, `Position`

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,decimal, stable, false)

unused imports: `Dynamic`, `EvalAltResult`, `INT`, `LexError`, `ParseErrorType`, `Position`

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,debugging, stable, false)

unused imports: `Dynamic`, `EvalAltResult`, `INT`, `LexError`, `ParseErrorType`, `Position`

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, stable, false)

unused imports: `Dynamic`, `EvalAltResult`, `INT`, `LexError`, `ParseErrorType`, `Position`

Check warning on line 1 in tests/parse_json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,decimal,serde,metadata,internals,debugging, stab...

unused imports: `Dynamic`, `EvalAltResult`, `INT`, `LexError`, `ParseErrorType`, `Position`

#[cfg(not(feature = "no_object"))]
use rhai::Map;

#[cfg(not(feature = "metadata"))]
mod without_metadata {
Expand All @@ -13,7 +16,7 @@ mod without_metadata {
let mut scope = Scope::new();

let map = engine
.eval_with_scope::<rhai::Map>(
.eval_with_scope::<Map>(
&mut scope,
r#"
parse_json("{\
Expand All @@ -37,20 +40,21 @@ mod without_metadata {
assert_eq!(map["age"].as_int().expect("age should exist"), 43);
assert_eq!(map["phones"].clone().into_typed_array::<String>().expect("phones should exist"), ["+44 1234567", "+44 2345678"]);

let address = map["address"].read_lock::<rhai::Map>().expect("address should exist");
let address = map["address"].read_lock::<Map>().expect("address should exist");
assert_eq!(address["city"].clone().into_immutable_string().expect("address.city should exist"), "London");
assert_eq!(address["street"].clone().into_immutable_string().expect("address.street should exist"), "10 Downing Street");
}

#[test]
#[cfg(feature = "no_index")]
#[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "no_function"))]
fn test_parse_json_err_no_index() {
let engine = Engine::new();
let mut scope = Scope::new();

let err = engine
.eval_with_scope::<rhai::Dynamic>(
.eval_with_scope::<Dynamic>(
&mut scope,
r#"
parse_json("{\
Expand All @@ -63,9 +67,9 @@ mod without_metadata {
)
.unwrap_err();

assert!(matches!(err.as_ref(), rhai::EvalAltResult::ErrorParsing(
assert!(matches!(err.as_ref(), EvalAltResult::ErrorParsing(
ParseErrorType::BadInput(LexError::UnexpectedInput(token)), pos)
if token == "[" && *pos == rhai::Position::new(1, 7)));
if token == "[" && *pos == Position::new(1, 7)));
}

#[test]
Expand All @@ -76,7 +80,7 @@ mod without_metadata {
let mut scope = Scope::new();

let err = engine
.eval_with_scope::<rhai::Dynamic>(
.eval_with_scope::<Dynamic>(
&mut scope,
r#"
parse_json("{\
Expand All @@ -89,8 +93,8 @@ mod without_metadata {
)
.unwrap_err();

assert!(matches!(err.as_ref(), rhai::EvalAltResult::ErrorFunctionNotFound(msg, pos)
if msg == "parse_json (&str | ImmutableString | String)" && *pos == rhai::Position::new(2, 13)));
assert!(matches!(err.as_ref(), EvalAltResult::ErrorFunctionNotFound(msg, pos)
if msg == "parse_json (&str | ImmutableString | String)" && *pos == Position::new(2, 13)));
}
}

Expand All @@ -107,7 +111,7 @@ mod with_metadata {
let mut scope = Scope::new();

let map = engine
.eval_with_scope::<rhai::Map>(
.eval_with_scope::<Map>(
&mut scope,
r#"
parse_json("{\
Expand All @@ -131,20 +135,21 @@ mod with_metadata {
assert_eq!(map["age"].as_int().expect("age should exist"), 43);
assert_eq!(map["phones"].clone().into_typed_array::<String>().expect("phones should exist"), ["+44 1234567", "+44 2345678"]);

let address = map["address"].read_lock::<rhai::Map>().expect("address should exist");
let address = map["address"].read_lock::<Map>().expect("address should exist");
assert_eq!(address["city"].clone().into_immutable_string().expect("address.city should exist"), "London");
assert_eq!(address["street"].clone().into_immutable_string().expect("address.street should exist"), "10 Downing Street");
}

#[test]
#[cfg(feature = "no_index")]
#[cfg(not(feature = "no_object"))]
#[cfg(not(feature = "no_function"))]
fn test_parse_json_err_no_index() {
let engine = Engine::new();
let mut scope = Scope::new();

let err = engine
.eval_with_scope::<rhai::Dynamic>(
.eval_with_scope::<Dynamic>(
&mut scope,
r#"
parse_json("{\
Expand All @@ -157,9 +162,8 @@ mod with_metadata {
)
.unwrap_err();

assert!(matches!(err.as_ref(), rhai::EvalAltResult::ErrorParsing(
ParseErrorType::BadInput(LexError::UnexpectedInput(token)), pos)
if token == "[" && *pos == rhai::Position::new(1, 7)));
assert!(matches!(err.as_ref(), EvalAltResult::ErrorRuntime(msg, pos)
if msg.is_string() && *pos == Position::new(1, 7)));
}

#[test]
Expand All @@ -170,7 +174,7 @@ mod with_metadata {
let mut scope = Scope::new();

let err = engine
.eval_with_scope::<rhai::Dynamic>(
.eval_with_scope::<Dynamic>(
&mut scope,
r#"
parse_json("{\
Expand All @@ -183,7 +187,7 @@ mod with_metadata {
)
.unwrap_err();

assert!(matches!(err.as_ref(), rhai::EvalAltResult::ErrorFunctionNotFound(msg, pos)
if msg == "parse_json (&str | ImmutableString | String)" && *pos == rhai::Position::new(2, 17)));
assert!(matches!(err.as_ref(), EvalAltResult::ErrorFunctionNotFound(msg, pos)
if msg == "parse_json (&str | ImmutableString | String)" && *pos == Position::new(2, 17)));
}
}

0 comments on commit 34bede9

Please sign in to comment.