parent
							
								
									5159beee68
								
							
						
					
					
						commit
						fb9ee07cbe
					
				| @ -0,0 +1,21 @@ | |||||||
|  | package space.kklochko.jdbc_hospital_example.cli.validators; | ||||||
|  | 
 | ||||||
|  | import java.util.regex.Pattern; | ||||||
|  | 
 | ||||||
|  | public class InputStringFormatValidator extends Validator { | ||||||
|  |     String command; | ||||||
|  |     String formatRegEx = "\\w+\\s\\w+\\((|[^)]+)\\)"; | ||||||
|  | 
 | ||||||
|  |     public InputStringFormatValidator(String command) { | ||||||
|  |         this.command = command; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public boolean isValid() { | ||||||
|  |         Pattern pattern = Pattern.compile(formatRegEx); | ||||||
|  |         return pattern.matcher(command).matches(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public String getMessage() { | ||||||
|  |         return "Please, check the format: `command item(arg1=value1, ...)`. Or check the `help`"; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,26 @@ | |||||||
|  | package space.kklochko.jdbc_hospital_example.cli.validators | ||||||
|  | 
 | ||||||
|  | import space.kklochko.jdbc_hospital_example.cli.commands.CommandData | ||||||
|  | import space.kklochko.jdbc_hospital_example.cli.parsers.CommandParser | ||||||
|  | import spock.lang.Specification | ||||||
|  | import spock.lang.Subject | ||||||
|  | 
 | ||||||
|  | class InputStringFormatValidatorSpec extends Specification { | ||||||
|  |     def "Parser parse the commands"() { | ||||||
|  |         given: "I have the validator" | ||||||
|  |         @Subject | ||||||
|  |         def validator = new InputStringFormatValidator(command) | ||||||
|  | 
 | ||||||
|  |         when: "check if valid" | ||||||
|  |         boolean status = validator.isValid() | ||||||
|  | 
 | ||||||
|  |         then: "the result must be as expected" | ||||||
|  |         status == expectedStatus | ||||||
|  | 
 | ||||||
|  |         where: | ||||||
|  |         command                                                                                                  || expectedStatus | ||||||
|  |         "insert indicator(id='1', type='blood', values='Good samples.', timestamp='2023-10-17 14:30:00')"        || true | ||||||
|  |         "insert indicator(id='1', type='blood, blood', values='Good samples.', timestamp='2023-10-17 14:30:00')" || true | ||||||
|  |         "readAll indicator()"                                                                                    || true | ||||||
|  |     } | ||||||
|  | } | ||||||
					Loading…
					
					
				
		Reference in new issue