From 3432078e77fb6535e64ff4c8444be8d53f794c9b Mon Sep 17 00:00:00 2001 From: k-brk <25877802+k-brk@users.noreply.github.com> Date: Sat, 18 Jul 2020 19:19:03 +0200 Subject: [PATCH] Fix uniq to work with simple values (#2214) --- crates/nu-cli/tests/commands/uniq.rs | 40 ++++++++++++++++++++++++++++ crates/nu-protocol/src/value.rs | 16 +++++++++-- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/crates/nu-cli/tests/commands/uniq.rs b/crates/nu-cli/tests/commands/uniq.rs index 6449918785..6551f22acd 100644 --- a/crates/nu-cli/tests/commands/uniq.rs +++ b/crates/nu-cli/tests/commands/uniq.rs @@ -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); +} diff --git a/crates/nu-protocol/src/value.rs b/crates/nu-protocol/src/value.rs index 0bd68d1fd4..c2e7964f13 100644 --- a/crates/nu-protocol/src/value.rs +++ b/crates/nu-protocol/src/value.rs @@ -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(&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;