Skip to content

Commit d1568d1

Browse files
committed
#53 - auto mock
1 parent d2b6561 commit d1568d1

File tree

10 files changed

+99
-68
lines changed

10 files changed

+99
-68
lines changed

src/Domain/HydraScript.Domain.FrontEnd/Lexer/TokenTypes/EndOfProgramType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
namespace HydraScript.Domain.FrontEnd.Lexer.TokenTypes;
44

5-
internal record EndOfProgramType() : TokenType(Eop.Tag);
5+
public record EndOfProgramType() : TokenType(Eop.Tag);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace HydraScript.Domain.FrontEnd.Lexer.TokenTypes;
22

3-
internal record ErrorType() : TokenType("ERROR")
3+
public record ErrorType() : TokenType("ERROR")
44
{
55
public override bool Error() => true;
66
}

tests/HydraScript.UnitTests/Application/SymbolTableTests.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@ namespace HydraScript.UnitTests.Application;
55

66
public class SymbolTableTests
77
{
8-
[Fact]
9-
public void FindSymbolTest()
8+
[Theory, AutoHydraScriptData]
9+
public void FindSymbolTest(ISymbol symbol)
1010
{
11-
const string id = "ident";
12-
var type = new Type(id);
13-
14-
var symbol = Substitute.For<ISymbol>();
15-
symbol.Id.Returns(id);
16-
symbol.Type.Returns(type);
11+
var id = symbol.Id;
1712

1813
var outerScope = new SymbolTable();
1914
var innerScope = new SymbolTable();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using AutoFixture;
2+
using AutoFixture.AutoNSubstitute;
3+
using AutoFixture.Xunit2;
4+
using HydraScript.Domain.FrontEnd.Lexer;
5+
using HydraScript.Domain.FrontEnd.Lexer.Impl;
6+
using HydraScript.Domain.IR;
7+
using HydraScript.Domain.IR.Impl.Symbols;
8+
using PolymorphicContracts.AutoFixture;
9+
10+
namespace HydraScript.UnitTests;
11+
12+
public class AutoHydraScriptDataAttribute() :
13+
AutoDataAttribute(() =>
14+
{
15+
var fixture = new Fixture();
16+
fixture.Customize(new AutoNSubstituteCustomization { ConfigureMembers = true });
17+
18+
fixture.Inject<ITextCoordinateSystemComputer>(new TextCoordinateSystemComputer());
19+
20+
fixture.CustomizePolymorphismFor<ISymbol>()
21+
.WithDerivedType<VariableSymbol>()
22+
.WithDerivedType<TypeSymbol>()
23+
.WithDerivedType<ObjectSymbol>()
24+
.BuildCustomization();
25+
26+
return fixture;
27+
});

tests/HydraScript.UnitTests/Domain/BackEnd/VirtualMachineTests.cs

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using AutoFixture.Xunit2;
12
using HydraScript.Domain.BackEnd;
23
using HydraScript.Domain.BackEnd.Impl;
34
using HydraScript.Domain.BackEnd.Impl.Addresses;
@@ -12,18 +13,12 @@ namespace HydraScript.UnitTests.Domain.BackEnd;
1213

1314
public class VirtualMachineTests
1415
{
15-
private readonly VirtualMachine _vm = new(Substitute.For<IOutputWriter>());
16-
17-
[Fact]
18-
public void CorrectPrintToOutTest()
16+
[Theory, AutoHydraScriptData]
17+
public void CorrectPrintToOutTest([Frozen] IOutputWriter writer, IExecuteParams exParams)
1918
{
20-
var writer = Substitute.For<IOutputWriter>();
21-
22-
var exParams = Substitute.For<IExecuteParams>();
2319
exParams.CallStack.Returns(new Stack<Call>());
2420
exParams.Frames.Returns(new Stack<Frame>([new Frame(new HashAddress(0))]));
2521
exParams.Arguments.Returns(new Queue<object?>());
26-
exParams.Writer.Returns(writer);
2722

2823
var print = new Print(new Constant(223))
2924
{
@@ -34,18 +29,18 @@ public void CorrectPrintToOutTest()
3429
writer.Received(1).WriteLine(223);
3530
}
3631

37-
[Fact]
38-
public void ProgramWithoutHaltWillNotRunTest()
32+
[Theory, AutoHydraScriptData]
33+
public void ProgramWithoutHaltWillNotRunTest(VirtualMachine vm)
3934
{
4035
var program = new AddressedInstructions();
41-
Assert.Throws<ArgumentNullException>(() => _vm.Run(program));
42-
36+
Assert.Throws<ArgumentNullException>(() => vm.Run(program));
37+
4338
program.Add(new Halt());
44-
Assert.Null(Record.Exception(() => _vm.Run(program)));
39+
Assert.Null(Record.Exception(() => vm.Run(program)));
4540
}
4641

47-
[Fact]
48-
public void VirtualMachineFramesClearedAfterExecutionTest()
42+
[Theory, AutoHydraScriptData]
43+
public void VirtualMachineFramesClearedAfterExecutionTest(VirtualMachine vm)
4944
{
5045
AddressedInstructions program =
5146
[
@@ -56,13 +51,13 @@ public void VirtualMachineFramesClearedAfterExecutionTest()
5651
},
5752
new Halt()
5853
];
59-
60-
_vm.Run(program);
61-
Assert.Empty(_vm.ExecuteParams.Frames);
54+
55+
vm.Run(program);
56+
Assert.Empty(vm.ExecuteParams.Frames);
6257
}
6358

64-
[Fact]
65-
public void VirtualMachineHandlesRecursionTest()
59+
[Theory, AutoHydraScriptData]
60+
public void VirtualMachineHandlesRecursionTest(VirtualMachine vm)
6661
{
6762
var halt = HaltTrackable();
6863
var factorial = new FunctionInfo("fact");
@@ -90,47 +85,46 @@ public void VirtualMachineHandlesRecursionTest()
9085
},
9186
halt
9287
};
93-
94-
_vm.Run(program);
95-
Assert.Empty(_vm.ExecuteParams.CallStack);
96-
Assert.Empty(_vm.ExecuteParams.Arguments);
88+
89+
vm.Run(program);
90+
Assert.Empty(vm.ExecuteParams.CallStack);
91+
Assert.Empty(vm.ExecuteParams.Arguments);
9792
halt.Received(1).Execute(
9893
Arg.Is<IExecuteParams>(
99-
vm =>
100-
Convert.ToInt32(vm.Frames.Peek()["fa6"]) == 720));
101-
_vm.ExecuteParams.Frames.Pop();
94+
vmParam =>
95+
Convert.ToInt32(vmParam.Frames.Peek()["fa6"]) == 720));
96+
vm.ExecuteParams.Frames.Pop();
10297
}
10398

104-
[Fact]
105-
public void CreateArrayReservesCertainSpaceTest()
99+
[Theory, AutoHydraScriptData]
100+
public void CreateArrayReservesCertainSpaceTest(ExecuteParams vm)
106101
{
107-
var vm = new ExecuteParams(Substitute.For<IOutputWriter>());
108102
vm.Frames.Push(new Frame(new HashAddress(0)));
109-
103+
110104
var createArray = new CreateArray("arr", 6)
111105
{
112106
Address = new HashAddress(1)
113107
};
114108
createArray.Execute(vm);
115-
Assert.Equal(6, ((List<object>) vm.Frames.Peek()["arr"]!).Count);
109+
Assert.Equal(6, ((List<object>)vm.Frames.Peek()["arr"]!).Count);
116110

117111
var indexAssignment = new IndexAssignment("arr", new Constant(0), new Constant(0))
118112
{
119113
Address = new HashAddress(2)
120114
};
121115
indexAssignment.Execute(vm);
122-
Assert.Equal(0, ((List<object>) vm.Frames.Peek()["arr"]!)[0]);
116+
Assert.Equal(0, ((List<object>)vm.Frames.Peek()["arr"]!)[0]);
123117

124118
var removeFromArray = new RemoveFromArray("arr", new Constant(5))
125119
{
126120
Address = new HashAddress(3)
127121
};
128122
removeFromArray.Execute(vm);
129-
Assert.Equal(5, ((List<object>) vm.Frames.Peek()["arr"]!).Count);
123+
Assert.Equal(5, ((List<object>)vm.Frames.Peek()["arr"]!).Count);
130124
}
131125

132-
[Fact]
133-
public void ObjectCreationTest()
126+
[Theory, AutoHydraScriptData]
127+
public void ObjectCreationTest(VirtualMachine vm)
134128
{
135129
var halt = HaltTrackable();
136130
AddressedInstructions program =
@@ -139,13 +133,13 @@ public void ObjectCreationTest()
139133
new DotAssignment("obj", new Constant("prop"), new Constant(null, "null")),
140134
halt
141135
];
142-
143-
_vm.Run(program);
136+
137+
vm.Run(program);
144138
halt.Received(1).Execute(
145139
Arg.Is<IExecuteParams>(
146-
vm =>
147-
((Dictionary<string, object?>)vm.Frames.Peek()["obj"]!)["prop"] == null));
148-
_vm.ExecuteParams.Frames.Pop();
140+
vmParam =>
141+
((Dictionary<string, object?>)vmParam.Frames.Peek()["obj"]!)["prop"] == null));
142+
vm.ExecuteParams.Frames.Pop();
149143
}
150144

151145
private static Halt HaltTrackable()

tests/HydraScript.UnitTests/Domain/FrontEnd/AstNodeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public void PrecedenceTest()
1313
{
1414
var lexicalDecl = new LexicalDeclaration(false);
1515
List<StatementListItem> stmtItemList = [lexicalDecl];
16-
// ReSharper disable once UnusedVariable
16+
1717
var func = new FunctionDeclaration(
1818
name: new IdentifierReference(name: Guid.NewGuid().ToString()),
1919
new TypeIdentValue(
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Text.RegularExpressions;
2+
using HydraScript.Domain.FrontEnd.Lexer;
3+
4+
namespace HydraScript.UnitTests.Domain.FrontEnd;
5+
6+
public partial class DummyContainer : IGeneratedRegexContainer
7+
{
8+
[GeneratedRegex(TokenInput.Pattern)]
9+
public static partial Regex Regex { get; }
10+
}

tests/HydraScript.UnitTests/Domain/FrontEnd/RegexLexerTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,14 @@ public void GetTokens_KeywordInsideIdent_Ident(string input)
5353
token.Type.Should().Be(new TokenType("Ident"));
5454
}
5555

56-
[Theory, AutoData]
57-
public void GetTokens_MockedRegex_ValidOutput([MinLength(10), MaxLength(25)] TokenInput[] tokenInputs)
56+
[Theory, AutoHydraScriptData]
57+
public void GetTokens_MockedRegex_ValidOutput(
58+
[MinLength(10), MaxLength(25)] TokenInput[] tokenInputs,
59+
[Frozen] IStructure structure,
60+
RegexLexer lexer)
5861
{
5962
var patterns = TokenInput.Pattern.Split('|');
6063

61-
var structure = Substitute.For<IStructure>();
62-
var lexer = new RegexLexer(structure, new TextCoordinateSystemComputer());
6364
structure.Regex.ReturnsForAnyArgs(
6465
new Regex(string.Join('|', patterns.Select((x, i) => $"(?<TYPE{i}>{x})"))));
6566
var tokenTypes = Enumerable.Range(0, patterns.Length)

tests/HydraScript.UnitTests/Domain/FrontEnd/StructureTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,12 @@ public void ToStringCorrectTest()
2929
]);
3030
Assert.Equal(expectedText, structure.ToString());
3131
}
32+
33+
[Theory, AutoHydraScriptData]
34+
public void GetTokenTypes_NoMatterWhat_AlwaysHaveEopAndError(Structure<DummyContainer> structure)
35+
{
36+
var tokenTypes = structure.ToList();
37+
List<TokenType> expected = [new EndOfProgramType(), new ErrorType()];
38+
tokenTypes.Should().Contain(expected);
39+
}
3240
}

tests/HydraScript.UnitTests/Domain/IR/ObjectTypeTests.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,32 @@ public void ObjectTypeEqualityTest()
99
{
1010
var number = new Type("number");
1111
var p2d1 = new ObjectType(
12-
new PropertyType[]
13-
{
12+
[
1413
new("x", number),
1514
new("y", number)
16-
}
15+
]
1716
);
1817
var p2d2 = new ObjectType(
19-
new PropertyType[]
20-
{
18+
[
2119
new("y", number),
2220
new("x", number)
23-
}
21+
]
2422
);
2523
Assert.Equal(p2d1, p2d2);
2624

2725
var p3d1 = new ObjectType(
28-
new PropertyType[]
29-
{
26+
[
3027
new("a", number),
3128
new("x", number),
3229
new("y", number)
33-
}
30+
]
3431
);
3532
var p3d2 = new ObjectType(
36-
new PropertyType[]
37-
{
33+
[
3834
new("y", number),
3935
new("x", number),
4036
new("z", number)
41-
}
37+
]
4238
);
4339
Assert.NotEqual(p3d1, p3d2);
4440
Assert.NotEqual(p3d2, p2d1);

0 commit comments

Comments
 (0)