1+ // -- BEGIN LICENSE BLOCK ----------------------------------------------
2+ // Copyright 2025 Universal Robots A/S
3+ //
4+ // Redistribution and use in source and binary forms, with or without
5+ // modification, are permitted provided that the following conditions are met:
6+ //
7+ // * Redistributions of source code must retain the above copyright
8+ // notice, this list of conditions and the following disclaimer.
9+ //
10+ // * Redistributions in binary form must reproduce the above copyright
11+ // notice, this list of conditions and the following disclaimer in the
12+ // documentation and/or other materials provided with the distribution.
13+ //
14+ // * Neither the name of the {copyright_holder} nor the names of its
15+ // contributors may be used to endorse or promote products derived from
16+ // this software without specific prior written permission.
17+ //
18+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+ // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+ // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+ // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22+ // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+ // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+ // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+ // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+ // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+ // POSSIBILITY OF SUCH DAMAGE.
29+ // -- END LICENSE BLOCK ------------------------------------------------
30+
31+ #include < gtest/gtest.h>
32+ #include " ur_client_library/control/script_reader.h"
33+
34+ #include < fstream>
35+
36+ using namespace urcl ::control;
37+
38+ class ScriptReaderTest : public ::testing::Test
39+ {
40+ protected:
41+ std::string valid_script_path_;
42+ std::string invalid_script_path_;
43+ std::string empty_script_path_;
44+
45+ std::stringstream simple_script_;
46+
47+ ScriptReader::RobotInfo robot_info_;
48+
49+ void SetUp () override
50+ {
51+ invalid_script_path_ = " test_resources/non_existent_script.urscript" ;
52+ empty_script_path_ = " resources/empty.txt" ;
53+ char existing_script_file[] = " urscript.XXXXXX" ;
54+ #ifdef _WIN32
55+ # define mkstemp _mktemp_s
56+ #endif
57+ std::ignore = mkstemp (existing_script_file);
58+ std::ofstream ofs (existing_script_file);
59+ if (ofs.bad ())
60+ {
61+ std::cout << " Failed to create temporary files" << std::endl;
62+ GTEST_FAIL ();
63+ }
64+ ofs.close ();
65+
66+ valid_script_path_ = existing_script_file;
67+
68+ simple_script_ << " movej([0,0,0,0,0,0])" ;
69+
70+ // Create test resources
71+ std::ofstream valid_script (valid_script_path_);
72+ valid_script << simple_script_.str ();
73+ valid_script.close ();
74+
75+ std::ofstream empty_script (empty_script_path_);
76+ empty_script.close ();
77+
78+ robot_info_.robot_type = urcl::RobotType::UR3;
79+ }
80+
81+ void TearDown () override
82+ {
83+ std::remove (valid_script_path_.c_str ());
84+ std::remove (empty_script_path_.c_str ());
85+ }
86+ };
87+
88+ TEST_F (ScriptReaderTest, ReadValidScript)
89+ {
90+ ScriptReader reader (robot_info_);
91+ std::string content = reader.readScriptFile (valid_script_path_);
92+ EXPECT_EQ (content, " movej([0,0,0,0,0,0])" );
93+ }
94+
95+ TEST_F (ScriptReaderTest, ReadEmptyScript)
96+ {
97+ ScriptReader reader (robot_info_);
98+ std::string content = reader.readScriptFile (empty_script_path_);
99+ EXPECT_EQ (content, " " );
100+ }
101+
102+ TEST_F (ScriptReaderTest, ReadNonExistentScript)
103+ {
104+ ScriptReader reader (robot_info_);
105+ EXPECT_THROW (reader.readScriptFile (invalid_script_path_), std::runtime_error);
106+ }
0 commit comments