mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
Add tests for builtin string::format_int method (#65)
This commit is contained in:
@@ -66,17 +66,7 @@ fn endswith(span: &Span, params: &[Ref<Expr>], args: &[Value], _strict: bool) ->
|
||||
Ok(Value::Bool(s1.ends_with(s2.as_ref())))
|
||||
}
|
||||
|
||||
fn format_number(n: &Number, base: u64) -> String {
|
||||
match base {
|
||||
2 => n.format_bin(),
|
||||
8 => n.format_octal(),
|
||||
10 => n.format_decimal(),
|
||||
16 => n.format_hex(),
|
||||
_ => "".to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
fn format_int(span: &Span, params: &[Ref<Expr>], args: &[Value], _strict: bool) -> Result<Value> {
|
||||
fn format_int(span: &Span, params: &[Ref<Expr>], args: &[Value], strict: bool) -> Result<Value> {
|
||||
let name = "format_int";
|
||||
ensure_args_count(span, name, params, args, 2)?;
|
||||
let mut n = ensure_numeric(name, ¶ms[0], &args[0])?;
|
||||
@@ -87,14 +77,24 @@ fn format_int(span: &Span, params: &[Ref<Expr>], args: &[Value], _strict: bool)
|
||||
}
|
||||
let n = n.floor();
|
||||
|
||||
Ok(Value::String(
|
||||
(sign.to_owned()
|
||||
+ &match ensure_numeric(name, ¶ms[1], &args[1])?.as_u64() {
|
||||
Some(b) => format_number(&n, b),
|
||||
_ => return Ok(Value::Undefined),
|
||||
})
|
||||
.into(),
|
||||
))
|
||||
let base = ensure_numeric(name, ¶ms[1], &args[1])?;
|
||||
|
||||
let num = match base.as_u64() {
|
||||
Some(2) => n.format_bin(),
|
||||
Some(8) => n.format_octal(),
|
||||
Some(10) => n.format_decimal(),
|
||||
Some(16) => n.format_hex(),
|
||||
_ => {
|
||||
if strict {
|
||||
let span = params[1].span();
|
||||
bail!(span.error(&format!("`{name}` expects base to be one of 2, 8, 10, 16")));
|
||||
}
|
||||
|
||||
return Ok(Value::Undefined);
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Value::String((sign.to_owned() + &num).into()))
|
||||
}
|
||||
|
||||
fn indexof(span: &Span, params: &[Ref<Expr>], args: &[Value], _strict: bool) -> Result<Value> {
|
||||
|
||||
101
tests/interpreter/cases/builtins/strings/format_int.yaml
Normal file
101
tests/interpreter/cases/builtins/strings/format_int.yaml
Normal file
@@ -0,0 +1,101 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# Licensed under the MIT License.
|
||||
|
||||
cases:
|
||||
- note: bases
|
||||
data: {}
|
||||
modules:
|
||||
- |
|
||||
package test
|
||||
|
||||
binary := format_int(10, 2)
|
||||
octal := format_int(10, 8)
|
||||
decimal := format_int(10, 10)
|
||||
hex := format_int(10, 16)
|
||||
query: data.test
|
||||
want_result:
|
||||
binary: "1010"
|
||||
octal: "12"
|
||||
decimal: "10"
|
||||
hex: "a"
|
||||
|
||||
- note: bases-with-floats
|
||||
data: {}
|
||||
modules:
|
||||
- |
|
||||
package test
|
||||
|
||||
binary := format_int(10.2, 2)
|
||||
octal := format_int(10.7, 8)
|
||||
decimal := format_int(10.325436, 10)
|
||||
hex := format_int(10.0, 16)
|
||||
query: data.test
|
||||
want_result:
|
||||
binary: "1010"
|
||||
octal: "12"
|
||||
decimal: "10"
|
||||
hex: "a"
|
||||
|
||||
- note: bases-with-negative-values
|
||||
data: {}
|
||||
modules:
|
||||
- |
|
||||
package test
|
||||
|
||||
binary := format_int(-10, 2)
|
||||
octal := format_int(-10, 8)
|
||||
decimal := format_int(-10, 10)
|
||||
hex := format_int(-10, 16)
|
||||
query: data.test
|
||||
want_result:
|
||||
binary: "-1010"
|
||||
octal: "-12"
|
||||
decimal: "-10"
|
||||
hex: "-a"
|
||||
|
||||
- note: bases-with-negative-floats
|
||||
data: {}
|
||||
modules:
|
||||
- |
|
||||
package test
|
||||
|
||||
binary := format_int(-10.765, 2)
|
||||
octal := format_int(-10.0, 8)
|
||||
decimal := format_int(-10.999999999, 10)
|
||||
hex := format_int(-10.00, 16)
|
||||
query: data.test
|
||||
want_result:
|
||||
binary: "-1010"
|
||||
octal: "-12"
|
||||
decimal: "-10"
|
||||
hex: "-a"
|
||||
|
||||
- note: invalid-num-type
|
||||
data: {}
|
||||
modules:
|
||||
- |
|
||||
package test
|
||||
|
||||
x := format_int("10", 2)
|
||||
query: data.test
|
||||
error: '`format_int` expects numeric argument. Got `"10"` instead'
|
||||
|
||||
- note: invalid-base
|
||||
data: {}
|
||||
modules:
|
||||
- |
|
||||
package test
|
||||
|
||||
x := format_int(10, 4)
|
||||
query: data.test
|
||||
error: "`format_int` expects base to be one of 2, 8, 10, 16"
|
||||
|
||||
- note: invalid-base-type
|
||||
data: {}
|
||||
modules:
|
||||
- |
|
||||
package test
|
||||
|
||||
x := format_int(10, "2")
|
||||
query: data.test
|
||||
error: '`format_int` expects numeric argument. Got `"2"` instead'
|
||||
Reference in New Issue
Block a user