Skip to content

Commit

Permalink
Test: Add tests with NFD_NATIVE (btzy#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
btzy authored Dec 15, 2023
1 parent 5786fab commit 83191b6
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 2 deletions.
9 changes: 7 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
set(TEST_LIST
test_opendialog.c
test_opendialog_cpp.cpp
test_opendialog_native.c
test_opendialogmultiple.c
test_opendialogmultiple_cpp.cpp
test_opendialogmultiple_native.c
test_opendialogmultiple_enum.c
test_opendialogmultiple_enum_native.c
test_pickfolder.c
test_pickfolder_cpp.cpp
test_savedialog.c)
test_pickfolder_native.c
test_savedialog.c
test_savedialog_native.c)

foreach (TEST ${TEST_LIST})
string(REPLACE "." "_" CLEAN_TEST_NAME ${TEST})
add_executable(${CLEAN_TEST_NAME}
${TEST})
target_link_libraries(${CLEAN_TEST_NAME}
PUBLIC nfd)
endforeach()
endforeach()
49 changes: 49 additions & 0 deletions test/test_opendialog_native.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#define NFD_NATIVE
#include <nfd.h>

#include <stdio.h>
#include <stdlib.h>

/* this test should compile on all supported platforms */

int main(void) {
// initialize NFD
// either call NFD_Init at the start of your program and NFD_Quit at the end of your program,
// or before/after every time you want to show a file dialog.
NFD_Init();

nfdchar_t* outPath;

// prepare filters for the dialog
#ifdef _WIN32
nfdfilteritem_t filterItem[2] = {{L"Source code", L"c,cpp,cc"}, {L"Headers", L"h,hpp"}};
#else
nfdfilteritem_t filterItem[2] = {{"Source code", "c,cpp,cc"}, {"Headers", "h,hpp"}};
#endif

// show the dialog
nfdresult_t result = NFD_OpenDialog(&outPath, filterItem, 2, NULL);
if (result == NFD_OKAY) {
puts("Success!");
#ifdef _WIN32
#ifdef _MSC_VER
_putws(outPath);
#else
fputws(outPath, stdin);
#endif
#else
puts(outPath);
#endif
// remember to free the memory (since NFD_OKAY is returned)
NFD_FreePath(outPath);
} else if (result == NFD_CANCEL) {
puts("User pressed cancel.");
} else {
printf("Error: %s\n", NFD_GetError());
}

// Quit NFD
NFD_Quit();

return 0;
}
62 changes: 62 additions & 0 deletions test/test_opendialogmultiple_enum_native.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#define NFD_NATIVE
#include <nfd.h>

#include <stdio.h>
#include <stdlib.h>

/* this test should compile on all supported platforms */

int main(void) {
// initialize NFD
// either call NFD_Init at the start of your program and NFD_Quit at the end of your program,
// or before/after every time you want to show a file dialog.
NFD_Init();

const nfdpathset_t* outPaths;

// prepare filters for the dialog
#ifdef _WIN32
nfdfilteritem_t filterItem[2] = {{L"Source code", L"c,cpp,cc"}, {L"Headers", L"h,hpp"}};
#else
nfdfilteritem_t filterItem[2] = {{"Source code", "c,cpp,cc"}, {"Headers", "h,hpp"}};
#endif

// show the dialog
nfdresult_t result = NFD_OpenDialogMultiple(&outPaths, filterItem, 2, NULL);

if (result == NFD_OKAY) {
puts("Success!");

// declare enumerator (not a pointer)
nfdpathsetenum_t enumerator;

NFD_PathSet_GetEnum(outPaths, &enumerator);
nfdchar_t* path;
unsigned i = 0;
while (NFD_PathSet_EnumNext(&enumerator, &path) && path) {
#ifdef _WIN32
wprintf(L"Path %u: %s\n", i++, path);
#else
printf("Path %u: %s\n", i++, path);
#endif

// remember to free the pathset path with NFD_PathSet_FreePath (not NFD_FreePath!)
NFD_PathSet_FreePath(path);
}

// remember to free the pathset enumerator memory (before freeing the pathset)
NFD_PathSet_FreeEnum(&enumerator);

// remember to free the pathset memory (since NFD_OKAY is returned)
NFD_PathSet_Free(outPaths);
} else if (result == NFD_CANCEL) {
puts("User pressed cancel.");
} else {
printf("Error: %s\n", NFD_GetError());
}

// Quit NFD
NFD_Quit();

return 0;
}
59 changes: 59 additions & 0 deletions test/test_opendialogmultiple_native.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#define NFD_NATIVE
#include <nfd.h>

#include <stdio.h>
#include <stdlib.h>

/* this test should compile on all supported platforms */

int main(void) {
// initialize NFD
// either call NFD_Init at the start of your program and NFD_Quit at the end of your program,
// or before/after every time you want to show a file dialog.
NFD_Init();

const nfdpathset_t* outPaths;

// prepare filters for the dialog
#ifdef _WIN32
nfdfilteritem_t filterItem[2] = {{L"Source code", L"c,cpp,cc"}, {L"Headers", L"h,hpp"}};
#else
nfdfilteritem_t filterItem[2] = {{"Source code", "c,cpp,cc"}, {"Headers", "h,hpp"}};
#endif

// show the dialog
nfdresult_t result = NFD_OpenDialogMultiple(&outPaths, filterItem, 2, NULL);

if (result == NFD_OKAY) {
puts("Success!");

nfdpathsetsize_t numPaths;
NFD_PathSet_GetCount(outPaths, &numPaths);

nfdpathsetsize_t i;
for (i = 0; i < numPaths; ++i) {
nfdchar_t* path;
NFD_PathSet_GetPath(outPaths, i, &path);
#ifdef _WIN32
wprintf(L"Path %i: %s\n", (int)i, path);
#else
printf("Path %i: %s\n", (int)i, path);
#endif

// remember to free the pathset path with NFD_PathSet_FreePath (not NFD_FreePath!)
NFD_PathSet_FreePath(path);
}

// remember to free the pathset memory (since NFD_OKAY is returned)
NFD_PathSet_Free(outPaths);
} else if (result == NFD_CANCEL) {
puts("User pressed cancel.");
} else {
printf("Error: %s\n", NFD_GetError());
}

// Quit NFD
NFD_Quit();

return 0;
}
42 changes: 42 additions & 0 deletions test/test_pickfolder_native.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#define NFD_NATIVE
#include <nfd.h>

#include <stdio.h>
#include <stdlib.h>

/* this test should compile on all supported platforms */

int main(void) {
// initialize NFD
// either call NFD_Init at the start of your program and NFD_Quit at the end of your program,
// or before/after every time you want to show a file dialog.
NFD_Init();

nfdchar_t* outPath;

// show the dialog
nfdresult_t result = NFD_PickFolder(&outPath, NULL);
if (result == NFD_OKAY) {
puts("Success!");
#ifdef _WIN32
#ifdef _MSC_VER
_putws(outPath);
#else
fputws(outPath, stdin);
#endif
#else
puts(outPath);
#endif
// remember to free the memory (since NFD_OKAY is returned)
NFD_FreePath(outPath);
} else if (result == NFD_CANCEL) {
puts("User pressed cancel.");
} else {
printf("Error: %s\n", NFD_GetError());
}

// Quit NFD
NFD_Quit();

return 0;
}
55 changes: 55 additions & 0 deletions test/test_savedialog_native.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#define NFD_NATIVE
#include <nfd.h>

#include <stdio.h>
#include <stdlib.h>

/* this test should compile on all supported platforms */

int main(void) {
// initialize NFD
// either call NFD_Init at the start of your program and NFD_Quit at the end of your program,
// or before/after every time you want to show a file dialog.
NFD_Init();

nfdchar_t* savePath;

// prepare filters for the dialog
#ifdef _WIN32
nfdfilteritem_t filterItem[2] = {{L"Source code", L"c,cpp,cc"}, {L"Headers", L"h,hpp"}};
#else
nfdfilteritem_t filterItem[2] = {{"Source code", "c,cpp,cc"}, {"Headers", "h,hpp"}};
#endif

#ifdef _WIN32
const wchar_t* defaultPath = L"Untitled.c";
#else
const char* defaultPath = "Untitled.c";
#endif

// show the dialog
nfdresult_t result = NFD_SaveDialog(&savePath, filterItem, 2, NULL, defaultPath);
if (result == NFD_OKAY) {
puts("Success!");
#ifdef _WIN32
#ifdef _MSC_VER
_putws(savePath);
#else
fputws(savePath, stdin);
#endif
#else
puts(savePath);
#endif
// remember to free the memory (since NFD_OKAY is returned)
NFD_FreePath(savePath);
} else if (result == NFD_CANCEL) {
puts("User pressed cancel.");
} else {
printf("Error: %s\n", NFD_GetError());
}

// Quit NFD
NFD_Quit();

return 0;
}

0 comments on commit 83191b6

Please sign in to comment.