Skip to content

Commit

Permalink
Merge pull request #5 from Michal123456747/dev
Browse files Browse the repository at this point in the history
Version 0.1.0-preview
  • Loading branch information
michalramus authored Feb 27, 2022
2 parents 3862838 + 0f14f14 commit b0d9582
Show file tree
Hide file tree
Showing 64 changed files with 3,813 additions and 36 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ $RECYCLE.BIN/
# Build folders
obj/
bin/

# keys
*.snk
252 changes: 252 additions & 0 deletions IntegrationTests/CarTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
using Moq;
using Xunit;
using ROELibrary;

namespace IntegrationTests.Robots
{
public class CarDevice
{
[Fact]
public void setupCarDevice()
{
Mock<ISerialPort> serialPortMock = new Mock<ISerialPort>();
serialPortMock.Setup(x => x.Write(It.IsAny<string>()));
serialPortMock.Setup(x => x.IsOpen()).Returns(true);
serialPortMock.Setup(x => x.BytesToRead()).Returns(2); //more than 1
serialPortMock.Setup(x => x.ReadLine()).Returns("~{\"type\":\"info\",\"data\":[[\"msgReceived\", \"true\"]]}~");


Configure configure = new Configure()
{
serialPortName = "COM3",
serialPort = serialPortMock.Object,
};


CarModel carModel = new CarModel()
{
id = 1,
pins = new uint[] { 1, 2, 3, 4, 5 },
impulsesPerRotation = 70,
circumference = 100,
};

Robot robot = new Robot(configure, new CarModel[] { carModel });

serialPortMock.Verify(x => x.Write("~{\"type\":\"conf\",\"dev\":[{\"devType\":\"car\",\"ID\":1,\"pins\":[1,2,3,4,5]}]}~"), Times.Once);
serialPortMock.Verify(x => x.Write("~{\"type\":\"task\",\"tasks\":[{\"devType\":\"car\",\"ID\":1,\"task\":\"nothing\",\"exValNum\":2,\"exVal\":[[\"vCarImpPerRot\",\"70\"],[\"vCarCircumf\",\"100\"]]}]}~"), Times.Once);
}

[Fact]
public void CarDeviceGoForward()
{
Mock<ISerialPort> serialPortMock = new Mock<ISerialPort>();
serialPortMock.Setup(x => x.Write(It.IsAny<string>()));
serialPortMock.Setup(x => x.IsOpen()).Returns(true);
serialPortMock.Setup(x => x.BytesToRead()).Returns(2); //more than 1
serialPortMock.Setup(x => x.ReadLine()).Returns("~{\"type\":\"info\",\"data\":[[\"msgReceived\", \"true\"]]}~");


Configure configure = new Configure()
{
serialPortName = "COM3",
serialPort = serialPortMock.Object,
};


CarModel carModel = new CarModel()
{
id = 1,
pins = new uint[] { 1, 2, 3, 4, 5 },
impulsesPerRotation = 70,
circumference = 100,
};

Robot robot = new Robot(configure, new CarModel[] { carModel });
robot.cars[1].goForward(250);

serialPortMock.Verify(x => x.Write("~{\"type\":\"task\",\"tasks\":[{\"devType\":\"car\",\"ID\":1,\"task\":\"tCarGo\",\"exValNum\":2,\"exVal\":[[\"vCarDist\",\"250\"],[\"vCarDirect\",\"forw\"]]}]}~"), Times.Once);
}


[Fact]
public void CarDeviceGoBackward()
{
Mock<ISerialPort> serialPortMock = new Mock<ISerialPort>();
serialPortMock.Setup(x => x.Write(It.IsAny<string>()));
serialPortMock.Setup(x => x.IsOpen()).Returns(true);
serialPortMock.Setup(x => x.BytesToRead()).Returns(2); //more than 1
serialPortMock.Setup(x => x.ReadLine()).Returns("~{\"type\":\"info\",\"data\":[[\"msgReceived\", \"true\"]]}~");


Configure configure = new Configure()
{
serialPortName = "COM3",
serialPort = serialPortMock.Object,
};


CarModel carModel = new CarModel()
{
id = 1,
pins = new uint[] { 1, 2, 3, 4, 5 },
impulsesPerRotation = 70,
circumference = 100,
};

Robot robot = new Robot(configure, new CarModel[] { carModel });
robot.cars[1].goBackward(143);

serialPortMock.Verify(x => x.Write("~{\"type\":\"task\",\"tasks\":[{\"devType\":\"car\",\"ID\":1,\"task\":\"tCarGo\",\"exValNum\":2,\"exVal\":[[\"vCarDist\",\"143\"],[\"vCarDirect\",\"back\"]]}]}~"), Times.Once);
}

[Fact]
public void CarDeviceRotate()
{
Mock<ISerialPort> serialPortMock = new Mock<ISerialPort>();
serialPortMock.Setup(x => x.Write(It.IsAny<string>()));
serialPortMock.Setup(x => x.IsOpen()).Returns(true);
serialPortMock.Setup(x => x.BytesToRead()).Returns(2); //more than 1
serialPortMock.Setup(x => x.ReadLine()).Returns("~{\"type\":\"info\",\"data\":[[\"msgReceived\", \"true\"]]}~");


Configure configure = new Configure()
{
serialPortName = "COM3",
serialPort = serialPortMock.Object,
};


CarModel carModel = new CarModel()
{
id = 1,
pins = new uint[] { 1, 2, 3, 4, 5 },
impulsesPerRotation = 70,
circumference = 100,
};

Robot robot = new Robot(configure, new CarModel[] { carModel });
robot.cars[1].rotate(1430, false);
robot.cars[1].rotate(324, true);

serialPortMock.Verify(x => x.Write("~{\"type\":\"task\",\"tasks\":[{\"devType\":\"car\",\"ID\":1,\"task\":\"tCarTurn\",\"exValNum\":2,\"exVal\":[[\"vCarAngle\",\"1430\"],[\"vCarDirect\",\"left\"]]}]}~"), Times.Once);
serialPortMock.Verify(x => x.Write("~{\"type\":\"task\",\"tasks\":[{\"devType\":\"car\",\"ID\":1,\"task\":\"tCarTurn\",\"exValNum\":2,\"exVal\":[[\"vCarAngle\",\"324\"],[\"vCarDirect\",\"right\"]]}]}~"), Times.Once);
}


[Fact]
public void CarDeviceSetSpeed()
{
Mock<ISerialPort> serialPortMock = new Mock<ISerialPort>();
serialPortMock.Setup(x => x.Write(It.IsAny<string>()));
serialPortMock.Setup(x => x.IsOpen()).Returns(true);
serialPortMock.Setup(x => x.BytesToRead()).Returns(2); //more than 1
serialPortMock.Setup(x => x.ReadLine()).Returns("~{\"type\":\"info\",\"data\":[[\"msgReceived\", \"true\"]]}~");


Configure configure = new Configure()
{
serialPortName = "COM3",
serialPort = serialPortMock.Object,
};


CarModel carModel = new CarModel()
{
id = 1,
pins = new uint[] { 1, 2, 3, 4, 5 },
impulsesPerRotation = 70,
circumference = 100,
};

Robot robot = new Robot(configure, new CarModel[] { carModel });
robot.cars[1].setSpeed(75);

serialPortMock.Verify(x => x.Write("~{\"type\":\"task\",\"tasks\":[{\"devType\":\"car\",\"ID\":1,\"task\":\"nothing\",\"exValNum\":1,\"exVal\":[[\"vCarSpeed\",\"75\"]]}]}~"), Times.Once);
}


[Fact]
public void CarDeviceSetRotationalSpeed()
{
Mock<ISerialPort> serialPortMock = new Mock<ISerialPort>();
serialPortMock.Setup(x => x.Write(It.IsAny<string>()));
serialPortMock.Setup(x => x.IsOpen()).Returns(true);
serialPortMock.Setup(x => x.BytesToRead()).Returns(2); //more than 1
serialPortMock.Setup(x => x.ReadLine()).Returns("~{\"type\":\"info\",\"data\":[[\"msgReceived\", \"true\"]]}~");


Configure configure = new Configure()
{
serialPortName = "COM3",
serialPort = serialPortMock.Object,
};


CarModel carModel = new CarModel()
{
id = 1,
pins = new uint[] { 1, 2, 3, 4, 5 },
impulsesPerRotation = 70,
circumference = 100,
};

Robot robot = new Robot(configure, new CarModel[] { carModel });
robot.cars[1].setRotationalSpeed(21);

serialPortMock.Verify(x => x.Write("~{\"type\":\"task\",\"tasks\":[{\"devType\":\"car\",\"ID\":1,\"task\":\"nothing\",\"exValNum\":1,\"exVal\":[[\"vCarRotSpeed\",\"21\"]]}]}~"), Times.Once);
}


[Fact]
public void CarDeviceCallMultiActions()
{
Mock<ISerialPort> serialPortMock = new Mock<ISerialPort>();
serialPortMock.Setup(x => x.Write(It.IsAny<string>()));
serialPortMock.Setup(x => x.IsOpen()).Returns(true);
serialPortMock.Setup(x => x.BytesToRead()).Returns(2); //more than 1
serialPortMock.Setup(x => x.ReadLine()).Returns("~{\"type\":\"info\",\"data\":[[\"msgReceived\", \"true\"]]}~");


Configure configure = new Configure()
{
serialPortName = "COM3",
serialPort = serialPortMock.Object,
};


CarModel carModel = new CarModel()
{
id = 3,
pins = new uint[] { 1, 25, 13, 47, 50 },
impulsesPerRotation = 349,
circumference = 68,
};

Robot robot = new Robot(configure, new CarModel[] { carModel });

serialPortMock.Verify(x => x.Write("~{\"type\":\"conf\",\"dev\":[{\"devType\":\"car\",\"ID\":3,\"pins\":[1,25,13,47,50]}]}~"), Times.Once);
serialPortMock.Verify(x => x.Write("~{\"type\":\"task\",\"tasks\":[{\"devType\":\"car\",\"ID\":3,\"task\":\"nothing\",\"exValNum\":2,\"exVal\":[[\"vCarImpPerRot\",\"349\"],[\"vCarCircumf\",\"68\"]]}]}~"), Times.Once);

robot.cars[3].setSpeed(44);
serialPortMock.Verify(x => x.Write("~{\"type\":\"task\",\"tasks\":[{\"devType\":\"car\",\"ID\":3,\"task\":\"nothing\",\"exValNum\":1,\"exVal\":[[\"vCarSpeed\",\"44\"]]}]}~"), Times.Once);

robot.cars[3].setRotationalSpeed(37);
serialPortMock.Verify(x => x.Write("~{\"type\":\"task\",\"tasks\":[{\"devType\":\"car\",\"ID\":3,\"task\":\"nothing\",\"exValNum\":1,\"exVal\":[[\"vCarRotSpeed\",\"37\"]]}]}~"), Times.Once);

robot.cars[3].goForward(463);
serialPortMock.Verify(x => x.Write("~{\"type\":\"task\",\"tasks\":[{\"devType\":\"car\",\"ID\":3,\"task\":\"tCarGo\",\"exValNum\":2,\"exVal\":[[\"vCarDist\",\"463\"],[\"vCarDirect\",\"forw\"]]}]}~"), Times.Once);

robot.cars[3].goBackward(113);
serialPortMock.Verify(x => x.Write("~{\"type\":\"task\",\"tasks\":[{\"devType\":\"car\",\"ID\":3,\"task\":\"tCarGo\",\"exValNum\":2,\"exVal\":[[\"vCarDist\",\"113\"],[\"vCarDirect\",\"back\"]]}]}~"), Times.Once);

robot.cars[3].rotate(40, true);
serialPortMock.Verify(x => x.Write("~{\"type\":\"task\",\"tasks\":[{\"devType\":\"car\",\"ID\":3,\"task\":\"tCarTurn\",\"exValNum\":2,\"exVal\":[[\"vCarAngle\",\"40\"],[\"vCarDirect\",\"right\"]]}]}~"), Times.Once);

robot.cars[3].rotate(60, false);
serialPortMock.Verify(x => x.Write("~{\"type\":\"task\",\"tasks\":[{\"devType\":\"car\",\"ID\":3,\"task\":\"tCarTurn\",\"exValNum\":2,\"exVal\":[[\"vCarAngle\",\"60\"],[\"vCarDirect\",\"left\"]]}]}~"), Times.Once);
}

//TODO: 2 the same id's; test rotate method; incorrect car model setup; check errorMsg
}
}
1 change: 1 addition & 0 deletions IntegrationTests/ROELibrary.IntegrationTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="moq" Version="4.16.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
14 changes: 0 additions & 14 deletions IntegrationTests/UnitTest1.cs

This file was deleted.

8 changes: 0 additions & 8 deletions Library/Class1.cs

This file was deleted.

54 changes: 54 additions & 0 deletions Library/Configure.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace ROELibrary
{
//model with properties to configure Robot class
public class Configure
{
//serialPort
public string serialPortName = null;
public uint? baudRate = 9600;
public uint? readBufferSize = 4096;
public uint? writeBufferSize = 4096;

/// <summary>
/// Use different serial port. null - use standard serial port
/// only for development
/// Note that correct configuration is checking first, so set every port properties
/// </summary>
internal ISerialPort serialPort = null;

/// <summary>
/// in milliseconds
/// </summary>
public uint? readTimeout = 2000;

//loging
public bool logToDatabase = true;


internal bool isModelSetup()
{
if (serialPortName == null)
{
return false;
}
else if (baudRate == null)
{
return false;
}
else if (readBufferSize == null)
{
return false;
}
else if (writeBufferSize == null)
{
return false;
}
else if (readTimeout == null)
{
return false;
}

return true;
}
}
}
23 changes: 23 additions & 0 deletions Library/Exceptions/DeviceModelIncorrectSetupException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

//error generated, when model of device in setup incorrectly

namespace ROELibrary
{
public class DeviceModelIncorrectSetupException : Exception
{
public DeviceModelIncorrectSetupException()
{
}

public DeviceModelIncorrectSetupException(string message)
: base(message)
{
}

public DeviceModelIncorrectSetupException(string message, Exception inner)
: base(message, inner)
{
}
}
}
23 changes: 23 additions & 0 deletions Library/Exceptions/IncorrectMessageException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

//error generated, when received message is in incorrect format

namespace ROELibrary
{
public class IncorrectMessageException : Exception
{
public IncorrectMessageException()
{
}

public IncorrectMessageException(string message)
: base(message)
{
}

public IncorrectMessageException(string message, Exception inner)
: base(message, inner)
{
}
}
}
Loading

0 comments on commit b0d9582

Please sign in to comment.