From 59eacb2eeb1d1da11fcea2a3a091cb1c29edd73a Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Thu, 8 Feb 2018 12:19:56 +0100
Subject: [PATCH] OffSpecularResonator example now runs as a PyStandard
 functional tests

---
 Core/Export/PythonFormatting.cpp                     | 12 ++++++++++--
 Core/Export/PythonFormatting.h                       |  1 +
 Core/Export/SimulationToPython.cpp                   |  5 ++++-
 Tests/Functional/Python/PyStandard/CMakeLists.txt    |  2 +-
 .../Core/ExportToPython/PythonFormattingTest.h       |  4 ++--
 5 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/Core/Export/PythonFormatting.cpp b/Core/Export/PythonFormatting.cpp
index 31f7c3c0c65..f51a19134a5 100644
--- a/Core/Export/PythonFormatting.cpp
+++ b/Core/Export/PythonFormatting.cpp
@@ -161,6 +161,13 @@ std::string printValue(double value, const std::string& units)
                                  + "'");
 }
 
+std::string printString(const std::string& value)
+{
+    std::ostringstream result;
+    result << "\"" << value << "\"";
+    return result.str();
+}
+
 bool isSquare(double length1, double length2, double angle)
 {
     return length1 == length2 && Numeric::areAlmostEqual(angle, M_PI_2);
@@ -285,6 +292,7 @@ std::string printAxis(const IAxis& axis, const std::string& units)
 
     if (auto fixedAxis = dynamic_cast<const FixedBinAxis*>(&axis)) {
         result << "ba.FixedBinAxis("
+               << printString(fixedAxis->getName()) << ", "
                << fixedAxis->size() << ", "
                << printValue(fixedAxis->getMin(), units) << ", "
                << printValue(fixedAxis->getMax(), units) << ")";
@@ -293,7 +301,7 @@ std::string printAxis(const IAxis& axis, const std::string& units)
         throw std::runtime_error("PythonFormatting::printAxis() -> Error. Unsupported axis");
     }
 
-                  return result.str();
-    }
+    return result.str();
+}
 
 } // namespace PythonFormatting
diff --git a/Core/Export/PythonFormatting.h b/Core/Export/PythonFormatting.h
index ed45d92bb30..40c8591fc58 100644
--- a/Core/Export/PythonFormatting.h
+++ b/Core/Export/PythonFormatting.h
@@ -43,6 +43,7 @@ BA_CORE_API_ std::string printNm(double input);
 BA_CORE_API_ std::string printScientificDouble(double input);
 BA_CORE_API_ std::string printDegrees(double input);
 BA_CORE_API_ std::string printValue(double value, const std::string& units = "");
+BA_CORE_API_ std::string printString(const std::string& value);
 
 BA_CORE_API_ bool isSquare(double length1, double length2, double angle);
 BA_CORE_API_ bool isHexagonal(double length1, double length2, double angle);
diff --git a/Core/Export/SimulationToPython.cpp b/Core/Export/SimulationToPython.cpp
index 906649e2eb8..5e8fe8abc58 100644
--- a/Core/Export/SimulationToPython.cpp
+++ b/Core/Export/SimulationToPython.cpp
@@ -258,8 +258,11 @@ std::string SimulationToPython::defineOffSpecBeam(const OffSpecSimulation& simul
     std::ostringstream result;
     const Beam& beam = simulation.getInstrument().getBeam();
 
+    result << indent() << "alpha_i_axis = "
+           << PythonFormatting::printAxis(*simulation.beamAxis(), BornAgain::UnitsRad) << "\n";
+
     result << indent() << "simulation.setBeamParameters(" << printNm(beam.getWavelength()) << ", "
-           << printDegrees(beam.getAlpha()) << ", " << printDegrees(beam.getPhi()) << ")\n";
+           << "alpha_i_axis, " << printDegrees(beam.getPhi()) << ")\n";
 
     result << defineBeamPolarization(beam);
     result << defineBeamIntensity(beam);
diff --git a/Tests/Functional/Python/PyStandard/CMakeLists.txt b/Tests/Functional/Python/PyStandard/CMakeLists.txt
index 699b0936be8..2c0b77e2087 100644
--- a/Tests/Functional/Python/PyStandard/CMakeLists.txt
+++ b/Tests/Functional/Python/PyStandard/CMakeLists.txt
@@ -56,7 +56,7 @@ set(test_cases
     RotatedPyramidsDistribution
     SpheresWithLimitsDistribution
     ConesWithLimitsDistribution
-#    OffSpecularResonator
+    OffSpecularResonator
 )
 
 add_executable(PyStandardTest main.cpp PyStandardTest.h PyStandardTest.cpp)
diff --git a/Tests/UnitTests/Core/ExportToPython/PythonFormattingTest.h b/Tests/UnitTests/Core/ExportToPython/PythonFormattingTest.h
index 826d0840585..b44c527d57b 100644
--- a/Tests/UnitTests/Core/ExportToPython/PythonFormattingTest.h
+++ b/Tests/UnitTests/Core/ExportToPython/PythonFormattingTest.h
@@ -111,9 +111,9 @@ TEST_F(PythonFormattingTest, printParameterDistribution)
 TEST_F(PythonFormattingTest, printAxis)
 {
     FixedBinAxis axis1("axis0", 10, -1.0, 2.0);
-    EXPECT_EQ(PythonFormatting::printAxis(axis1), "ba.FixedBinAxis(10, -1.0, 2.0)");
+    EXPECT_EQ(PythonFormatting::printAxis(axis1), "ba.FixedBinAxis(\"axis0\", 10, -1.0, 2.0)");
 
     FixedBinAxis axis2("axis0", 10, -1.0*Units::deg, 2.0*Units::deg);
     EXPECT_EQ(PythonFormatting::printAxis(axis2, BornAgain::UnitsRad),
-              "ba.FixedBinAxis(10, -1.0*deg, 2.0*deg)");
+              "ba.FixedBinAxis(\"axis0\", 10, -1.0*deg, 2.0*deg)");
 }
-- 
GitLab