Skip to content

Commit

Permalink
solved bug test_pine_tree.py
Browse files Browse the repository at this point in the history
  • Loading branch information
fevziismailsahin committed Jan 12, 2025
1 parent 7dd77f6 commit a990dca
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 89 deletions.
28 changes: 14 additions & 14 deletions solutions/pine_tree_art.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""


def draw_tree(height: int = 10, trunk_width: int = 3, trunk_height: int = 3) -> str:
def pine_tree_art(height: int = 10, trunk_width: int = 3, trunk_height: int = 3) -> str:
"""
Generate an ASCII art representation of a pine tree.
Expand All @@ -20,17 +20,16 @@ def draw_tree(height: int = 10, trunk_width: int = 3, trunk_height: int = 3) ->
Returns:
str: The generated ASCII art as a string.
Example:
>>> print(draw_tree(height=5, trunk_width=3, trunk_height=2))
*
***
*****
*******
*********
|||
|||
"""

# Type checks
if (
not isinstance(height, int)
or not isinstance(trunk_width, int)
or not isinstance(trunk_height, int)
):
raise TypeError("Height, trunk_width, and trunk_height must be integers")

# Defensive assertions
if height <= 0:
raise ValueError("Height must be a positive integer")
Expand All @@ -51,9 +50,10 @@ def draw_tree(height: int = 10, trunk_width: int = 3, trunk_height: int = 3) ->
for _ in range(trunk_height):
tree.append(trunk)

# Return the tree as a single string
return "\n".join(tree)
# Return the tree as a single string with an extra newline at the end
return "\n".join(tree) + "\n"


# Example usage
if __name__ == "__main__":
print(draw_tree())
print(pine_tree_art())
150 changes: 75 additions & 75 deletions solutions/tests/test_pine_tree_art.py
Original file line number Diff line number Diff line change
@@ -1,83 +1,83 @@
"""Test module for pine_tree_art.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Unit test module for generating ASCII art of a pine tree.
Test categories:
- Standard cases: correct inputs for height, trunk_width, trunk_height;
- Edge cases;
- Defensive tests: wrong input types, assertions;
@author: fevziismailsahin
@created: 01/10/2025
Created on 2025-01-11
Author: fevziismailsahin
"""

import unittest

from ..pine_tree_art import draw_tree


class TestDrawTree(unittest.TestCase):
"""
Test class to verify the functionality of the draw_tree function.
"""

maxDiff = None # To see the full difference in case of failure

def test_default_tree(self):
"""
Test case for the default tree with default parameters.
"""
expected_result = """ *
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
|||
|||"""

result = draw_tree()
self.assertEqual(result, expected_result)

def test_tree_with_large_height(self):
"""
Test case for a tree with a large height of 15.
"""
expected_result = """ *
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
*************************
***************************
*****************************
*******************************
*********************************
|||
|||"""

result = draw_tree(15)
self.assertEqual(result, expected_result)

def test_tree_with_narrow_trunk(self):
"""
Test case for a tree with a narrow trunk width of 1.
"""
expected_result = """ *
***
*****
*******
*********
|
|"""

result = draw_tree(5, 1, 2)
self.assertEqual(result, expected_result)
from ..pine_tree_art import pine_tree_art


class TestPineTreeArt(unittest.TestCase):
"""Test suite for generating ASCII art of a pine tree."""

# Standard cases: correct inputs
def test_standard_case_1(self):
"""Test with typical valid inputs for height, trunk_width, trunk_height."""
expected_result = (
" *\n"
" ***\n"
" *****\n"
" *******\n"
" *********\n"
" ***********\n"
" *************\n"
" ***************\n"
" *****************\n"
"*******************\n"
" |||\n"
" |||\n"
" |||\n"
)
self.assertEqual(pine_tree_art(10, 3, 3), expected_result)

def test_standard_case_2(self):
"""Test with smaller tree height and trunk dimensions."""
expected_result = (
" *\n ***\n *****\n *******\n*********\n |||\n |||\n |||\n"
)
self.assertEqual(pine_tree_art(5, 3, 3), expected_result)

# Edge cases: testing zero or near-zero values
def test_zero_height(self):
"""Test when height is zero, which should raise a ValueError."""
with self.assertRaises(ValueError):
pine_tree_art(0, 3, 3)

def test_zero_trunk_width(self):
"""Test when trunk width is zero, which should raise a ValueError."""
with self.assertRaises(ValueError):
pine_tree_art(10, 0, 3)

def test_zero_trunk_height(self):
"""Test when trunk height is zero, which should raise a ValueError."""
with self.assertRaises(ValueError):
pine_tree_art(10, 3, 0)

# Defensive tests: wrong input types, assertions
def test_invalid_height_type(self):
"""Test when 'height' is not an integer, which should raise a TypeError."""
with self.assertRaises(TypeError):
pine_tree_art("ten", 3, 3)

def test_invalid_trunk_width_type(self):
"""Test when 'trunk_width' is not an integer, which should raise a TypeError."""
with self.assertRaises(TypeError):
pine_tree_art(10, "three", 3)

def test_invalid_trunk_height_type(self):
"""Test when 'trunk_height' is not an integer, which should raise a TypeError."""
with self.assertRaises(TypeError):
pine_tree_art(10, 3, "three")


if __name__ == "__main__":
Expand Down

0 comments on commit a990dca

Please sign in to comment.