@@ -63,6 +63,87 @@ let exename = `$(Base.julia_cmd()) --compiled-modules=yes --startup-file=no --co
6363 @test ! endswith (s_dir, Base. Filesystem. path_separator)
6464end
6565
66+ @testset " Tests for @__FUNCTION__" begin
67+ let
68+ @testset " Basic usage" begin
69+ test_function_basic () = @__FUNCTION__
70+ @test test_function_basic () === test_function_basic
71+ end
72+
73+ @testset " Factorial function" begin
74+ factorial_function (n) = n <= 1 ? 1 : n * (@__FUNCTION__ )(n - 1 )
75+ @test factorial_function (5 ) == 120
76+ end
77+
78+ @testset " Prevents boxed closures" begin
79+ function make_closure ()
80+ fib (n) = n <= 1 ? 1 : (@__FUNCTION__ )(n - 1 ) + (@__FUNCTION__ )(n - 2 )
81+ return fib
82+ end
83+ Test. @inferred make_closure ()
84+ closure = make_closure ()
85+ @test closure (5 ) == 8
86+ Test. @inferred closure (5 )
87+ end
88+
89+ @testset " Will return innermost, even if comprehension" begin
90+ f () = [(@__FUNCTION__ ) for _ in 1 : 10 ]
91+
92+ funcs = f ()
93+ @test first (funcs) != = f
94+ @test all (fi -> fi === funcs[1 ], funcs[2 : end ])
95+ end
96+
97+ @testset " Complex closure of closures" begin
98+ function f1 ()
99+ function f2 ()
100+ function f3 ()
101+ return @__FUNCTION__
102+ end
103+ return (@__FUNCTION__ ), f3 ()
104+ end
105+ return (@__FUNCTION__ ), f2 ()...
106+ end
107+ Test. @inferred f1 ()
108+ @test f1 ()[1 ] === f1
109+ @test f1 ()[2 ] != = f1
110+ @test f1 ()[3 ] != = f1
111+ @test f1 ()[3 ]() === f1 ()[3 ]
112+ @test f1 ()[2 ]()[2 ]() === f1 ()[3 ]
113+ end
114+
115+ @testset " Anonymous function" begin
116+ @test (n -> n <= 1 ? 1 : n * (@__FUNCTION__ )(n - 1 ))(5 ) == 120
117+ end
118+
119+ @testset " Do block" begin
120+ function test_do_block ()
121+ result = map ([1 , 2 , 3 ]) do x
122+ return (@__FUNCTION__ , x)
123+ end
124+ # All should refer to the same do-block function
125+ @test all (r -> r[1 ] === result[1 ][1 ], result)
126+ # Values should be different
127+ @test [r[2 ] for r in result] == [1 , 2 , 3 ]
128+ # It should be different than `test_do_block`
129+ @test result[1 ][1 ] != = test_do_block
130+ end
131+ test_do_block ()
132+ end
133+
134+ @testset " Callable structs throw error" begin
135+ struct CallableStruct{T}
136+ val:: T
137+ end
138+ function (c:: CallableStruct )()
139+ return @__FUNCTION__
140+ end
141+ c = CallableStruct (5 )
142+ @test_throws UndefVarError c ()
143+ end
144+ end
145+ end
146+
66147@test Base. in_sysimage (Base. PkgId (Base. UUID (" 8f399da3-3557-5675-b5ff-fb832c97cbdb" ), " Libdl" ))
67148@test Base. in_sysimage (Base. PkgId (Base. UUID (" 3a7fdc7e-7467-41b4-9f64-ea033d046d5b" ), " NotAPackage" )) == false
68149
0 commit comments