Fix units and add test

This commit is contained in:
Jonathan Turner 2019-06-24 19:59:23 +12:00
parent 36a4289d37
commit 160074346f
9 changed files with 124 additions and 18 deletions

View file

@ -79,7 +79,7 @@ crate fn evaluate_baseline_expr(
fn evaluate_literal(literal: Spanned<hir::Literal>, source: &Text) -> Spanned<Value> { fn evaluate_literal(literal: Spanned<hir::Literal>, source: &Text) -> Spanned<Value> {
let result = match literal.item { let result = match literal.item {
hir::Literal::Integer(int) => Value::int(int), 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::String(span) => Value::string(span.slice(source)),
hir::Literal::Bare => Value::string(literal.span().slice(source)), hir::Literal::Bare => Value::string(literal.span().slice(source)),
}; };

View file

@ -292,17 +292,6 @@ impl From<&str> for Unit {
} }
impl 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 { crate fn to_string(&self) -> &str {
match self { match self {
Unit::B => "B", Unit::B => "B",

View file

@ -213,11 +213,22 @@ pub fn raw_unit(input: NomSpan) -> IResult<NomSpan, Spanned<Unit>> {
let start = input.offset; let start = input.offset;
let (input, unit) = alt(( let (input, unit) = alt((
tag("B"), tag("B"),
tag("b"),
tag("KB"), tag("KB"),
tag("kb"),
tag("Kb"),
tag("MB"), tag("MB"),
tag("mb"),
tag("Mb"),
tag("GB"), tag("GB"),
tag("gb"),
tag("Gb"),
tag("TB"), tag("TB"),
tag("tb"),
tag("Tb"),
tag("PB"), tag("PB"),
tag("pb"),
tag("Pb"),
))(input)?; ))(input)?;
let end = input.offset; let end = input.offset;

View file

@ -1,5 +1,6 @@
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use std::str::FromStr; use std::str::FromStr;
use crate::object::base::Value;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
pub enum Unit { pub enum Unit {
@ -22,6 +23,17 @@ impl Unit {
Unit::PB => "PB", 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 { impl From<&str> for Unit {
@ -34,12 +46,12 @@ impl FromStr for Unit {
type Err = (); type Err = ();
fn from_str(input: &str) -> Result<Self, <Self as std::str::FromStr>::Err> { fn from_str(input: &str) -> Result<Self, <Self as std::str::FromStr>::Err> {
match input { match input {
"B" => Ok(Unit::B), "B" | "b" => Ok(Unit::B),
"KB" => Ok(Unit::KB), "KB" | "kb" | "Kb" => Ok(Unit::KB),
"MB" => Ok(Unit::MB), "MB" | "mb" | "Mb" => Ok(Unit::MB),
"GB" => Ok(Unit::GB), "GB" | "gb" | "Gb" => Ok(Unit::GB),
"TB" => Ok(Unit::TB), "TB" | "tb" | "Tb" => Ok(Unit::TB),
"PB" => Ok(Unit::PB), "PB" | "pb" | "Pb" => Ok(Unit::PB),
_ => Err(()), _ => Err(()),
} }
} }

26
tests/dirtest/test.json Normal file
View file

@ -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"
}
}
}
}
}

58
tests/dirtest/test.toml Normal file
View file

@ -0,0 +1,58 @@
[package]
name = "nu"
version = "0.1.1"
authors = ["Yehuda Katz <wycats@gmail.com>"]
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"

View file

@ -107,4 +107,9 @@ mod tests {
fn external_num() { fn external_num() {
test_helper("external_num"); test_helper("external_num");
} }
#[test]
fn unit() {
test_helper("unit");
}
} }

1
tests/unit.out Normal file
View file

@ -0,0 +1 @@
test.toml

4
tests/unit.txt Normal file
View file

@ -0,0 +1,4 @@
cd tests
cd dirtest
ls | where size > 1kb | get "file name" | trim | echo $it
exit