Skip to content

Commit

Permalink
data validation: add integration tests
Browse files Browse the repository at this point in the history
Feature #97
  • Loading branch information
jmcnamara committed Jul 13, 2024
1 parent ac6b754 commit 9fea0ef
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 0 deletions.
Binary file added tests/input/data_validation01.xlsx
Binary file not shown.
Binary file added tests/input/data_validation02.xlsx
Binary file not shown.
Binary file added tests/input/data_validation03.xlsx
Binary file not shown.
35 changes: 35 additions & 0 deletions tests/integration/data_validation01.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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]

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

// Create rust_xlsxwriter file to compare against Excel file.
fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();

let worksheet = workbook.add_worksheet();

let data_validation = DataValidation::new().allow_list_strings(&["Foo", "Bar", "Baz"])?;

worksheet.add_data_validation(1, 2, 1, 2, &data_validation)?;

workbook.save(filename)?;

Ok(())
}

#[test]
fn test_data_validation01() {
let test_runner = common::TestRunner::new()
.set_name("data_validation01")
.set_function(create_new_xlsx_file)
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}
38 changes: 38 additions & 0 deletions tests/integration/data_validation02.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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]

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

// Create rust_xlsxwriter file to compare against Excel file.
fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();

let worksheet = workbook.add_worksheet();

let data_validation = DataValidation::new()
.allow_list_strings(&["Foo", "Bar", "Baz"])?
.set_input_title("This is the input title")?
.set_input_message("This is the input message")?;

worksheet.add_data_validation(1, 2, 1, 2, &data_validation)?;

workbook.save(filename)?;

Ok(())
}

#[test]
fn test_data_validation02() {
let test_runner = common::TestRunner::new()
.set_name("data_validation02")
.set_function(create_new_xlsx_file)
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}
56 changes: 56 additions & 0 deletions tests/integration/data_validation03.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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]

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

// Create rust_xlsxwriter file to compare against Excel file.
fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();

let worksheet = workbook.add_worksheet();

let data_validation = DataValidation::new()
.allow_list_strings(&["Foo", "Bar", "Baz"])?
.set_input_title("This is the input title")?
.set_input_message("This is the input message")?;

worksheet.add_data_validation(1, 2, 1, 2, &data_validation)?;

let invalid_title = "This is the longest input title1";
let padding = ["a"; 221];
let invalid_message = "This is the longest input message ".to_string() + &padding.concat();
let list_values = [
"Foobar", "Foobas", "Foobat", "Foobau", "Foobav", "Foobaw", "Foobax", "Foobay", "Foobaz",
"Foobba", "Foobbb", "Foobbc", "Foobbd", "Foobbe", "Foobbf", "Foobbg", "Foobbh", "Foobbi",
"Foobbj", "Foobbk", "Foobbl", "Foobbm", "Foobbn", "Foobbo", "Foobbp", "Foobbq", "Foobbr",
"Foobbs", "Foobbt", "Foobbu", "Foobbv", "Foobbw", "Foobbx", "Foobby", "Foobbz", "Foobca",
"End",
];

let data_validation = DataValidation::new()
.allow_list_strings(&list_values)?
.set_input_title(invalid_title)?
.set_input_message(invalid_message)?;

worksheet.add_data_validation(5, 3, 5, 3, &data_validation)?;

workbook.save(filename)?;

Ok(())
}

#[test]
fn test_data_validation03() {
let test_runner = common::TestRunner::new()
.set_name("data_validation03")
.set_function(create_new_xlsx_file)
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}
3 changes: 3 additions & 0 deletions tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ mod cond_format16;
mod cond_format18;
mod cond_format19;
mod cond_format20;
mod data_validation01;
mod data_validation02;
mod data_validation03;
mod defined_name01;
mod defined_name02;
mod defined_name03;
Expand Down

0 comments on commit 9fea0ef

Please sign in to comment.