|
10 | 10 | "import pytest\n", |
11 | 11 | "from unittest.mock import patch\n", |
12 | 12 | "\n", |
13 | | - "def test_number_literals():\n", |
14 | | - " \"\"\"Tests various number literals.\"\"\"\n", |
15 | | - " a = 0b1010\n", |
16 | | - " b = 100\n", |
17 | | - " c = 0o310\n", |
18 | | - " d = 0x12c\n", |
19 | | - " float_1 = 10.5\n", |
20 | | - " float_2 = 1.5e2\n", |
21 | | - " float_3 = 1.5e-3\n", |
22 | | - " x = 3.14j\n", |
23 | | - "\n", |
24 | | - " assert a == 10\n", |
25 | | - " assert b == 100\n", |
26 | | - " assert c == 200\n", |
27 | | - " assert d == 300\n", |
28 | | - " assert float_1 == 10.5\n", |
29 | | - " assert float_2 == pytest.approx(150.0, rel=1e-9)\n", |
30 | | - " assert float_3 == pytest.approx(0.0015, rel=1e-9)\n", |
31 | | - " assert x == 3.14j\n", |
32 | | - " assert x.imag == 3.14\n", |
33 | | - " assert x.real == 0.0\n", |
34 | | - "\n", |
35 | | - "def test_string_literals():\n", |
36 | | - " \"\"\"Tests various string literals.\"\"\"\n", |
37 | | - " string_1 = 'Hello, world!'\n", |
38 | | - " string_2 = \"Python programming\"\n", |
39 | | - " string_3 = '''This is a\n", |
40 | | - "multi-line string.'''\n", |
41 | | - " string_4 = \"\"\"This is another\n", |
42 | | - "multi-line string.\"\"\"\n", |
43 | | - " string_5 = \"This is a newline:\\nAnd this is a tab:\\tHello\"\n", |
44 | | - " string_6 = r\"C:\\Users\\Name\\Folder\"\n", |
45 | | - " name = \"Alice\"\n", |
46 | | - " string_7 = f\"Hello, {name}!\"\n", |
47 | | - " string_8 = u\"\\U0001F604\"\n", |
48 | | - " string_9 = b\"Hello, bytes!\"\n", |
49 | | - "\n", |
50 | | - " assert string_1 == 'Hello, world!'\n", |
51 | | - " assert string_2 == \"Python programming\"\n", |
52 | | - " assert string_3.strip() == '''This is a\n", |
53 | | - "multi-line string.'''\n", |
54 | | - " assert string_4.strip() == \"\"\"This is another\n", |
55 | | - "multi-line string.\"\"\"\n", |
56 | | - " assert string_5 == \"This is a newline:\\nAnd this is a tab:\\tHello\"\n", |
57 | | - " assert string_6 == r\"C:\\Users\\Name\\Folder\"\n", |
58 | | - " assert string_7 == \"Hello, Alice!\"\n", |
59 | | - " assert string_8 == '😄'\n", |
60 | | - " assert string_9 == b\"Hello, bytes!\"\n", |
61 | | - "\n", |
62 | | - " expected_bytes = [72, 101, 108, 108, 111, 44, 32, 98, 121, 116, 101, 115, 33]\n", |
63 | | - " byte_values = [byte for byte in string_9]\n", |
64 | | - " assert byte_values == expected_bytes\n", |
65 | | - "\n", |
66 | | - "def test_boolean_literals():\n", |
67 | | - " \"\"\"Tests boolean literals.\"\"\"\n", |
68 | | - " a = True + 4\n", |
69 | | - " b = False + 10\n", |
70 | | - "\n", |
71 | | - " assert a == 5\n", |
72 | | - " assert b == 10\n", |
73 | | - "\n", |
74 | | - "def test_special_literal():\n", |
75 | | - " \"\"\"Tests the None literal.\"\"\"\n", |
76 | | - " a = None\n", |
77 | | - " assert a is None\n", |
78 | | - "\n", |
79 | | - "@patch('builtins.input', return_value='5')\n", |
80 | | - "def test_input_function(mock_input):\n", |
81 | | - " \"\"\"Tests the input function with a mocked input.\"\"\"\n", |
82 | | - " # Use the mock_input directly within the test\n", |
83 | | - " user_input = mock_input() \n", |
84 | | - " assert user_input == '5'\n", |
85 | | - " assert mock_input.call_count == 1" |
| 13 | + "def test_number_input():\n", |
| 14 | + " \"\"\"Tests number input with mocked input.\"\"\"\n", |
| 15 | + " with patch('builtins.input', side_effect=['10', '5']):\n", |
| 16 | + " first_number = input('Enter your first number :')\n", |
| 17 | + " second_number = input('Enter your second number :')\n", |
| 18 | + " sum_of_num = int(first_number) + int(second_number)\n", |
| 19 | + " assert sum_of_num == 15\n", |
| 20 | + "\n", |
| 21 | + "def test_number_input_second_way():\n", |
| 22 | + " \"\"\"Tests number input with mocked input (second way).\"\"\"\n", |
| 23 | + " with patch('builtins.input', side_effect=['10', '5']):\n", |
| 24 | + " first_number = int(input('Enter your first number :'))\n", |
| 25 | + " second_number = int(input('Enter your second number :'))\n", |
| 26 | + " sum_of_num = first_number + second_number\n", |
| 27 | + " assert sum_of_num == 15" |
86 | 28 | ] |
87 | 29 | }, |
88 | 30 | { |
|
0 commit comments