Skip to content

Commit

Permalink
Fix failing tests and add a CI workflow to run tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nemuelw committed Oct 26, 2024
1 parent 64c7148 commit e7e49a7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Continuous Integration

on:
push:
branches:
main
pull_request:
branches:
main

jobs:
ci:
runs-on: ubuntu-latest
env:
CJSON_INCLUDE_PATH: /usr/include/cjson
CJSON_LIB_PATH: /usr/local/lib

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Install cJSON
run: sudo apt-get install -y libcjson-dev

- name: Set up Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true

- name: Test
run: cargo test --verbose
41 changes: 23 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ pub const CJSON_VERSION_PATCH: u32 = bindings::CJSON_VERSION_PATCH;
/// use cjson_rs::cjson_version;
///
/// fn main() {
/// assert_eq!(cjson_version(), "1.7.18");
/// assert_eq!(cjson_version(), "1.7.17");
/// println!("Test passed"); // output: Test passed
/// }
/// ```
pub fn cjson_version() -> String {
let cjson_version = unsafe { cJSON_Version() };
let version = unsafe { cJSON_Version() };
unsafe {
CStr::from_ptr(cjson_version)
CStr::from_ptr(version)
.to_str()
.unwrap_or_default()
.to_string()
Expand All @@ -32,16 +32,6 @@ pub fn cjson_version() -> String {
/// Fields:
/// - `malloc_fn`: Optional function pointer for custom memory allocation.
/// - `free_fn`: Optional function pointer for custom memory deallocation.
///
/// To create a new instance of `Hooks`, use its `new` method:
/// ```rust
/// let hooks = Hooks::new(Some(custom_malloc), Some(custom_free));
/// ```
///
/// To initialize hooks, use its `init` method:
/// ```rust
/// hooks.init();
/// ```
pub struct Hooks {
pub malloc_fn: Option<fn(sz: usize) -> *mut libc::c_void>,
pub free_fn: Option<fn(*mut libc::c_void)>,
Expand Down Expand Up @@ -119,8 +109,23 @@ impl Hooks {
///
/// Usage:
/// ```rust
/// let hooks = Hooks::new(Some(custom_malloc), Some(custom_free));
/// hooks.init();
/// use cjson_rs::Hooks;
/// use libc::{malloc, free};
///
/// fn custom_malloc(size: usize) -> *mut libc::c_void {
/// println!("allocating memory ...");
/// unsafe { malloc(size) }
/// }
///
/// fn custom_free(ptr: *mut libc::c_void) {
/// println!("freeing memory ...");
/// unsafe { free(ptr); }
/// }
///
/// fn main() {
/// let hooks = Hooks::new(Some(custom_malloc), Some(custom_free)); //
/// hooks.init();
/// }
/// ```
pub fn init(&self) {
unsafe {
Expand Down Expand Up @@ -643,7 +648,7 @@ pub fn cjson_minify(json: &mut String) -> Result<(), JsonError> {
///
/// fn main() {
/// let value = "{\"name\":\"Nemuel\", \"age\":20}".to_string();
/// match parse_json(value) {
/// match cjson_parse_json(value) {
/// Ok(json) => println!("{}", json.print().unwrap()),
/// Err(err) => eprintln!("{}", err),
/// }
Expand Down Expand Up @@ -694,7 +699,7 @@ pub fn cjson_parse_json(value: String) -> Result<*mut Json, JsonError> {
///
/// fn main() {
/// let value = "{\"rps\":500} more text".to_string();
/// match parse_json_with_length(value, 11) {
/// match cjson_parse_json_with_length(value, 11) {
/// Ok(json) => println!("{}", json.print().unwrap()),
/// Err(err) => eprintln!("{}", err),
/// }
Expand Down Expand Up @@ -934,7 +939,7 @@ pub fn cjson_create_null() -> *mut Json {
/// use cjson_rs::{cjson_create_object, Json};
///
/// fn main() {
/// let json: *mut Json = cjson_cjson_create_object();
/// let json: *mut Json = cjson_create_object();
/// }
/// ```
pub fn cjson_create_object() -> *mut Json {
Expand Down

0 comments on commit e7e49a7

Please sign in to comment.