Skip to content

Commit

Permalink
chore: Use FluentAssertations instead of Assert.That
Browse files Browse the repository at this point in the history
We now use FluentAssertations instead of using Assert.That when
asserting post-conditions of our unit tests.
  • Loading branch information
PrestonLTaylor committed Apr 5, 2024
1 parent 4cb0a7a commit bb52dcc
Show file tree
Hide file tree
Showing 6 changed files with 477 additions and 471 deletions.
145 changes: 73 additions & 72 deletions JSS.Lib.UnitTests/ASTTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using String = JSS.Lib.AST.Values.String;
using ValueType = JSS.Lib.AST.Values.ValueType;
using JSS.Lib.AST;
using FluentAssertions;

namespace JSS.Lib.UnitTests;

Expand Down Expand Up @@ -720,7 +721,7 @@ public void ScriptEvaluation_ReturnsExpectedCompletionAndValue(string testCase,
var actualCompletion = script.ScriptEvaluation();

// Assert
Assert.That(actualCompletion, Is.EqualTo(expectedCompletion));
actualCompletion.Should().Be(expectedCompletion);
}

[Test]
Expand All @@ -733,10 +734,10 @@ public void NullLiteral_Evalute_ReturnsNormalCompletion_WithGlobalVMNullValue()
var completion = script.ScriptEvaluation();

// Assert
Assert.That(completion.IsNormalCompletion(), Is.True);
completion.IsNormalCompletion().Should().BeTrue();

// NOTE: This assert makes sure we use the global null value
Assert.That(completion.Value, Is.SameAs(Null.The));
// NOTE: This makes sure we use the global null value
completion.Value.Should().Be(Null.The);
}

[Test]
Expand All @@ -751,9 +752,9 @@ public void BreakStatement_Evaluate_ReturnsBreakCompletion_WithNoTarget_WhenProv
var completion = breakStatement.Evaluate(vm);

// Assert
Assert.That(completion.IsBreakCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(Empty.The));
Assert.That(completion.Target, Is.Empty);
completion.IsBreakCompletion().Should().BeTrue();
completion.Value.Should().Be(Empty.The);
completion.Target.Should().BeEmpty();
}

[Test]
Expand All @@ -770,9 +771,9 @@ public void BreakStatement_Evaluate_ReturnsBreakCompletion_WithTarget_WhenProvid
var completion = breakStatement.Evaluate(vm);

// Assert
Assert.That(completion.IsBreakCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(Empty.The));
Assert.That(completion.Target, Is.EqualTo(expectedTarget));
completion.IsBreakCompletion().Should().BeTrue();
completion.Value.Should().Be(Empty.The);
completion.Target.Should().Be(expectedTarget);
}

[Test]
Expand All @@ -787,9 +788,9 @@ public void ContinueStatement_Evaluate_ReturnsContinueCompletion_WithNoTarget_Wh
var completion = continueStatement.Evaluate(vm);

// Assert
Assert.That(completion.IsContinueCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(Empty.The));
Assert.That(completion.Target, Is.Empty);
completion.IsContinueCompletion().Should().BeTrue();
completion.Value.Should().Be(Empty.The);
completion.Target.Should().BeEmpty();
}

[Test]
Expand All @@ -806,9 +807,9 @@ public void ContinueStatement_Evaluate_ReturnsContinueCompletion_WithTarget_When
var completion = continueStatement.Evaluate(vm);

// Assert
Assert.That(completion.IsContinueCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(Empty.The));
Assert.That(completion.Target, Is.EqualTo(expectedTarget));
completion.IsContinueCompletion().Should().BeTrue();
completion.Value.Should().Be(Empty.The);
completion.Target.Should().Be(expectedTarget);
}

// FIXME: More tests when we parse more object literals
Expand All @@ -827,11 +828,11 @@ public void AssignmentWithObjectLiterals_AssignsAsExpectedObjectValue(string obj
var actualCompletion = script.ScriptEvaluation();

// Assert
Assert.That(actualCompletion.IsNormalCompletion(), Is.True);
actualCompletion.IsNormalCompletion().Should().BeTrue();

var actualObject = actualCompletion.Value as Object;
Assert.That(actualObject, Is.Not.Null);
Assert.That(actualObject.DataProperties, Has.Count.EqualTo(0));
actualObject.Should().NotBeNull();
actualObject.DataProperties.Should().HaveCount(0);

Check warning on line 835 in JSS.Lib.UnitTests/ASTTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}

[Test]
Expand All @@ -844,11 +845,11 @@ public void ComputedProperties_HaveTheSameValue_AsNormalProperties()
var actualCompletion = script.ScriptEvaluation();

// Assert
Assert.That(actualCompletion.IsNormalCompletion(), Is.True);
actualCompletion.IsNormalCompletion().Should().BeTrue();

var shouldBeTrue = actualCompletion.Value as Boolean;
Assert.That(shouldBeTrue, Is.Not.Null);
Assert.That(shouldBeTrue.Value, Is.True);
shouldBeTrue.Should().NotBeNull();
shouldBeTrue.Value.Should().BeTrue();

Check warning on line 852 in JSS.Lib.UnitTests/ASTTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}

// FIXME: We should have a different file for runtime tests
Expand All @@ -862,11 +863,11 @@ public void TypeOf_ObjectConstructor_ReturnsAString_EqualToFunction()
var actualCompletion = script.ScriptEvaluation();

// Assert
Assert.That(actualCompletion.IsNormalCompletion(), Is.True);
actualCompletion.IsNormalCompletion().Should().BeTrue();

var actualTypeOf = actualCompletion.Value as String;
Assert.That(actualTypeOf, Is.Not.Null);
Assert.That(actualTypeOf.Value, Is.EqualTo("function"));
actualTypeOf.Should().NotBeNull();
actualTypeOf.Value.Should().Be("function");

Check warning on line 870 in JSS.Lib.UnitTests/ASTTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}

[Test]
Expand All @@ -879,12 +880,12 @@ public void Calling_ObjectConstrcutor_ReturnsANewEmptyObject()
var actualCompletion = script.ScriptEvaluation();

// Assert
Assert.That(actualCompletion.IsNormalCompletion(), Is.True);
actualCompletion.IsNormalCompletion().Should().BeTrue();

var actualObject = actualCompletion.Value as Object;
Assert.That(actualObject, Is.Not.Null);
actualObject.Should().NotBeNull();
// FIXME: Assert for %Object.prototype%
Assert.That(actualObject.DataProperties, Has.Count.Zero);
actualObject.DataProperties.Should().BeEmpty();

Check warning on line 888 in JSS.Lib.UnitTests/ASTTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}

[Test]
Expand All @@ -897,12 +898,12 @@ public void NewExpression_WithObjectConstructor_ReturnsANewEmptyObject()
var actualCompletion = script.ScriptEvaluation();

// Assert
Assert.That(actualCompletion.IsNormalCompletion(), Is.True);
actualCompletion.IsNormalCompletion().Should().BeTrue();

var actualObject = actualCompletion.Value as Object;
Assert.That(actualObject, Is.Not.Null);
actualObject.Should().NotBeNull();
// FIXME: Assert for %Object.prototype%
Assert.That(actualObject.DataProperties, Has.Count.Zero);
actualObject.DataProperties.Should().BeEmpty();

Check warning on line 906 in JSS.Lib.UnitTests/ASTTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}

[Test]
Expand All @@ -915,12 +916,12 @@ public void NewExpression_WithEmptyObjectReturningFunction_ReturnsANewEmptyObjec
var actualCompletion = script.ScriptEvaluation();

// Assert
Assert.That(actualCompletion.IsNormalCompletion(), Is.True);
actualCompletion.IsNormalCompletion().Should().BeTrue();

var actualObject = actualCompletion.Value as Object;
Assert.That(actualObject, Is.Not.Null);
actualObject.Should().NotBeNull();
// FIXME: Assert for %Object.prototype%
Assert.That(actualObject.DataProperties, Has.Count.Zero);
actualObject.DataProperties.Should().BeEmpty();

Check warning on line 924 in JSS.Lib.UnitTests/ASTTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}

[Test]
Expand All @@ -933,11 +934,11 @@ public void NewExpression_WithThisSettingFunction_ReturnsANewObject_WithExpected
var actualCompletion = script.ScriptEvaluation();

// Assert
Assert.That(actualCompletion.IsNormalCompletion(), Is.True);
actualCompletion.IsNormalCompletion().Should().BeTrue();

var actualValue = actualCompletion.Value as Number;
Assert.That(actualValue, Is.Not.Null);
Assert.That(actualValue.Value, Is.EqualTo(1));
actualValue.Should().NotBeNull();
actualValue.Value.Should().Be(1);
}

[Test]
Expand All @@ -950,16 +951,16 @@ public void ThisExpression_ReturnsObject_WithTheRealmsGlobalObjectProps_WhenProv
var actualCompletion = script.ScriptEvaluation();

// Assert
Assert.That(actualCompletion.IsNormalCompletion(), Is.True);
actualCompletion.IsNormalCompletion().Should().BeTrue();

var thisObject = actualCompletion.Value as Object;
Assert.That(thisObject, Is.Not.Null);
thisObject.Should().NotBeNull();
// FIXME: Assert for %Object.prototype%

var globalObject = script.Realm.GlobalObject;
foreach (var prop in globalObject.DataProperties)
{
Assert.That(thisObject.DataProperties.ContainsKey(prop.Key), Is.True);
thisObject.DataProperties.ContainsKey(prop.Key).Should().BeTrue();
}
}

Expand All @@ -982,8 +983,8 @@ public void BitwiseAndExpression_Evaluate_ReturnsNormalCompletion_WithExpectedRe
var completion = bitwiseAndExpression.Evaluate(vm);

// Assert
Assert.That(completion.IsNormalCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(expectedValue));
completion.IsNormalCompletion().Should().BeTrue();
completion.Value.Should().Be(expectedValue);
}

static private readonly object[] normalCompletionBitwiseOrTestCases =
Expand All @@ -1004,8 +1005,8 @@ public void BitwiseOrExpression_Evaluate_ReturnsNormalCompletion_WithExpectedRes
var completion = bitwiseOrExpression.Evaluate(vm);

// Assert
Assert.That(completion.IsNormalCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(expectedValue));
completion.IsNormalCompletion().Should().BeTrue();
completion.Value.Should().Be(expectedValue);
}

static private readonly object[] normalCompletionBitwiseXorTestCases =
Expand All @@ -1026,8 +1027,8 @@ public void BitwiseXorExpression_Evaluate_ReturnsNormalCompletion_WithExpectedRe
var completion = bitwiseXorExpression.Evaluate(vm);

// Assert
Assert.That(completion.IsNormalCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(expectedValue));
completion.IsNormalCompletion().Should().BeTrue();
completion.Value.Should().Be(expectedValue);
}

static private readonly object[] normalCompletionDivisionTestCases =
Expand All @@ -1048,8 +1049,8 @@ public void DivisionExpression_Evaluate_ReturnsNormalCompletion_WithExpectedResu
var completion = divisionExpression.Evaluate(vm);

// Assert
Assert.That(completion.IsNormalCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(expectedValue));
completion.IsNormalCompletion().Should().BeTrue();
completion.Value.Should().Be(expectedValue);
}

static private readonly object[] normalCompletionExponentiationTestCases =
Expand All @@ -1070,8 +1071,8 @@ public void ExponentiationExpression_Evaluate_ReturnsNormalCompletion_WithExpect
var completion = exponentiationExpression.Evaluate(vm);

// Assert
Assert.That(completion.IsNormalCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(expectedValue));
completion.IsNormalCompletion().Should().BeTrue();
completion.Value.Should().Be(expectedValue);
}

static private readonly object[] normalCompletionLeftShiftTestCases =
Expand All @@ -1092,8 +1093,8 @@ public void LeftShiftExpression_Evaluate_ReturnsNormalCompletion_WithExpectedRes
var completion = leftShiftExpression.Evaluate(vm);

// Assert
Assert.That(completion.IsNormalCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(expectedValue));
completion.IsNormalCompletion().Should().BeTrue();
completion.Value.Should().Be(expectedValue);
}

static private readonly object[] normalCompletionModuloTestCases =
Expand All @@ -1114,8 +1115,8 @@ public void ModuloExpression_Evaluate_ReturnsNormalCompletion_WithExpectedResult
var completion = moduloExpression.Evaluate(vm);

// Assert
Assert.That(completion.IsNormalCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(expectedValue));
completion.IsNormalCompletion().Should().BeTrue();
completion.Value.Should().Be(expectedValue);
}

static private readonly object[] normalCompletionMultiplyTestCases =
Expand All @@ -1136,8 +1137,8 @@ public void MultiplicationExpression_Evaluate_ReturnsNormalCompletion_WithExpect
var completion = multiplicationExpression.Evaluate(vm);

// Assert
Assert.That(completion.IsNormalCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(expectedValue));
completion.IsNormalCompletion().Should().BeTrue();
completion.Value.Should().Be(expectedValue);
}

static private readonly object[] normalCompletionRightShiftTestCases =
Expand All @@ -1158,8 +1159,8 @@ public void RightShiftExpression_Evaluate_ReturnsNormalCompletion_WithExpectedRe
var completion = rightShiftExpression.Evaluate(vm);

// Assert
Assert.That(completion.IsNormalCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(expectedValue));
completion.IsNormalCompletion().Should().BeTrue();
completion.Value.Should().Be(expectedValue);
}

static private readonly object[] normalCompletionSubtractionTestCases =
Expand All @@ -1180,8 +1181,8 @@ public void SubtractionExpression_Evaluate_ReturnsNormalCompletion_WithExpectedR
var completion = subtractionExpression.Evaluate(vm);

// Assert
Assert.That(completion.IsNormalCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(expectedValue));
completion.IsNormalCompletion().Should().BeTrue();
completion.Value.Should().Be(expectedValue);
}

static private readonly object[] normalCompletionUnsignedRightShiftTestCases =
Expand All @@ -1202,8 +1203,8 @@ public void UnsignedRightShiftExpression_Evaluate_ReturnsNormalCompletion_WithEx
var completion = unsignedRightShiftExpression.Evaluate(vm);

// Assert
Assert.That(completion.IsNormalCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(expectedValue));
completion.IsNormalCompletion().Should().BeTrue();
completion.Value.Should().Be(expectedValue);
}

[Test]
Expand All @@ -1217,8 +1218,8 @@ public void LetDeclarations_InsideBlock_DontEscapeToOuterScope()
var completion = script.ScriptEvaluation();

// Assert
Assert.That(completion.IsThrowCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(new String($"{identifier} is not defined")));
completion.IsThrowCompletion().Should().BeTrue();
completion.Value.Should().Be(new String($"{identifier} is not defined"));
}

[Test]
Expand All @@ -1232,8 +1233,8 @@ public void ConstDeclarations_InsideBlock_DontEscapeToOuterScope()
var completion = script.ScriptEvaluation();

// Assert
Assert.That(completion.IsThrowCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(new String($"{identifier} is not defined")));
completion.IsThrowCompletion().Should().BeTrue();
completion.Value.Should().Be(new String($"{identifier} is not defined"));
}

[Test]
Expand All @@ -1247,8 +1248,8 @@ public void AssignmentToConstDeclaration_ReturnsThrowCompletion()
var completion = script.ScriptEvaluation();

// Assert
Assert.That(completion.IsThrowCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(new String($"invalid assignment to const {identifier}")));
completion.IsThrowCompletion().Should().BeTrue();
completion.Value.Should().Be(new String($"invalid assignment to const {identifier}"));
}

[Test]
Expand All @@ -1262,8 +1263,8 @@ public void LetDeclarations_InsideFunctionDeclaration_DontEscapeToOuterScope()
var completion = script.ScriptEvaluation();

// Assert
Assert.That(completion.IsThrowCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(new String($"{identifier} is not defined")));
completion.IsThrowCompletion().Should().BeTrue();
completion.Value.Should().Be(new String($"{identifier} is not defined"));
}

[Test]
Expand All @@ -1277,8 +1278,8 @@ public void ConstDeclarations_InsideFunctionDeclaration_DontEscapeToOuterScope()
var completion = script.ScriptEvaluation();

// Assert
Assert.That(completion.IsThrowCompletion(), Is.True);
Assert.That(completion.Value, Is.EqualTo(new String($"{identifier} is not defined")));
completion.IsThrowCompletion().Should().BeTrue();
completion.Value.Should().Be(new String($"{identifier} is not defined"));
}

static private string EscapeString(string toEscape, char quote = '"')
Expand Down
Loading

0 comments on commit bb52dcc

Please sign in to comment.