Skip to content

Commit 3309786

Browse files
github-actions[bot]Copilotdbrattliclaude
authored
test: add tests for sys, html and time stdlib modules (#258)
* test: add tests for sys, html and time stdlib modules Add test coverage for three stdlib modules that had bindings but no tests: - TestSys.fs: platform, version, maxsize, maxunicode, path, argv, byteorder - TestHtml.fs: escape (with/without quote flag), unescape, roundtrip - TestTime.fs: time, monotonic, perf_counter, process_time, ctime, sleep, timezone Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * test: address review feedback on sys/time tests - Remove trivially-true Count >= 0 assertions; assert argv/path are non-empty instead - Drop redundant |> ignore no-ops - Remove sys.maxsize test — binding types it as int (Int32) but Python value overflows on 64-bit systems, so comparison returns False - Simplify time.sleep test to just call sleep (no-op assertion removed) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(sys): type maxsize as nativeint instead of int Python's sys.maxsize is 2^63−1 on 64-bit systems, which overflows Fable's Int32 (the target type for F# int). nativeint maps to Python's native int, preserving the full value. Re-adds the maxsize positivity test. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Dag Brattli <dag@brattli.net> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 17549dc commit 3309786

5 files changed

Lines changed: 127 additions & 1 deletion

File tree

src/stdlib/Sys.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type IExports =
3535
/// Version information encoded as a single integer
3636
abstract hexversion: int
3737
/// Maximum value a variable of type int can take
38-
abstract maxsize: int
38+
abstract maxsize: nativeint
3939
/// Maximum Unicode code point value
4040
abstract maxunicode: int
4141
/// Module search path - list of directory names where Python looks for modules

test/Fable.Python.Test.fsproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
<Compile Include="TestMath.fs" />
2828
<Compile Include="TestRandom.fs" />
2929
<Compile Include="TestBase64.fs" />
30+
<Compile Include="TestHtml.fs" />
31+
<Compile Include="TestSys.fs" />
32+
<Compile Include="TestTime.fs" />
3033
<Compile Include="TestString.fs" />
3134
<Compile Include="TestPydantic.fs" />
3235
<Compile Include="TestFastAPI.fs" />

test/TestHtml.fs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module Fable.Python.Tests.Html
2+
3+
open Fable.Python.Testing
4+
open Fable.Python.Html
5+
6+
[<Fact>]
7+
let ``test html.escape escapes ampersand`` () =
8+
html.escape "a & b" |> equal "a &amp; b"
9+
10+
[<Fact>]
11+
let ``test html.escape escapes less-than`` () =
12+
html.escape "<tag>" |> equal "&lt;tag&gt;"
13+
14+
[<Fact>]
15+
let ``test html.escape escapes double quotes by default`` () =
16+
html.escape "\"hello\"" |> equal "&quot;hello&quot;"
17+
18+
[<Fact>]
19+
let ``test html.escape with quote=false leaves quotes unescaped`` () =
20+
html.escape ("\"hello\"", false) |> equal "\"hello\""
21+
22+
[<Fact>]
23+
let ``test html.escape with quote=true escapes quotes`` () =
24+
html.escape ("\"hello\"", true) |> equal "&quot;hello&quot;"
25+
26+
[<Fact>]
27+
let ``test html.unescape reverses escape`` () =
28+
html.unescape "&lt;tag&gt;" |> equal "<tag>"
29+
30+
[<Fact>]
31+
let ``test html.unescape reverses ampersand`` () =
32+
html.unescape "a &amp; b" |> equal "a & b"
33+
34+
[<Fact>]
35+
let ``test html.unescape reverses quot`` () =
36+
html.unescape "&quot;hello&quot;" |> equal "\"hello\""
37+
38+
[<Fact>]
39+
let ``test html roundtrip`` () =
40+
let original = "<div class=\"main\">Hello & World</div>"
41+
html.unescape (html.escape original) |> equal original

test/TestSys.fs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module Fable.Python.Tests.Sys
2+
3+
open Fable.Python.Testing
4+
open Fable.Python.Sys
5+
6+
[<Fact>]
7+
let ``test sys.platform is non-empty string`` () =
8+
sys.platform.Length > 0 |> equal true
9+
10+
[<Fact>]
11+
let ``test sys.version is non-empty string`` () =
12+
sys.version.Length > 0 |> equal true
13+
14+
[<Fact>]
15+
let ``test sys.maxsize is positive`` () =
16+
sys.maxsize > 0n |> equal true
17+
18+
[<Fact>]
19+
let ``test sys.maxunicode is 1114111`` () =
20+
sys.maxunicode |> equal 1114111
21+
22+
[<Fact>]
23+
let ``test sys.path has at least one element`` () =
24+
sys.path.Count > 0 |> equal true
25+
26+
[<Fact>]
27+
let ``test sys.argv has at least one element`` () =
28+
sys.argv.Count > 0 |> equal true
29+
30+
[<Fact>]
31+
let ``test sys.byteorder is little or big`` () =
32+
let order = sys.byteorder
33+
(order = ByteOrder.Little || order = ByteOrder.Big) |> equal true

test/TestTime.fs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module Fable.Python.Tests.Time
2+
3+
open Fable.Python.Testing
4+
open Fable.Python.Time
5+
6+
[<Fact>]
7+
let ``test time.time returns positive float`` () =
8+
let t = time.time ()
9+
t > 0.0 |> equal true
10+
11+
[<Fact>]
12+
let ``test time.monotonic returns positive float`` () =
13+
let t = time.monotonic ()
14+
t > 0.0 |> equal true
15+
16+
[<Fact>]
17+
let ``test time.perf_counter returns positive float`` () =
18+
let t = time.perf_counter ()
19+
t > 0.0 |> equal true
20+
21+
[<Fact>]
22+
let ``test time.process_time returns non-negative float`` () =
23+
let t = time.process_time ()
24+
t >= 0.0 |> equal true
25+
26+
[<Fact>]
27+
let ``test time.monotonic increases over calls`` () =
28+
let t1 = time.monotonic ()
29+
let t2 = time.monotonic ()
30+
t2 >= t1 |> equal true
31+
32+
[<Fact>]
33+
let ``test time.ctime returns non-empty string`` () =
34+
let s = time.ctime ()
35+
s.Length > 0 |> equal true
36+
37+
[<Fact>]
38+
let ``test time.ctime with seconds returns non-empty string`` () =
39+
let s = time.ctime 0.0
40+
s.Length > 0 |> equal true
41+
42+
[<Fact>]
43+
let ``test time.sleep does not throw`` () =
44+
time.sleep 0.0
45+
46+
[<Fact>]
47+
let ``test time.timezone is int`` () =
48+
let tz = time.timezone
49+
(tz >= -50400 && tz <= 50400) |> equal true

0 commit comments

Comments
 (0)