Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for builtin string::format_int method #65

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions src/builtins/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, &params[0], &args[0])?;
Expand All @@ -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, &params[1], &args[1])?.as_u64() {
Some(b) => format_number(&n, b),
_ => return Ok(Value::Undefined),
})
.into(),
))
let base = ensure_numeric(name, &params[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> {
Expand Down
101 changes: 101 additions & 0 deletions tests/interpreter/cases/builtins/strings/format_int.yaml
Original file line number Diff line number Diff line change
@@ -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'