Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
timyhac committed Jul 19, 2024
1 parent fb6fc49 commit 658baa4
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/libplctag.Tests/AsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class AsyncTests
public async Task Cancelled_cancellation_token_throws_a_TaskCanceledException()
{
// Arrange
var nativeTag = new Mock<INativeTag>();
var nativeTag = new Mock<INative>();

nativeTag // The initial creation of the tag object returns a status, so we return pending
.Setup(m => m.plc_tag_create_ex(It.IsAny<string>(), It.IsAny<NativeImport.plctag.callback_func_ex>(), It.IsAny<IntPtr>(), 0))
Expand All @@ -49,7 +49,7 @@ await Assert.ThrowsAsync<TaskCanceledException>(async () => {
public async Task Timeout_throws_a_LibPlcTagException()
{
// Arrange
var nativeTag = new Mock<INativeTag>();
var nativeTag = new Mock<INative>();

nativeTag // The initial creation of the tag object returns a status, so we return pending
.Setup(m => m.plc_tag_create_ex(It.IsAny<string>(), It.IsAny<NativeImport.plctag.callback_func_ex>(), It.IsAny<IntPtr>(), 0))
Expand Down Expand Up @@ -114,14 +114,14 @@ public async Task AsyncRead_completes_within_timeout_period()
}


Mock<INativeTag> GetMock()
Mock<INative> GetMock()
{
const int tagId = 11;

NativeImport.plctag.callback_func_ex callback = null;
Status? status = null;

var nativeTag = new Mock<INativeTag>();
var nativeTag = new Mock<INative>();

// The NativeTagWrapper should provide the native tag with a callback.
// We will store this locally when a create call occurs, and fire it shortly after ...
Expand Down
6 changes: 3 additions & 3 deletions src/libplctag.Tests/DisposeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DisposeTests
public void Destroy_is_called_if_initialized_and_disposed()
{
// Arrange
var nativeTag = new Mock<INativeTag>();
var nativeTag = new Mock<INative>();
var tag = new Tag(nativeTag.Object);

// Act
Expand All @@ -33,7 +33,7 @@ public void Destroy_is_called_if_initialized_and_disposed()
public void Can_not_use_if_already_disposed()
{
// Arrange
var nativeTag = new Mock<INativeTag>();
var nativeTag = new Mock<INative>();
var tag = new Tag(nativeTag.Object);

// Act
Expand All @@ -51,7 +51,7 @@ public void Finalizer_calls_destroy()


// Arrange
var nativeTag = new Mock<INativeTag>();
var nativeTag = new Mock<INative>();
Action dispose = () =>
{
// This will go out of scope after dispose() is executed, so the garbage collector will be able to call the finalizer
Expand Down
6 changes: 3 additions & 3 deletions src/libplctag.Tests/OtherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class OtherTests
public void Status_ok_when_first_created()
{
// Arrange
var nativeTag = new Mock<INativeTag>();
var nativeTag = new Mock<INative>();
var tag = new Tag(nativeTag.Object);

// Act
Expand All @@ -32,7 +32,7 @@ public void Status_ok_when_first_created()
public void Attribute_string_formatted_correctly()
{
// Arrange
var nativeTag = new Mock<INativeTag>();
var nativeTag = new Mock<INative>();
var tag = new Tag(nativeTag.Object)
{
ElementSize = 4,
Expand All @@ -56,7 +56,7 @@ public void Attribute_string_formatted_correctly()
public void Attribute_string_does_not_contain_unset_properties()
{
// Arrange
var nativeTag = new Mock<INativeTag>();
var nativeTag = new Mock<INative>();
var tag = new Tag(nativeTag.Object);

// Act
Expand Down
2 changes: 1 addition & 1 deletion src/libplctag/INativeTag.cs → src/libplctag/INative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace libplctag
{
interface INativeTag
interface INative
{
int plc_tag_abort(int tag);
int plc_tag_check_lib_version(int req_major, int req_minor, int req_patch);
Expand Down
2 changes: 1 addition & 1 deletion src/libplctag/LibPlcTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace libplctag
public static class LibPlcTag
{

static INativeTag _native = new NativeTag();
static INative _native = new Native();

private const int LIB_ATTRIBUTE_POINTER = 0;

Expand Down
5 changes: 2 additions & 3 deletions src/libplctag/NativeTag.cs → src/libplctag/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

using libplctag.NativeImport;
using System;
using System.Text;
using libplctag.NativeImport;
using static libplctag.NativeImport.plctag;

namespace libplctag
{
class NativeTag : INativeTag
class Native : INative
{

public int plc_tag_check_lib_version(int req_major, int req_minor, int req_patch) => plctag.plc_tag_check_lib_version(req_major, req_minor, req_patch);
public Int32 plc_tag_create(string lpString, int timeout) => plctag.plc_tag_create(lpString, timeout);
public Int32 plc_tag_create_ex(string lpString, callback_func_ex func, IntPtr userdata, int timeout) => plctag.plc_tag_create_ex(lpString, func, userdata, timeout);
Expand Down
11 changes: 6 additions & 5 deletions src/libplctag/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static libplctag.NativeImport.plctag;

[assembly: InternalsVisibleTo("libplctag.Tests")]

Expand All @@ -25,10 +26,10 @@ public sealed class Tag : IDisposable
private static readonly TimeSpan defaultTimeout = TimeSpan.FromSeconds(10);
private static readonly TimeSpan maxTimeout = TimeSpan.FromMilliseconds(int.MaxValue);

private readonly INativeTag _native;
private readonly INative _native;

private int nativeTagHandle;
private libplctag.NativeImport.plctag.callback_func_ex coreLibCallbackFuncExDelegate;
private callback_func_ex coreLibCallbackFuncExDelegate;

private bool _isDisposed = false;
private bool _isInitialized = false;
Expand Down Expand Up @@ -63,10 +64,10 @@ public sealed class Tag : IDisposable

public Tag()
{
_native = new NativeTag();
_native = new Native();
}

internal Tag(INativeTag nativeMethods)
internal Tag(INative nativeMethods)
{
_native = nativeMethods;
}
Expand Down Expand Up @@ -1173,7 +1174,7 @@ void SetUpEvents()
Created += CreatedTaskCompleter;

// Need to keep a reference to the delegate in memory so it doesn't get garbage collected
coreLibCallbackFuncExDelegate = new libplctag.NativeImport.plctag.callback_func_ex(coreLibEventCallback);
coreLibCallbackFuncExDelegate = new callback_func_ex(coreLibEventCallback);

}

Expand Down

0 comments on commit 658baa4

Please sign in to comment.