File tree Expand file tree Collapse file tree 2 files changed +22
-6
lines changed Expand file tree Collapse file tree 2 files changed +22
-6
lines changed Original file line number Diff line number Diff line change 4
4
INDENT_TYPE_TAB = 'tab'
5
5
6
6
7
- def _verify_indent_type (indent_type : str ):
8
- # indent_type must be 'space' or 'tab'
9
- assert indent_type in [INDENT_TYPE_SPACE , INDENT_TYPE_TAB ]
10
- return indent_type
7
+ class ConfigInitError (Exception ):
8
+ pass
11
9
12
10
13
11
class CodeGenConfig :
14
12
def __init__ (self ,
15
13
indent_type : str = INDENT_TYPE_SPACE ,
16
14
indent_width : int = 4 ,
17
15
):
18
- self .indent_type = _verify_indent_type (indent_type )
16
+
17
+ if indent_type not in [INDENT_TYPE_SPACE , INDENT_TYPE_TAB ]:
18
+ raise ConfigInitError ("indent_type must be 'space' or 'tab'" )
19
+
20
+ if indent_width < 0 :
21
+ raise ConfigInitError ("indent_width must be a positive integer" )
22
+
23
+ self .indent_type = indent_type
19
24
self .indent_width = indent_width
20
25
21
26
def indent (self , depth ):
Original file line number Diff line number Diff line change 3
3
import unittest
4
4
import os
5
5
6
- from atcodertools .codegen .code_gen_config import CodeGenConfig , INDENT_TYPE_SPACE
6
+ from atcodertools .codegen .code_gen_config import CodeGenConfig , INDENT_TYPE_SPACE , ConfigInitError
7
7
from atcodertools .codegen .code_generator import CodeGenerator
8
8
from atcodertools .codegen .java_code_generator import JavaCodeGenerator
9
9
from atcodertools .codegen .cpp_code_generator import CppCodeGenerator
@@ -88,6 +88,17 @@ def test_load_code_gen_config(self):
88
88
self .assertEqual (8 , config .indent_width )
89
89
self .assertEqual (INDENT_TYPE_SPACE , config .indent_type )
90
90
91
+ def test_init_code_gen_config_with_invalid_parameters (self ):
92
+ self ._expect_error_when_init (indent_type = 'SPACE' , indent_width = 4 )
93
+ self ._expect_error_when_init (indent_type = 'space' , indent_width = - 1 )
94
+
95
+ def _expect_error_when_init (self , ** kwargs ):
96
+ try :
97
+ CodeGenConfig (** kwargs )
98
+ self .fail ("Must not reach here" )
99
+ except ConfigInitError :
100
+ pass
101
+
91
102
92
103
if __name__ == "__main__" :
93
104
unittest .main ()
You can’t perform that action at this time.
0 commit comments