Skip to content

Commit

Permalink
tests: add tests for rust_decimal
Browse files Browse the repository at this point in the history
PR #127
  • Loading branch information
jmcnamara committed Jan 13, 2025
1 parent 5537e9e commit b14fe75
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/rust_decimal_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Rust - test rust_decimal feature

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Run the integration tests for the rust_decimal feature set
run: cargo test --test integration --features rust_decimal

4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@
//! easier to write.
//! - `wasm`: Adds a dependency on `js-sys` and `wasm-bindgen` to allow
//! compilation for wasm/JavaScript targets.
//! - `rust_decimal`: Adds support for writing the
//! [`rust_decimal`](https://crates.io/crates/rust_decimal) `Decimal` type
//! with `Worksheet::write_number()`, provided it can be represented by
//! [`f64`].
//! - `ryu`: Adds a dependency on `ryu`. This speeds up writing numeric
//! worksheet cells for large data files. It gives a performance boost above
//! 300,000 numeric cells and can be up to 30% faster than the default number
Expand Down
Binary file added tests/input/decimal01.xlsx
Binary file not shown.
69 changes: 69 additions & 0 deletions tests/integration/decimal01.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Test case that compares a file generated by rust_xlsxwriter with a file
// created by Excel.
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2024, John McNamara, [email protected]

#[cfg(feature = "rust_decimal")]
use rust_decimal::Decimal;

use crate::common;
use rust_xlsxwriter::{Format, Workbook, XlsxError};

// Test case to demonstrate creating a basic file with some numeric cell data.
fn create_new_xlsx_file_1(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();
let format = Format::new().set_num_format("\\$0.00");

worksheet.write(0, 0, 2.1)?;
worksheet.write_with_format(1, 0, 2.1, &format)?;

workbook.save(filename)?;

Ok(())
}

// Write numbers with rust_decimal.
#[cfg(feature = "rust_decimal")]
fn create_new_xlsx_file_2(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();
let format = Format::new().set_num_format("\\$0.00");

let decimal = Decimal::new(210, 2);

worksheet.write(0, 0, decimal)?;
worksheet.write_with_format(1, 0, decimal, &format)?;

workbook.save(filename)?;

workbook.save(filename)?;

Ok(())
}

#[test]
fn decimal01_1() {
let test_runner = common::TestRunner::new()
.set_name("decimal01")
.set_function(create_new_xlsx_file_1)
.unique("1")
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}

#[test]
fn decimal01_2() {
let test_runner = common::TestRunner::new()
.set_name("decimal01")
.set_function(create_new_xlsx_file_2)
.unique("2")
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}
2 changes: 2 additions & 0 deletions tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ mod cond_format22;
mod data_validation01;
mod data_validation02;
mod data_validation03;
#[cfg(feature = "rust_decimal")]
mod decimal01;
mod default_row01;
mod default_row02;
mod default_row03;
Expand Down

0 comments on commit b14fe75

Please sign in to comment.