Skip to content

Commit

Permalink
add checks
Browse files Browse the repository at this point in the history
  • Loading branch information
hyorigo committed Feb 26, 2024
1 parent d99aee8 commit dd27c78
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/random/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,12 @@ func shuffle(thread *starlark.Thread, bn *starlark.Builtin, args starlark.Tuple,
}

// random() returns a random floating point number in the range 0.0 <= X < 1.0.
func random(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
func random(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
// check the arguments: no arguments
if err := starlark.UnpackPositionalArgs(b.Name(), args, kwargs, 0); err != nil {
return nil, err
}
// get random float
f, err := getRandomFloat(1 << 53)
if err != nil {
return nil, err
Expand All @@ -235,7 +240,12 @@ func random(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, k
}

// uuid() returns a random UUID (RFC 4122 version 4).
func uuid(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
func uuid(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
// check the arguments: no arguments
if err := starlark.UnpackPositionalArgs(b.Name(), args, kwargs, 0); err != nil {
return nil, err
}
// get random UUID
u := guuid.New()
return starlark.String(u.String()), nil
}
Expand All @@ -257,6 +267,8 @@ func uniform(thread *starlark.Thread, bn *starlark.Builtin, args starlark.Tuple,
return starlark.Float(float64(a) + diff*f), nil
}

// the following functions are not exposed to Starlark directly, but can be used in other Starlark builtins.

// getRandomInt returns a random integer in the range [0, max).
func getRandomInt(max int) (int, error) {
if max <= 0 {
Expand Down
16 changes: 16 additions & 0 deletions lib/random/random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,14 @@ func TestLoadModule_Random(t *testing.T) {
return f >= 0 && f < 1
},
},
{
name: "random with invalid args",
script: itn.HereDoc(`
load('random', 'random')
random(1)
`),
wantErr: `random.random: got 1 arguments, want 0`,
},
{
name: "uniform with less than 2 args",
script: itn.HereDoc(`
Expand Down Expand Up @@ -415,6 +423,14 @@ func TestLoadModule_Random(t *testing.T) {
assert.eq(len(val.replace("-", "")), 32)
`),
},
{
name: "uuid with invalid args",
script: itn.HereDoc(`
load('random', 'uuid')
uuid(2)
`),
wantErr: `random.uuid: got 1 arguments, want 0`,
},
{
name: "randb32 with 0 or 1 args",
script: itn.HereDoc(`
Expand Down
3 changes: 3 additions & 0 deletions lib/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,8 @@ var (

// getUpTime returns time elapsed since the app started.
func getUpTime(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if err := starlark.UnpackPositionalArgs(b.Name(), args, kwargs, 0); err != nil {
return nil, err
}
return stdtime.Duration(time.Since(appStart)), nil
}
8 changes: 8 additions & 0 deletions lib/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func TestLoadModule_Runtime(t *testing.T) {
print(s, u)
`),
},
{
name: `uptime invalid`,
script: itn.HereDoc(`
load('runtime', 'uptime')
uptime(123)
`),
wantErr: `runtime.uptime: got 1 arguments, want 0`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit dd27c78

Please sign in to comment.