forked from microsoft/regorus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added semver.is_valid and semver.compare (microsoft#49)
* Completed semver.is_valid and semver.compare * Addressed PR comments Signed-off-by: Minh Nguyen <[email protected]> --------- Signed-off-by: Minh Nguyen <[email protected]> Co-authored-by: Anand Krishnamoorthi <[email protected]>
- Loading branch information
1 parent
a75f7c5
commit e838d5a
Showing
5 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
use crate::ast::{Expr, Ref}; | ||
use crate::builtins; | ||
use crate::builtins::utils::{ensure_args_count, ensure_string}; | ||
use crate::lexer::Span; | ||
use crate::value::Value; | ||
|
||
use semver::Version; | ||
|
||
use std::cmp::Ordering; | ||
use std::collections::HashMap; | ||
|
||
use anyhow::{Ok, Result}; | ||
|
||
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) { | ||
m.insert("semver.compare", (compare, 2)); | ||
m.insert("semver.is_valid", (is_valid, 1)); | ||
} | ||
|
||
fn compare(span: &Span, params: &[Ref<Expr>], args: &[Value]) -> Result<Value> { | ||
let name = "semver.compare"; | ||
ensure_args_count(span, name, params, args, 2)?; | ||
|
||
let v1 = ensure_string(name, ¶ms[0], &args[0])?; | ||
let v2 = ensure_string(name, ¶ms[1], &args[1])?; | ||
let version1 = Version::parse(&v1)?; | ||
let version2 = Version::parse(&v2)?; | ||
let result = match version1.cmp_precedence(&version2) { | ||
Ordering::Less => -1, | ||
Ordering::Equal => 0, | ||
Ordering::Greater => 1, | ||
}; | ||
Ok(Value::from_float(result as f64)) | ||
} | ||
|
||
fn is_valid(span: &Span, params: &[Ref<Expr>], args: &[Value]) -> Result<Value> { | ||
let name = "semver.is_valid"; | ||
ensure_args_count(span, name, params, args, 1)?; | ||
let v = ensure_string(name, ¶ms[0], &args[0])?; | ||
Ok(Value::Bool(Version::parse(&v).is_ok())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
cases: | ||
- note: semver.compare passing | ||
data: {} | ||
modules: | ||
- | | ||
package test | ||
vers = [ | ||
"1.9.10", | ||
"1.8.15" | ||
] | ||
# Compare each version against itself and others. | ||
r = [ a | a = semver.compare(vers[_], vers[_]) ] | ||
query: data.test.r | ||
want_result: [0, 1, -1, 1] | ||
skip: true # TODO: remove this | ||
|
||
- note: semver.compare wrong arg1 type | ||
data: {} | ||
modules: | ||
- | | ||
package test | ||
a = semver.compare("", 1) | ||
query: data.test.a | ||
error: expects string argument | ||
|
||
- note: semver.compare wrong arg2 type | ||
data: {} | ||
modules: | ||
- | | ||
package test | ||
a = semver.compare(1, "") | ||
query: data.test.a | ||
error: expects string argument | ||
|
||
- note: semver extra arg | ||
data: {} | ||
modules: | ||
- | | ||
package test | ||
a = semver.compare("", "", "") | ||
query: data.test.a | ||
error: expects 2 arguments |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
cases: | ||
- note: semver.is_valid passing | ||
data: {} | ||
modules: | ||
- | | ||
package test | ||
vers = [ | ||
"1.9.10", | ||
"1.8.15", | ||
"1.1", | ||
"1.1.12-rc1+foo" | ||
] | ||
r = [ a | a = semver.is_valid(vers[_]) ] | ||
query: data.test.r | ||
want_result: [true, true, false] | ||
skip: true # TODO: remove this | ||
|
||
- note: semver.compare wrong arg1 type | ||
data: {} | ||
modules: | ||
- | | ||
package test | ||
a = semver.is_valid(1) | ||
query: data.test.a | ||
error: expects string argument | ||
|
||
- note: semver.is_valid extra arg | ||
data: {} | ||
modules: | ||
- | | ||
package test | ||
a = semver.is_valid("", "") | ||
query: data.test.a | ||
error: expects 1 argument |