mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 06:58:36 +02:00
Declare input and output types of commands (#6796)
* Add failing test that list of ints and floats is List<Number> * Start defining subtype relation * Make it possible to declare input and output types for commands - Enforce them in tests * Declare input and output types of commands * Add formatted signatures to `help commands` table * Revert SyntaxShape::Table -> Type::Table change * Revert unnecessary derive(Hash) on SyntaxShape Co-authored-by: JT <547158+jntrnr@users.noreply.github.com>
This commit is contained in:
@ -399,7 +399,11 @@ impl Value {
|
||||
match &ty {
|
||||
Some(x) => {
|
||||
if &val_ty != x {
|
||||
ty = Some(Type::Any)
|
||||
if x.is_numeric() && val_ty.is_numeric() {
|
||||
ty = Some(Type::Number)
|
||||
} else {
|
||||
ty = Some(Type::Any)
|
||||
}
|
||||
}
|
||||
}
|
||||
None => ty = Some(val_ty),
|
||||
@ -3181,4 +3185,68 @@ mod tests {
|
||||
assert!(!one_column_with_empty_string_and_one_value_with_a_string.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
mod get_type {
|
||||
use crate::Type;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_list() {
|
||||
let list_of_ints = Value::List {
|
||||
vals: vec![Value::Int {
|
||||
val: 0,
|
||||
span: Span::unknown(),
|
||||
}],
|
||||
span: Span::unknown(),
|
||||
};
|
||||
let list_of_floats = Value::List {
|
||||
vals: vec![Value::Float {
|
||||
val: 0.0,
|
||||
span: Span::unknown(),
|
||||
}],
|
||||
span: Span::unknown(),
|
||||
};
|
||||
let list_of_ints_and_floats = Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 0,
|
||||
span: Span::unknown(),
|
||||
},
|
||||
Value::Float {
|
||||
val: 0.0,
|
||||
span: Span::unknown(),
|
||||
},
|
||||
],
|
||||
span: Span::unknown(),
|
||||
};
|
||||
let list_of_ints_and_floats_and_bools = Value::List {
|
||||
vals: vec![
|
||||
Value::Int {
|
||||
val: 0,
|
||||
span: Span::unknown(),
|
||||
},
|
||||
Value::Float {
|
||||
val: 0.0,
|
||||
span: Span::unknown(),
|
||||
},
|
||||
Value::Bool {
|
||||
val: false,
|
||||
span: Span::unknown(),
|
||||
},
|
||||
],
|
||||
span: Span::unknown(),
|
||||
};
|
||||
assert_eq!(list_of_ints.get_type(), Type::List(Box::new(Type::Int)));
|
||||
assert_eq!(list_of_floats.get_type(), Type::List(Box::new(Type::Float)));
|
||||
assert_eq!(
|
||||
list_of_ints_and_floats_and_bools.get_type(),
|
||||
Type::List(Box::new(Type::Any))
|
||||
);
|
||||
assert_eq!(
|
||||
list_of_ints_and_floats.get_type(),
|
||||
Type::List(Box::new(Type::Number))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user