1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /*************************************************************************** 3 * configtest.cc 4 * 5 * Thu May 14 20:58:29 CEST 2015 6 * Copyright 2015 Bent Bisballe Nyeng 7 * deva@aasimon.org 8 ****************************************************************************/ 9 10 /* 11 * This file is part of DrumGizmo. 12 * 13 * DrumGizmo is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU Lesser General Public License as published by 15 * the Free Software Foundation; either version 3 of the License, or 16 * (at your option) any later version. 17 * 18 * DrumGizmo is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU Lesser General Public License for more details. 22 * 23 * You should have received a copy of the GNU Lesser General Public License 24 * along with DrumGizmo; if not, write to the Free Software 25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 26 */ 27 #include <uunit.h> 28 29 #include <unistd.h> 30 #include <stdio.h> 31 32 #include "../src/configfile.h" 33 34 class TestConfigFile 35 : public ConfigFile { 36 public: TestConfigFile()37 TestConfigFile() 38 : ConfigFile("") 39 { 40 } 41 42 protected: 43 // Overload the built-in open method to use local file instead of homedir. open(std::ios_base::openmode mode)44 virtual bool open(std::ios_base::openmode mode) 45 { 46 current_file.open("test.conf", mode); 47 return current_file.is_open(); 48 } 49 }; 50 51 class test_configtest 52 : public uUnit 53 { 54 public: test_configtest()55 test_configtest() 56 { 57 uUNIT_TEST(test_configtest::loading_no_newline); 58 uUNIT_TEST(test_configtest::loading_equal_sign); 59 uUNIT_TEST(test_configtest::loading_newline); 60 uUNIT_TEST(test_configtest::loading_padding_space); 61 uUNIT_TEST(test_configtest::loading_padding_space_newline); 62 uUNIT_TEST(test_configtest::loading_padding_tab); 63 uUNIT_TEST(test_configtest::loading_padding_tab_newline); 64 uUNIT_TEST(test_configtest::loading_comment); 65 uUNIT_TEST(test_configtest::loading_inline_comment); 66 uUNIT_TEST(test_configtest::loading_single_quoted_string); 67 uUNIT_TEST(test_configtest::loading_double_quoted_string); 68 uUNIT_TEST(test_configtest::loading_error_no_key); 69 uUNIT_TEST(test_configtest::loading_error_no_value); 70 uUNIT_TEST(test_configtest::loading_error_string_not_terminated_single); 71 uUNIT_TEST(test_configtest::loading_error_string_not_terminated_double); 72 uUNIT_TEST(test_configtest::empty_value); 73 } 74 teardown()75 void teardown() override 76 { 77 unlink("test.conf"); 78 } 79 writeFile(const std::string & content,bool newline=true)80 void writeFile(const std::string& content, bool newline = true) 81 { 82 std::fstream file("test.conf", std::ios_base::out); 83 file << content; 84 if (newline) 85 { 86 file << std::endl; 87 } 88 file.close(); 89 } 90 loading_no_newline()91 void loading_no_newline() 92 { 93 writeFile("a:b", false); 94 95 TestConfigFile cf; 96 uUNIT_ASSERT_EQUAL(true, cf.load()); 97 uUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); 98 } 99 loading_equal_sign()100 void loading_equal_sign() 101 { 102 writeFile(" a =\tb\t"); 103 104 TestConfigFile cf; 105 uUNIT_ASSERT_EQUAL(true, cf.load()); 106 uUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); 107 } 108 loading_newline()109 void loading_newline() 110 { 111 writeFile("a:b"); 112 113 TestConfigFile cf; 114 uUNIT_ASSERT_EQUAL(true, cf.load()); 115 uUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); 116 } 117 loading_padding_space()118 void loading_padding_space() 119 { 120 writeFile(" a : b ", false); 121 122 TestConfigFile cf; 123 uUNIT_ASSERT_EQUAL(true, cf.load()); 124 uUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); 125 } 126 loading_padding_tab()127 void loading_padding_tab() 128 { 129 writeFile("\ta\t:\tb\t", false); 130 131 TestConfigFile cf; 132 uUNIT_ASSERT_EQUAL(true, cf.load()); 133 uUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); 134 } 135 loading_padding_space_newline()136 void loading_padding_space_newline() 137 { 138 writeFile(" a : b "); 139 140 TestConfigFile cf; 141 uUNIT_ASSERT_EQUAL(true, cf.load()); 142 uUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); 143 } 144 loading_padding_tab_newline()145 void loading_padding_tab_newline() 146 { 147 writeFile("\ta\t:\tb\t"); 148 149 TestConfigFile cf; 150 uUNIT_ASSERT_EQUAL(true, cf.load()); 151 uUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); 152 } 153 loading_comment()154 void loading_comment() 155 { 156 writeFile("# comment\na:b"); 157 158 TestConfigFile cf; 159 uUNIT_ASSERT_EQUAL(true, cf.load()); 160 uUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); 161 } 162 loading_inline_comment()163 void loading_inline_comment() 164 { 165 writeFile("a:b #comment"); 166 167 TestConfigFile cf; 168 uUNIT_ASSERT_EQUAL(true, cf.load()); 169 uUNIT_ASSERT_EQUAL(std::string("b"), cf.getValue("a")); 170 } 171 loading_single_quoted_string()172 void loading_single_quoted_string() 173 { 174 writeFile("a: '#\"b\" ' "); 175 176 TestConfigFile cf; 177 uUNIT_ASSERT(cf.load()); 178 uUNIT_ASSERT_EQUAL(std::string("#\"b\" "), cf.getValue("a")); 179 } 180 loading_double_quoted_string()181 void loading_double_quoted_string() 182 { 183 writeFile("a: \"#'b' \" "); 184 185 TestConfigFile cf; 186 uUNIT_ASSERT_EQUAL(true, cf.load()); 187 uUNIT_ASSERT_EQUAL(std::string("#'b' "), cf.getValue("a")); 188 } 189 loading_error_no_key()190 void loading_error_no_key() 191 { 192 writeFile(":foo"); 193 194 TestConfigFile cf; 195 uUNIT_ASSERT_EQUAL(false, cf.load()); 196 } 197 loading_error_no_value()198 void loading_error_no_value() 199 { 200 writeFile("key"); 201 202 TestConfigFile cf; 203 uUNIT_ASSERT_EQUAL(false, cf.load()); 204 } 205 loading_error_string_not_terminated_single()206 void loading_error_string_not_terminated_single() 207 { 208 writeFile("a:'b"); 209 210 TestConfigFile cf; 211 uUNIT_ASSERT_EQUAL(false, cf.load()); 212 } 213 loading_error_string_not_terminated_double()214 void loading_error_string_not_terminated_double() 215 { 216 writeFile("a:\"b"); 217 218 TestConfigFile cf; 219 uUNIT_ASSERT_EQUAL(false, cf.load()); 220 } 221 empty_value()222 void empty_value() 223 { 224 writeFile("a:"); 225 226 TestConfigFile cf; 227 uUNIT_ASSERT_EQUAL(true, cf.load()); 228 uUNIT_ASSERT_EQUAL(std::string(""), cf.getValue("a")); 229 } 230 }; 231 232 // Registers the fixture into the 'registry' 233 static test_configtest test; 234