Skip to content

Commit 51a6831

Browse files
committed
stdlib/time: add simple tests
1 parent b8d4a91 commit 51a6831

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed

stdlib/time/testdata/test.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2022 The go-python Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
import time
6+
7+
now = time.time()
8+
now = time.time_ns()
9+
now = time.clock()
10+
11+
def notimplemented(fn, *args, **kwargs):
12+
try:
13+
fn(*args, **kwargs)
14+
print("error for %s(%s, %s)" % (fn,args,kwargs))
15+
except NotImplementedError:
16+
pass
17+
18+
notimplemented(time.clock_gettime)
19+
notimplemented(time.clock_settime)
20+
21+
print("# sleep")
22+
time.sleep(0.1)
23+
try:
24+
time.sleep(-1)
25+
print("no error sleep(-1)")
26+
except ValueError as e:
27+
print("caught error: %s" % (e,))
28+
pass
29+
try:
30+
time.sleep("1")
31+
print("no error sleep('1')")
32+
except TypeError as e:
33+
print("caught error: %s" % (e,))
34+
pass
35+
36+
notimplemented(time.gmtime)
37+
notimplemented(time.localtime)
38+
notimplemented(time.asctime)
39+
notimplemented(time.ctime)
40+
notimplemented(time.mktime, 1)
41+
notimplemented(time.strftime)
42+
notimplemented(time.strptime)
43+
notimplemented(time.tzset)
44+
notimplemented(time.monotonic)
45+
notimplemented(time.process_time)
46+
notimplemented(time.perf_counter)
47+
notimplemented(time.get_clock_info)
48+
49+
print("OK")

stdlib/time/testdata/test_golden.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# sleep
2+
caught error: ValueError: 'sleep length must be non-negative'
3+
caught error: TypeError: 'sleep() argument 1 must be float, not str'
4+
OK

stdlib/time/time.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func time_sleep(self py.Object, args py.Tuple) (py.Object, error) {
150150
if secs < 0 {
151151
return nil, py.ExceptionNewf(py.ValueError, "sleep length must be non-negative")
152152
}
153-
time.Sleep(time.Duration(secs * 1e9))
153+
time.Sleep(time.Duration(secs * py.Float(time.Second)))
154154
return py.None, nil
155155
}
156156

@@ -1007,17 +1007,15 @@ func init() {
10071007
py.MustNewMethod("perf_counter", time_perf_counter, 0, perf_counter_doc),
10081008
py.MustNewMethod("get_clock_info", time_get_clock_info, 0, get_clock_info_doc),
10091009
}
1010-
1010+
10111011
py.RegisterModule(&py.ModuleImpl{
10121012
Info: py.ModuleInfo{
1013-
Name: "time",
1014-
Doc: module_doc,
1013+
Name: "time",
1014+
Doc: module_doc,
10151015
},
10161016
Methods: methods,
1017-
Globals: py.StringDict{
1018-
},
1017+
Globals: py.StringDict{},
10191018
})
1020-
10211019
}
10221020

10231021
const module_doc = `This module provides various functions to manipulate time values.

stdlib/time/time_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2022 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package time_test
6+
7+
import (
8+
"testing"
9+
10+
"github.com/go-python/gpython/pytest"
11+
)
12+
13+
func TestTime(t *testing.T) {
14+
pytest.RunScript(t, "./testdata/test.py")
15+
}

0 commit comments

Comments
 (0)