1/* 2 * Copyright © 2020 Michael Gratton <mike@vee.net> 3 * 4 * This software is licensed under the GNU Lesser General Public License 5 * (version 2.1 or later). See the COPYING file in this distribution. 6 */ 7 8public class CollectionAssertions : ValaUnit.TestCase { 9 10 11 12 public CollectionAssertions() { 13 base("CollectionAssertions"); 14 add_test("string_collection", string_collection); 15 add_test("string_array_collection", string_array_collection); 16 add_test("int_array_collection", int_array_collection); 17 add_test("string_gee_collection", string_gee_collection); 18 add_test("int_gee_collection", int_gee_collection); 19 } 20 21 public void string_collection() throws GLib.Error { 22 assert_string("hello", "non-empty string") 23 .is_non_empty() 24 .size(5) 25 .contains("lo") 26 .not_contains("☃") 27 .first_is("h") 28 .first_is("hell") 29 .at_index_is(1, "e") 30 .at_index_is(1, "ell"); 31 32 33 assert_string("", "empty string") 34 .is_empty() 35 .size(0) 36 .contains("") 37 .not_contains("☃"); 38 39 try { 40 assert_string("").is_non_empty(); 41 fail("Expected ::is_non_empty to fail"); 42 } catch (ValaUnit.TestError.FAILED err) { 43 // all good 44 } 45 46 try { 47 assert_string("hello").is_empty(); 48 fail("Expected ::is_empty to fail"); 49 } catch (ValaUnit.TestError.FAILED err) { 50 // all good 51 } 52 53 try { 54 assert_string("hello").contains("☃"); 55 fail("Expected ::contains to fail"); 56 } catch (ValaUnit.TestError.FAILED err) { 57 // all good 58 } 59 } 60 61 public void string_array_collection() throws GLib.Error { 62 assert_array(new string[] { "hello", "world"}) 63 .is_non_empty() 64 .size(2) 65 .contains("hello") 66 .not_contains("☃") 67 .first_is("hello") 68 .at_index_is(1, "world"); 69 70 71 assert_array(new string[0]) 72 .is_empty() 73 .size(0) 74 .not_contains("☃"); 75 76 try { 77 assert_array(new string[0]).is_non_empty(); 78 fail("Expected ::is_non_empty to fail"); 79 } catch (ValaUnit.TestError.FAILED err) { 80 // all good 81 } 82 83 try { 84 assert_array(new string[] { "hello", "world"}).is_empty(); 85 fail("Expected ::is_empty to fail"); 86 } catch (ValaUnit.TestError.FAILED err) { 87 // all good 88 } 89 90 try { 91 assert_array(new string[] { "hello", "world"}).contains("☃"); 92 fail("Expected ::contains to fail"); 93 } catch (ValaUnit.TestError.FAILED err) { 94 // all good 95 } 96 } 97 98 public void int_array_collection() throws GLib.Error { 99 skip("Arrays containing non-pointer values not currently supported. See GNOME/vala#964"); 100 int[] array = new int[] { 42, 1337 }; 101 int[] empty = new int[0]; 102 103 assert_array(array) 104 .is_non_empty() 105 .size(2) 106 .contains(42) 107 .not_contains(-1) 108 .first_is(42) 109 .at_index_is(1, 1337); 110 111 assert_array(empty) 112 .is_empty() 113 .size(0) 114 .not_contains(42); 115 116 try { 117 assert_array(empty).is_non_empty(); 118 fail("Expected ::is_non_empty to fail"); 119 } catch (ValaUnit.TestError.FAILED err) { 120 // all good 121 } 122 123 try { 124 assert_array(array).is_empty(); 125 fail("Expected ::is_empty to fail"); 126 } catch (ValaUnit.TestError.FAILED err) { 127 // all good 128 } 129 130 try { 131 assert_array(array).contains(-1); 132 fail("Expected ::contains to fail"); 133 } catch (ValaUnit.TestError.FAILED err) { 134 // all good 135 } 136 } 137 138 public void string_gee_collection() throws GLib.Error { 139 var strv = new string[] { "hello", "world" }; 140 assert_collection(new_gee_collection(strv)) 141 .is_non_empty() 142 .size(2) 143 .contains("hello") 144 .not_contains("☃") 145 .first_is("hello") 146 .at_index_is(1, "world"); 147 148 assert_collection(new_gee_collection(new string[0])) 149 .is_empty() 150 .size(0) 151 .not_contains("☃"); 152 153 try { 154 assert_collection(new_gee_collection(new string[0])).is_non_empty(); 155 fail("Expected ::is_non_empty to fail"); 156 } catch (ValaUnit.TestError.FAILED err) { 157 // all good 158 } 159 160 try { 161 assert_collection(new_gee_collection(strv)).is_empty(); 162 fail("Expected ::is_empty to fail"); 163 } catch (ValaUnit.TestError.FAILED err) { 164 // all good 165 } 166 167 try { 168 assert_collection(new_gee_collection(strv)).contains("☃"); 169 fail("Expected ::contains to fail"); 170 } catch (ValaUnit.TestError.FAILED err) { 171 // all good 172 } 173 } 174 175 public void int_gee_collection() throws GLib.Error { 176#if !VALA_0_50 177 skip("Collections containing non-pointer values not currently supported. See GNOME/vala#992"); 178#endif 179 var intv = new int[] { 42, 1337 }; 180 assert_collection(new_gee_collection(intv)) 181 .is_non_empty() 182 .size(2) 183 .contains(42) 184 .not_contains(-1) 185 .first_is(42) 186 .at_index_is(1, 1337); 187 188 assert_collection(new_gee_collection(new int[0])) 189 .is_empty() 190 .size(0) 191 .not_contains(42); 192 193 try { 194 assert_collection(new_gee_collection(new int[0])).is_non_empty(); 195 fail("Expected ::is_non_empty to fail"); 196 } catch (ValaUnit.TestError.FAILED err) { 197 // all good 198 } 199 200 try { 201 assert_collection(new_gee_collection(intv)).is_empty(); 202 fail("Expected ::is_empty to fail"); 203 } catch (ValaUnit.TestError.FAILED err) { 204 // all good 205 } 206 207 try { 208 assert_collection(new_gee_collection(intv)).contains(-1); 209 fail("Expected ::contains to fail"); 210 } catch (ValaUnit.TestError.FAILED err) { 211 // all good 212 } 213 } 214 215 private Gee.Collection<T> new_gee_collection<T>(T[] values) { 216 return new Gee.ArrayList<T>.wrap(values); 217 } 218 219} 220