tags: - testing - assertions - AssertTrue - AssertEquals
Testing and Assertions¶
EverSharp includes built-in assertion functions for test-driven development and runtime validation.
Assertion Functions¶
$AssertTrue¶
Asserts that a condition is true. Throws an error if false.
Syntax:
Parameters:
condition- Boolean expression to test
Example:
// Test basic arithmetic
result = 2 + 2;
$AssertTrue(result == 4); // Passes
// Test object property
user = { age: 25, active: true };
$AssertTrue(user.active); // Passes
// Test array contains value
numbers = [1, 2, 3, 4, 5];
$AssertTrue(numbers.Any((x) => x == 3)); // Passes
Throws: Error if condition is false
$AssertFalse¶
Asserts that a condition is false. Throws an error if true.
Syntax:
Parameters:
condition- Boolean expression to test
Example:
// Test user is not admin
user = { role: "user" };
$AssertFalse(user.role == "admin"); // Passes
// Test array is not empty
items = [1, 2, 3];
$AssertFalse(items.Length() == 0); // Passes
// Test value is not null
value = "Hello";
$AssertFalse(value == null); // Passes
Throws: Error if condition is true
$AssertAreEqual¶
Asserts that two values are equal. Throws an error if not equal.
Syntax:
Parameters:
expected- The expected valueactual- The actual value to compare
Example:
// Test calculation result
expected = 10;
actual = 5 * 2;
$AssertAreEqual(expected, actual); // Passes
// Test string operation
expected = "HELLO";
actual = "hello".ToUpper();
$AssertAreEqual(expected, actual); // Passes
// Test object property
user = { name: "Alice" };
$AssertAreEqual("Alice", user.name); // Passes
// Test array length
items = [1, 2, 3];
$AssertAreEqual(3, items.Length()); // Passes
Throws: Error if values are not equal
Testing Patterns¶
Pattern 1: Unit Test Structure¶
// Test function: calculateDiscount
function testCalculateDiscount() {
// Arrange
price = 100;
discountPercent = 20;
// Act
result = calculateDiscount(price, discountPercent);
// Assert
$AssertAreEqual(80, result);
}
// Run test
testCalculateDiscount();
Pattern 2: Multiple Assertions¶
// Test user validation
function testUserValidation() {
user = {
name: "Alice",
age: 25,
email: "alice@example.com",
};
$AssertTrue(user.name != null);
$AssertTrue(user.age >= 18);
$AssertTrue(user.email.Contains("@"));
$AssertAreEqual(25, user.age);
}
Pattern 3: Data Transformation Tests¶
// Test array transformation
function testArrayTransform() {
input = [1, 2, 3, 4, 5];
result = input.Map((x) => x * 2);
$AssertAreEqual(5, result.Length());
$AssertAreEqual(2, result[0]);
$AssertAreEqual(10, result[4]);
$AssertTrue(result.All((x) => x % 2 == 0));
}
Pattern 4: Edge Case Testing¶
// Test with empty array
function testEmptyArray() {
empty = [];
result = empty.Filter((x) => x > 0);
$AssertAreEqual(0, result.Length());
$AssertTrue(result.Length() == 0);
}
// Test with null values
function testNullHandling() {
value = null;
result = value == null ? "default" : value;
$AssertAreEqual("default", result);
}
Pattern 5: Comparison Operations¶
// Test greater than
function testGreaterThan() {
value = 10;
$AssertTrue(value > 5);
$AssertFalse(value > 20);
$AssertTrue(value >= 10);
}
// Test range checking
function testInRange() {
age = 25;
minAge = 18;
maxAge = 65;
$AssertTrue(age >= minAge);
$AssertTrue(age <= maxAge);
}
Pattern 6: String Operations¶
// Test string methods
function testStringMethods() {
text = "Hello World";
$AssertTrue(text.StartsWith("Hello"));
$AssertTrue(text.EndsWith("World"));
$AssertTrue(text.Contains("o W"));
$AssertAreEqual(11, text.Length());
$AssertAreEqual("HELLO WORLD", text.ToUpper());
}
Test Organization¶
Grouping Tests by Feature¶
// User management tests
function runUserTests() {
testUserCreation();
testUserValidation();
testUserUpdate();
$Log("User tests passed");
}
// Order processing tests
function runOrderTests() {
testOrderCalculation();
testOrderStatus();
testOrderValidation();
$Log("Order tests passed");
}
// Run all tests
runUserTests();
runOrderTests();
Test Data Setup¶
// Create test data
function createTestUser() {
return {
id: 1,
name: "Test User",
email: "test@example.com",
age: 30,
active: true,
};
}
// Use in tests
function testUserObject() {
user = createTestUser();
$AssertAreEqual(1, user.id);
$AssertTrue(user.active);
}
Best Practices¶
1. Test One Thing¶
// Good: Focused test
function testAddition() {
result = 2 + 2;
$AssertAreEqual(4, result);
}
// Avoid: Testing multiple unrelated things
function testEverything() {
$AssertAreEqual(4, 2 + 2);
$AssertTrue("hello".Contains("o"));
// Too many unrelated assertions
}
2. Use Descriptive Names¶
// Good
function testCalculateDiscountWithTwentyPercent() {
// ...
}
// Less clear
function test1() {
// ...
}
3. Arrange-Act-Assert Pattern¶
function testPriceCalculation() {
// Arrange - Set up test data
basePrice = 100;
taxRate = 0.1;
// Act - Perform operation
total = basePrice * (1 + taxRate);
// Assert - Verify result
$AssertAreEqual(110, total);
}
4. Test Edge Cases¶
function testArrayOperations() {
// Normal case
normal = [1, 2, 3];
$AssertAreEqual(3, normal.Length());
// Edge: Empty array
empty = [];
$AssertAreEqual(0, empty.Length());
// Edge: Single element
single = [42];
$AssertAreEqual(1, single.Length());
$AssertAreEqual(42, single[0]);
}
5. Keep Tests Independent¶
// Good: Each test is self-contained
function testUserCreation() {
user = { name: "Alice" };
$AssertAreEqual("Alice", user.name);
}
function testUserUpdate() {
user = { name: "Bob" };
user.name = "Charlie";
$AssertAreEqual("Charlie", user.name);
}
// Avoid: Tests depending on each other
Error Messages¶
When an assertion fails, EverSharp throws an error with details about the failure:
// This will throw an error with message indicating the failure
$AssertAreEqual(5, 3);
// Error: Assertion failed: Expected 5 but was 3
$AssertTrue(false);
// Error: Assertion failed: Expected true but was false