11
11
#include < filesystem>
12
12
#include < fstream>
13
13
#include < iostream>
14
+ #include < nlohmann/json.hpp>
14
15
#include < sstream>
15
16
#include < vector>
16
17
#include < sys/stat.h> // for mkfifo
@@ -61,6 +62,10 @@ class TestDirectory
61
62
std::to_string ( std::time ( nullptr ) ) ) )
62
63
.string ();
63
64
std::filesystem::create_directories ( test_dir );
65
+
66
+ // Create database directory for test data
67
+ database_dir = test_dir + " /test-database" ;
68
+ std::filesystem::create_directories ( database_dir );
64
69
}
65
70
66
71
~TestDirectory () { std::filesystem::remove_all ( test_dir ); }
@@ -70,6 +75,7 @@ class TestDirectory
70
75
TestDirectory &operator =( const TestDirectory & ) = delete ;
71
76
72
77
const std::string &path () const { return test_dir; }
78
+ const std::string &get_database_path () const { return database_dir; }
73
79
74
80
void create_test_files ()
75
81
{
@@ -120,8 +126,51 @@ class TestDirectory
120
126
}
121
127
}
122
128
129
+ // / Creates a test data file (camera or illuminant) with the specified header data
130
+ // / @param type The type of test data to create (e.g. camera or illuminant)
131
+ // / @param header_data JSON object containing the header data to include
132
+ // / @return The full path to the created file
133
+ std::string create_test_data_file (
134
+ const std::string &type, const nlohmann::json &header_data )
135
+ {
136
+ // Generate random filename
137
+ static int file_counter = 0 ;
138
+ std::string filename = " test_data_" + std::to_string ( ++file_counter ) +
139
+ " _" + std::to_string ( std::time ( nullptr ) ) +
140
+ " .json" ;
141
+
142
+ // Create target directory dynamically based on type
143
+ std::string target_dir = database_dir + " /" + type;
144
+ std::filesystem::create_directories ( target_dir );
145
+ std::string file_path = target_dir + " /" + filename;
146
+
147
+ // Create JSON object using nlohmann/json
148
+ nlohmann::json json_data;
149
+
150
+ // Start with default header and merge user data
151
+ nlohmann::json header = header_data;
152
+
153
+ // Build spectral_data object
154
+ nlohmann::json spectral_data = { { " units" , " relative" },
155
+ { " index" ,
156
+ { { " main" , { " R" , " G" , " B" } } } },
157
+ { " data" , nlohmann::json::object () } };
158
+
159
+ // Assemble final JSON
160
+ json_data[" header" ] = header;
161
+ json_data[" spectral_data" ] = spectral_data;
162
+
163
+ // Write to file with pretty formatting
164
+ std::ofstream file ( file_path );
165
+ file << json_data.dump ( 4 ) << std::endl;
166
+ file.close ();
167
+
168
+ return file_path;
169
+ }
170
+
123
171
private:
124
172
std::string test_dir;
173
+ std::string database_dir;
125
174
};
126
175
127
176
// / Verifies that collect_image_files can traverse a directory, identify valid RAW image files,
@@ -545,15 +594,18 @@ struct ParseParametersTestResult
545
594
// /
546
595
// / @param args Vector of command-line arguments to pass to parse_parameters (excluding program name).
547
596
// / For example, {"--list-cameras"} or {"--list-illuminants", "--verbose"}.
597
+ // / @param database_path Path to the test database directory (optional, uses default if not provided)
548
598
// /
549
599
// / @return ParseParametersTestResult containing:
550
600
// / - success: true if parse_parameters executed successfully, false if argument parsing failed
551
601
// / - output: captured stdout output from the parse_parameters execution
552
- ParseParametersTestResult
553
- run_parse_parameters_test ( const std::vector<std::string> &args )
602
+ ParseParametersTestResult run_parse_parameters_test (
603
+ const std::vector<std::string> &args,
604
+ const std::string &database_path = " " )
554
605
{
555
- // Set up test data path to use the test database
556
- std::string test_data_path = get_test_database_path ();
606
+ // Set up test data path to use the provided database path or default
607
+ std::string test_data_path =
608
+ database_path.empty () ? get_test_database_path () : database_path;
557
609
set_env_var ( " RAWTOACES_DATA_PATH" , test_data_path.c_str () );
558
610
559
611
// Create ImageConverter instance
@@ -604,8 +656,18 @@ void test_parse_parameters_list_cameras()
604
656
std::cout << std::endl
605
657
<< " test_parse_parameters_list_cameras()" << std::endl;
606
658
607
- // Run the test with --list-cameras argument
608
- auto result = run_parse_parameters_test ( { " --list-cameras" } );
659
+ // Create test directory with dynamic database
660
+ TestDirectory test_dir;
661
+
662
+ // Create test camera data files
663
+ test_dir.create_test_data_file (
664
+ " camera" , { { " manufacturer" , " Canon" }, { " model" , " EOS R6" } } );
665
+ test_dir.create_test_data_file (
666
+ " camera" , { { " manufacturer" , " Mamiya" }, { " model" , " Mamiya 7" } } );
667
+
668
+ // Run the test with --list-cameras argument using the dynamic database
669
+ auto result = run_parse_parameters_test (
670
+ { " --list-cameras" }, test_dir.get_database_path () );
609
671
610
672
// The method should return true (though it calls exit in real usage)
611
673
OIIO_CHECK_EQUAL ( result.success , true );
@@ -620,10 +682,9 @@ void test_parse_parameters_list_cameras()
620
682
// Verify that actual camera names from test data are present
621
683
// The format is "manufacturer / model" as defined in supported_cameras()
622
684
OIIO_CHECK_EQUAL (
623
- result.output .find ( " cheburashka / model 1" ) != std::string::npos,
624
- true );
685
+ result.output .find ( " Canon / EOS R6" ) != std::string::npos, true );
625
686
OIIO_CHECK_EQUAL (
626
- result.output .find ( " karamba / M2 " ) != std::string::npos, true );
687
+ result.output .find ( " Mamiya / Mamiya 7 " ) != std::string::npos, true );
627
688
628
689
// Count occurrences of " / " to verify we have 2 camera entries
629
690
size_t camera_count = 0 ;
@@ -643,8 +704,16 @@ void test_parse_parameters_list_illuminants()
643
704
std::cout << std::endl
644
705
<< " test_parse_parameters_list_illuminants()" << std::endl;
645
706
646
- // Run the test with --list-illuminants argument
647
- auto result = run_parse_parameters_test ( { " --list-illuminants" } );
707
+ // Create test directory with dynamic database
708
+ TestDirectory test_dir;
709
+
710
+ // Create test illuminant data file
711
+ test_dir.create_test_data_file (
712
+ " illuminant" , { { " illuminant" , " my-illuminant" } } );
713
+
714
+ // Run the test with --list-illuminants argument using the dynamic database
715
+ auto result = run_parse_parameters_test (
716
+ { " --list-illuminants" }, test_dir.get_database_path () );
648
717
649
718
// The method should return true (though it calls exit in real usage)
650
719
OIIO_CHECK_EQUAL ( result.success , true );
@@ -667,7 +736,7 @@ void test_parse_parameters_list_illuminants()
667
736
668
737
// Verify that the specific illuminant from our test data is present
669
738
OIIO_CHECK_EQUAL (
670
- result.output .find ( " iso4242 " ) != std::string::npos, true );
739
+ result.output .find ( " my-illuminant " ) != std::string::npos, true );
671
740
672
741
// Verify we have exactly 3 illuminants total (2 hardcoded + 1 from test data)
673
742
// Count newlines in the illuminant list section to verify count
0 commit comments