-
Notifications
You must be signed in to change notification settings - Fork 326
Expand file tree
/
Copy pathTestReflection.fs
More file actions
679 lines (555 loc) · 21.9 KB
/
Copy pathTestReflection.fs
File metadata and controls
679 lines (555 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
module Fable.Tests.Reflection
open Util.Testing
open FSharp.Data.UnitSystems.SI.UnitSymbols
#if !OPTIMIZE_FCS
type MyDelegate = delegate of int * float -> string
type MyGenDelegate<'a,'b> = delegate of 'b * string -> 'a
// I can declare a type with delegate fields (both C# and F# style). See #1698
type WithDelegates =
{ Del1: MyDelegate
Del2: MyGenDelegate<int,string>
Del3: System.Func<int, float, string>
Del4: System.Func<float>
Del5: System.Action
Del6: System.Action<int>
Del7: System.Action<int, string> }
type MyType =
| Union1 of string
type MyType2 =
| Union2 of string
type MyType3 = class end
type MyType4 = class end
type MyType5 = class end
type GenericRecord<'A,'B> = { a: 'A; b: 'B }
type MyEnum =
| Foo = 1y
| Bar = 5y
| Baz = 8y
type MyInterface<'T> =
abstract Value: 'T
type MyClass() =
class end
type MyClass2() =
class end
type MyClass3<'T>(v) =
interface MyInterface<'T> with
member _.Value = v
type MyClass<'T1, 'T2>(v1: 'T1, v2: 'T2) =
inherit MyClass()
member _.Value1 = v1
member _.Value2 = v2
type MyRecord = {
String: string
Int: int
}
[<Fact>]
let ``test typedefof works`` () =
let tdef1 = typedefof<int list>
let tdef2 = typedefof<string list>
equal tdef1 tdef2
[<Fact>]
let ``test IsGenericType works`` () =
typeof<int list>.IsGenericType |> equal true
typeof<MyType>.IsGenericType |> equal false
let t1 = typeof<int list>
let t2 = typeof<MyType>
let t3 = typeof<string>
t1.IsGenericType |> equal true
t2.IsGenericType |> equal false
t3.IsGenericType |> equal false
[<Fact>]
let ``test GetGenericTypeDefinition works`` () =
let tdef1 = typedefof<int list>
let tdef2 = typeof<int list>.GetGenericTypeDefinition()
let t = typeof<int list>
let tdef3 = t.GetGenericTypeDefinition()
equal tdef1 tdef2
equal tdef1 tdef3
tdef1 = typeof<int list> |> equal false
[<Fact>]
let ``test Comparing generic types works`` () =
let t1 = typeof<GenericRecord<string, MyType>>
let t2 = typeof<GenericRecord<string, MyType>>
let t3 = typeof<GenericRecord<string, int>>
t1 = t2 |> equal true
t1 = t3 |> equal false
let inline getName<'t> = function
| "namespace" -> typedefof<'t>.Namespace
| "name" -> typedefof<'t>.Name
| _ -> typedefof<'t>.FullName
let inline getName2 (d:'t) = function
| "namespace" -> typeof<'t>.Namespace
| "name" -> typeof<'t>.Name
| _ -> typeof<'t>.FullName
let getName3 (t:System.Type) = function
| "namespace" -> t.Namespace
| "name" -> t.Name
| _ -> t.FullName
type Firm = { name: string }
let normalize (x: string) =
#if FABLE_COMPILER
x
#else
x.Replace("+",".")
#endif
let inline fullname<'T> () = typeof<'T>.FullName |> normalize
[<Fact>]
let ``test Type Namespace`` () =
let x = typeof<MyType>.Namespace
#if FABLE_COMPILER
equal "Fable.Tests.Reflection" x
#else
equal "Fable.Tests" x
#endif
[<Fact>]
let ``test Type FullName`` () =
let x = typeof<MyType>.FullName
x |> normalize |> equal "Fable.Tests.Reflection.MyType"
[<Fact>]
let ``test Type Name`` () =
let x = typeof<MyType>.Name
equal "MyType" x
[<Fact>]
let ``test Get fullname of generic types with inline function`` () =
fullname<MyType3>() |> equal "Fable.Tests.Reflection.MyType3"
fullname<MyType4>() |> equal "Fable.Tests.Reflection.MyType4"
[<Fact>]
let ``test Type name is accessible`` () =
let x = { name = "" }
getName<Firm> "name" |> equal "Firm"
getName2 x "name" |> equal "Firm"
getName3 typedefof<Firm> "name" |> equal "Firm"
// getName4 x "name" |> equal "Firm"
[<Fact>]
let ``test Type namespace is accessible`` () =
let test (x: string) =
x.StartsWith("Fable.Tests") |> equal true
let x = { name = "" }
getName<Firm> "namespace" |> test
getName2 x "namespace" |> test
getName3 typedefof<Firm> "namespace" |> test
// getName4 x "namespace" |> test
[<Fact>]
let ``test Type full name is accessible`` () =
let test (x: string) =
x.Replace("+", ".") |> equal "Fable.Tests.Reflection.Firm"
let x = { name = "" }
getName<Firm> "fullname" |> test
getName2 x "fullname" |> test
getName3 typedefof<Firm> "fullname" |> test
// getName4 x "fullname" |> test
// FSharpType and FSharpValue reflection tests
open FSharp.Reflection
exception MyException of String: string * Int: int
let flip f b a = f a b
type MyUnion =
| StringCase of SomeString: string * string
| IntCase of SomeInt: int
type RecordF = { F : int -> string }
type AsyncRecord = {
asyncProp : Async<string>
}
type MyList<'T> =
| Nil
| Cons of 'T * MyList<'T>
let inline create<'T when 'T: (new: unit->'T)> () = new 'T()
type A() = member _.Value = 5
type B() = member _.Value = 10
type AnonRec1 = {| name: string; child: {| name: string |} |}
type AnonRec2 = {| numbers: int list |}
type RecordGetValueType = {
Firstname : string
Age : int
}
[<Fact>]
let ``test Reflection Array`` () =
let arType = typeof<int[]>
let liType = typeof<int list>
equal true arType.IsArray
equal false liType.IsArray
let elType = arType.GetElementType()
typeof<int> = elType |> equal true
typeof<bool> = elType |> equal false
liType.GetElementType() |> equal null
#if FABLE_COMPILER
[<Fact>]
let ``test FSharp.Reflection Record`` () =
let typ = typeof<MyRecord>
let record = { String = "a"; Int = 1 }
let recordTypeFields = FSharpType.GetRecordFields typ
let recordValueFields = FSharpValue.GetRecordFields record
let expectedRecordFields =
[|
"string", box "a"
"int", box 1
|]
let recordFields =
recordTypeFields
|> Array.map (fun field -> field.Name)
|> flip Array.zip recordValueFields
let isRecord = FSharpType.IsRecord typ
let matchRecordFields = recordFields = expectedRecordFields
let matchIndividualRecordFields =
Array.zip recordTypeFields recordValueFields
|> Array.forall (fun (info, value) ->
FSharpValue.GetRecordField(record, info) = value
)
let canMakeSameRecord =
unbox<MyRecord> (FSharpValue.MakeRecord(typ, recordValueFields)) = record
let all = isRecord && matchRecordFields && matchIndividualRecordFields && canMakeSameRecord
all |> equal true
[<Fact>]
let ``test PropertyInfo.GetValue works`` () =
let value: obj = { Firstname = "Maxime"; Age = 12 } :> obj
let theType: System.Type = typeof<RecordGetValueType>
// now we want to print out the fields
let fieldNameToValue: Map<string, obj> =
match theType with
| t when FSharpType.IsRecord t ->
FSharpType.GetRecordFields(t)
|> Seq.fold
(fun acc field ->
let fieldValue = field.GetValue value
acc.Add (field.Name, fieldValue)
)
Map.empty
| _ -> Map.empty
let expected = "map [(age, 12); (firstname, Maxime)]"
equal expected (sprintf "%O" fieldNameToValue)
#else
[<Fact>]
let ``test FSharp.Reflection Record`` () =
let typ = typeof<MyRecord>
let record = { String = "a"; Int = 1 }
let recordTypeFields = FSharpType.GetRecordFields typ
let recordValueFields = FSharpValue.GetRecordFields record
let expectedRecordFields =
[|
"String", box "a"
"Int", box 1
|]
let recordFields =
recordTypeFields
|> Array.map (fun field -> field.Name)
|> flip Array.zip recordValueFields
let isRecord = FSharpType.IsRecord typ
let matchRecordFields = recordFields = expectedRecordFields
let matchIndividualRecordFields =
Array.zip recordTypeFields recordValueFields
|> Array.forall (fun (info, value) ->
FSharpValue.GetRecordField(record, info) = value
)
let canMakeSameRecord =
unbox<MyRecord> (FSharpValue.MakeRecord(typ, recordValueFields)) = record
let all = isRecord && matchRecordFields && matchIndividualRecordFields && canMakeSameRecord
all |> equal true
[<Fact>]
let ``test PropertyInfo.GetValue works`` () =
let value: obj = { Firstname = "Maxime"; Age = 12 } :> obj
let theType: System.Type = typeof<RecordGetValueType>
// now we want to print out the fields
let fieldNameToValue: Map<string, obj> =
match theType with
| t when FSharpType.IsRecord t ->
FSharpType.GetRecordFields(t)
|> Seq.fold
(fun acc field ->
let fieldValue = field.GetValue value
acc.Add (field.Name, fieldValue)
)
Map.empty
| _ -> Map.empty
let expected = "map [(Age, 12); (Firstname, Maxime)]"
equal expected (sprintf "%O" fieldNameToValue)
#endif
[<Fact>]
let ``test Comparing anonymous record types works`` () =
let x = {| numbers = [3; 4] |}
typeof<AnonRec1> = typeof<AnonRec2> |> equal false
typeof<AnonRec1> = typeof<AnonRec1> |> equal true
typeof<AnonRec2> = x.GetType() |> equal true
let generic = typeof<Result<AnonRec2, string>>
generic = typeof<Result<AnonRec1, string>> |> equal false
generic = typeof<Result<{| numbers: int list |}, string>> |> equal true
[<Fact>]
let ``test FSharp.Reflection: Anonymous Record`` () =
let typ = typeof<{| String: string; Int: int |}>
let record = {| String = "a"; Int = 1 |}
let recordTypeFields = FSharpType.GetRecordFields typ
let recordValueFields = FSharpValue.GetRecordFields record
let expectedRecordFields = // Alphabetical order
[| "Int", box 1
"String", box "a" |]
let recordFields =
recordTypeFields
|> Array.map (fun field -> field.Name)
|> flip Array.zip recordValueFields
FSharpType.IsRecord typ |> equal true
recordFields |> equal expectedRecordFields
Array.zip recordTypeFields recordValueFields
|> Array.forall (fun (info, value) ->
FSharpValue.GetRecordField(record, info) = value)
|> equal true
FSharpValue.MakeRecord(typ, recordValueFields)
|> unbox<{| String: string; Int: int |}>
|> equal record
[<Fact>]
let ``test FSharp.Reflection Functions`` () =
let recordType = typeof<RecordF>
let fields = FSharpType.GetRecordFields recordType
let funcProperty = Array.head fields
let funcType = funcProperty.PropertyType
let domain, range = FSharpType.GetFunctionElements funcType
equal domain typeof<int>
equal range typeof<string>
equal true (FSharpType.IsFunction funcType)
[<Fact>]
let ``test FSharp.Reflection: Tuple`` () =
let typ = typeof<string * int>
let tuple = "a", 1
let tupleTypeFields = FSharpType.GetTupleElements typ
let tupleValueFields = FSharpValue.GetTupleFields tuple
let expectedTupleFields =
[|
typeof<string>, box "a"
typeof<int>, box 1
|]
let tupleFields = Array.zip tupleTypeFields tupleValueFields
let isTuple = FSharpType.IsTuple typ
let matchTupleFields = tupleFields = expectedTupleFields
let matchIndividualTupleFields =
tupleValueFields
|> Array.mapi (fun i value -> i, value)
|> Array.forall (fun (i, value) ->
FSharpValue.GetTupleField(tuple, i) = value
)
let canMakeSameTuple =
unbox<string * int> (FSharpValue.MakeTuple(tupleValueFields, typ)) = tuple
let all = isTuple && matchTupleFields && matchIndividualTupleFields && canMakeSameTuple
all |> equal true
[<Fact>]
let ``test FSharp.Reflection: Array of tuples is not classified as a tuple`` () =
let typ = typeof<(string * int * int)[]>
equal true typ.IsArray
FSharpType.IsTuple typ |> equal false
[<Fact>]
let ``test FSharp.Reflection: MakeTupleType`` () =
let t = FSharpType.MakeTupleType [|typeof<float>; typeof<string>; typeof<int[]>|]
FSharpValue.MakeTuple([|5.; "foo"; [|2;3|]|], t)
|> unbox<float * string * int[]>
|> equal (5., "foo", [|2;3|])
let real = typeof<float * string * int[]>
let generated = FSharpType.MakeTupleType [|typeof<float>; typeof<string>; typeof<int[]>|]
equal real generated
[<Fact>]
let ``test FSharp.Reflection Union`` () =
let typ = typeof<MyUnion>
let unionCase1 = StringCase("a", "b")
let unionCase2 = IntCase 1
let unionTypeFields = FSharpType.GetUnionCases typ
unionTypeFields |> Array.map (fun x -> x.Name) |> equal [| "StringCase"; "IntCase" |]
let unionCase1Info, unionCase1ValueFields = FSharpValue.GetUnionFields(unionCase1, typ)
let unionCase2Info, unionCase2ValueFields = FSharpValue.GetUnionFields(unionCase2, typ)
let unionCaseInfos = [| unionCase1Info; unionCase2Info |]
let unionCaseValueFields = [| unionCase1ValueFields; unionCase2ValueFields |]
let expectedUnionCase1Fields = 0, "StringCase", [| typeof<string>; typeof<string> |], [| "SomeString"; "Item2" |], [| box "a"; box "b" |]
let expectedUnionCase2Fields = 1, "IntCase", [| typeof<int> |], [| "SomeInt" |], [| box 1 |]
let expectedUnionFields = [| expectedUnionCase1Fields; expectedUnionCase2Fields |]
let unionFields =
Array.zip unionCaseInfos unionCaseValueFields
|> Array.map (fun (info, values) ->
let types =
info.GetFields()
|> Array.map (fun field -> field.PropertyType)
let names =
info.GetFields()
|> Array.map (fun field -> field.Name)
info.Tag, info.Name, types, names, values)
let canMakeSameUnionCases =
unbox<MyUnion> (FSharpValue.MakeUnion(unionCase1Info, unionCase1ValueFields)) = unionCase1
&& unbox<MyUnion> (FSharpValue.MakeUnion(unionCase2Info, unionCase2ValueFields)) = unionCase2
FSharpType.IsUnion typ |> equal true
unionFields |> equal expectedUnionFields
canMakeSameUnionCases |> equal true
[<Fact>]
let ``test FSharp.Reflection: Result`` () =
let typ = typeof<Result<int,string>>
let ucis = FSharpType.GetUnionCases typ
FSharpValue.MakeUnion(ucis.[0], [|box 5|]) |> equal (box (Result<_,string>.Ok 5))
FSharpValue.MakeUnion(ucis.[1], [|box "foo"|]) |> equal (box (Result<int,_>.Error "foo"))
// See https://github.com/fable-compiler/Fable/issues/4082
[<Fact>]
let ``test FSharp.Reflection: Option is a union type`` () =
let typ = typeof<int option>
FSharpType.IsUnion(typ) |> equal true
let ucis = FSharpType.GetUnionCases(typ)
ucis.Length |> equal 2
ucis.[0].Name |> equal "None"
ucis.[1].Name |> equal "Some"
FSharpValue.MakeUnion(ucis.[0], [||]) |> equal (box (None: int option))
FSharpValue.MakeUnion(ucis.[1], [|box 42|]) |> equal (box (Some 42))
let noneCase, noneFields = FSharpValue.GetUnionFields(box (None: int option), typ)
noneCase.Name |> equal "None"
noneFields.Length |> equal 0
let someCase, someFields = FSharpValue.GetUnionFields(box (Some 42), typ)
someCase.Name |> equal "Some"
someFields.Length |> equal 1
someFields.[0] |> equal (box 42)
[<Fact>]
let ``test FSharp.Reflection: Option round-trips through Some(None) and Some(Some x)`` () =
let typ = typeof<int option option>
let ucis = FSharpType.GetUnionCases(typ)
let someCase, someFields = FSharpValue.GetUnionFields(box (Some (None: int option)), typ)
someCase.Name |> equal "Some"
someFields.[0] |> equal (box (None: int option))
let someCase2, someFields2 = FSharpValue.GetUnionFields(box (Some (Some 42)), typ)
someCase2.Name |> equal "Some"
someFields2.[0] |> equal (box (Some 42))
FSharpValue.MakeUnion(ucis.[1], [|box (None: int option)|]) |> equal (box (Some (None: int option)))
FSharpValue.MakeUnion(ucis.[1], [|box (Some 42)|]) |> equal (box (Some (Some 42)))
[<Fact>]
let ``test FSharp.Reflection: Choice`` () =
let typ = typeof<Choice<int,string>>
let ucis = FSharpType.GetUnionCases typ
FSharpValue.MakeUnion(ucis.[0], [|box 5|]) |> equal (box (Choice<_,string>.Choice1Of2 5))
FSharpValue.MakeUnion(ucis.[1], [|box "foo"|]) |> equal (box (Choice<int,_>.Choice2Of2 "foo"))
let typ = typeof<Choice<float,string list,float>>
let ucis = FSharpType.GetUnionCases typ
FSharpValue.MakeUnion(ucis.[0], [|box -0.3|]) |> equal (box (Choice<_,string list,float>.Choice1Of3 -0.3))
FSharpValue.MakeUnion(ucis.[1], [|box ["foo";"bar"]|]) |> equal (box (Choice<float,_,float>.Choice2Of3 ["foo";"bar"]))
FSharpValue.MakeUnion(ucis.[2], [|box 3.5|]) |> equal (box (Choice<float,string list,_>.Choice3Of3 3.5))
FSharpValue.MakeUnion(ucis.[2], [|box 3.5|]) |> (=) (box (Choice<float,string list,_>.Choice1Of3 3.5)) |> equal false
[<Fact>]
let ``test Type.GenericTypeArguments works`` () =
let recordType = typeof<AsyncRecord>
let asyncProp = FSharpType.GetRecordFields recordType |> Array.head
asyncProp.PropertyType.GenericTypeArguments |> Array.head |> equal typeof<string>
[<Fact>]
let ``test Recursive types work`` () =
let cons =
FSharpType.GetUnionCases(typeof<MyList<int>>)
|> Array.find (fun x -> x.Name = "Cons")
let fieldTypes = cons.GetFields()
fieldTypes.[0].PropertyType.FullName |> equal typeof<int>.FullName
fieldTypes.[1].PropertyType.GetGenericTypeDefinition().FullName |> equal typedefof<MyList<obj>>.FullName
[<Fact>]
let ``test Calling constructor of generic type in inline functions works`` () =
let a = create<A>()
let b = create<B>()
a.Value |> equal 5
b.Value |> equal 10
// See https://github.com/Microsoft/visualfsharp/issues/5992
[<Fact>]
let ``test Generic numbers type info doesn't get into runtime`` () =
let value = 0.7833263478179128134089M
value.GetType().FullName |> equal "System.Decimal"
// See https://github.com/thoth-org/Thoth.Json/issues/74
[<Fact>]
let ``test Reflection info of int64/decimal with units of measure works`` () =
typeof< int64 > = typeof< int64<m> > |> equal true
typeof< decimal > = typeof< decimal<m> > |> equal true
[<Fact>]
let ``test Reflection works with enums`` () =
typeof<MyEnum>.IsEnum |> equal true
typeof<int>.IsEnum |> equal false
let t = typeof<MyEnum>
t.IsEnum |> equal true
t.GetEnumUnderlyingType() |> equal typeof<sbyte>
System.Enum.GetUnderlyingType(t) |> equal typeof<sbyte>
[<Fact>]
let ``test Can create generic classes at runtime`` () =
let t = typedefof<MyClass<obj, obj>>
let t = t.MakeGenericType(typeof<int>, typeof<string>)
let x = System.Activator.CreateInstance(t, 123, "abc")
x :? MyClass |> equal true
x :? MyClass2 |> equal false
let x = x :?> MyClass<int, string>
x.Value1 |> equal 123
x.Value2 |> equal "abc"
[<Fact>]
let ``test IsSubclassOf works`` () =
let t1 = typeof<MyClass>
let t2 = typeof<MyClass<obj, obj>>
let t3 = typeof<MyClass2>
t3.IsSubclassOf(t1) |> equal false
t2.IsSubclassOf(t1) |> equal true
t1.IsSubclassOf(t2) |> equal false
[<Fact>]
let ``test .GetInterface works when types are know at compile time`` () = // #2321
let t = typeof<MyClass3<int>>.GetInterface("MyInterface`1")
t.GetGenericArguments().[0] = typeof<int> |> equal true
t.GetGenericArguments().[0] = typeof<string> |> equal false
typeof<MyClass3<int>>.GetInterface("myInterface`1") |> isNull |> equal true
typeof<MyClass3<int>>.GetInterface("myInterface`1", true) |> isNull |> equal false
typeof<MyClass2>.GetInterface("MyInterface`1") |> isNull |> equal true
[<Fact>]
let ``test GetType and typeof return the same Type instance for primitive types`` () =
let inline getTypeGeneric (x: 'T) =
x.GetType()
obj.ReferenceEquals(getTypeGeneric 2, typeof<int>) |> equal true
obj.ReferenceEquals(getTypeGeneric 2, typeof<uint32>) |> equal false
obj.ReferenceEquals(getTypeGeneric 2u, typeof<uint32>) |> equal true
obj.ReferenceEquals(getTypeGeneric 2uy, typeof<byte>) |> equal true
obj.ReferenceEquals(getTypeGeneric 2s, typeof<int16>) |> equal true
obj.ReferenceEquals(getTypeGeneric([| 2 |]).GetElementType(), typeof<int>) |> equal true
obj.ReferenceEquals(getTypeGeneric([| 2 |]).GetElementType(), typeof<uint32>) |> equal false
#if FABLE_COMPILER
open Fable.Core
open Fable.Core.Reflection
type R1 = { x: int }
type R2 = { y: int }
type Maybe<'t> =
| Just of 't
| Nothing
type RecWithGenDU<'t> = { Other: 't; Value : Maybe<string> }
type GenericTestRecord<'t> = { Other: 't; Value : Maybe<string> }
// helper class to extract the name of a type argument
type Types() =
static member inline getNameOf<'t> () : string =
typeof<'t>.Name
static member inline get<'t> () : System.Type =
typeof<'t>
type MyUnion20 =
| Union20_A
| Union20_B of int * string
type MyRecord20 =
{ FieldA: int
FieldB: string }
[<Fact>]
let ``test Recursively reading generic arguments of nested generic types works`` () =
let typeInfo = Types.get<Maybe<Maybe<int>>>()
// recursively reads the generic arguments
let rec getGenericArgs (typeDef: System.Type) : string list =
[ yield typeDef.Name
for genericTypeArg in typeDef.GetGenericArguments() do
yield! getGenericArgs genericTypeArg ]
getGenericArgs typeInfo
|> equal ["Maybe`1"; "Maybe`1"; "Int32"]
[<Fact>]
let ``test Name can be extracted from RecWithGenDU`` () =
let name = Types.getNameOf<Maybe<list<RecWithGenDU<string>>>>()
equal false (name = "")
[<Fact>]
let ``test Name can be extracted from GenericTestRecord`` () =
let name = Types.getNameOf<Maybe<list<GenericTestRecord<string>>>>()
equal false (name = "")
[<Fact>]
let ``test Can check unions and records without type`` () =
let x = box Union20_A
let y = box { FieldA = 5; FieldB = "foo" }
isUnion x |> equal true
isRecord x |> equal false
isUnion y |> equal false
isRecord y |> equal true
[<Fact>]
let ``test Can get union values without type`` () =
let x = box Union20_A
let y = Union20_B(5, "foo") |> box
getCaseTag x |> equal 0
getCaseTag y |> equal 1
getCaseName x |> equal "Union20_A"
getCaseName y |> equal "Union20_B"
getCaseFields x |> equal [||]
getCaseFields y |> equal [|5; "foo"|]
#endif
#endif