Fix uniq to work with simple values (#2214)

This commit is contained in:
k-brk 2020-07-18 19:19:03 +02:00 committed by GitHub
parent 9e5170b3dc
commit 3432078e77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 2 deletions

View File

@ -163,3 +163,43 @@ fn uniq_counting() {
print!("{}", expected.out);
assert_eq!(actual.out, expected.out);
}
#[test]
fn uniq_simple_vals_ints() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo [1 2 3 4 1 5]
| uniq
"#
));
let expected = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo [1 2 3 4 5]
"#
));
print!("{}", actual.out);
print!("{}", expected.out);
assert_eq!(actual.out, expected.out);
}
#[test]
fn uniq_simple_vals_strs() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo [A B C A]
| uniq
"#
));
let expected = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo [A B C]
"#
));
print!("{}", actual.out);
print!("{}", expected.out);
assert_eq!(actual.out, expected.out);
}

View File

@ -25,9 +25,9 @@ use nu_source::{AnchorLocation, HasSpan, Span, Spanned, Tag};
use num_bigint::BigInt;
use num_traits::ToPrimitive;
use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};
use std::path::PathBuf;
use std::time::SystemTime;
/// The core structured values that flow through a pipeline
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)]
pub enum UntaggedValue {
@ -220,12 +220,24 @@ impl UntaggedValue {
}
/// The fundamental structured value that flows through the pipeline, with associated metadata
#[derive(Debug, Clone, PartialOrd, PartialEq, Ord, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialOrd, Ord, Eq, Serialize, Deserialize)]
pub struct Value {
pub value: UntaggedValue,
pub tag: Tag,
}
impl PartialEq for Value {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl Hash for Value {
fn hash<H: Hasher>(&self, state: &mut H) {
self.value.hash(state);
}
}
/// Overload deferencing to give back the UntaggedValue inside of a Value
impl std::ops::Deref for Value {
type Target = UntaggedValue;