Skip to content

Commit

Permalink
Run all tests in no_std environments if std feature is disabled (#…
Browse files Browse the repository at this point in the history
…269)

This adds the following line to all of our test files.
```rust
 #![cfg_attr(not(feature = "std"), no_std)]
```

This way, we test that all our features also work in `no_std` environments.

Co-authored-by: Kai Ren <tyranron@gmail.com>
  • Loading branch information
JelteF and tyranron authored Jul 5, 2023
1 parent d1ff0cf commit ae5b263
Show file tree
Hide file tree
Showing 24 changed files with 261 additions and 92 deletions.
1 change: 1 addition & 0 deletions tests/add.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(dead_code)]

use derive_more::Add;
Expand Down
1 change: 1 addition & 0 deletions tests/add_assign.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(dead_code)]

use derive_more::AddAssign;
Expand Down
118 changes: 65 additions & 53 deletions tests/as_mut.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(dead_code)]

use std::path::PathBuf;
use std::ptr;
#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
use alloc::{string::String, vec, vec::Vec};
use core::ptr;

use derive_more::AsMut;

Expand Down Expand Up @@ -39,36 +44,64 @@ fn single_field_struct() {
assert!(ptr::eq(&mut item.first, item.as_mut()));
}

#[derive(AsMut)]
struct MultiFieldTuple(#[as_mut] String, #[as_mut] PathBuf, Vec<usize>);

#[test]
fn multi_field_tuple() {
let mut item = MultiFieldTuple("test".into(), PathBuf::new(), vec![]);

assert!(ptr::eq(&mut item.0, item.as_mut()));
assert!(ptr::eq(&mut item.1, item.as_mut()));
}

#[derive(AsMut)]
struct MultiFieldStruct {
#[as_mut]
first: String,
#[as_mut]
second: PathBuf,
third: Vec<usize>,
}

#[test]
fn multi_field_struct() {
let mut item = MultiFieldStruct {
first: "test".into(),
second: PathBuf::new(),
third: vec![],
};

assert!(ptr::eq(&mut item.first, item.as_mut()));
assert!(ptr::eq(&mut item.second, item.as_mut()));
#[cfg(feature = "std")]
mod pathbuf {
use std::path::PathBuf;

use super::*;

#[derive(AsMut)]
struct MultiFieldTuple(#[as_mut] String, #[as_mut] PathBuf, Vec<usize>);

#[test]
fn multi_field_tuple() {
let mut item = MultiFieldTuple("test".into(), PathBuf::new(), vec![]);

assert!(ptr::eq(&mut item.0, item.as_mut()));
assert!(ptr::eq(&mut item.1, item.as_mut()));
}

#[derive(AsMut)]
struct MultiFieldStruct {
#[as_mut]
first: String,
#[as_mut]
second: PathBuf,
third: Vec<usize>,
}

#[test]
fn multi_field_struct() {
let mut item = MultiFieldStruct {
first: "test".into(),
second: PathBuf::new(),
third: vec![],
};

assert!(ptr::eq(&mut item.first, item.as_mut()));
assert!(ptr::eq(&mut item.second, item.as_mut()));
}

#[derive(AsMut)]
struct MultiFieldGenericStruct<T> {
#[as_mut]
first: Vec<T>,
#[as_mut]
second: PathBuf,
third: Vec<usize>,
}

#[test]
fn multi_field_generic_struct() {
let mut item = MultiFieldGenericStruct {
first: b"test".to_vec(),
second: PathBuf::new(),
third: vec![],
};

assert!(ptr::eq(&mut item.first, item.as_mut()));
assert!(ptr::eq(&mut item.second, item.as_mut()));
}
}

#[derive(AsMut)]
Expand All @@ -82,24 +115,3 @@ fn single_field_generic_struct() {

assert!(ptr::eq(&mut item.first, item.as_mut()));
}

#[derive(AsMut)]
struct MultiFieldGenericStruct<T> {
#[as_mut]
first: Vec<T>,
#[as_mut]
second: PathBuf,
third: Vec<usize>,
}

#[test]
fn multi_field_generic_struct() {
let mut item = MultiFieldGenericStruct {
first: b"test".to_vec(),
second: PathBuf::new(),
third: vec![],
};

assert!(ptr::eq(&mut item.first, item.as_mut()));
assert!(ptr::eq(&mut item.second, item.as_mut()));
}
76 changes: 44 additions & 32 deletions tests/as_ref.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(dead_code)]

use std::path::PathBuf;
use std::ptr;
#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
use alloc::{string::String, vec, vec::Vec};
use core::ptr;

use derive_more::AsRef;

Expand Down Expand Up @@ -39,36 +44,43 @@ fn single_field_struct() {
assert!(ptr::eq(&item.first, item.as_ref()));
}

#[derive(AsRef)]
struct MultiFieldTuple(#[as_ref] String, #[as_ref] PathBuf, Vec<usize>);

#[test]
fn multi_field_tuple() {
let item = MultiFieldTuple("test".into(), PathBuf::new(), vec![]);

assert!(ptr::eq(&item.0, item.as_ref()));
assert!(ptr::eq(&item.1, item.as_ref()));
}

#[derive(AsRef)]
struct MultiFieldStruct {
#[as_ref]
first: String,
#[as_ref]
second: PathBuf,
third: Vec<usize>,
}

#[test]
fn multi_field_struct() {
let item = MultiFieldStruct {
first: "test".into(),
second: PathBuf::new(),
third: vec![],
};

assert!(ptr::eq(&item.first, item.as_ref()));
assert!(ptr::eq(&item.second, item.as_ref()));
#[cfg(feature = "std")]
mod pathbuf {
use std::path::PathBuf;

use super::*;

#[derive(AsRef)]
struct MultiFieldTuple(#[as_ref] String, #[as_ref] PathBuf, Vec<usize>);

#[test]
fn multi_field_tuple() {
let item = MultiFieldTuple("test".into(), PathBuf::new(), vec![]);

assert!(ptr::eq(&item.0, item.as_ref()));
assert!(ptr::eq(&item.1, item.as_ref()));
}

#[derive(AsRef)]
struct MultiFieldStruct {
#[as_ref]
first: String,
#[as_ref]
second: PathBuf,
third: Vec<usize>,
}

#[test]
fn multi_field_struct() {
let item = MultiFieldStruct {
first: "test".into(),
second: PathBuf::new(),
third: vec![],
};

assert!(ptr::eq(&item.first, item.as_ref()));
assert!(ptr::eq(&item.second, item.as_ref()));
}
}

#[derive(AsRef)]
Expand Down
1 change: 1 addition & 0 deletions tests/constructor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(dead_code)]

use derive_more::Constructor;
Expand Down
Loading

0 comments on commit ae5b263

Please sign in to comment.