From c6af3d750d1f27e346156dc90eb345228eb5fd3f Mon Sep 17 00:00:00 2001 From: esynr3z Date: Wed, 5 Jun 2024 23:11:04 +0300 Subject: [PATCH] refactor: make json_value methods shorter --- docs/modules/ROOT/examples/decoder0.svh | 4 +- docs/modules/ROOT/examples/decoder1.svh | 2 +- docs/modules/ROOT/pages/user.adoc | 6 +- src/json_decoder.sv | 2 +- src/values/json_array.sv | 2 +- src/values/json_bool.sv | 2 +- src/values/json_int.sv | 2 +- src/values/json_object.sv | 2 +- src/values/json_real.sv | 2 +- src/values/json_string.sv | 2 +- src/values/json_value.sv | 132 ++++++++++++------------ tests/json_array_unit_test.sv | 84 +++++++-------- tests/json_bool_unit_test.sv | 38 +++---- tests/json_int_unit_test.sv | 38 +++---- tests/json_object_unit_test.sv | 74 ++++++------- tests/json_real_unit_test.sv | 38 +++---- tests/json_string_unit_test.sv | 38 +++---- 17 files changed, 234 insertions(+), 234 deletions(-) diff --git a/docs/modules/ROOT/examples/decoder0.svh b/docs/modules/ROOT/examples/decoder0.svh index 32a7cf0..9edf84b 100644 --- a/docs/modules/ROOT/examples/decoder0.svh +++ b/docs/modules/ROOT/examples/decoder0.svh @@ -8,11 +8,11 @@ string recipe_name; // or `json_value`. First unwrap is required to get `json_value` from // load result and avoid error handling. Second unwrap is required // to avoid error handling of possible unsuccessfull cast to `json_object`. -jobject = json_decoder::load_string(data).unwrap().as_json_object().unwrap(); +jobject = json_decoder::load_string(data).unwrap().as_object().unwrap(); // Try to get a string for recipe name. // Unwrap is here to avoid error handling of possible unsuccessfull // cast to `json_string`. -recipe_name = jobject.get("recipeName").as_json_string().unwrap().get(); +recipe_name = jobject.get("recipeName").as_string().unwrap().get(); $display("Recipe name is %s", recipe_name); \ No newline at end of file diff --git a/docs/modules/ROOT/examples/decoder1.svh b/docs/modules/ROOT/examples/decoder1.svh index d14c96c..22e0127 100644 --- a/docs/modules/ROOT/examples/decoder1.svh +++ b/docs/modules/ROOT/examples/decoder1.svh @@ -18,7 +18,7 @@ case (1) load_res.matches_ok(jvalue): begin json_object jobject; - json_result#(json_object) cast_res = jvalue.as_json_object(); + json_result#(json_object) cast_res = jvalue.as_object(); // Traditional if..else can be used as well if (cast_res.matches_err(jerror)) begin diff --git a/docs/modules/ROOT/pages/user.adoc b/docs/modules/ROOT/pages/user.adoc index 31ca394..84ff6fa 100644 --- a/docs/modules/ROOT/pages/user.adoc +++ b/docs/modules/ROOT/pages/user.adoc @@ -112,9 +112,9 @@ Class `{class-json-value}` is a base abstract class for all JSON values. This cl |=== | `bit compare(json_value value)` | Perform compare with another instance. Return 1 if instances are equal and 0 otherwise. | `json_value clone()` | Create a deep copy of an instance. -| `bit is_json_` (e.g. `is_json_object`, `is_json_array`, etc.) | Check if current instance is specified type. -| `json_result#(json_) as_json_()` (e.g. `as_json_object`, `as_json_array`, etc.) | Try to cast to specified concrete class. -| `bit matches_json_(output json_ value)` (e.g. `matches_json_object`, `matches_json_array`, etc.) | Another option of trying to cast to specified concrete class. In this case, instance is an `output` argument, and returned result is 1 for success, 0 otherwise. +| `bit is_` (e.g. `is_object`, `is_array`, etc.) | Check if current instance is specified type. +| `json_result#(json_) as_()` (e.g. `as_object`, `as_array`, etc.) | Try to cast to specified concrete class. +| `bit matches_(output json_ value)` (e.g. `matches_object`, `matches_array`, etc.) | Another option of trying to cast to specified concrete class. In this case, instance is an `output` argument, and returned result is 1 for success, 0 otherwise. |=== === Object diff --git a/src/json_decoder.sv b/src/json_decoder.sv index 3258b21..21a9549 100644 --- a/src/json_decoder.sv +++ b/src/json_decoder.sv @@ -244,7 +244,7 @@ function json_decoder::parser_result json_decoder::parse_object( result.matches_err(error): return result; result.matches_ok(parsed): begin - key = parsed.value.as_json_string().unwrap().value; + key = parsed.value.as_string().unwrap().value; curr_pos = parsed.end_pos + 1; // move from last string token state = EXPECT_COLON; end diff --git a/src/values/json_array.sv b/src/values/json_array.sv index d0e8c7e..af8764d 100644 --- a/src/values/json_array.sv +++ b/src/values/json_array.sv @@ -95,7 +95,7 @@ function bit json_array::compare(json_value value); return 0; end - casted = value.as_json_array(); + casted = value.as_array(); case (1) casted.matches_err(err): return 0; casted.matches_ok(rhs): begin diff --git a/src/values/json_bool.sv b/src/values/json_bool.sv index 3c008c8..a72cffb 100644 --- a/src/values/json_bool.sv +++ b/src/values/json_bool.sv @@ -53,7 +53,7 @@ function bit json_bool::compare(json_value value); return 0; end - casted = value.as_json_bool(); + casted = value.as_bool(); case (1) casted.matches_err(err): return 0; casted.matches_ok(rhs): return get() == rhs.get(); diff --git a/src/values/json_int.sv b/src/values/json_int.sv index fd306ff..600494e 100644 --- a/src/values/json_int.sv +++ b/src/values/json_int.sv @@ -56,7 +56,7 @@ function bit json_int::compare(json_value value); return 0; end - casted = value.as_json_int(); + casted = value.as_int(); case (1) casted.matches_err(err): return 0; casted.matches_ok(rhs): return get() == rhs.get(); diff --git a/src/values/json_object.sv b/src/values/json_object.sv index baa1395..3ae7d64 100644 --- a/src/values/json_object.sv +++ b/src/values/json_object.sv @@ -108,7 +108,7 @@ function bit json_object::compare(json_value value); return 0; end - casted = value.as_json_object(); + casted = value.as_object(); case (1) casted.matches_err(err): return 0; casted.matches_ok(rhs): begin diff --git a/src/values/json_real.sv b/src/values/json_real.sv index 2d49052..ca92808 100644 --- a/src/values/json_real.sv +++ b/src/values/json_real.sv @@ -56,7 +56,7 @@ function bit json_real::compare(json_value value); return 0; end - casted = value.as_json_real(); + casted = value.as_real(); case (1) casted.matches_err(err): return 0; casted.matches_ok(rhs): return get() == rhs.get(); diff --git a/src/values/json_string.sv b/src/values/json_string.sv index 1a44ca8..2106717 100644 --- a/src/values/json_string.sv +++ b/src/values/json_string.sv @@ -53,7 +53,7 @@ function bit json_string::compare(json_value value); return 0; end - casted = value.as_json_string(); + casted = value.as_string(); case (1) casted.matches_err(err): return 0; casted.matches_ok(rhs): return get() == rhs.get(); diff --git a/src/values/json_value.sv b/src/values/json_value.sv index ce68ce4..ca61802 100644 --- a/src/values/json_value.sv +++ b/src/values/json_value.sv @@ -7,182 +7,182 @@ virtual class json_value implements json_value_encodable; pure virtual function bit compare(json_value value); // Try to cast current value to `json_object` - extern virtual function bit matches_json_object(output json_object value); + extern virtual function bit matches_object(output json_object value); // Try to cast current value to `json_array` - extern virtual function bit matches_json_array(output json_array value); + extern virtual function bit matches_array(output json_array value); // Try to cast current value to `json_string` - extern virtual function bit matches_json_string(output json_string value); + extern virtual function bit matches_string(output json_string value); // Try to cast current value to `json_int` - extern virtual function bit matches_json_int(output json_int value); + extern virtual function bit matches_int(output json_int value); // Try to cast current value to `json_real` - extern virtual function bit matches_json_real(output json_real value); + extern virtual function bit matches_real(output json_real value); // Try to cast current value to `json_bool` - extern virtual function bit matches_json_bool(output json_bool value); + extern virtual function bit matches_bool(output json_bool value); // Try to represent current value as `json_object` - extern virtual function json_result#(json_object) as_json_object(); + extern virtual function json_result#(json_object) as_object(); // Try to represent current value as `json_array` - extern virtual function json_result#(json_array) as_json_array(); + extern virtual function json_result#(json_array) as_array(); // Try to represent current value as `json_string` - extern virtual function json_result#(json_string) as_json_string(); + extern virtual function json_result#(json_string) as_string(); // Try to represent current value as `json_int` - extern virtual function json_result#(json_int) as_json_int(); + extern virtual function json_result#(json_int) as_int(); // Try to represent current value as `json_real` - extern virtual function json_result#(json_real) as_json_real(); + extern virtual function json_result#(json_real) as_real(); // Try to represent current value as `json_bool` - extern virtual function json_result#(json_bool) as_json_bool(); + extern virtual function json_result#(json_bool) as_bool(); // Check if current value is `json_object` - extern virtual function bit is_json_object(); + extern virtual function bit is_object(); // Check if current value is `json_array` - extern virtual function bit is_json_array(); + extern virtual function bit is_array(); // Check if current value is `json_string` - extern virtual function bit is_json_string(); + extern virtual function bit is_string(); // Check if current value is `json_int` - extern virtual function bit is_json_int(); + extern virtual function bit is_int(); // Check if current value is `json_real` - extern virtual function bit is_json_real(); + extern virtual function bit is_real(); // Check if current value is `json_bool` - extern virtual function bit is_json_bool(); + extern virtual function bit is_bool(); endclass : json_value -function bit json_value::matches_json_object(output json_object value); +function bit json_value::matches_object(output json_object value); return $cast(value, this); -endfunction : matches_json_object +endfunction : matches_object -function bit json_value::matches_json_array(output json_array value); +function bit json_value::matches_array(output json_array value); return $cast(value, this); -endfunction : matches_json_array +endfunction : matches_array -function bit json_value::matches_json_string(output json_string value); +function bit json_value::matches_string(output json_string value); return $cast(value, this); -endfunction : matches_json_string +endfunction : matches_string -function bit json_value::matches_json_int(output json_int value); +function bit json_value::matches_int(output json_int value); return $cast(value, this); -endfunction : matches_json_int +endfunction : matches_int -function bit json_value::matches_json_real(output json_real value); +function bit json_value::matches_real(output json_real value); return $cast(value, this); -endfunction : matches_json_real +endfunction : matches_real -function bit json_value::matches_json_bool(output json_bool value); +function bit json_value::matches_bool(output json_bool value); return $cast(value, this); -endfunction : matches_json_bool +endfunction : matches_bool -function json_result#(json_object) json_value::as_json_object(); +function json_result#(json_object) json_value::as_object(); json_object value; - if (this.matches_json_object(value)) begin + if (this.matches_object(value)) begin return json_result#(json_object)::ok(value); end else begin return json_result#(json_object)::err(json_error::create(json_error::TYPE_CONVERSION)); end -endfunction : as_json_object +endfunction : as_object -function json_result#(json_array) json_value::as_json_array(); +function json_result#(json_array) json_value::as_array(); json_array value; - if (this.matches_json_array(value)) begin + if (this.matches_array(value)) begin return json_result#(json_array)::ok(value); end else begin return json_result#(json_array)::err(json_error::create(json_error::TYPE_CONVERSION)); end -endfunction : as_json_array +endfunction : as_array -function json_result#(json_string) json_value::as_json_string(); +function json_result#(json_string) json_value::as_string(); json_string value; - if (this.matches_json_string(value)) begin + if (this.matches_string(value)) begin return json_result#(json_string)::ok(value); end else begin return json_result#(json_string)::err(json_error::create(json_error::TYPE_CONVERSION)); end -endfunction : as_json_string +endfunction : as_string -function json_result#(json_int) json_value::as_json_int(); +function json_result#(json_int) json_value::as_int(); json_int value; - if (this.matches_json_int(value)) begin + if (this.matches_int(value)) begin return json_result#(json_int)::ok(value); end else begin return json_result#(json_int)::err(json_error::create(json_error::TYPE_CONVERSION)); end -endfunction : as_json_int +endfunction : as_int -function json_result#(json_real) json_value::as_json_real(); +function json_result#(json_real) json_value::as_real(); json_real value; - if (this.matches_json_real(value)) begin + if (this.matches_real(value)) begin return json_result#(json_real)::ok(value); end else begin return json_result#(json_real)::err(json_error::create(json_error::TYPE_CONVERSION)); end -endfunction : as_json_real +endfunction : as_real -function json_result#(json_bool) json_value::as_json_bool(); +function json_result#(json_bool) json_value::as_bool(); json_bool value; - if (this.matches_json_bool(value)) begin + if (this.matches_bool(value)) begin return json_result#(json_bool)::ok(value); end else begin return json_result#(json_bool)::err(json_error::create(json_error::TYPE_CONVERSION)); end -endfunction : as_json_bool +endfunction : as_bool -function bit json_value::is_json_object(); +function bit json_value::is_object(); json_object value; - return this.matches_json_object(value); -endfunction : is_json_object + return this.matches_object(value); +endfunction : is_object -function bit json_value::is_json_array(); +function bit json_value::is_array(); json_array value; - return this.matches_json_array(value); -endfunction : is_json_array + return this.matches_array(value); +endfunction : is_array -function bit json_value::is_json_string(); +function bit json_value::is_string(); json_string value; - return this.matches_json_string(value); -endfunction : is_json_string + return this.matches_string(value); +endfunction : is_string -function bit json_value::is_json_int(); +function bit json_value::is_int(); json_int value; - return this.matches_json_int(value); -endfunction : is_json_int + return this.matches_int(value); +endfunction : is_int -function bit json_value::is_json_real(); +function bit json_value::is_real(); json_real value; - return this.matches_json_real(value); -endfunction : is_json_real + return this.matches_real(value); +endfunction : is_real -function bit json_value::is_json_bool(); +function bit json_value::is_bool(); json_bool value; - return this.matches_json_bool(value); -endfunction : is_json_bool + return this.matches_bool(value); +endfunction : is_bool diff --git a/tests/json_array_unit_test.sv b/tests/json_array_unit_test.sv index d590fad..9433c10 100644 --- a/tests/json_array_unit_test.sv +++ b/tests/json_array_unit_test.sv @@ -28,7 +28,7 @@ module json_array_unit_test; `SVTEST(create_new_test) begin json_array jarray; jarray = new('{json_bool::from(0)}); - `FAIL_UNLESS(jarray.get(0).as_json_bool().unwrap().get() == 0) + `FAIL_UNLESS(jarray.get(0).as_bool().unwrap().get() == 0) end `SVTEST_END @@ -36,25 +36,25 @@ module json_array_unit_test; `SVTEST(create_static_test) begin json_array jarray; jarray = json_array::from('{json_bool::from(0)}); - `FAIL_UNLESS(jarray.get(0).as_json_bool().unwrap().get() == 0) + `FAIL_UNLESS(jarray.get(0).as_bool().unwrap().get() == 0) end `SVTEST_END // Clone json_array instance `SVTEST(clone_array_test) begin json_array orig = json_array::from('{json_bool::from(0), json_int::from(-13)}); - json_array clone = orig.clone().as_json_array().unwrap(); + json_array clone = orig.clone().as_array().unwrap(); `FAIL_IF(orig == clone) `FAIL_UNLESS(clone.size() == 2) - `FAIL_UNLESS(clone.get(0).as_json_bool().unwrap().get() == 0) - `FAIL_UNLESS(clone.get(1).as_json_int().unwrap().get() == -13) + `FAIL_UNLESS(clone.get(0).as_bool().unwrap().get() == 0) + `FAIL_UNLESS(clone.get(1).as_int().unwrap().get() == -13) end `SVTEST_END // Clone empty array `SVTEST(clone_empty_array_test) begin json_array orig = json_array::from('{}); - json_array clone = orig.clone().as_json_array().unwrap(); + json_array clone = orig.clone().as_array().unwrap(); `FAIL_IF(orig == clone) `FAIL_UNLESS(clone.size() == 0) end `SVTEST_END @@ -64,11 +64,11 @@ module json_array_unit_test; `SVTEST(clone_array_with_nulls_test) begin string str = "foo"; json_array orig = json_array::from('{null, json_string::from(str), null}); - json_array clone = orig.clone().as_json_array().unwrap(); + json_array clone = orig.clone().as_array().unwrap(); `FAIL_IF(orig == clone) `FAIL_UNLESS(clone.size() == 3) `FAIL_UNLESS(clone.get(0) == null) - `FAIL_UNLESS(clone.get(1).as_json_string().unwrap().value == str) + `FAIL_UNLESS(clone.get(1).as_string().unwrap().value == str) `FAIL_UNLESS(clone.get(2) == null) end `SVTEST_END @@ -81,24 +81,24 @@ module json_array_unit_test; json_array::from('{json_int::from(0), json_array::from('{})}), json_object::from('{"bar": json_bool::from(1)}) }); - json_array clone = orig.clone().as_json_array().unwrap(); + json_array clone = orig.clone().as_array().unwrap(); `FAIL_IF(orig == clone) `FAIL_UNLESS(clone.size() == 3) - `FAIL_UNLESS(clone.get(0).as_json_real().unwrap().get() == 0.008) - `FAIL_UNLESS(clone.get(1).is_json_array()) - `FAIL_UNLESS(clone.get(1).as_json_array().unwrap().size() == 2) - `FAIL_UNLESS(clone.get(1).as_json_array().unwrap().get(0).as_json_int().unwrap().get() == 0) - `FAIL_UNLESS(clone.get(1).as_json_array().unwrap().get(1).as_json_array().unwrap().size() == 0) - `FAIL_UNLESS(clone.get(2).is_json_object()) - `FAIL_UNLESS(clone.get(2).as_json_object().unwrap().size() == 1) - `FAIL_UNLESS(clone.get(2).as_json_object().unwrap().get(str).as_json_bool().unwrap().get() == 1) + `FAIL_UNLESS(clone.get(0).as_real().unwrap().get() == 0.008) + `FAIL_UNLESS(clone.get(1).is_array()) + `FAIL_UNLESS(clone.get(1).as_array().unwrap().size() == 2) + `FAIL_UNLESS(clone.get(1).as_array().unwrap().get(0).as_int().unwrap().get() == 0) + `FAIL_UNLESS(clone.get(1).as_array().unwrap().get(1).as_array().unwrap().size() == 0) + `FAIL_UNLESS(clone.get(2).is_object()) + `FAIL_UNLESS(clone.get(2).as_object().unwrap().size() == 1) + `FAIL_UNLESS(clone.get(2).as_object().unwrap().get(str).as_bool().unwrap().get() == 1) end `SVTEST_END // Compare json_array instances `SVTEST(compare_array_test) begin json_array jarray_a = json_array::from('{json_int::from(42), json_string::from("verilog")}); - json_array jarray_b = jarray_a.clone().as_json_array().unwrap(); + json_array jarray_b = jarray_a.clone().as_array().unwrap(); // OK `FAIL_UNLESS(jarray_a.compare(jarray_a)) `FAIL_UNLESS(jarray_a.compare(jarray_b)) @@ -140,7 +140,7 @@ module json_array_unit_test; "baz": json_array::from('{null, json_int::from(0)}) }) }); - json_array jarray_b = jarray_a.clone().as_json_array().unwrap(); + json_array jarray_b = jarray_a.clone().as_array().unwrap(); // OK `FAIL_UNLESS(jarray_a.compare(jarray_b)) @@ -148,9 +148,9 @@ module json_array_unit_test; // Fail jarray_b.set(1, null); `FAIL_IF(jarray_a.compare(jarray_b)) - jarray_b = jarray_a.clone().as_json_array().unwrap(); + jarray_b = jarray_a.clone().as_array().unwrap(); - jarray_b.get(2).as_json_object().unwrap().set("baz", json_int::from(42)); + jarray_b.get(2).as_object().unwrap().set("baz", json_int::from(42)); `FAIL_IF(jarray_a.compare(jarray_b)) end `SVTEST_END @@ -179,10 +179,10 @@ module json_array_unit_test; json_int jint1 = json_int::from(-113); json_int jint2 = json_int::from(512); json_array jarray = json_array::from('{jint0, jint1}); - `FAIL_UNLESS(jarray.get(0).as_json_int().unwrap() == jint0) - `FAIL_UNLESS(jarray.get(1).as_json_int().unwrap() == jint1) + `FAIL_UNLESS(jarray.get(0).as_int().unwrap() == jint0) + `FAIL_UNLESS(jarray.get(1).as_int().unwrap() == jint1) jarray.set(0, jint2); - `FAIL_UNLESS(jarray.get(0).as_json_int().unwrap() == jint2) + `FAIL_UNLESS(jarray.get(0).as_int().unwrap() == jint2) jarray.set(1, null); `FAIL_UNLESS(jarray.get(1) == null) end `SVTEST_END @@ -206,12 +206,12 @@ module json_array_unit_test; json_array jarray = json_array::from('{}); json_value jvalue = jarray; - `FAIL_IF(jvalue.is_json_object()) - `FAIL_UNLESS(jvalue.is_json_array()) - `FAIL_IF(jvalue.is_json_string()) - `FAIL_IF(jvalue.is_json_int()) - `FAIL_IF(jvalue.is_json_real()) - `FAIL_IF(jvalue.is_json_bool()) + `FAIL_IF(jvalue.is_object()) + `FAIL_UNLESS(jvalue.is_array()) + `FAIL_IF(jvalue.is_string()) + `FAIL_IF(jvalue.is_int()) + `FAIL_IF(jvalue.is_real()) + `FAIL_IF(jvalue.is_bool()) end `SVTEST_END @@ -227,12 +227,12 @@ module json_array_unit_test; json_array orig_jarray = json_array::from('{}); json_value jvalue = orig_jarray; - `FAIL_IF(jvalue.matches_json_object(jobject)) - `FAIL_UNLESS(jvalue.matches_json_array(jarray)) - `FAIL_IF(jvalue.matches_json_string(jstring)) - `FAIL_IF(jvalue.matches_json_int(jint)) - `FAIL_IF(jvalue.matches_json_real(jreal)) - `FAIL_IF(jvalue.matches_json_bool(jbool)) + `FAIL_IF(jvalue.matches_object(jobject)) + `FAIL_UNLESS(jvalue.matches_array(jarray)) + `FAIL_IF(jvalue.matches_string(jstring)) + `FAIL_IF(jvalue.matches_int(jint)) + `FAIL_IF(jvalue.matches_real(jreal)) + `FAIL_IF(jvalue.matches_bool(jbool)) `FAIL_UNLESS(jarray == orig_jarray) end `SVTEST_END @@ -250,12 +250,12 @@ module json_array_unit_test; json_array orig_jarray = json_array::from('{}); json_value jvalue = orig_jarray; - res_jobject = jvalue.as_json_object(); - res_jarray = jvalue.as_json_array(); - res_jstring = jvalue.as_json_string(); - res_jint = jvalue.as_json_int(); - res_jreal = jvalue.as_json_real(); - res_jbool = jvalue.as_json_bool(); + res_jobject = jvalue.as_object(); + res_jarray = jvalue.as_array(); + res_jstring = jvalue.as_string(); + res_jint = jvalue.as_int(); + res_jreal = jvalue.as_real(); + res_jbool = jvalue.as_bool(); `FAIL_IF(res_jobject.is_ok()) `FAIL_UNLESS(res_jarray.is_ok()) diff --git a/tests/json_bool_unit_test.sv b/tests/json_bool_unit_test.sv index 66f60be..ab297ea 100644 --- a/tests/json_bool_unit_test.sv +++ b/tests/json_bool_unit_test.sv @@ -43,7 +43,7 @@ module json_bool_unit_test; // Clone json_bool instance `SVTEST(clone_test) begin json_bool orig = json_bool::from(1); - json_bool clone = orig.clone().as_json_bool().unwrap(); + json_bool clone = orig.clone().as_bool().unwrap(); `FAIL_IF(orig == clone) `FAIL_UNLESS(orig.get() == clone.get()) end `SVTEST_END @@ -109,12 +109,12 @@ module json_bool_unit_test; json_bool jbool = json_bool::from(1); json_value jvalue = jbool; - `FAIL_IF(jvalue.is_json_object()) - `FAIL_IF(jvalue.is_json_array()) - `FAIL_IF(jvalue.is_json_string()) - `FAIL_IF(jvalue.is_json_int()) - `FAIL_IF(jvalue.is_json_real()) - `FAIL_UNLESS(jvalue.is_json_bool()) + `FAIL_IF(jvalue.is_object()) + `FAIL_IF(jvalue.is_array()) + `FAIL_IF(jvalue.is_string()) + `FAIL_IF(jvalue.is_int()) + `FAIL_IF(jvalue.is_real()) + `FAIL_UNLESS(jvalue.is_bool()) end `SVTEST_END @@ -130,12 +130,12 @@ module json_bool_unit_test; json_bool orig_jbool = json_bool::from(0); json_value jvalue = orig_jbool; - `FAIL_IF(jvalue.matches_json_object(jobject)) - `FAIL_IF(jvalue.matches_json_array(jarray)) - `FAIL_IF(jvalue.matches_json_string(jstring)) - `FAIL_IF(jvalue.matches_json_int(jint)) - `FAIL_IF(jvalue.matches_json_real(jreal)) - `FAIL_UNLESS(jvalue.matches_json_bool(jbool)) + `FAIL_IF(jvalue.matches_object(jobject)) + `FAIL_IF(jvalue.matches_array(jarray)) + `FAIL_IF(jvalue.matches_string(jstring)) + `FAIL_IF(jvalue.matches_int(jint)) + `FAIL_IF(jvalue.matches_real(jreal)) + `FAIL_UNLESS(jvalue.matches_bool(jbool)) `FAIL_UNLESS(jbool == orig_jbool) end `SVTEST_END @@ -153,12 +153,12 @@ module json_bool_unit_test; json_bool orig_jbool = json_bool::from(1); json_value jvalue = orig_jbool; - res_jobject = jvalue.as_json_object(); - res_jarray = jvalue.as_json_array(); - res_jstring = jvalue.as_json_string(); - res_jint = jvalue.as_json_int(); - res_jreal = jvalue.as_json_real(); - res_jbool = jvalue.as_json_bool(); + res_jobject = jvalue.as_object(); + res_jarray = jvalue.as_array(); + res_jstring = jvalue.as_string(); + res_jint = jvalue.as_int(); + res_jreal = jvalue.as_real(); + res_jbool = jvalue.as_bool(); `FAIL_IF(res_jobject.is_ok()) `FAIL_IF(res_jarray.is_ok()) diff --git a/tests/json_int_unit_test.sv b/tests/json_int_unit_test.sv index 0726780..becfb87 100644 --- a/tests/json_int_unit_test.sv +++ b/tests/json_int_unit_test.sv @@ -43,7 +43,7 @@ module json_int_unit_test; // Clone json_int instance `SVTEST(clone_test) begin json_int orig = json_int::from(42); - json_int clone = orig.clone().as_json_int().unwrap(); + json_int clone = orig.clone().as_int().unwrap(); `FAIL_IF(orig == clone) `FAIL_UNLESS(orig.get() == clone.get()) end `SVTEST_END @@ -109,12 +109,12 @@ module json_int_unit_test; json_int jint = json_int::from(42); json_value jvalue = jint; - `FAIL_IF(jvalue.is_json_object()) - `FAIL_IF(jvalue.is_json_array()) - `FAIL_IF(jvalue.is_json_string()) - `FAIL_UNLESS(jvalue.is_json_int()) - `FAIL_IF(jvalue.is_json_real()) - `FAIL_IF(jvalue.is_json_bool()) + `FAIL_IF(jvalue.is_object()) + `FAIL_IF(jvalue.is_array()) + `FAIL_IF(jvalue.is_string()) + `FAIL_UNLESS(jvalue.is_int()) + `FAIL_IF(jvalue.is_real()) + `FAIL_IF(jvalue.is_bool()) end `SVTEST_END @@ -130,12 +130,12 @@ module json_int_unit_test; json_int orig_jint = json_int::from(42); json_value jvalue = orig_jint; - `FAIL_IF(jvalue.matches_json_object(jobject)) - `FAIL_IF(jvalue.matches_json_array(jarray)) - `FAIL_IF(jvalue.matches_json_string(jstring)) - `FAIL_UNLESS(jvalue.matches_json_int(jint)) - `FAIL_IF(jvalue.matches_json_real(jreal)) - `FAIL_IF(jvalue.matches_json_bool(jbool)) + `FAIL_IF(jvalue.matches_object(jobject)) + `FAIL_IF(jvalue.matches_array(jarray)) + `FAIL_IF(jvalue.matches_string(jstring)) + `FAIL_UNLESS(jvalue.matches_int(jint)) + `FAIL_IF(jvalue.matches_real(jreal)) + `FAIL_IF(jvalue.matches_bool(jbool)) `FAIL_UNLESS(jint == orig_jint) end `SVTEST_END @@ -153,12 +153,12 @@ module json_int_unit_test; json_int orig_jint = json_int::from(42); json_value jvalue = orig_jint; - res_jobject = jvalue.as_json_object(); - res_jarray = jvalue.as_json_array(); - res_jstring = jvalue.as_json_string(); - res_jint = jvalue.as_json_int(); - res_jreal = jvalue.as_json_real(); - res_jbool = jvalue.as_json_bool(); + res_jobject = jvalue.as_object(); + res_jarray = jvalue.as_array(); + res_jstring = jvalue.as_string(); + res_jint = jvalue.as_int(); + res_jreal = jvalue.as_real(); + res_jbool = jvalue.as_bool(); `FAIL_IF(res_jobject.is_ok()) `FAIL_IF(res_jarray.is_ok()) diff --git a/tests/json_object_unit_test.sv b/tests/json_object_unit_test.sv index d23c479..1e51c61 100644 --- a/tests/json_object_unit_test.sv +++ b/tests/json_object_unit_test.sv @@ -29,7 +29,7 @@ module json_object_unit_test; string key = "foo"; json_object jobject; jobject = new('{"foo": json_bool::from(0)}); - `FAIL_UNLESS(jobject.get(key).as_json_bool().unwrap().get() == 0) + `FAIL_UNLESS(jobject.get(key).as_bool().unwrap().get() == 0) end `SVTEST_END @@ -38,7 +38,7 @@ module json_object_unit_test; string key = "foo"; json_object jobject; jobject = json_object::from('{"foo": json_bool::from(0)}); - `FAIL_UNLESS(jobject.get(key).as_json_bool().unwrap().get() == 0) + `FAIL_UNLESS(jobject.get(key).as_bool().unwrap().get() == 0) end `SVTEST_END @@ -46,10 +46,10 @@ module json_object_unit_test; `SVTEST(clone_object_test) begin string str = "foo"; json_object orig = json_object::from('{"foo": json_int::from(12345)}); - json_object clone = orig.clone().as_json_object().unwrap(); + json_object clone = orig.clone().as_object().unwrap(); `FAIL_IF(orig == clone) `FAIL_UNLESS(clone.size() == 1) - `FAIL_UNLESS(clone.get(str).as_json_int().unwrap().get() == 12345) + `FAIL_UNLESS(clone.get(str).as_int().unwrap().get() == 12345) end `SVTEST_END @@ -57,11 +57,11 @@ module json_object_unit_test; `SVTEST(clone_object_with_nulls_test) begin json_value jvalue; json_object orig = json_object::from('{"foo": json_string::from("blabla"), "bar": null}); - json_object clone = orig.clone().as_json_object().unwrap(); + json_object clone = orig.clone().as_object().unwrap(); `FAIL_IF(orig == clone) `FAIL_UNLESS(clone.size() == 2) jvalue = clone.get("foo"); - `FAIL_UNLESS_STR_EQUAL(jvalue.as_json_string().unwrap().value, "blabla") + `FAIL_UNLESS_STR_EQUAL(jvalue.as_string().unwrap().value, "blabla") jvalue = clone.get("bar"); `FAIL_UNLESS(jvalue == null) end `SVTEST_END @@ -80,27 +80,27 @@ module json_object_unit_test; json_object::from('{"bar": json_bool::from(1)}) }) }); - json_object clone = orig.clone().as_json_object().unwrap(); + json_object clone = orig.clone().as_object().unwrap(); `FAIL_IF(orig == clone) `FAIL_UNLESS(clone.size() == 2) - jreal = clone.get("real").as_json_real().unwrap(); + jreal = clone.get("real").as_real().unwrap(); `FAIL_UNLESS(jreal.get() == 0.002) - jarray = clone.get("array").as_json_array().unwrap(); + jarray = clone.get("array").as_array().unwrap(); `FAIL_UNLESS(jarray.size() == 2) - `FAIL_UNLESS(jarray.get(0).as_json_array().unwrap().size() == 0) + `FAIL_UNLESS(jarray.get(0).as_array().unwrap().size() == 0) - jobject = jarray.get(1).as_json_object().unwrap(); + jobject = jarray.get(1).as_object().unwrap(); `FAIL_UNLESS(jobject.size() == 1) - `FAIL_UNLESS(jobject.get(str).as_json_bool().unwrap().get() == 1) + `FAIL_UNLESS(jobject.get(str).as_bool().unwrap().get() == 1) end `SVTEST_END // Compare object instances `SVTEST(compare_object_test) begin json_object jobject_a = json_object::from('{"key": json_int::from(42)}); - json_object jobject_b = jobject_a.clone().as_json_object().unwrap(); + json_object jobject_b = jobject_a.clone().as_object().unwrap(); // OK `FAIL_UNLESS(jobject_a.compare(jobject_a)) `FAIL_UNLESS(jobject_a.compare(jobject_b)) @@ -139,7 +139,7 @@ module json_object_unit_test; "baz": json_array::from('{null, json_int::from(0)}) }) }); - json_object jobject_b = jobject_a.clone().as_json_object().unwrap(); + json_object jobject_b = jobject_a.clone().as_object().unwrap(); // OK `FAIL_UNLESS(jobject_a.compare(jobject_b)) @@ -147,9 +147,9 @@ module json_object_unit_test; // Fail jobject_b.set("key2", null); `FAIL_IF(jobject_a.compare(jobject_b)) - jobject_b = jobject_a.clone().as_json_object().unwrap(); + jobject_b = jobject_a.clone().as_object().unwrap(); - jobject_b.get("key3").as_json_object().unwrap().set("baz", json_int::from(42)); + jobject_b.get("key3").as_object().unwrap().set("baz", json_int::from(42)); `FAIL_IF(jobject_a.compare(jobject_b)) end `SVTEST_END @@ -180,12 +180,12 @@ module json_object_unit_test; json_value jvalue; json_object jobject = json_object::from('{"aaa": jint0, "bbb": jint1}); - jint_temp = jobject.get("aaa").as_json_int().unwrap(); + jint_temp = jobject.get("aaa").as_int().unwrap(); `FAIL_UNLESS(jint_temp == jint0) - jint_temp = jobject.get("bbb").as_json_int().unwrap(); + jint_temp = jobject.get("bbb").as_int().unwrap(); `FAIL_UNLESS(jint_temp == jint1) jobject.set("aaa", jint2); - jint_temp = jobject.get("aaa").as_json_int().unwrap(); + jint_temp = jobject.get("aaa").as_int().unwrap(); `FAIL_UNLESS(jint_temp == jint2) jobject.set("ccc", null); jvalue = jobject.get("ccc"); @@ -217,12 +217,12 @@ module json_object_unit_test; json_object jobject = json_object::from('{}); json_value jvalue = jobject; - `FAIL_UNLESS(jvalue.is_json_object()) - `FAIL_IF(jvalue.is_json_array()) - `FAIL_IF(jvalue.is_json_string()) - `FAIL_IF(jvalue.is_json_int()) - `FAIL_IF(jvalue.is_json_real()) - `FAIL_IF(jvalue.is_json_bool()) + `FAIL_UNLESS(jvalue.is_object()) + `FAIL_IF(jvalue.is_array()) + `FAIL_IF(jvalue.is_string()) + `FAIL_IF(jvalue.is_int()) + `FAIL_IF(jvalue.is_real()) + `FAIL_IF(jvalue.is_bool()) end `SVTEST_END @@ -238,12 +238,12 @@ module json_object_unit_test; json_object orig_jobject = json_object::from('{}); json_value jvalue = orig_jobject; - `FAIL_UNLESS(jvalue.matches_json_object(jobject)) - `FAIL_IF(jvalue.matches_json_array(jarray)) - `FAIL_IF(jvalue.matches_json_string(jstring)) - `FAIL_IF(jvalue.matches_json_int(jint)) - `FAIL_IF(jvalue.matches_json_real(jreal)) - `FAIL_IF(jvalue.matches_json_bool(jbool)) + `FAIL_UNLESS(jvalue.matches_object(jobject)) + `FAIL_IF(jvalue.matches_array(jarray)) + `FAIL_IF(jvalue.matches_string(jstring)) + `FAIL_IF(jvalue.matches_int(jint)) + `FAIL_IF(jvalue.matches_real(jreal)) + `FAIL_IF(jvalue.matches_bool(jbool)) `FAIL_UNLESS(jobject == orig_jobject) end `SVTEST_END @@ -261,12 +261,12 @@ module json_object_unit_test; json_object orig_jobject = json_object::from('{}); json_value jvalue = orig_jobject; - res_jobject = jvalue.as_json_object(); - res_jarray = jvalue.as_json_array(); - res_jstring = jvalue.as_json_string(); - res_jint = jvalue.as_json_int(); - res_jreal = jvalue.as_json_real(); - res_jbool = jvalue.as_json_bool(); + res_jobject = jvalue.as_object(); + res_jarray = jvalue.as_array(); + res_jstring = jvalue.as_string(); + res_jint = jvalue.as_int(); + res_jreal = jvalue.as_real(); + res_jbool = jvalue.as_bool(); `FAIL_UNLESS(res_jobject.is_ok()) `FAIL_IF(res_jarray.is_ok()) diff --git a/tests/json_real_unit_test.sv b/tests/json_real_unit_test.sv index 638fd6a..4f0ad8e 100644 --- a/tests/json_real_unit_test.sv +++ b/tests/json_real_unit_test.sv @@ -43,7 +43,7 @@ module json_real_unit_test; // Clone json_real instance `SVTEST(clone_test) begin json_real orig = json_real::from(0.234); - json_real clone = orig.clone().as_json_real().unwrap(); + json_real clone = orig.clone().as_real().unwrap(); `FAIL_IF(orig == clone) `FAIL_UNLESS(orig.get() == clone.get()) end `SVTEST_END @@ -109,12 +109,12 @@ module json_real_unit_test; json_real jreal = json_real::from(42.0); json_value jvalue = jreal; - `FAIL_IF(jvalue.is_json_object()) - `FAIL_IF(jvalue.is_json_array()) - `FAIL_IF(jvalue.is_json_string()) - `FAIL_IF(jvalue.is_json_int()) - `FAIL_UNLESS(jvalue.is_json_real()) - `FAIL_IF(jvalue.is_json_bool()) + `FAIL_IF(jvalue.is_object()) + `FAIL_IF(jvalue.is_array()) + `FAIL_IF(jvalue.is_string()) + `FAIL_IF(jvalue.is_int()) + `FAIL_UNLESS(jvalue.is_real()) + `FAIL_IF(jvalue.is_bool()) end `SVTEST_END @@ -130,12 +130,12 @@ module json_real_unit_test; json_real orig_jreal = json_real::from(2.71); json_value jvalue = orig_jreal; - `FAIL_IF(jvalue.matches_json_object(jobject)) - `FAIL_IF(jvalue.matches_json_array(jarray)) - `FAIL_IF(jvalue.matches_json_string(jstring)) - `FAIL_IF(jvalue.matches_json_int(jint)) - `FAIL_UNLESS(jvalue.matches_json_real(jreal)) - `FAIL_IF(jvalue.matches_json_bool(jbool)) + `FAIL_IF(jvalue.matches_object(jobject)) + `FAIL_IF(jvalue.matches_array(jarray)) + `FAIL_IF(jvalue.matches_string(jstring)) + `FAIL_IF(jvalue.matches_int(jint)) + `FAIL_UNLESS(jvalue.matches_real(jreal)) + `FAIL_IF(jvalue.matches_bool(jbool)) `FAIL_UNLESS(jreal == orig_jreal) end `SVTEST_END @@ -153,12 +153,12 @@ module json_real_unit_test; json_real orig_jreal = json_real::from(-3e5); json_value jvalue = orig_jreal; - res_jobject = jvalue.as_json_object(); - res_jarray = jvalue.as_json_array(); - res_jstring = jvalue.as_json_string(); - res_jint = jvalue.as_json_int(); - res_jreal = jvalue.as_json_real(); - res_jbool = jvalue.as_json_bool(); + res_jobject = jvalue.as_object(); + res_jarray = jvalue.as_array(); + res_jstring = jvalue.as_string(); + res_jint = jvalue.as_int(); + res_jreal = jvalue.as_real(); + res_jbool = jvalue.as_bool(); `FAIL_IF(res_jobject.is_ok()) `FAIL_IF(res_jarray.is_ok()) diff --git a/tests/json_string_unit_test.sv b/tests/json_string_unit_test.sv index aad1037..566eb07 100644 --- a/tests/json_string_unit_test.sv +++ b/tests/json_string_unit_test.sv @@ -43,7 +43,7 @@ module json_string_unit_test; // Clone json_string instance `SVTEST(clone_test) begin json_string orig = json_string::from("!@#"); - json_string clone = orig.clone().as_json_string().unwrap(); + json_string clone = orig.clone().as_string().unwrap(); `FAIL_IF(orig == clone) `FAIL_UNLESS(orig.get() == clone.get()) end `SVTEST_END @@ -109,12 +109,12 @@ module json_string_unit_test; json_string jstring = json_string::from("42"); json_value jvalue = jstring; - `FAIL_IF(jvalue.is_json_object()) - `FAIL_IF(jvalue.is_json_array()) - `FAIL_UNLESS(jvalue.is_json_string()) - `FAIL_IF(jvalue.is_json_int()) - `FAIL_IF(jvalue.is_json_real()) - `FAIL_IF(jvalue.is_json_bool()) + `FAIL_IF(jvalue.is_object()) + `FAIL_IF(jvalue.is_array()) + `FAIL_UNLESS(jvalue.is_string()) + `FAIL_IF(jvalue.is_int()) + `FAIL_IF(jvalue.is_real()) + `FAIL_IF(jvalue.is_bool()) end `SVTEST_END @@ -130,12 +130,12 @@ module json_string_unit_test; json_string orig_jstring = json_string::from("42"); json_value jvalue = orig_jstring; - `FAIL_IF(jvalue.matches_json_object(jobject)) - `FAIL_IF(jvalue.matches_json_array(jarray)) - `FAIL_UNLESS(jvalue.matches_json_string(jstring)) - `FAIL_IF(jvalue.matches_json_int(jint)) - `FAIL_IF(jvalue.matches_json_real(jreal)) - `FAIL_IF(jvalue.matches_json_bool(jbool)) + `FAIL_IF(jvalue.matches_object(jobject)) + `FAIL_IF(jvalue.matches_array(jarray)) + `FAIL_UNLESS(jvalue.matches_string(jstring)) + `FAIL_IF(jvalue.matches_int(jint)) + `FAIL_IF(jvalue.matches_real(jreal)) + `FAIL_IF(jvalue.matches_bool(jbool)) `FAIL_UNLESS(jstring == orig_jstring) end `SVTEST_END @@ -153,12 +153,12 @@ module json_string_unit_test; json_string orig_jstring = json_string::from("baz"); json_value jvalue = orig_jstring; - res_jobject = jvalue.as_json_object(); - res_jarray = jvalue.as_json_array(); - res_jstring = jvalue.as_json_string(); - res_jint = jvalue.as_json_int(); - res_jreal = jvalue.as_json_real(); - res_jbool = jvalue.as_json_bool(); + res_jobject = jvalue.as_object(); + res_jarray = jvalue.as_array(); + res_jstring = jvalue.as_string(); + res_jint = jvalue.as_int(); + res_jreal = jvalue.as_real(); + res_jbool = jvalue.as_bool(); `FAIL_IF(res_jobject.is_ok()) `FAIL_IF(res_jarray.is_ok())