Commit 6e5aa6cd authored by Tapio Mäntysalo's avatar Tapio Mäntysalo
Browse files

Now there are unit tests for the tasks

parent 4eb60a22
......@@ -31,15 +31,19 @@
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Choose action</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton" id="dropdown-main">
<a class="dropdown-item" href="#" id="dropdown-example">Example</a>
<a class="dropdown-item" href="#" id="dropdown-task-1">Task 1</a>
<a class="dropdown-item" href="#" id="dropdown-task-2">Task 2</a>
<a class="dropdown-item" href="#" id="dropdown-task-3">Task 3</a>
<a class="dropdown-item" href="#" id="dropdown-example">Example</a>
<a class="dropdown-item" href="#" id="dropdown-tests">Unit tests</a>
</div>
</div>
</div>
<hr>
<!-- Unit tests -->
<div class="task-container" id="unit-tests"><div id="mocha">Unit test results</div></div>
<!-- Task 3 -->
<div class="container-md task-container" id="task-3">
<div class="jumbotron">
......@@ -235,6 +239,11 @@
case "dropdown-task-3":
$("#task-3").show();
break;
case "dropdown-tests":
$("#unit-tests").show();
$("#mocha").text("");
mocha.run();
break;
}
});
</script>
......@@ -243,6 +252,11 @@
<script src="task1.js"></script>
<script src="example.js"></script>
<script src="util.js"></script>
<script src="test/example_test.js"></script>
<script src="test/task1_test.js"></script>
<script src="test/task2_test.js"></script>
<script src="test/task3_test.js"></script>
</body>
</html>
\ No newline at end of file
......@@ -56,27 +56,12 @@ function getResults(divider1, divider2, sum1, sum2, sumBoth) {
};
function calculateSumOfInts(limit1, limit2, divider1, divider2) {
if (limit2 < limit1) // This is not allowed
return null;
let sum = 0;
for (let i = limit1; i <= limit2; i++) {
sum += i % divider1 == 0 && i % divider2 == 0 ? i : 0;
}
return sum;
}
$("").click(function () {
let age = +$("#input-task-3").val();
let statement = getStatementForAge(age);
$("#card-task-3-result").text(statement);
});
function getStatementForAge(age) {
if (age >= 1 && age <= 122) {
return "Welcome!";
}
else if (age >= 123 && age <= 150) {
return "We have a new age record!";
}
else {
return "You seem to be lying.";
}
}
\ No newline at end of file
describe("Example", function() {
it("Finds greatest common divisor", function() {
assert.equal(getGreatestCommonDivisor(2, 1), 1);
assert.equal(getGreatestCommonDivisor(12, 4), 4);
assert.equal(getGreatestCommonDivisor(2, 0), 2);
assert.equal(getGreatestCommonDivisor(21, 7), 7);
});
});
describe("Task 1", function() {
it("Selects response based on users age", function() {
assert.isTrue(getStatementForAge(0).includes("lying"));
assert.isTrue(getStatementForAge(-12).includes("lying"));
assert.isTrue(getStatementForAge(160).includes("lying"));
assert.isFalse(getStatementForAge(1).includes("lying"));
assert.isFalse(getStatementForAge(60).includes("lying"));
assert.isFalse(getStatementForAge(123).includes("lying"));
assert.isFalse(getStatementForAge(0).includes("Welcome"));
assert.isFalse(getStatementForAge(-12).includes("Welcome"));
assert.isFalse(getStatementForAge(160).includes("Welcome"));
assert.isTrue(getStatementForAge(1).includes("Welcome"));
assert.isTrue(getStatementForAge(60).includes("Welcome"));
assert.isFalse(getStatementForAge(123).includes("Welcome"));
assert.isFalse(getStatementForAge(0).includes("record"));
assert.isFalse(getStatementForAge(-12).includes("record"));
assert.isFalse(getStatementForAge(160).includes("record"));
assert.isFalse(getStatementForAge(1).includes("record"));
assert.isFalse(getStatementForAge(60).includes("record"));
assert.isTrue(getStatementForAge(123).includes("record"));
});
});
describe("Task 2", function() {
it("Calculates the sum 0 + 0.1 + 0.2 + … + 0.9", function() {
assert.equal(getSum(), 0+0.1+0.2+0.3+0.4+0.5+0.6+0.7+0.8+0.9);
});
});
describe("Task 3", function () {
it("Returns the sum of all integers in the range [lower, upper] that are divisible by d1 or d2 (or both)", function () {
assert.equal(calculateSumOfInts(1, 6, 2, 2), 2 + 4 + 6);
assert.equal(calculateSumOfInts(1, 6, 3, 3), 3 + 6);
assert.equal(calculateSumOfInts(1, 6, 2, 3), 6);
assert.equal(calculateSumOfInts(-7, 7, 2, 2), -6 - 4 - 2 + 0 + 2 + 4 + 6);
assert.equal(calculateSumOfInts(-7, 10, 3, 3), -6 - 3 - 0 + 3 + 6 + 9);
assert.equal(calculateSumOfInts(-9, 0, 2, 3), -6);
assert.equal(calculateSumOfInts(2, 2, 2, 2), 2);
assert.equal(calculateSumOfInts(2, 2, 3, 3), 0);
assert.isNull(calculateSumOfInts(2, 1, 2, 3));
});
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment