From 160074346f1f87a510351959daccc81cebd07863 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Mon, 24 Jun 2019 19:59:23 +1200 Subject: [PATCH] Fix units and add test --- src/evaluate/evaluator.rs | 2 +- src/parser/ast/expression.rs | 11 ------- src/parser/parse2/parser.rs | 11 +++++++ src/parser/parse2/unit.rs | 24 +++++++++++---- tests/dirtest/test.json | 26 ++++++++++++++++ tests/dirtest/test.toml | 58 ++++++++++++++++++++++++++++++++++++ tests/tests.rs | 5 ++++ tests/unit.out | 1 + tests/unit.txt | 4 +++ 9 files changed, 124 insertions(+), 18 deletions(-) create mode 100644 tests/dirtest/test.json create mode 100644 tests/dirtest/test.toml create mode 100644 tests/unit.out create mode 100644 tests/unit.txt diff --git a/src/evaluate/evaluator.rs b/src/evaluate/evaluator.rs index 16da0ad78c..4642ede45c 100644 --- a/src/evaluate/evaluator.rs +++ b/src/evaluate/evaluator.rs @@ -79,7 +79,7 @@ crate fn evaluate_baseline_expr( fn evaluate_literal(literal: Spanned, source: &Text) -> Spanned { let result = match literal.item { hir::Literal::Integer(int) => Value::int(int), - hir::Literal::Size(_int, _unit) => unimplemented!(), + hir::Literal::Size(int, unit) => unit.compute(int), hir::Literal::String(span) => Value::string(span.slice(source)), hir::Literal::Bare => Value::string(literal.span().slice(source)), }; diff --git a/src/parser/ast/expression.rs b/src/parser/ast/expression.rs index 9d2c9833ca..1df0e8c3b2 100644 --- a/src/parser/ast/expression.rs +++ b/src/parser/ast/expression.rs @@ -292,17 +292,6 @@ impl From<&str> for Unit { } impl Unit { - crate fn compute(&self, size: i64) -> Value { - Value::int(match self { - Unit::B => size, - Unit::KB => size * 1024, - Unit::MB => size * 1024 * 1024, - Unit::GB => size * 1024 * 1024 * 1024, - Unit::TB => size * 1024 * 1024 * 1024 * 1024, - Unit::PB => size * 1024 * 1024 * 1024 * 1024 * 1024, - }) - } - crate fn to_string(&self) -> &str { match self { Unit::B => "B", diff --git a/src/parser/parse2/parser.rs b/src/parser/parse2/parser.rs index 2a069bd552..41d4443877 100644 --- a/src/parser/parse2/parser.rs +++ b/src/parser/parse2/parser.rs @@ -213,11 +213,22 @@ pub fn raw_unit(input: NomSpan) -> IResult> { let start = input.offset; let (input, unit) = alt(( tag("B"), + tag("b"), tag("KB"), + tag("kb"), + tag("Kb"), tag("MB"), + tag("mb"), + tag("Mb"), tag("GB"), + tag("gb"), + tag("Gb"), tag("TB"), + tag("tb"), + tag("Tb"), tag("PB"), + tag("pb"), + tag("Pb"), ))(input)?; let end = input.offset; diff --git a/src/parser/parse2/unit.rs b/src/parser/parse2/unit.rs index 7e51103cfc..78f223bd99 100644 --- a/src/parser/parse2/unit.rs +++ b/src/parser/parse2/unit.rs @@ -1,5 +1,6 @@ use serde_derive::{Deserialize, Serialize}; use std::str::FromStr; +use crate::object::base::Value; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)] pub enum Unit { @@ -22,6 +23,17 @@ impl Unit { Unit::PB => "PB", } } + + crate fn compute(&self, size: i64) -> Value { + Value::int(match self { + Unit::B => size, + Unit::KB => size * 1024, + Unit::MB => size * 1024 * 1024, + Unit::GB => size * 1024 * 1024 * 1024, + Unit::TB => size * 1024 * 1024 * 1024 * 1024, + Unit::PB => size * 1024 * 1024 * 1024 * 1024 * 1024, + }) + } } impl From<&str> for Unit { @@ -34,12 +46,12 @@ impl FromStr for Unit { type Err = (); fn from_str(input: &str) -> Result::Err> { match input { - "B" => Ok(Unit::B), - "KB" => Ok(Unit::KB), - "MB" => Ok(Unit::MB), - "GB" => Ok(Unit::GB), - "TB" => Ok(Unit::TB), - "PB" => Ok(Unit::PB), + "B" | "b" => Ok(Unit::B), + "KB" | "kb" | "Kb" => Ok(Unit::KB), + "MB" | "mb" | "Mb" => Ok(Unit::MB), + "GB" | "gb" | "Gb" => Ok(Unit::GB), + "TB" | "tb" | "Tb" => Ok(Unit::TB), + "PB" | "pb" | "Pb" => Ok(Unit::PB), _ => Err(()), } } diff --git a/tests/dirtest/test.json b/tests/dirtest/test.json new file mode 100644 index 0000000000..8feebaa0ab --- /dev/null +++ b/tests/dirtest/test.json @@ -0,0 +1,26 @@ +{ + "glossary": { + "title": "example glossary", + "GlossDiv": { + "title": "S", + "GlossList": { + "GlossEntry": { + "ID": "SGML", + "SortAs": "SGML", + "GlossTerm": "Standard Generalized Markup Language", + "Acronym": "SGML", + "Abbrev": "ISO 8879:1986", + "Height": 10, + "GlossDef": { + "para": "A meta-markup language, used to create markup languages such as DocBook.", + "GlossSeeAlso": [ + "GML", + "XML" + ] + }, + "GlossSee": "markup" + } + } + } + } +} \ No newline at end of file diff --git a/tests/dirtest/test.toml b/tests/dirtest/test.toml new file mode 100644 index 0000000000..f845705179 --- /dev/null +++ b/tests/dirtest/test.toml @@ -0,0 +1,58 @@ +[package] +name = "nu" +version = "0.1.1" +authors = ["Yehuda Katz "] +description = "A shell for the GitHub era" +license = "ISC" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rustyline = "4.1.0" +sysinfo = "0.8.4" +chrono = { version = "0.4.6", features = ["serde"] } +chrono-tz = "0.5.1" +derive-new = "0.5.6" +prettytable-rs = "0.8.0" +itertools = "0.8.0" +ansi_term = "0.11.0" +conch-parser = "0.1.1" +nom = "5.0.0-beta1" +subprocess = "0.1.18" +dunce = "1.0.0" +indexmap = { version = "1.0.2", features = ["serde-1"] } +chrono-humanize = "0.0.11" +byte-unit = "2.1.0" +ordered-float = "1.0.2" +prettyprint = "0.6.0" +cursive = { version = "0.12.0", features = ["pancurses-backend"], default-features = false } +futures-preview = { version = "0.3.0-alpha.16", features = ["compat", "io-compat"] } +futures-sink-preview = "0.3.0-alpha.16" +tokio-fs = "0.1.6" +futures_codec = "0.2.2" +term = "0.5.2" +bytes = "0.4.12" +log = "0.4.6" +pretty_env_logger = "0.3.0" +lalrpop-util = "0.17.0" +regex = "1.1.6" +serde = "1.0.91" +serde_json = "1.0.39" +serde_derive = "1.0.91" +getset = "0.0.7" +logos = "0.10.0-rc2" +logos-derive = "0.10.0-rc2" +language-reporting = "0.3.0" +app_dirs = "1.2.1" +toml = "0.5.1" +toml-query = "0.9.0" +clap = "2.33.0" +git2 = "0.8.0" + +[dependencies.pancurses] +version = "0.16" +features = ["win32a"] + +[dev-dependencies] +pretty_assertions = "0.6.1" diff --git a/tests/tests.rs b/tests/tests.rs index 802eea0a5a..3974e2f80f 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -107,4 +107,9 @@ mod tests { fn external_num() { test_helper("external_num"); } + + #[test] + fn unit() { + test_helper("unit"); + } } diff --git a/tests/unit.out b/tests/unit.out new file mode 100644 index 0000000000..cacefde74f --- /dev/null +++ b/tests/unit.out @@ -0,0 +1 @@ +test.toml diff --git a/tests/unit.txt b/tests/unit.txt new file mode 100644 index 0000000000..8804a0f7b0 --- /dev/null +++ b/tests/unit.txt @@ -0,0 +1,4 @@ +cd tests +cd dirtest +ls | where size > 1kb | get "file name" | trim | echo $it +exit \ No newline at end of file