
Problem Statement
The objective is to develop a versatile testing utility function named expect
. This function is designed to assess the equality of values in unit testing scenarios primarily used during software development. When a value is passed into the expect
function, it returns an object comprising two methods: toBe
and notToBe
.
The
toBe(val)
method evaluates whether the passed value strictly equals the original value (===
). If the values match, it returnstrue
. Conversely, if the values do not match, it raises an error with the message"Not Equal"
.The
notToBe(val)
method checks if the passed value is strictly not equal to the original value (!==
). If the values are indeed not equal, it returnstrue
. If they are equal, it throws an error stating"Equal"
.
Examples
Example 1
Input:
expect(5).toBe(5)
Output:
true
Explanation:
The original value is 5 and the comparison is also 5. Since 5 === 5 is true, the function returns true without error.
Example 2
Input:
expect(5).notToBe(3)
Output:
true
Explanation:
The original value is 5 and the compared value is 3. Since 5 !== 3 is true, the function returns true without error.
Example 3
Input:
expect(5).toBe(3)
Output:
Error: Not Equal
Explanation:
The original value is 5 and the compared value is 3. Since 5 === 3 is false, the function throws an error "Not Equal".
Example 4
Input:
expect(5).notToBe(5)
Output:
Error: Equal
Explanation:
The original value is 5 and the compared value is also 5. Since 5 !== 5 is false, the function throws an error "Equal".
Constraints
- All values passed to
expect
and its methods will be primitive values (number, string, boolean). - The
expect
function and returned methods are guaranteed to be called with valid arguments.
Approach and Intuition
The implementation of the expect
function and its methods reflects basic principles in software testing, specifically in unit testing, where outcomes need to be predictable and strictly verified.
Understanding the Function Structure: The
expect
function initializes with a single argument and returns an object. This object's methods (toBe
andnotToBe
) allow for further interactions based on strict equality checks.Approach to
toBe()
: This method compares two values for strict equality using the===
operator. It’s used when the exactness of the value, including the type, is crucial for the test's integrity.- If the comparison is true, return
true
. - If it fails, an error
"Not Equal"
is thrown to indicate that the expected result was not met.
- If the comparison is true, return
Approach to
notToBe()
: Conversely, this method uses the negation of strict equality (!==
) to ensure two compared values are not the same. This is useful when ensuring that certain conditions lead to different or changed values.- If the values do not match, it returns
true
. - If they match, however, it throws an
"Equal"
error as the outcome is contrary to the expected non-equality.
- If the values do not match, it returns
By ensuring these two methods behave predictably and correctly, developers can more effectively write tests that help maintain robust and error-free codebases.
Solutions
- JavaScript
class Assertion {
constructor(value) {
this.value = value;
}
isEqualTo(other) {
if (this.value !== other) {
throw new Error("Values do not match");
}
return true;
}
isNotEqualTo(other) {
if (this.value === other) {
throw new Error("Values should not match");
}
return true;
}
}
function expect(value) {
return new Assertion(value);
}
In this Javascript solution, a basic assertion library is implemented to facilitate testing by ensuring that values meet specified conditions. The implementation consists of an Assertion
class and a helper function expect
.
The
Assertion
class initializes with a single value. It includes two methods:isEqualTo(other)
: Checks if the class instance's value strictly equals another specified value. If they do not match, it throws an error with the message "Values do not match". If the values match, it returns true.isNotEqualTo(other)
: Checks if the class instance's value does not strictly equal another specified value. If they match, it throws an error with the message "Values should not match". If the values do not match, it returns true.
The
expect
function serves as a factory for theAssertion
class. Passing a value toexpect
returns a new instance of theAssertion
class associated with the given value.
This structure provides a straightforward way to perform assertions in tests, ensuring values precisely meet expectations, either by matching or by not matching, thereby throwing descriptive errors if the assertions fail.
No comments yet.