8000 pytest: introduce RunScript by sbinet · Pull Request #176 · go-python/gpython · GitHub
[go: up one dir, main page]

Skip to content

pytest: introduce RunScript #176

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

Merged
merged 2 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
stdlib/time: add simple tests
  • Loading branch information
sbinet committed Mar 24, 2022
commit 51a6831b9041ec7105912a574548eb60ea22a1fd
49 changes: 49 additions & 0 deletions stdlib/time/testdata/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2022 The go-python Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

import time

now = time.time()
now = time.time_ns()
now = time.clock()

def notimplemented(fn, *args, **kwargs):
try:
fn(*args, **kwargs)
print("error for %s(%s, %s)" % (fn,args,kwargs))
except NotImplementedError:
pass

notimplemented(time.clock_gettime)
notimplemented(time.clock_settime)

print("# sleep")
time.sleep(0.1)
try:
time.sleep(-1)
print("no error sleep(-1)")
except ValueError as e:
print("caught error: %s" % (e,))
pass
try:
time.sleep("1")
print("no error sleep('1')")
except TypeError as e:
print("caught error: %s" % (e,))
pass

notimplemented(time.gmtime)
notimplemented(time.localtime)
notimplemented(time.asctime)
notimplemented(time.ctime)
notimplemented(time.mktime, 1)
notimplemented(time.strftime)
notimplemented(time.strptime)
notimplemented(time.tzset)
notimplemented(time.monotonic)
notimplemented(time.process_time)
notimplemented(time.perf_counter)
notimplemented(time.get_clock_info)

print("OK")
4 changes: 4 additions & 0 deletions stdlib/time/testdata/test_golden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# sleep
caught error: ValueError: 'sleep length must be non-negative'
caught error: TypeError: 'sleep() argument 1 must be float, not str'
OK
12 changes: 5 additions & 7 deletions stdlib/time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func time_sleep(self py.Object, args py.Tuple) (py.Object, error) {
if secs < 0 {
return nil, py.ExceptionNewf(py.ValueError, "sleep length must be non-negative")
}
time.Sleep(time.Duration(secs * 1e9))
time.Sleep(time.Duration(secs * py.Float(time.Second)))
return py.None, nil
}

Expand Down Expand Up @@ -1007,17 +1007,15 @@ func init() {
py.MustNewMethod("perf_counter", time_perf_counter, 0, perf_counter_doc),
py.MustNewMethod("get_clock_info", time_get_clock_info, 0, get_clock_info_doc),
}

py.RegisterModule(&py.ModuleImpl{
Info: py.ModuleInfo{
Name: "time",
Doc: module_doc,
Name: "time",
Doc: module_doc,
},
Methods: methods,
Globals: py.StringDict{
},
Globals: py.StringDict{},
})

}

const module_doc = `This module provides various functions to manipulate time values.
Expand Down
15 changes: 15 additions & 0 deletions stdlib/time/time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2022 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package time_test

import (
"testing"

"github.com/go-python/gpython/pytest"
)

func TestTime(t *testing.T) {
pytest.RunScript(t, "./testdata/test.py")
}
0