Skip to content

Commit a6616b5

Browse files
committed
Merge branch 'slicer-issue-2712-support-parameter-file-with-empty-value'
* slicer-issue-2712-support-parameter-file-with-empty-value: BUG: Support parameter file with empty value
2 parents feef59e + 05af78d commit a6616b5

File tree

4 files changed

+126
-16
lines changed

4 files changed

+126
-16
lines changed

ModuleDescriptionParser/ModuleDescription.cxx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,14 @@ bool ModuleDescription ::ReadParameterFile(const std::string& filename)
316316

317317
std::string::size_type start = line.find_first_not_of(" \t");
318318
std::string::size_type stop = line.find_first_of("=", start);
319-
319+
320320
key = line.substr(start, stop-start);
321321
start = line.find_first_not_of(" \t", stop+1);
322-
value = line.substr(start, line.length() - start + 1);
323-
322+
if (start != std::string::npos)
323+
{
324+
value = line.substr(start, line.length() - start + 1);
325+
}
326+
324327
trimLeadingAndTrailing(key);
325328
trimLeadingAndTrailing(value);
326329

ModuleDescriptionParser/Testing/CMakeLists.txt

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
project(ModuleDescriptionParserTesting)
21

3-
#-----------------------------------------------------------------------------
4-
cmake_minimum_required(VERSION 2.8.2)
5-
#-----------------------------------------------------------------------------
2+
# --------------------------------------------------------------------------
3+
set(INPUT ${CMAKE_CURRENT_SOURCE_DIR}/TestData)
4+
5+
# --------------------------------------------------------------------------
6+
# Build tests
7+
# --------------------------------------------------------------------------
8+
set(KIT ${PROJECT_NAME})
9+
create_test_sourcelist(Tests ${KIT}CxxTests.cxx
10+
ModuleDescriptionTest.cxx
11+
)
612

7-
#-----------------------------------------------------------------------------
8-
# See http://cmake.org/cmake/help/cmake-2-8-docs.html#section_Policies for details
9-
#-----------------------------------------------------------------------------
10-
if(POLICY CMP0016)
11-
cmake_policy(SET CMP0016 NEW)
12-
endif()
13-
if(POLICY CMP0017)
14-
cmake_policy(SET CMP0017 OLD)
15-
endif()
13+
add_executable(${KIT}CxxTests ${Tests})
14+
target_link_libraries(${KIT}CxxTests ${KIT})
1615

1716
# --------------------------------------------------------------------------
1817
# Build Parser1Test executable
@@ -45,6 +44,11 @@ add_test(
4544
NAME Parser1Test2
4645
COMMAND ${Slicer_LAUNCH_COMMAND} $<TARGET_FILE:Parser1Test> ${TEST_DATA}/ParserTest2.xml)
4746

47+
add_test(
48+
NAME ModuleDescriptionTest
49+
COMMAND ${Slicer_LAUNCH_COMMAND} $<TARGET_FILE:${KIT}CxxTests> ModuleDescriptionTest ${INPUT}
50+
)
51+
4852
#add_test(
4953
# NAME ModuleFactoryTest
5054
# COMMAND ${Slicer_LAUNCH_COMMAND} $<TARGET_FILE:ModuleFactoryTest>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
// ModuleDescriptionParser includes
3+
#include "ModuleDescription.h"
4+
5+
// STD includes
6+
#include <cstdlib>
7+
#include <string>
8+
9+
//---------------------------------------------------------------------------
10+
bool TestReadParameterFileWithMissingValue();
11+
12+
//---------------------------------------------------------------------------
13+
namespace
14+
{
15+
std::string INPUT_DIR;
16+
}
17+
18+
//---------------------------------------------------------------------------
19+
int ModuleDescriptionTest(int argc, char * argv[])
20+
{
21+
if (argc < 2)
22+
{
23+
std::cout << "Usage: " << argv[0] << " /path/to/inputs" << std::endl;
24+
return EXIT_FAILURE;
25+
}
26+
27+
INPUT_DIR = std::string(argv[1]);
28+
29+
if (!TestReadParameterFileWithMissingValue())
30+
{
31+
return EXIT_FAILURE;
32+
}
33+
34+
return EXIT_SUCCESS;
35+
}
36+
37+
//---------------------------------------------------------------------------
38+
bool TestReadParameterFileWithMissingValue()
39+
{
40+
std::string input = INPUT_DIR
41+
+ "/parameter-file-with-missing-value-slicer-issue2712.params";
42+
43+
ModuleParameterGroup group;
44+
45+
{
46+
ModuleParameter parameter;
47+
parameter.SetName("OutputLabel");
48+
group.AddParameter(parameter);
49+
}
50+
51+
{
52+
ModuleParameter parameter;
53+
parameter.SetName("SUVMean");
54+
group.AddParameter(parameter);
55+
}
56+
57+
ModuleDescription desc;
58+
desc.AddParameterGroup(group);
59+
60+
if (!desc.HasParameter("OutputLabel") || !desc.HasParameter("SUVMean"))
61+
{
62+
std::cerr << "Line " << __LINE__
63+
<< " - Parameters are expected."
64+
<< std::endl;
65+
return false;
66+
}
67+
68+
if (!desc.ReadParameterFile(input))
69+
{
70+
std::cerr << "Line " << __LINE__
71+
<< " - 'SUVMean' set to a new value. Modification are expected."
72+
<< std::endl;
73+
return false;
74+
}
75+
76+
if (!desc.HasParameter("OutputLabel") || !desc.HasParameter("SUVMean"))
77+
{
78+
std::cerr << "Line " << __LINE__
79+
<< " - Problem reading parameters - Parameters are expected."
80+
<< std::endl;
81+
return false;
82+
}
83+
84+
if (desc.GetParameterDefaultValue("OutputLabel") != "")
85+
{
86+
std::cerr << "Line " << __LINE__
87+
<< " - Problem reading parameters - Value is expected to be empty."
88+
<< std::endl;
89+
return false;
90+
}
91+
92+
if (desc.GetParameterDefaultValue("SUVMean") != "2")
93+
{
94+
std::cerr << "Line " << __LINE__
95+
<< " - Problem reading parameters - Value is expected to be '2'."
96+
<< std::endl;
97+
return false;
98+
}
99+
100+
return true;
101+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
OutputLabel =
2+
SUVMean = 2

0 commit comments

Comments
 (0)