add tests

This commit is contained in:
DozorovaA.A 2023-03-06 13:21:05 +04:00
parent f9143fb60b
commit d60fbbe32f
3 changed files with 86 additions and 15 deletions

View File

@ -8,10 +8,10 @@
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<link href="node_modules/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="node_modules/@fortawesome/fontawesome-free/css/all.min.css" rel="stylesheet"/>
<title>Volt's Site</title>
<title>Calculate Site</title>
</head>
<body>
<form id="frm-items" class="row g-3">
<form id="frm-items" class="row g-3" style="text-align: center">
<div class="d-flex flex-column w-25 my-5 mx-3">
Введите первое число
<input id="first"></input>

View File

@ -17,34 +17,70 @@ public class MethodService {
public String Sum(Object first, Object second, String type) {
final IMethod operation = (IMethod) applicationContext.getBean(type);
if (operation instanceof MethodString){
return String.format("%s", operation.Sum(first,second));
if(first instanceof String && second instanceof String)
{
return String.format("%s", operation.Sum(first,second));
}
return "Uncorrect type";
}else if (operation instanceof MethodBoolean){
return operation.Sum(Boolean.parseBoolean(first.toString()),Boolean.parseBoolean(second.toString())).toString();
if(first instanceof Boolean && second instanceof Boolean)
{
return operation.Sum(Boolean.parseBoolean(first.toString()),Boolean.parseBoolean(second.toString())).toString();
}
else return "Uncorrect type";
}
else{
return String.format("%s", operation.Sum(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
if(first instanceof Integer && second instanceof Integer)
{
return String.format("%s", operation.Sum(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
}
else return "Uncorrect type";
}
}
public String Ras(Object first, Object second, String type) {
final IMethod operation = (IMethod) applicationContext.getBean(type);
if (operation instanceof MethodString){
return String.format("%s", operation.Minus(first,Integer.parseInt(second.toString())));
if(first instanceof String && second instanceof Integer)
{
return String.format("%s", operation.Minus(first,Integer.parseInt(second.toString())));
}
return "Uncorrect type";
}else if (operation instanceof MethodBoolean){
return operation.Minus(Boolean.parseBoolean(first.toString()), Integer.parseInt(second.toString())).toString();
if(first instanceof Boolean && second instanceof Integer)
{
return operation.Minus(Boolean.parseBoolean(first.toString()), Integer.parseInt(second.toString())).toString();
}
else return "Uncorrect type";
}else{
return String.format("%s", operation.Minus(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
if(first instanceof Integer && second instanceof Integer)
{
return String.format("%s", operation.Minus(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
}
else return "Uncorrect type";
}
}
public String Pros(Object first, Object second, String type) {
final IMethod operation = (IMethod) applicationContext.getBean(type);
if (operation instanceof MethodString){
return String.format("%s", operation.Multiply(first,Integer.parseInt(second.toString())));
if(first instanceof String && second instanceof Integer)
{
return String.format("%s", operation.Multiply(first,Integer.parseInt(second.toString())));
}
return "Uncorrect type";
}else if (operation instanceof MethodBoolean){
return operation.Multiply(Boolean.parseBoolean(first.toString()), Integer.parseInt(second.toString())).toString();
if(first instanceof Boolean && second instanceof Integer)
{
return operation.Multiply(Boolean.parseBoolean(first.toString()), Integer.parseInt(second.toString())).toString();
}
else return "Uncorrect type";
}else{
return String.format("%s", operation.Multiply(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
if(first instanceof Integer && second instanceof Integer)
{
return String.format("%s", operation.Multiply(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
}
else return "Uncorrect type";
}
}
@ -53,6 +89,10 @@ public class MethodService {
if (operation instanceof MethodString){
return String.format("%s", operation.Div(first,second));
}else if (operation instanceof MethodBoolean){
if(first.toString()!="true" && second.toString() != "false")
{
throw new ClassCastException("Uncorrect type");
}
return String.format("%s", operation.Div(Boolean.parseBoolean(first.toString()), Boolean.parseBoolean(second.toString())).toString());
}else {
return String.format("%s", operation.Div(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));

View File

@ -11,6 +11,22 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
class DemoApplicationTests {
@Autowired
private MethodService methodService;
@Test
void testIntType() {
String res = methodService.Sum(2, "2", "int");
Assertions.assertEquals("Uncorrect type", res);
}
@Test
void testStringType() {
String res = methodService.Sum(2, 2, "string");
Assertions.assertEquals("Uncorrect type", res);
}
@Test
void testBooleanType() {
String res = methodService.Sum("3", "2", "boolean");
Assertions.assertEquals("Uncorrect type", res);
}
@Test
void testIntSum() {
Integer res = Integer.valueOf(methodService.Sum(2, 2, "int"));
@ -18,7 +34,7 @@ class DemoApplicationTests {
}
@Test
void testStringSum() {
String res = methodService.Sum("hello, ", 2, "string");
String res = methodService.Sum("hello, ", "2", "string");
Assertions.assertEquals("hello, 2", res);
}
@Test
@ -39,8 +55,8 @@ class DemoApplicationTests {
}
@Test
void testBooleanDiff() {
String res = methodService.Ras(1, 2, "boolean");
Assertions.assertEquals(true, res);
String res = methodService.Ras(true, 2, "boolean");
Assertions.assertEquals("false", res);
}
@Test
@ -66,7 +82,7 @@ class DemoApplicationTests {
}
@Test
void testStringDivide() {
String res = methodService.Del("test", 2, "string");
String res = methodService.Del("test", "2", "string");
Assertions.assertEquals("false", res);
}
@Test
@ -80,4 +96,19 @@ class DemoApplicationTests {
Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> methodService.Sum(1, 2, "date"));
}
@Test
void testErrorTypeString() {
Assertions.assertThrows(ClassCastException.class, () -> methodService.Del(1, 2, "string"));
}
@Test
void testErrorTypeBoolean() {
Assertions.assertThrows(ClassCastException.class, () -> methodService.Del("jjj15", 3, "boolean"));
}
@Test
void testErrorTypeInt() {
Assertions.assertThrows(NumberFormatException.class, () -> methodService.Del(true, 2, "int"));
}
}