forked from NModbus4/NModbus4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request NModbus4#66 from Maxwe11/improve-code-coverage
Improve code coverage
- Loading branch information
Showing
13 changed files
with
381 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
language: csharp | ||
mono: | ||
- alpha | ||
solution: NModbus4.sln | ||
script: | ||
- xbuild /p:Configuration=Debug NModbus4.sln |
31 changes: 31 additions & 0 deletions
31
NModbus4.UnitTests/Data/BoolModbusDataCollectionFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Collections.ObjectModel; | ||
using Modbus.Data; | ||
using Xunit; | ||
|
||
namespace Modbus.UnitTests.Data | ||
{ | ||
public class BoolModbusDataCollectionFixture : ModbusDataCollectionFixture<bool> | ||
{ | ||
protected override bool[] GetArray() | ||
{ | ||
return new[] { false, false, true, false, false }; | ||
} | ||
|
||
protected override bool GetNonExistentElement() | ||
{ | ||
return true; | ||
} | ||
|
||
[Fact] | ||
public void Remove_FromReadOnly() | ||
{ | ||
bool[] source = { false, false, false, true, false, false }; | ||
var col = new ModbusDataCollection<bool>(new ReadOnlyCollection<bool>(source)); | ||
int expectedCount = source.Length; | ||
|
||
Assert.True(col.Remove(source[3])); | ||
|
||
Assert.Equal(expectedCount, col.Count); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,119 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using Modbus.Data; | ||
using Xunit; | ||
|
||
namespace Modbus.UnitTests.Data | ||
{ | ||
public class ModbusDataCollectionFixture | ||
public abstract class ModbusDataCollectionFixture<TData> | ||
{ | ||
[Fact] | ||
public void FromReadOnlyList() | ||
protected abstract TData[] GetArray(); | ||
|
||
protected abstract TData GetNonExistentElement(); | ||
|
||
protected List<TData> GetList() | ||
{ | ||
ModbusDataCollection<bool> col = new ModbusDataCollection<bool>(new bool[] { true, false }); | ||
Assert.Equal(3, col.Count); | ||
return new List<TData>(GetArray()); | ||
} | ||
|
||
[Fact] | ||
public void ModbusDataCollection_FromParams() | ||
public void DefaultContstructor() | ||
{ | ||
ModbusDataCollection<bool> col = new ModbusDataCollection<bool>(true, false); | ||
Assert.Equal(3, col.Count); | ||
var col = new ModbusDataCollection<TData>(); | ||
Assert.NotEmpty(col); | ||
Assert.Equal(1, col.Count); | ||
|
||
col.Add(default(TData)); | ||
Assert.Equal(2, col.Count); | ||
} | ||
|
||
[Fact] | ||
public void Empty() | ||
public void ContstructorWithParams() | ||
{ | ||
ModbusDataCollection<bool> col = new ModbusDataCollection<bool>(); | ||
Assert.Equal(1, col.Count); | ||
TData[] source = GetArray(); | ||
var col = new ModbusDataCollection<TData>(source); | ||
Assert.Equal(source.Length + 1, col.Count); | ||
Assert.NotEmpty(col); | ||
|
||
col.Add(default(TData)); | ||
Assert.Equal(source.Length + 2, col.Count); | ||
} | ||
|
||
[Fact] | ||
public void AddDefaultBool() | ||
public void ContstructorWithIList() | ||
{ | ||
ModbusDataCollection<bool> col = new ModbusDataCollection<bool>(true, true); | ||
Assert.Equal(3, col.Count); | ||
Assert.Equal(new bool[] { false, true, true }, col.ToArray()); | ||
List<TData> source = GetList(); | ||
int expectedCount = source.Count; | ||
|
||
var col = new ModbusDataCollection<TData>(source); | ||
|
||
Assert.Equal(expectedCount + 1, source.Count); | ||
Assert.Equal(expectedCount + 1, col.Count); | ||
|
||
source.Insert(0, default(TData)); | ||
Assert.Equal(source, col); | ||
} | ||
|
||
[Fact] | ||
public void AddDefaultUshort() | ||
public void ContstructorWithIList_FromReadOnlyList() | ||
{ | ||
ModbusDataCollection<ushort> col = new ModbusDataCollection<ushort>(1, 1); | ||
Assert.Equal(3, col.Count); | ||
Assert.Equal(new ushort[] { 0, 1, 1 }, col.ToArray()); | ||
List<TData> source = GetList(); | ||
var readOnly = new ReadOnlyCollection<TData>(source); | ||
int expectedCount = source.Count; | ||
|
||
var col = new ModbusDataCollection<TData>(readOnly); | ||
|
||
Assert.Equal(expectedCount, source.Count); | ||
Assert.Equal(expectedCount + 1, col.Count); | ||
|
||
source.Insert(0, default(TData)); | ||
Assert.Equal(source, col); | ||
} | ||
|
||
[Fact] | ||
public void SetZeroElementUsingItem() | ||
{ | ||
ModbusDataCollection<bool> col = new ModbusDataCollection<bool>(true, false); | ||
Assert.Throws<ArgumentOutOfRangeException>(() => col[0] = true); | ||
var source = GetArray(); | ||
var col = new ModbusDataCollection<TData>(source); | ||
Assert.Throws<ArgumentOutOfRangeException>(() => col[0] = source[3]); | ||
} | ||
|
||
[Fact] | ||
public void InsertZeroElement() | ||
public void ZeroElementUsingItem_Negative() | ||
{ | ||
ModbusDataCollection<bool> col = new ModbusDataCollection<bool>(true, false); | ||
Assert.Throws<ArgumentOutOfRangeException>(() => col.Insert(0, true)); | ||
var source = GetArray(); | ||
var col = new ModbusDataCollection<TData>(source); | ||
|
||
Assert.Throws<ArgumentOutOfRangeException>(() => col[0] = source[3]); | ||
Assert.Throws<ArgumentOutOfRangeException>(() => col.Insert(0, source[3])); | ||
Assert.Throws<ArgumentOutOfRangeException>(() => col.RemoveAt(0)); | ||
|
||
// Remove forst zero/false | ||
Assert.Throws<ArgumentOutOfRangeException>(() => col.Remove(default(TData))); | ||
} | ||
|
||
[Fact] | ||
public void Clear() | ||
{ | ||
ModbusDataCollection<bool> col = new ModbusDataCollection<bool>(true, false); | ||
var col = new ModbusDataCollection<TData>(GetArray()); | ||
col.Clear(); | ||
|
||
Assert.Equal(1, col.Count); | ||
} | ||
|
||
[Fact] | ||
public void RemoveAtZeroElement() | ||
public void Remove() | ||
{ | ||
ModbusDataCollection<bool> col = new ModbusDataCollection<bool>(true, false); | ||
Assert.Throws<ArgumentOutOfRangeException>(() => col.RemoveAt(0)); | ||
} | ||
List<TData> source = GetList(); | ||
var col = new ModbusDataCollection<TData>(source); | ||
int expectedCount = source.Count - 1; | ||
|
||
[Fact] | ||
public void RemoveZeroElement() | ||
{ | ||
ModbusDataCollection<bool> col = new ModbusDataCollection<bool>(); | ||
Assert.Throws<ArgumentOutOfRangeException>(() => col.Remove(default(bool))); | ||
Assert.True(col.Remove(source[3])); | ||
|
||
Assert.Equal(expectedCount, col.Count); | ||
Assert.Equal(expectedCount, source.Count); | ||
Assert.Equal(source, col); | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
NModbus4.UnitTests/Data/UshortModbusDataCollectionFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Collections.ObjectModel; | ||
using Modbus.Data; | ||
using Xunit; | ||
|
||
namespace Modbus.UnitTests.Data | ||
{ | ||
public class UshortModbusDataCollectionFixture : ModbusDataCollectionFixture<ushort> | ||
{ | ||
protected override ushort[] GetArray() | ||
{ | ||
return new ushort[] { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; | ||
} | ||
|
||
protected override ushort GetNonExistentElement() | ||
{ | ||
return 42; | ||
} | ||
|
||
[Fact] | ||
public void Remove_FromReadOnly() | ||
{ | ||
ushort[] source = this.GetArray(); | ||
var col = new ModbusDataCollection<ushort>(new ReadOnlyCollection<ushort>(source)); | ||
int expectedCount = source.Length; | ||
|
||
Assert.False(col.Remove(this.GetNonExistentElement())); | ||
Assert.True(col.Remove(source[3])); | ||
|
||
Assert.Equal(expectedCount, col.Count); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using Modbus.IO; | ||
using Modbus.Message; | ||
using Xunit; | ||
|
||
namespace Modbus.UnitTests.IO | ||
{ | ||
public static class EmptyTransportFixture | ||
{ | ||
[Fact] | ||
public static void Negative() | ||
{ | ||
var transport = new EmptyTransport(); | ||
Assert.Throws<NotImplementedException>(() => transport.ReadRequest()); | ||
Assert.Throws<NotImplementedException>(() => transport.ReadResponse<ReadCoilsInputsResponse>()); | ||
Assert.Throws<NotImplementedException>(() => transport.BuildMessageFrame(null)); | ||
Assert.Throws<NotImplementedException>(() => transport.Write(null)); | ||
Assert.Throws<NotImplementedException>(() => transport.OnValidateResponse(null, null)); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.