summary refs log blame commit diff stats
path: root/tests/arguments.rs
blob: 4b5a2a03dbf2afcb3efff78818f75b8a0169305a (plain) (tree)





























































































































































                                                                           
// Copyright (c) 2021 Soni L.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// Documentation and comments licensed under CC BY-SA 4.0.

// because we wanna use double underscore (__) for test names
#![allow(non_snake_case)]

use ::std::io::Cursor;
use ::std::marker::PhantomData;

use ::iosonism::args::ArgumentType;
use ::iosonism::args::BoolArgumentType;
use ::iosonism::args::FloatArgumentType;
use ::iosonism::args::IntegerArgumentType;
use ::iosonism::strcursor::StringReader;

mod common;

use self::common::ErrorCall;
use self::common::ErrorFunc;
use self::common::ErrorPanic;
use self::common::ErrorType;

#[test]
fn test_bool__parse() {
    assert_eq!(
        ArgumentType::<(), ErrorPanic>::parse(
            &BoolArgumentType,
            &mut Cursor::new("true"),
        ),
        Ok(true),
    );
    assert_eq!(
        ArgumentType::<(), ErrorPanic>::parse(
            &BoolArgumentType,
            &mut Cursor::new("false"),
        ),
        Ok(false),
    );
}

#[test]
fn test_i32__parse() {
    let mut reader = Cursor::new("15");
    assert_eq!(
        ArgumentType::<(), ErrorPanic>::parse(
            &IntegerArgumentType { range: .., _ty: PhantomData::<i32> },
            &mut reader,
        ),
        Ok(15),
    );
    assert!(!reader.can_read());
}

#[test]
fn test_i32__parse__range() {
    let mut reader = Cursor::new("-5");
    assert!(ArgumentType::<(), ErrorCall<ErrFn>>::parse(
        &IntegerArgumentType { range: 0..=100, _ty: PhantomData::<i32> },
        &mut reader,
    ).is_err());
    struct ErrFn;
    impl<'a> ErrorFunc<'a, Cursor<&'a str>> for ErrFn {
        fn call(context: &Cursor<&'a str>, ty: ErrorType) {
            assert!(matches!(ty, ErrorType::RangeErrori32(..)));
            assert_eq!(context.position(), 0);
        }
    }
}

#[test]
fn test_i64__parse() {
    let mut reader = Cursor::new("15");
    assert_eq!(
        ArgumentType::<(), ErrorPanic>::parse(
            &IntegerArgumentType { range: .., _ty: PhantomData::<i64> },
            &mut reader,
        ),
        Ok(15),
    );
    assert!(!reader.can_read());
}

#[test]
fn test_i64__parse__range() {
    let mut reader = Cursor::new("-5");
    assert!(ArgumentType::<(), ErrorCall<ErrFn>>::parse(
        &IntegerArgumentType { range: 0..=100, _ty: PhantomData::<i64> },
        &mut reader,
    ).is_err());
    struct ErrFn;
    impl<'a> ErrorFunc<'a, Cursor<&'a str>> for ErrFn {
        fn call(context: &Cursor<&'a str>, ty: ErrorType) {
            assert!(matches!(ty, ErrorType::RangeErrori64(..)));
            assert_eq!(context.position(), 0);
        }
    }
}

#[test]
fn test_f32__parse() {
    let mut reader = Cursor::new("15");
    assert_eq!(
        ArgumentType::<(), ErrorPanic>::parse(
            &FloatArgumentType { range: .., _ty: PhantomData::<f32> },
            &mut reader,
        ),
        Ok(15.0),
    );
    assert!(!reader.can_read());
}

#[test]
fn test_f32__parse__range() {
    let mut reader = Cursor::new("-5");
    assert!(ArgumentType::<(), ErrorCall<ErrFn>>::parse(
        &FloatArgumentType { range: 0.0..=100.0, _ty: PhantomData::<f32> },
        &mut reader,
    ).is_err());
    struct ErrFn;
    impl<'a> ErrorFunc<'a, Cursor<&'a str>> for ErrFn {
        fn call(context: &Cursor<&'a str>, ty: ErrorType) {
            assert!(matches!(ty, ErrorType::RangeErrorf32(..)));
            assert_eq!(context.position(), 0);
        }
    }
}

#[test]
fn test_f64__parse() {
    let mut reader = Cursor::new("15");
    assert_eq!(
        ArgumentType::<(), ErrorPanic>::parse(
            &FloatArgumentType { range: .., _ty: PhantomData::<f64> },
            &mut reader,
        ),
        Ok(15.0),
    );
    assert!(!reader.can_read());
}

#[test]
fn test_f64__parse__range() {
    let mut reader = Cursor::new("-5");
    assert!(ArgumentType::<(), ErrorCall<ErrFn>>::parse(
        &FloatArgumentType { range: 0.0..=100.0, _ty: PhantomData::<f64> },
        &mut reader,
    ).is_err());
    struct ErrFn;
    impl<'a> ErrorFunc<'a, Cursor<&'a str>> for ErrFn {
        fn call(context: &Cursor<&'a str>, ty: ErrorType) {
            assert!(matches!(ty, ErrorType::RangeErrorf64(..)));
            assert_eq!(context.position(), 0);
        }
    }
}