Skip to content

Commit 85f8ce9

Browse files
authored
add templates for allergies and perfect-numbers (#486)
* add templates for allergies and perfect-numbers * adjust template formatting
1 parent f0b545c commit 85f8ce9

7 files changed

Lines changed: 536 additions & 69 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{%- import "generator_macros.j2" as macros with context -%}
2+
{{ macros.canonical_ref() }}
3+
4+
{{ macros.header() }}
5+
6+
{% for case in cases %}
7+
# {{ case["description"] }}
8+
{% for subcase in case["cases"] %}
9+
test_that("{{ subcase["description"] }}", {
10+
allergies <- allergy({{ subcase["input"]["score"] }})
11+
{% if subcase["property"] == "list" -%}
12+
expect_true(setequal(
13+
list_allergies(allergies),
14+
c{{ subcase["expected"] | list_to_tuple }}
15+
))
16+
{% else -%}
17+
expect_{{ subcase["expected"] | lower }}(allergic_to(allergies, "{{ subcase["input"]["item"] }}"))
18+
{% endif %}
19+
})
20+
{% endfor %}
21+
{% endfor %}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
source("./allergies.R")
2+
library(testthat)
3+
4+
test_that("no allergies means not allergic", {
5+
x <- allergy(0)
6+
expect_false(allergic_to(x, "peanuts"))
7+
expect_false(allergic_to(x, "cats"))
8+
expect_false(allergic_to(x, "strawberries"))
9+
})
10+
11+
test_that("is allergic to eggs", {
12+
x <- allergy(1)
13+
expect_true(allergic_to(x, "eggs"))
14+
})
15+
16+
test_that("allergic to eggs in addition to other stuff", {
17+
x <- allergy(5)
18+
expect_true(allergic_to(x, "eggs"))
19+
expect_true(allergic_to(x, "shellfish"))
20+
expect_false(allergic_to(x, "strawberries"))
21+
})
22+
23+
test_that("no allergies at all", {
24+
x <- allergy(0)
25+
expect_equal(list_allergies(x), character())
26+
})
27+
28+
test_that("allergic to just eggs", {
29+
x <- allergy(1)
30+
expect_equal(list_allergies(x), c("eggs"))
31+
})
32+
33+
test_that("allergic to just peanuts", {
34+
x <- allergy(2)
35+
expect_equal(list_allergies(x), c("peanuts"))
36+
})
37+
38+
test_that("allergic to just strawberries", {
39+
x <- allergy(8)
40+
expect_equal(list_allergies(x), c("strawberries"))
41+
})
42+
43+
test_that("allergic to eggs and peanuts", {
44+
x <- allergy(3)
45+
expect_true(setequal(
46+
list_allergies(x),
47+
c("eggs", "peanuts")
48+
))
49+
})
50+
51+
test_that("allergic to more than eggs but not peanuts", {
52+
x <- allergy(5)
53+
expect_true(setequal(
54+
list_allergies(x),
55+
c("eggs", "shellfish")
56+
))
57+
})
58+
59+
test_that("allergic to lots of stuff", {
60+
x <- allergy(248)
61+
expect_true(setequal(
62+
list_allergies(x),
63+
c("strawberries", "tomatoes", "chocolate", "pollen", "cats")
64+
))
65+
})
66+
67+
test_that("allergic to everything", {
68+
x <- allergy(255)
69+
expect_true(setequal(
70+
list_allergies(x),
71+
c(
72+
"eggs", "peanuts", "shellfish", "strawberries", "tomatoes",
73+
"chocolate", "pollen", "cats"
74+
)
75+
))
76+
})
77+
78+
test_that("ignore non allergen score parts", {
79+
x <- allergy(509)
80+
expect_true(setequal(
81+
list_allergies(x),
82+
c(
83+
"eggs", "shellfish", "strawberries", "tomatoes",
84+
"chocolate", "pollen", "cats"
85+
)
86+
))
87+
})
88+
89+
test_that("ignore non allergen score parts", {
90+
x <- allergy(257)
91+
expect_true(setequal(
92+
list_allergies(x),
93+
c("eggs")
94+
))
95+
})

0 commit comments

Comments
 (0)