Coverage for tests\test_shortrich.py: 100%

61 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2024-02-13 11:18 -0600

1import random 

2 

3import pytest 

4 

5import shortrich 

6 

7 

8def test__Tag(): 

9 toggle = shortrich.Tag("pink1") 

10 assert f"{toggle}" == "[pink1]" 

11 assert toggle.o == "[/pink1]" 

12 

13 

14def test__ColorMap(): 

15 c = shortrich.ColorMap() 

16 assert f"{c.p1}" == "[pink1]" 

17 assert c.p1.o == "[/pink1]" 

18 assert len(c) 

19 assert list(c) 

20 assert len(c) == len(list(c)) 

21 assert isinstance(c[0], shortrich.Tag) 

22 assert isinstance(random.choice(c), shortrich.Tag) 

23 

24 

25def test__RGB_str(): 

26 color = shortrich.RGB(100.8, 100.3, 100.1) 

27 assert str(color) == "[rgb(100,100,100)]" 

28 

29 

30def test__RGB_arithmetic(): 

31 color1 = shortrich.RGB(100, 100, 100) 

32 color2 = shortrich.RGB(25, 50, 75) 

33 sub = color1 - color2 

34 assert sub.r == 75 

35 assert sub.g == 50 

36 assert sub.b == 25 

37 add = color1 + color2 

38 assert add.r == 125 

39 assert add.g == 150 

40 assert add.b == 175 

41 div = color2 / 10 

42 assert div.r == 2.5 

43 assert div.g == 5 

44 assert div.b == 7.5 

45 mul = color2 * 0.1 

46 assert mul.r == 2.5 

47 assert mul.g == 5 

48 assert mul.b == 7.5 

49 

50 

51def test__RGB_from_name(): 

52 color = shortrich.RGB(name="pink1") 

53 assert color.r 

54 assert color.g 

55 assert color.b 

56 

57 

58def test__Gradient_blend(): 

59 gradient = shortrich.Gradient((100, 100, 100), (200, 200, 200)) 

60 text = "01234567890" 

61 steps = gradient._get_num_steps(text) 

62 step_sizes = gradient._get_step_sizes(steps) 

63 assert step_sizes == shortrich.RGB(10, 10, 10) 

64 midcolor = gradient._get_gradient_color(5, step_sizes) 

65 assert midcolor == shortrich.RGB(150, 150, 150) 

66 richtext = gradient.apply(text) 

67 assert richtext.startswith(str(gradient.start)) 

68 assert richtext.endswith(f"{gradient.stop}{text[-1]}[/]") 

69 

70 

71@pytest.mark.parametrize( 

72 "start, stop", 

73 [(shortrich.ColorMap().grey0, shortrich.ColorMap().grey100), ("grey0", "grey100")], 

74) 

75def test__Gradient__parse(start, stop): 

76 gradient = shortrich.Gradient(start, stop) 

77 text = "01234567890" 

78 richtext = gradient.apply(text) 

79 assert richtext.startswith(str(gradient.start)) 

80 assert richtext.endswith(f"{gradient.stop}{text[-1]}[/]")