Skip to content

Commit

Permalink
Add simple unit test for macro object (#158)
Browse files Browse the repository at this point in the history
* Fix C warning

* Add unit test for simple macro object

* Fix XML comments

* Disable regeneration of test files

* Normalize test data file names

* Use more reliable way to get source directory

* Don't normalize name if it doesn't contain an underscore
  • Loading branch information
lithiumtoast authored Nov 15, 2023
1 parent 9ec7b58 commit f3b5c20
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/c/tests/_container_library/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <stdio.h>
#include "ffi_helper.h"

#include "../enums/_index.h"
#include "../macro_objects/_index.h"
#include "../function_pointers/_index.h"
2 changes: 1 addition & 1 deletion src/c/tests/enums/EnumForceUInt32.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ typedef enum EnumForceUInt32 {

FFI_API_DECL void EnumForceUInt32__print_EnumForceUInt32(const EnumForceUInt32 e)
{
printf("%d\n", e); // Print used for testing
printf("%lu\n", e); // Print used for testing
}

FFI_API_DECL EnumForceUInt32 EnumForceUInt32__return_EnumForceUInt32(const EnumForceUInt32 e)
Expand Down
8 changes: 8 additions & 0 deletions src/c/tests/macro_objects/MacroObjectIntValue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#define MACRO_OBJECT_INT_VALUE 42;

FFI_API_DECL int MacroObjectInt__return_int()
{
return MACRO_OBJECT_INT_VALUE;
}
1 change: 1 addition & 0 deletions src/c/tests/macro_objects/_index.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "MacroObjectIntValue.h"
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ namespace C2CS.Features.BuildCLibrary.Input.Unsanitized;
public class BuildCLibraryOptions : ToolUnsanitizedInput
{
/// <summary>
/// The directory path to the CMakeLists.txt file.
/// Gets or sets the directory path to the CMakeLists.txt file.
/// </summary>
[JsonPropertyName("cMakeDirectoryPath")]
public string? CMakeDirectoryPath { get; set; }

/// <summary>
/// The directory path where to place the built C shared library (`.dll`/`.dylib`/`.so`).
/// Gets or sets the directory path where to place the built C shared library (`.dll`/`.dylib`/`.so`).
/// </summary>
[JsonPropertyName("outputDirectoryPath")]
public string? OutputDirectoryPath { get; set; }

/// <summary>
/// Additional CMake arguments when generating build files.
/// Gets or sets additional CMake arguments when generating build files.
/// </summary>
[JsonPropertyName("cMakeArguments")]
public ImmutableArray<string?>? CMakeArguments { get; set; }

/// <summary>
/// Determines whether to delete CMake build files after they are no longer required.
/// Gets or sets whether to delete CMake build files after they are no longer required.
/// </summary>
/// <remarks>
/// <para>
Expand All @@ -43,7 +43,7 @@ public class BuildCLibraryOptions : ToolUnsanitizedInput
public bool? IsEnabledDeleteBuildFiles { get; set; }

/// <summary>
/// Determines whether to build the C shared library with debug symbols.
/// Gets or sets whether to build the C shared library with debug symbols.
/// </summary>
/// <remarks>
/// <para>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "MACRO_OBJECT_INT_VALUE",
"type_name": "int",
"value": "42"
}
35 changes: 33 additions & 2 deletions src/cs/tests/C2CS.Tests/Foundation/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the Git repository root directory for full license information.

using System;
using System.Globalization;
using System.IO;
using System.IO.Abstractions;
using System.Text.Json;
Expand All @@ -28,7 +29,8 @@ protected TestBase(string baseDataFilesDirectory, bool regenerateDataFiles = fal
Services = TestHost.Services;

_fileSystem = Services.GetService<IFileSystem>()!;
_sourceDirectoryPath = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../../src/cs/tests/C2CS.Tests"));

_sourceDirectoryPath = Path.Combine(GetGitDirectory(), "src/cs/tests/C2CS.Tests");
_regenerateDataFiles = regenerateDataFiles;

_jsonSerializerOptions = new JsonSerializerOptions
Expand All @@ -44,8 +46,18 @@ protected TestBase(string baseDataFilesDirectory, bool regenerateDataFiles = fal

protected void AssertValue<T>(string name, T value, string directory)
{
var nameNormalized = name;
if (name.Contains('_', StringComparison.InvariantCulture))
{
#pragma warning disable CA1308
var nameAsWords = name.ToLowerInvariant().Replace("_", " ", StringComparison.InvariantCulture);
#pragma warning restore CA1308
var nameAsWordsTitleCased = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(nameAsWords);
nameNormalized = nameAsWordsTitleCased.Replace(" ", string.Empty, StringComparison.InvariantCulture);
}

var relativeJsonFilePath =
_fileSystem.Path.Combine(_baseDataFilesDirectory, directory, $"{name}.json");
_fileSystem.Path.Combine(_baseDataFilesDirectory, directory, $"{nameNormalized}.json");
string jsonFilePath;
if (_regenerateDataFiles)
{
Expand Down Expand Up @@ -104,4 +116,23 @@ private void WriteValueToFile<T>(string filePath, T value)
textWriter.Close();
fileStream.Close();
}

private static string GetGitDirectory()
{
Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../../src/cs/tests/C2CS.Tests"));

var currentDirectory = AppContext.BaseDirectory;
while (!string.IsNullOrEmpty(currentDirectory) && Directory.Exists(currentDirectory))
{
var files = Directory.GetFiles(currentDirectory, "*.gitignore");
if (files.Length == 1)
{
return currentDirectory;
}

currentDirectory = Directory.GetParent(currentDirectory)?.FullName ?? string.Empty;
}

throw new InvalidOperationException("Could not find Git root directory");
}
}
4 changes: 2 additions & 2 deletions src/cs/tests/C2CS.Tests/Generated/AssemblyAttributes.gen.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// To disable generating this file set `isEnabledGenerateAssemblyAttributes` to `false` in the config file for generating C# code.
// <auto-generated>
// This code was generated by the following tool on 2023-07-23 05:51:56 GMT-04:00:
// https://github.com/bottlenoselabs/c2cs (v2.11.1.99)
// This code was generated by the following tool on 2023-11-15 18:22:18 GMT-05:00:
// https://github.com/bottlenoselabs/c2cs (v2.11.1.100)
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
Expand Down
4 changes: 2 additions & 2 deletions src/cs/tests/C2CS.Tests/Generated/Runtime.gen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// To disable generating this file set `isEnabledGeneratingRuntimeCode` to `false` in the config file for generating C# code.

// <auto-generated>
// This code was generated by the following tool on 2023-07-23 05:51:56 GMT-04:00:
// https://github.com/bottlenoselabs/c2cs (v2.11.1.99)
// This code was generated by the following tool on 2023-11-15 18:22:18 GMT-05:00:
// https://github.com/bottlenoselabs/c2cs (v2.11.1.100)
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
Expand Down
11 changes: 9 additions & 2 deletions src/cs/tests/C2CS.Tests/Generated/_container_library.gen.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

// <auto-generated>
// This code was generated by the following tool on 2023-07-23 05:51:56 GMT-04:00:
// https://github.com/bottlenoselabs/c2cs (v2.11.1.99)
// This code was generated by the following tool on 2023-11-15 18:22:18 GMT-05:00:
// https://github.com/bottlenoselabs/c2cs (v2.11.1.100)
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
Expand Down Expand Up @@ -91,6 +91,10 @@ public static unsafe partial class _container_library
[DllImport(LibraryName, EntryPoint = "EnumForceUInt8__return_EnumForceUInt8", CallingConvention = CallingConvention.Cdecl)]
public static extern EnumForceUInt8 EnumForceUInt8__return_EnumForceUInt8(EnumForceUInt8 e);

[CNode(Kind = "Function")]
[DllImport(LibraryName, EntryPoint = "MacroObjectInt__return_int", CallingConvention = CallingConvention.Cdecl)]
public static extern int MacroObjectInt__return_int();

[CNode(Kind = "Function")]
[DllImport(LibraryName, EntryPoint = "TypeDef_FunctionPointer_ReturnVoid_ArgsVoid__invoke", CallingConvention = CallingConvention.Cdecl)]
public static extern void TypeDef_FunctionPointer_ReturnVoid_ArgsVoid__invoke(TypeDef_FunctionPointer_ReturnVoid_ArgsVoid functionPointer);
Expand Down Expand Up @@ -213,5 +217,8 @@ public struct TypeDef_FunctionPointer_ReturnVoid_ArgsVoid
public static implicit operator TypeDef_FunctionPointer_ReturnVoid_ArgsVoid(FnPtr_Void data) => new() { Data = data };
}

[CNode(Kind = "MacroObject")]
public const int MACRO_OBJECT_INT_VALUE = 42;

#endregion
}
19 changes: 16 additions & 3 deletions src/cs/tests/C2CS.Tests/TestCSharpCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace C2CS.Tests;
[PublicAPI]
public sealed class TestCSharpCode : TestBase
{
public static TheoryData<string> Enums() => new()
public static TheoryData<string> EnumNames() => new()
{
"EnumForceSInt8",
"EnumForceSInt16",
Expand All @@ -25,13 +25,26 @@ public sealed class TestCSharpCode : TestBase
};

[Theory]
[MemberData(nameof(Enums))]
[MemberData(nameof(EnumNames))]
public void Enum(string name)
{
var value = _fixture.GetEnum(name);
AssertValue(name, value, "Enums");
}

public static TheoryData<string> MacroObjectNames() => new()
{
"MACRO_OBJECT_INT_VALUE"
};

[Theory]
[MemberData(nameof(MacroObjectNames))]
public void MacroObject(string name)
{
var value = _fixture.GetMacroObject(name);
AssertValue(name, value, "MacroObjects");
}

[Fact]
public void Compiles()
{
Expand All @@ -41,7 +54,7 @@ public void Compiles()
private readonly TestFixtureCSharpCode _fixture;

public TestCSharpCode()
: base("Data/Values", true)
: base("Data/Values", regenerateDataFiles: true)
{
var services = TestHost.Services;
_fixture = services.GetService<TestFixtureCSharpCode>()!;
Expand Down

0 comments on commit f3b5c20

Please sign in to comment.