-
Notifications
You must be signed in to change notification settings - Fork 1
/
NameTests.cs
66 lines (55 loc) · 2.47 KB
/
NameTests.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
using Rook.Compiling.Types;
using Should;
using Xunit;
namespace Rook.Compiling.Syntax
{
public class NameTests : ExpressionTests
{
[Fact]
public void CanBeIdentifier()
{
Parses("a").IntoTree("a");
Parses("abc").IntoTree("abc");
}
[Fact]
public void HasATypeProvidedByTheGivenScope()
{
Type("foo", foo => Boolean, bar => Integer).ShouldEqual(Boolean);
Type("bar", foo => Boolean, bar => Integer).ShouldEqual(Integer);
}
[Fact]
public void HasATypeInWhichTypeVariablesAreFreshenedOnEachScopeLookup()
{
Type("foo", foo => new TypeVariable(0)).ShouldEqual(new TypeVariable(2));
var expectedTypeAfterLookup = new NamedType("A", new TypeVariable(2), new TypeVariable(3), new NamedType("B", new TypeVariable(2), new TypeVariable(3)));
var definedType = new NamedType("A", new TypeVariable(0), new TypeVariable(1), new NamedType("B", new TypeVariable(0), new TypeVariable(1)));
Type("foo", foo => definedType).ShouldEqual(expectedTypeAfterLookup);
}
[Fact]
public void HasATypeInWhichOnlyGenericTypeVariablesAreFreshenedOnEachScopeLookup()
{
//Prevent type '1' from being freshened on type lookup by marking it as non-generic:
var typeVariable1 = new TypeVariable(1, isGeneric: false);
var expectedTypeAfterLookup = new NamedType("A", new TypeVariable(2), typeVariable1, new NamedType("B", new TypeVariable(2), typeVariable1));
var definedType = new NamedType("A", new TypeVariable(0), typeVariable1, new NamedType("B", new TypeVariable(0), typeVariable1));
var typeChecker = new TypeChecker();
var globalScope = new GlobalScope(typeChecker);
var localScope = new LocalScope(globalScope);
localScope.Bind("foo", definedType);
Type("foo", localScope, typeChecker).ShouldEqual(expectedTypeAfterLookup);
}
[Fact]
public void CanCreateFullyTypedInstance()
{
var name = (Name)Parse("foo");
name.Type.ShouldBeNull();
var typedName = WithTypes(name, foo => Boolean);
typedName.Type.ShouldEqual(Boolean);
}
[Fact]
public void FailsTypeCheckingForIdentifiersNotInScope()
{
ShouldFailTypeChecking("foo").WithError("Reference to undefined identifier: foo", 1, 1);
}
}
}