/
BasicTypesResolvationTests.cs
171 lines (150 loc) · 7.94 KB
/
BasicTypesResolvationTests.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using Reinforced.Typings.Ast.TypeNames;
using Xunit;
namespace Reinforced.Typings.Tests
{
public class BasicTypesResolvationTests
{
private readonly TypeResolver _tr;
private readonly TypeNameEqualityComparer _comparer;
private readonly ExportContext _context;
private static readonly RtSimpleTypeName AnyType = new RtSimpleTypeName("any");
private static readonly RtSimpleTypeName NumberType = new RtSimpleTypeName("number");
private static readonly RtSimpleTypeName StringType = new RtSimpleTypeName("string");
private static readonly RtArrayType AnyArrayType = new RtArrayType(AnyType);
public BasicTypesResolvationTests()
{
_context = new ExportContext()
{
SourceAssemblies = new[] { Assembly.GetExecutingAssembly() },
TargetDirectory = "targetDir/"
};
var exporter = new TsExporter(_context);
exporter.Initialize();
_tr = exporter.SetupExportedFile().TypeResolver;
_comparer = new TypeNameEqualityComparer();
}
[Fact]
public void NumericsToNumber()
{
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(int)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(uint)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(long)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(ulong)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(short)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(ushort)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(byte)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(double)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(float)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(decimal)), _comparer);
}
[Fact]
public void StringsAndCharsToString()
{
Assert.Equal(StringType, _tr.ResolveTypeName(typeof(string)), _comparer);
Assert.Equal(StringType, _tr.ResolveTypeName(typeof(char)), _comparer);
}
[Fact]
public void NullableNumbersToNumber()
{
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(int?)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(uint?)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(long?)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(ulong?)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(short?)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(ushort?)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(byte?)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(double?)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(float?)), _comparer);
Assert.Equal(NumberType, _tr.ResolveTypeName(typeof(decimal?)), _comparer);
}
[Fact]
public void VoidToVoid()
{
Assert.Equal(new RtSimpleTypeName("void"), _tr.ResolveTypeName(typeof(void)), _comparer);
}
[Fact]
public void ObjectToAny()
{
Assert.Equal(AnyType, _tr.ResolveTypeName(typeof(object)), _comparer);
}
[Fact]
public void NongenericCollectionsToAnyArray()
{
Assert.Equal(AnyArrayType, _tr.ResolveTypeName(typeof(IEnumerable)), _comparer);
Assert.Equal(AnyArrayType, _tr.ResolveTypeName(typeof(IQueryable)), _comparer);
Assert.Equal(AnyArrayType, _tr.ResolveTypeName(typeof(IList)), _comparer);
Assert.Equal(AnyArrayType, _tr.ResolveTypeName(typeof(Stack)), _comparer);
Assert.Equal(AnyArrayType, _tr.ResolveTypeName(typeof(Queue)), _comparer);
Assert.Equal(AnyArrayType, _tr.ResolveTypeName(typeof(ArrayList)), _comparer);
Assert.Equal(AnyArrayType, _tr.ResolveTypeName(typeof(CollectionBase)), _comparer);
}
[Fact]
public void DictionaryToObject()
{
Assert.Equal(new RtDictionaryType(AnyType, AnyType), _tr.ResolveTypeName(typeof(IDictionary)), _comparer);
Assert.True(_context.Warnings.Any(c => c.Code == 7));
_context.Warnings.Clear();
Assert.Equal(new RtDictionaryType(AnyType, AnyType), _tr.ResolveTypeName(typeof(IDictionary<object, object>)), _comparer);
Assert.True(_context.Warnings.Any(c => c.Code == 7));
_context.Warnings.Clear();
Assert.Equal(new RtDictionaryType(StringType, AnyType), _tr.ResolveTypeName(typeof(IDictionary<string, object>)), _comparer);
Assert.Equal(new RtDictionaryType(NumberType, AnyType), _tr.ResolveTypeName(typeof(IDictionary<int, object>)), _comparer);
Assert.Equal(new RtDictionaryType(AnyType, AnyType), _tr.ResolveTypeName(typeof(Dictionary<object, object>)), _comparer);
Assert.True(_context.Warnings.Any(c => c.Code == 7));
_context.Warnings.Clear();
Assert.Equal(new RtDictionaryType(StringType, AnyType), _tr.ResolveTypeName(typeof(Dictionary<string, object>)), _comparer);
Assert.Equal(new RtDictionaryType(NumberType, AnyType), _tr.ResolveTypeName(typeof(Dictionary<int, object>)), _comparer);
}
private void GenericCollectionsOfType<T>(RtTypeName targetType)
{
Assert.Equal(targetType, _tr.ResolveTypeName(typeof(IEnumerable<T>)), _comparer);
Assert.Equal(targetType, _tr.ResolveTypeName(typeof(IQueryable<T>)), _comparer);
Assert.Equal(targetType, _tr.ResolveTypeName(typeof(IList<T>)), _comparer);
Assert.Equal(targetType, _tr.ResolveTypeName(typeof(Stack<T>)), _comparer);
Assert.Equal(targetType, _tr.ResolveTypeName(typeof(Queue<T>)), _comparer);
Assert.Equal(targetType, _tr.ResolveTypeName(typeof(Collection<T>)), _comparer);
Assert.Equal(targetType, _tr.ResolveTypeName(typeof(ICollection<T>)), _comparer);
Assert.Equal(targetType, _tr.ResolveTypeName(typeof(T[])), _comparer);
}
[Fact]
public void CollectionsToCorrespondingArray()
{
var numArray = new RtArrayType(NumberType);
GenericCollectionsOfType<int>(numArray);
GenericCollectionsOfType<uint>(numArray);
GenericCollectionsOfType<long>(numArray);
GenericCollectionsOfType<ulong>(numArray);
GenericCollectionsOfType<short>(numArray);
GenericCollectionsOfType<ushort>(numArray);
GenericCollectionsOfType<byte>(numArray);
GenericCollectionsOfType<double>(numArray);
GenericCollectionsOfType<float>(numArray);
GenericCollectionsOfType<decimal>(numArray);
GenericCollectionsOfType<string>(new RtArrayType(StringType));
GenericCollectionsOfType<bool>(new RtArrayType(new RtSimpleTypeName("boolean")));
}
[Fact]
public void SystemTuplesToTsTuples()
{
for (int i = 1; i < 8; i++)
{
var expected = new RtTuple();
List<Type> args = new List<Type>();
for (int j = 0; j < i; j++)
{
expected.TupleTypes.Add(AnyType);
args.Add(typeof(object));
}
var correspondingTuple = Type.GetType("System.Tuple`" + i);
var arrange = correspondingTuple.MakeGenericType(args.ToArray());
Assert.Equal(expected, _tr.ResolveTypeName(arrange), _comparer);
}
}
}
}