Skip to content

Commit

Permalink
Minor cleanups for unit test and README (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunjhongwu authored Dec 3, 2023
1 parent 3dedf76 commit aa89e54
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# named-type
Macros to create strong typed and named values in Rust.
- `NamedType`: creates a strong type with a name.
- `NamedNumeric`: creates a strong type with a name and implements traits for arithmetic operations.
- `default_display`: implements `Display` trait for the type with the default format `TypeName(value)`.

## Supported underlying types:
- Both `NamedType` and `NamedNumeric`:
- `i8`, `i16`, `i32`, `i64`, `i128`, `isize`
- `u8`, `u16`, `u32`, `u64`, `u128`, `usize`
- `f32`, `f64`
- Only `NamedType`:
- `bool`
- `char`
- `String`

## Examples
```rust
Expand Down
27 changes: 27 additions & 0 deletions tests/named_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,67 @@ mod tests {
#[derive(NamedNumeric)]
struct NamedI8(i8);
let _ = NamedI8(1);

#[derive(NamedNumeric)]
struct NamedI16(i16);
let _ = NamedI16(1);

#[derive(NamedNumeric)]
struct NamedI32(i32);
let _ = NamedI32(1);

#[derive(NamedNumeric)]
struct NamedI64(i64);
let _ = NamedI64(1);

#[derive(NamedNumeric)]
struct NamedI128(i128);
let _ = NamedI128(1);

#[derive(NamedNumeric)]
struct NamedISize(isize);
let _ = NamedISize(1);

#[derive(NamedNumeric)]
struct NamedU8(u8);
let _ = NamedU8(1);

#[derive(NamedNumeric)]
struct NamedU16(u16);
let _ = NamedU16(1);

#[derive(NamedNumeric)]
struct NamedU32(u32);
let _ = NamedU32(1);

#[derive(NamedNumeric)]
struct NamedU64(u64);
let _ = NamedU64(1);

#[derive(NamedNumeric)]
struct NamedU128(u128);
let _ = NamedU128(1);

#[derive(NamedNumeric)]
struct NamedUSize(usize);
let _ = NamedUSize(1);

#[derive(NamedNumeric)]
struct NamedF32(f32);
let _ = NamedF32(1.0);

#[derive(NamedNumeric)]
struct NamedF64(f64);
let _ = NamedF64(1.0);

#[derive(NamedType)]
struct NamedBool(bool);
let _ = NamedBool(true);

#[derive(NamedType)]
struct NamedChar(char);
let _ = NamedChar('a');

#[derive(NamedType)]
struct NamedString(String);
let _ = NamedString("string".to_string());
Expand All @@ -64,8 +80,10 @@ mod tests {
#[derive(NamedType)]
struct IsTrue(bool);
let sign = IsTrue(true);

assert!(sign.value());
assert!(!(!sign).value());
assert!((!!sign).value());
}

#[test]
Expand Down Expand Up @@ -128,6 +146,15 @@ mod tests {
assert_eq!(map.len(), 2);
}

#[test]
fn test_float_nan() {
#[derive(NamedType)]
struct Meter(f64);

let y = Meter::nan();
assert!(y.value().is_nan());
}

#[test]
fn test_string() {
#[derive(NamedType)]
Expand Down

0 comments on commit aa89e54

Please sign in to comment.