Skip to content

Commit a54c8fa

Browse files
authored
test: Add more tests (#149)
1 parent 133d237 commit a54c8fa

File tree

6 files changed

+180
-1
lines changed

6 files changed

+180
-1
lines changed

src/stdlib/Builtins.fs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,30 @@ type IExports =
200200
abstract int: obj -> int
201201
/// Object to float
202202
abstract float: obj -> float
203+
204+
/// Return the largest item in an iterable or the largest of two or more arguments.
205+
abstract max: 'T * 'T -> 'T
206+
/// Return the largest item in an iterable or the largest of two or more arguments.
207+
abstract max: 'T * 'T * 'T -> 'T
208+
/// Return the largest item in an iterable or the largest of two or more arguments.
209+
abstract max: IEnumerable<'T> -> 'T
210+
211+
/// Return the smallest item in an iterable or the smallest of two or more arguments.
212+
abstract min: 'T * 'T -> 'T
213+
/// Return the smallest item in an iterable or the smallest of two or more arguments.
214+
abstract min: 'T * 'T * 'T -> 'T
215+
/// Return the smallest item in an iterable or the smallest of two or more arguments.
216+
abstract min: IEnumerable<'T> -> 'T
217+
218+
/// Return the sum of a 'start' value (default: 0) plus an iterable of numbers.
219+
abstract sum: IEnumerable<'T> -> 'T
220+
221+
/// Return True if bool(x) is True for all values x in the iterable.
222+
abstract all: IEnumerable<bool> -> bool
223+
224+
/// Return True if bool(x) is True for any x in the iterable.
225+
abstract any: IEnumerable<bool> -> bool
226+
203227
abstract print: obj: obj -> unit
204228

205229
[<NamedParams(fromIndex = 1)>]

src/stdlib/Os.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ and [<Erase>] PathModule =
8484
/// Return the base name of pathname path
8585
/// See https://docs.python.org/3/library/os.path.html#os.path.basename
8686
abstract basename: path: string -> string
87+
/// Return the argument with an initial component of ~ or ~user replaced
88+
/// See https://docs.python.org/3/library/os.path.html#os.path.expanduser
89+
abstract expanduser: path: string -> string
90+
/// Return a normalized absolutized version of the pathname path
91+
/// See https://docs.python.org/3/library/os.path.html#os.path.abspath
92+
abstract abspath: path: string -> string
93+
/// Split the pathname path into a pair (head, tail)
94+
/// See https://docs.python.org/3/library/os.path.html#os.path.split
95+
abstract split: path: string -> string * string
96+
/// Split the pathname path into a pair (root, ext)
97+
/// See https://docs.python.org/3/library/os.path.html#os.path.splitext
98+
abstract splitext: path: string -> string * string
8799

88100

89101
/// Miscellaneous operating system interfaces

test/Fable.Python.Test.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<Compile Include="TestAst.fs" />
1717
<Compile Include="TestAsyncIO.fs" />
1818
<Compile Include="TestBuiltins.fs" />
19+
<Compile Include="TestOs.fs" />
1920
<Compile Include="TestJson.fs" />
2021
<Compile Include="TestString.fs" />
2122
<Compile Include="Main.fs" />

test/TestBuiltins.fs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Fable.Python.Tests.Builtins
22

33
open Util.Testing
44
open Fable.Python.Builtins
5+
open Fable.Python.Os
56

67
[<Fact>]
78
let ``test print works`` () =
@@ -12,5 +13,55 @@ let ``test __name__ works`` () = __name__ |> equal "test_builtins"
1213

1314
[<Fact>]
1415
let ``test write works`` () =
15-
let result = builtins.``open``("test.txt", OpenTextMode.Write)
16+
let tempFile = os.path.join (os.path.expanduser "~", ".fable_test_temp.txt")
17+
let result = builtins.``open``(tempFile, OpenTextMode.Write)
1618
result.write "ABC" |> equal 3
19+
result.Dispose()
20+
os.remove tempFile
21+
22+
[<Fact>]
23+
let ``test max with two arguments works`` () =
24+
builtins.max (3, 5) |> equal 5
25+
builtins.max (10, 2) |> equal 10
26+
27+
[<Fact>]
28+
let ``test max with three arguments works`` () =
29+
builtins.max (3, 5, 1) |> equal 5
30+
builtins.max (1, 2, 10) |> equal 10
31+
32+
[<Fact>]
33+
let ``test max with iterable works`` () =
34+
builtins.max [1; 2; 3; 4; 5] |> equal 5
35+
builtins.max [10; -5; 3] |> equal 10
36+
37+
[<Fact>]
38+
let ``test min with two arguments works`` () =
39+
builtins.min (3, 5) |> equal 3
40+
builtins.min (10, 2) |> equal 2
41+
42+
[<Fact>]
43+
let ``test min with three arguments works`` () =
44+
builtins.min (3, 5, 1) |> equal 1
45+
builtins.min (1, 2, 10) |> equal 1
46+
47+
[<Fact>]
48+
let ``test min with iterable works`` () =
49+
builtins.min [1; 2; 3; 4; 5] |> equal 1
50+
builtins.min [10; -5; 3] |> equal -5
51+
52+
[<Fact>]
53+
let ``test sum works`` () =
54+
builtins.sum [1; 2; 3; 4; 5] |> equal 15
55+
builtins.sum [10; -5; 3] |> equal 8
56+
57+
[<Fact>]
58+
let ``test all works`` () =
59+
builtins.all [true; true; true] |> equal true
60+
builtins.all [true; false; true] |> equal false
61+
builtins.all [] |> equal true
62+
63+
[<Fact>]
64+
let ``test any works`` () =
65+
builtins.any [false; false; true] |> equal true
66+
builtins.any [false; false; false] |> equal false
67+
builtins.any [] |> equal false

test/TestJson.fs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,33 @@ let ``test json loads works`` () =
2020
let object = {| Foo = 10; Bar = "test" |}
2121
let result: {| Foo: int; Bar: string |} = unbox (json.loads input)
2222
result |> equal object
23+
24+
[<Fact>]
25+
let ``test json.dumps with nativeint works`` () =
26+
let value: nativeint = 42n
27+
let result = json.dumps value
28+
result |> equal "42"
29+
30+
[<Fact>]
31+
let ``test json.dumps with ResizeArray of nativeint works`` () =
32+
let values = ResizeArray([1n; 2n; 3n])
33+
let result = json.dumps values
34+
result |> equal "[1, 2, 3]"
35+
36+
[<Fact>]
37+
let ``test json.dumps with nested object works`` () =
38+
let obj = {| Name = "test"; Values = ResizeArray([1n; 2n; 3n]) |}
39+
let result = dumps obj
40+
result |> equal """{"Name": "test", "Values": [1, 2, 3]}"""
41+
42+
[<Fact>]
43+
let ``test json.loads with array works`` () =
44+
let input = "[1, 2, 3]"
45+
let result: int array = unbox (json.loads input)
46+
result.Length |> equal 3
47+
48+
[<Fact>]
49+
let ``test json.dumps with indent works`` () =
50+
let obj = {| A = 1n |}
51+
let result = dumpsIndented obj 2
52+
result.Contains("\n") |> equal true

test/TestOs.fs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module Fable.Python.Tests.Os
2+
3+
open Util.Testing
4+
open Fable.Python.Os
5+
6+
[<Fact>]
7+
let ``test os.path.exists works`` () =
8+
os.path.exists "." |> equal true
9+
os.path.exists "nonexistent_path_xyz" |> equal false
10+
11+
[<Fact>]
12+
let ``test os.path.isdir works`` () =
13+
os.path.isdir "." |> equal true
14+
15+
[<Fact>]
16+
let ``test os.path.join works`` () =
17+
os.path.join ("a", "b", "c") |> equal "a/b/c"
18+
19+
[<Fact>]
20+
let ``test os.path.dirname works`` () =
21+
os.path.dirname "/foo/bar/baz.txt" |> equal "/foo/bar"
22+
23+
[<Fact>]
24+
let ``test os.path.basename works`` () =
25+
os.path.basename "/foo/bar/baz.txt" |> equal "baz.txt"
26+
27+
[<Fact>]
28+
let ``test os.path.expanduser works`` () =
29+
let expanded = os.path.expanduser "~"
30+
// The expanded path should not start with ~ anymore
31+
expanded.StartsWith("~") |> equal false
32+
33+
[<Fact>]
34+
let ``test os.path.abspath works`` () =
35+
let absPath = os.path.abspath "."
36+
// Absolute path should start with /
37+
absPath.StartsWith("/") |> equal true
38+
39+
[<Fact>]
40+
let ``test os.path.split works`` () =
41+
let head, tail = os.path.split "/foo/bar/baz.txt"
42+
head |> equal "/foo/bar"
43+
tail |> equal "baz.txt"
44+
45+
[<Fact>]
46+
let ``test os.path.splitext works`` () =
47+
let root, ext = os.path.splitext "/foo/bar/baz.txt"
48+
root |> equal "/foo/bar/baz"
49+
ext |> equal ".txt"
50+
51+
[<Fact>]
52+
let ``test os.getcwd works`` () =
53+
let cwd = os.getcwd ()
54+
// Current working directory should be a non-empty string
55+
cwd.Length > 0 |> equal true
56+
57+
[<Fact>]
58+
let ``test os.listdir works`` () =
59+
let entries = os.listdir "."
60+
// Current directory should have at least some entries
61+
entries.Length > 0 |> equal true

0 commit comments

Comments
 (0)