Skip to content

Commit

Permalink
[ion-c-sys] Implements parameterized text read tests. (#66)
Browse files Browse the repository at this point in the history
* Implements parameterized text read tests for ion-c-sys.

* Removes the original unit tests.  The doc tests and these
  tests cover what they did and more.
* Adds dev-dependency to `rstest` to get the parameterized
  functionality.

Resolves #62.
  • Loading branch information
almann committed Aug 9, 2020
1 parent 04449f7 commit 09c0009
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 180 deletions.
2 changes: 2 additions & 0 deletions ion-c-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ paste = "1.0.0"
cmake = "0.1"
bindgen = "0.54"

[dev-dependencies]
rstest = "0.6.4"
180 changes: 0 additions & 180 deletions ion-c-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,183 +274,3 @@ ion_types!(
none, EOF, NULL, BOOL, INT, FLOAT, DECIMAL, TIMESTAMP, SYMBOL, STRING, CLOB, BLOB, LIST, SEXP,
STRUCT, DATAGRAM
);

#[cfg(test)]
mod tests {
use super::*;

use std::error::Error;
use std::ptr;
use std::string::String;

type TestResult = Result<(), Box<dyn Error>>;

#[test]
fn read_ion_null() -> TestResult {
let mut input = String::from("null");
let buf = input.as_mut_ptr();
let buf_size = input.len() as i32;
let mut ion_reader = ptr::null_mut();
let mut ion_type = ptr::null_mut();
let mut ion_type2 = ptr::null_mut();
let mut mybool: BOOL = 0;

ionc!(ion_reader_open_buffer(
&mut ion_reader,
buf,
buf_size,
ptr::null_mut()
))?;
ionc!(ion_reader_next(ion_reader, &mut ion_type))?;
ionc!(ion_reader_is_null(ion_reader, &mut mybool))?;
if mybool == 1 {
ionc!(ion_reader_read_null(ion_reader, &mut ion_type2))?;
}
assert_eq!(ION_TYPE_NULL, ion_type2);

ionc!(ion_reader_close(ion_reader))?;

Ok(())
}

#[test]
fn read_ion_timestamp_null() -> TestResult {
let mut input = String::from("null.timestamp");
let buf = input.as_mut_ptr();
let buf_size = input.len() as i32;
let mut ion_reader = ptr::null_mut();
let mut ion_type = ptr::null_mut();
let mut ion_type2 = ptr::null_mut();
let mut mybool: BOOL = 0;

ionc!(ion_reader_open_buffer(
&mut ion_reader,
buf,
buf_size,
ptr::null_mut()
))?;
ionc!(ion_reader_next(ion_reader, &mut ion_type))?;
ionc!(ion_reader_is_null(ion_reader, &mut mybool))?;
if mybool == 1 {
ionc!(ion_reader_read_null(ion_reader, &mut ion_type2))?;
}
assert_eq!(ION_TYPE_TIMESTAMP, ion_type2);

ionc!(ion_reader_close(ion_reader))?;

Ok(())
}

#[test]
fn read_ion_int() -> TestResult {
let mut input = String::from("42");
let buf = input.as_mut_ptr();
let buf_size = input.len() as i32;
let mut ion_reader = ptr::null_mut();
let mut ion_type = ptr::null_mut();
let mut ion_value = 0;

ionc!(ion_reader_open_buffer(
&mut ion_reader,
buf,
buf_size,
ptr::null_mut()
))?;
ionc!(ion_reader_next(ion_reader, &mut ion_type))?;
ionc!(ion_reader_read_int32(ion_reader, &mut ion_value))?;
assert_eq!(ION_TYPE_INT, ion_type);
assert_eq!(42, ion_value);

ionc!(ion_reader_close(ion_reader))?;

Ok(())
}

#[test]
fn read_ion_text() -> TestResult {
let mut input = String::from("\"this is a string\"");
let buf = input.as_mut_ptr();
let buf_size = input.len() as i32;
let mut ion_reader = ptr::null_mut();
let mut ion_type = ptr::null_mut();

ionc!(ion_reader_open_buffer(
&mut ion_reader,
buf,
buf_size,
ptr::null_mut()
))?;
ionc!(ion_reader_next(ion_reader, &mut ion_type))?;

let mut ion_str = ION_STRING::default();
ionc!(ion_reader_read_string(ion_reader, &mut ion_str))?;
assert_eq!("this is a string", ion_str.try_as_str()?);

ionc!(ion_reader_close(ion_reader))?;

Ok(())
}

#[test]
fn read_ion_symbol() -> TestResult {
let mut input = String::from("'this is a symbol'");
let buf = input.as_mut_ptr();
let buf_size = input.len() as i32;

let mut ion_reader = ptr::null_mut();
let mut ion_type = ptr::null_mut();

ionc!(ion_reader_open_buffer(
&mut ion_reader,
buf,
buf_size,
ptr::null_mut()
))?;
ionc!(ion_reader_next(ion_reader, &mut ion_type))?;

let mut ion_str = ION_STRING::default();
ionc!(ion_reader_read_string(ion_reader, &mut ion_str))?;
assert_eq!("this is a symbol", ion_str.try_as_str()?);

ionc!(ion_reader_close(ion_reader))?;

Ok(())
}

#[test]
fn read_sexp() -> TestResult {
let mut input = String::from("(1 2 3)");
let expected_vals = vec![1, 2, 3];
let mut read_vals: Vec<i32> = Vec::new();

let buf = input.as_mut_ptr();
let buf_size = input.len() as i32;
let mut ion_reader = ptr::null_mut();
let mut ion_type = ptr::null_mut();

ionc!(ion_reader_open_buffer(
&mut ion_reader,
buf,
buf_size,
ptr::null_mut()
))?;
ionc!(ion_reader_next(ion_reader, &mut ion_type))?;
ionc!(ion_reader_step_in(ion_reader))?;
while ion_type != ION_TYPE_EOF {
ionc!(ion_reader_next(ion_reader, &mut ion_type))?;

let mut ion_value = 0;
if ION_TYPE_INT == ion_type {
ionc!(ion_reader_read_int32(ion_reader, &mut ion_value))?;
read_vals.push(ion_value);
}
}
ionc!(ion_reader_step_out(ion_reader))?;

assert_eq!(read_vals, expected_vals);

ionc!(ion_reader_close(ion_reader))?;

Ok(())
}
}
Loading

0 comments on commit 09c0009

Please sign in to comment.