diff options
-rw-r--r-- | color.py | 1 | ||||
-rw-r--r-- | tests/fixtures/test.gitconfig | 8 | ||||
-rw-r--r-- | tests/test_color.py | 74 |
3 files changed, 83 insertions, 0 deletions
@@ -210,6 +210,7 @@ class Coloring: | |||
210 | if have_fg: | 210 | if have_fg: |
211 | bg = a | 211 | bg = a |
212 | else: | 212 | else: |
213 | have_fg = True | ||
213 | fg = a | 214 | fg = a |
214 | elif is_attr(a): | 215 | elif is_attr(a): |
215 | attr = a | 216 | attr = a |
diff --git a/tests/fixtures/test.gitconfig b/tests/fixtures/test.gitconfig index 9b3f2574..4c7111d4 100644 --- a/tests/fixtures/test.gitconfig +++ b/tests/fixtures/test.gitconfig | |||
@@ -11,3 +11,11 @@ | |||
11 | intk = 10k | 11 | intk = 10k |
12 | intm = 10m | 12 | intm = 10m |
13 | intg = 10g | 13 | intg = 10g |
14 | |||
15 | [color "status"] | ||
16 | one = yellow | ||
17 | two = magenta cyan | ||
18 | three = black red ul | ||
19 | reset = reset | ||
20 | none | ||
21 | empty = | ||
diff --git a/tests/test_color.py b/tests/test_color.py new file mode 100644 index 00000000..91a1bf25 --- /dev/null +++ b/tests/test_color.py | |||
@@ -0,0 +1,74 @@ | |||
1 | # Copyright (C) 2024 The Android Open Source Project | ||
2 | # | ||
3 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
4 | # you may not use this file except in compliance with the License. | ||
5 | # You may obtain a copy of the License at | ||
6 | # | ||
7 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
8 | # | ||
9 | # Unless required by applicable law or agreed to in writing, software | ||
10 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
12 | # See the License for the specific language governing permissions and | ||
13 | # limitations under the License. | ||
14 | |||
15 | """Unittests for the color.py module.""" | ||
16 | |||
17 | import os | ||
18 | import unittest | ||
19 | |||
20 | import color | ||
21 | import git_config | ||
22 | |||
23 | |||
24 | def fixture(*paths): | ||
25 | """Return a path relative to test/fixtures.""" | ||
26 | return os.path.join(os.path.dirname(__file__), "fixtures", *paths) | ||
27 | |||
28 | |||
29 | class ColoringTests(unittest.TestCase): | ||
30 | """tests of the Coloring class.""" | ||
31 | |||
32 | def setUp(self): | ||
33 | """Create a GitConfig object using the test.gitconfig fixture.""" | ||
34 | config_fixture = fixture("test.gitconfig") | ||
35 | self.config = git_config.GitConfig(config_fixture) | ||
36 | color.SetDefaultColoring("true") | ||
37 | self.color = color.Coloring(self.config, "status") | ||
38 | |||
39 | def test_Color_Parse_all_params_none(self): | ||
40 | """all params are None""" | ||
41 | val = self.color._parse(None, None, None, None) | ||
42 | self.assertEqual("", val) | ||
43 | |||
44 | def test_Color_Parse_first_parameter_none(self): | ||
45 | """check fg & bg & attr""" | ||
46 | val = self.color._parse(None, "black", "red", "ul") | ||
47 | self.assertEqual("\x1b[4;30;41m", val) | ||
48 | |||
49 | def test_Color_Parse_one_entry(self): | ||
50 | """check fg""" | ||
51 | val = self.color._parse("one", None, None, None) | ||
52 | self.assertEqual("\033[33m", val) | ||
53 | |||
54 | def test_Color_Parse_two_entry(self): | ||
55 | """check fg & bg""" | ||
56 | val = self.color._parse("two", None, None, None) | ||
57 | self.assertEqual("\033[35;46m", val) | ||
58 | |||
59 | def test_Color_Parse_three_entry(self): | ||
60 | """check fg & bg & attr""" | ||
61 | val = self.color._parse("three", None, None, None) | ||
62 | self.assertEqual("\033[4;30;41m", val) | ||
63 | |||
64 | def test_Color_Parse_reset_entry(self): | ||
65 | """check reset entry""" | ||
66 | val = self.color._parse("reset", None, None, None) | ||
67 | self.assertEqual("\033[m", val) | ||
68 | |||
69 | def test_Color_Parse_empty_entry(self): | ||
70 | """check empty entry""" | ||
71 | val = self.color._parse("none", "blue", "white", "dim") | ||
72 | self.assertEqual("\033[2;34;47m", val) | ||
73 | val = self.color._parse("empty", "green", "white", "bold") | ||
74 | self.assertEqual("\033[1;32;47m", val) | ||