Skip to content

Commit 90efc4c

Browse files
committed
Modified error messages for hello world to be more direct and clearer.
1 parent 0191253 commit 90efc4c

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

exercises/practice/hello-world/.meta/template.j2

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
{%- import "generator_macros.j2" as macros with context -%}
2-
{{ macros.header() }}
2+
3+
import unittest
4+
5+
try:
6+
from hello_world import (
7+
hello,
8+
)
9+
10+
except ImportError as import_fail:
11+
message = import_fail.args[0].split('(', maxsplit=1)
12+
item_name = import_fail.args[0].split()[3]
13+
14+
item_name = item_name[:-1] + "()'"
15+
16+
# pylint: disable=raise-missing-from
17+
raise ImportError("\n\nMISSING FUNCTION --> In your 'hello_world.py' file, we can not find or import the"
18+
f' function named {item_name}. \nThe tests for this first exercise expect a function that'
19+
f' returns the string "Hello, World!"'
20+
f'\n\nDid you use print("Hello, World!") instead?') from None
21+
322

423
class {{ exercise | camel_case }}Test(unittest.TestCase):
524
{% for case in cases -%}
625
def test_{{ case["description"] | to_snake }}(self):
7-
self.assertEqual({{ case["property"] }}(), "{{ case["expected"] }}")
26+
msg = "\n\nThis test expects a return of the string 'Hello, World!' \nDid you use print('Hello, World!') by mistake?"
27+
self.assertEqual({{ case["property"] }}(), "{{ case["expected"] }}", msg=msg)
828
{% endfor %}
9-
10-
{{ macros.footer() }}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
def hello():
2-
return 'Goodbye, Mars!'
1+
print('Hello, World!')
2+
#'Goodbye, Mars!'
Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
import unittest
22

3-
from hello_world import (
4-
hello,
5-
)
3+
try:
4+
from hello_world import (
5+
hello,
6+
)
67

7-
# Tests adapted from `problem-specifications//canonical-data.json`
8+
except ImportError as import_fail:
9+
message = import_fail.args[0].split("(", maxsplit=1)
10+
item_name = import_fail.args[0].split()[3]
811

12+
item_name = item_name[:-1] + "()'"
913

10-
class HelloWorldTest(unittest.TestCase):
11-
def test_say_hi(self):
12-
self.assertEqual(hello(), "Hello, World!")
14+
# pylint: disable=raise-missing-from
15+
raise ImportError(
16+
"\n\nMISSING FUNCTION --> In your 'hello_world.py' file, we can not find or import the"
17+
f" function named {item_name}. \nThe tests for this first exercise expect a function that"
18+
f' returns the string "Hello, World!"'
19+
f'\n\nDid you use print("Hello, World!") instead?'
20+
) from None
1321

1422

15-
if __name__ == "__main__":
16-
unittest.main()
23+
class HelloWorldTest(unittest.TestCase):
24+
def test_say_hi(self):
25+
msg = "\n\nThis test expects a return of the string 'Hello, World!' \nDid you use print('Hello, World!') by mistake?"
26+
self.assertEqual(hello(), "Hello, World!", msg=msg)

0 commit comments

Comments
 (0)