Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

Switch to assert-style tests (#260)

authored by

Joel Drapper and committed by
GitHub
b563e0e8 951291df

+534 -398
+1 -1
Gemfile
··· 4 4 5 5 gemspec 6 6 7 - gem "quickdraw", git: "https://github.com/joeldrapper/quickdraw.git" 7 + gem "quickdraw", git: "https://github.com/joeldrapper/quickdraw.git", branch: "assert-style" 8 8 gem "benchmark-ips" 9 9 10 10 group :development do
+5 -2
lib/literal/array.rb
··· 276 276 end 277 277 278 278 def inspect 279 - @__value__.inspect 279 + "Literal::Array(#{@__type__.inspect})#{@__value__.inspect}" 280 280 end 281 281 282 282 def intersect?(other) ··· 497 497 498 498 def select!(...) 499 499 @__value__.select!(...) 500 + self 500 501 end 501 502 502 503 def shift(...) ··· 535 536 536 537 alias_method :to_ary, :to_a 537 538 538 - alias_method :to_s, :inspect 539 + def to_s 540 + @__value__.to_s 541 + end 539 542 540 543 def uniq 541 544 __with__(@__value__.uniq)
+7
lib/literal/failure.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Failure < Literal::Result 4 + def initialize(error) 5 + @error = error 6 + end 7 + end
+4
lib/literal/result.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Listeral::Result 4 + end
+7
lib/literal/success.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Success < Literal::Result 4 + def initialize(value) 5 + @value = value 6 + end 7 + end
+4
lib/literal/tuple.rb
··· 57 57 end 58 58 59 59 attr_reader :__values__, :__types__ 60 + 61 + def ==(other) 62 + Literal::Tuple === other && @__values__ == other.__values__ 63 + end 60 64 end
test/allocations_test.rb allocations_test.rb
+274 -164
test/array.test.rb
··· 21 21 end 22 22 23 23 test "#initialize" do 24 - expect { 24 + assert_raises(Literal::TypeError) do 25 25 Literal::Array(String).new(1, 2, 3) 26 - }.to_raise(Literal::TypeError) 26 + end 27 27 end 28 28 29 29 test "#to_a" do 30 30 array = Literal::Array(Integer).new(1, 2, 3) 31 - expect(array.to_a) == [1, 2, 3] 31 + assert_equal array.to_a, [1, 2, 3] 32 + end 32 33 33 - internal_array = array.__value__ 34 - refute internal_array.equal?(array.to_a) 34 + test "#to_a doesn't return the internal array" do 35 + array = Literal::Array(Integer).new(1, 2, 3) 36 + refute_same array.__value__, array.to_a 35 37 end 36 38 37 39 test "#map maps each item correctly" do 38 40 array = Literal::Array(Integer).new(1, 2, 3) 39 41 40 - expect( 41 - array.map(String, &:to_s).to_a 42 - ) == ["1", "2", "3"] 42 + mapped = array.map(String, &:to_s) 43 + assert_equal mapped.to_a, ["1", "2", "3"] 43 44 end 44 45 45 46 test "#map raises if the type is wrong" do 46 47 array = Literal::Array(Integer).new(1, 2, 3) 47 48 48 - expect { 49 + assert_raises(Literal::TypeError) do 49 50 array.map(Integer, &:to_s) 50 - }.to_raise(Literal::TypeError) 51 + end 51 52 end 52 53 53 54 test "#map! maps each item correctly" do 54 55 array = Literal::Array(Integer).new(1, 2, 3) 55 56 56 - expect(array.map!(&:succ).to_a) == [2, 3, 4] 57 + array.map!(&:succ) 58 + 59 + assert_equal array.to_a, [2, 3, 4] 57 60 end 58 61 59 62 test "map! raises if the type is wrong" do 60 63 array = Literal::Array(Integer).new(1, 2, 3) 61 64 62 - expect { array.map!(&:to_s) }.to_raise(Literal::TypeError) 65 + assert_raises(Literal::TypeError) do 66 + array.map!(&:to_s) 67 + end 63 68 end 64 69 65 70 test "#[]" do 66 71 array = Literal::Array(Integer).new(1, 2, 3) 67 72 68 - expect(array[0]) == 1 69 - expect(array[1]) == 2 70 - expect(array[2]) == 3 71 - expect(array[3]) == nil 73 + assert_equal array[0], 1 74 + assert_equal array[1], 2 75 + assert_equal array[2], 3 76 + assert_equal array[3], nil 72 77 end 73 78 74 79 test "#[]= raises if the type is wrong" do 75 80 array = Literal::Array(Integer).new(1, 2, 3) 76 81 77 - expect { array[0] = "1" }.to_raise(Literal::TypeError) 82 + assert_raises Literal::TypeError do 83 + array[0] = "1" 84 + end 78 85 end 79 86 80 87 test "#[]= works as expected" do ··· 83 90 array[0] = 5 84 91 array[3] = 6 85 92 86 - expect(array[0]) == 5 87 - expect(array[3]) == 6 93 + assert_equal array[0], 5 94 + assert_equal array[3], 6 88 95 end 89 96 90 97 test "#== compares the Literal::Arrays" do 91 - array = Literal::Array(Integer).new(1, 2, 3) 92 - other = Literal::Array(Integer).new(1, 2, 3) 98 + array = Literal::Array(Integer).new(1, 2, 3) 99 + other = Literal::Array(Integer).new(1, 2, 3) 93 100 94 - expect(array == other) == true 101 + assert_equal (array == other), true 95 102 end 96 103 97 104 test "#<< inserts a new item" do ··· 99 106 100 107 array << 4 101 108 102 - expect(array.to_a) == [1, 2, 3, 4] 109 + assert_equal array.to_a, [1, 2, 3, 4] 103 110 end 104 111 105 112 test "#<< raises if the type is wrong" do 106 113 array = Literal::Array(Integer).new(1, 2, 3) 107 114 108 - expect { array << "4" }.to_raise(Literal::TypeError) 115 + assert_raises(Literal::TypeError) do 116 + array << "4" 117 + end 109 118 end 110 119 111 120 test "#& performs bitwise AND with another Literal::Array" do 112 121 array = Literal::Array(Integer).new(1, 2, 3) 113 122 other = Literal::Array(Integer).new(2, 3, 4) 114 123 115 - expect((array & other).to_a) == [2, 3] 124 + assert_equal (array & other).to_a, [2, 3] 116 125 end 117 126 118 - test "#& performs bitwise AND with an Array" do 127 + test "#& performs bitwise AND with a regular Array" do 119 128 array = Literal::Array(Integer).new(1, 2, 3) 120 129 other = [2, 3, 4] 121 130 122 - expect((array & other).to_a) == [2, 3] 131 + assert_equal (array & other).to_a, [2, 3] 123 132 end 124 133 125 134 test "#* with an integer multiplies the array" do 126 135 array = Literal::Array(Integer).new(1, 2, 3) 127 136 128 137 result = array * 2 138 + 129 139 assert Literal::Array(Integer) === result 130 - expect(result.to_a) == [1, 2, 3, 1, 2, 3] 140 + assert_equal result.to_a, [1, 2, 3, 1, 2, 3] 131 141 end 132 142 133 143 test "#* raises with a negative integer" do 134 144 array = Literal::Array(Integer).new(1, 2, 3) 135 145 136 - expect { array * -1 }.to_raise(ArgumentError) 146 + assert_raises(ArgumentError) do 147 + array * -1 148 + end 137 149 end 138 150 139 151 test "#* with a string joins the elements" do 140 152 array = Literal::Array(Integer).new(1, 2, 3) 153 + result = array * "," 141 154 142 - expect(array * ",") == "1,2,3" 155 + assert_equal result, "1,2,3" 143 156 end 144 157 145 158 test "#+ adds another Literal::Array" do ··· 148 161 149 162 result = array + other 150 163 assert Literal::Array(Integer) === result 151 - expect(result.to_a) == [1, 2, 3, 4, 5] 164 + assert_equal result.to_a, [1, 2, 3, 4, 5] 152 165 end 153 166 154 167 test "#+ adds an array" do ··· 157 170 158 171 result = array + other 159 172 assert Literal::Array(Integer) === result 160 - expect(result.to_a) == [1, 2, 3, 4, 5] 173 + assert_equal result.to_a, [1, 2, 3, 4, 5] 161 174 end 162 175 163 176 test "#+ raises if the type is wrong" do ··· 165 178 other = Literal::Array(String).new("a", "b") 166 179 other_primitive = ["a", "b"] 167 180 168 - expect { array + other }.to_raise(Literal::TypeError) 169 - expect { array + other_primitive }.to_raise(Literal::TypeError) 181 + assert_raises(Literal::TypeError) do 182 + array + other 183 + end 184 + 185 + assert_raises(Literal::TypeError) do 186 + array + other_primitive 187 + end 170 188 end 171 189 172 190 test "#- removes elements from another Literal::Array" do ··· 174 192 other = Literal::Array(Integer).new(1) 175 193 176 194 result = array - other 177 - expect(result.to_a) == [2, 3] 195 + 196 + assert_equal result.to_a, [2, 3] 178 197 end 179 198 180 199 test "#- removes elements from an array" do ··· 182 201 other = [1] 183 202 184 203 result = array - other 185 - expect(result.to_a) == [2, 3] 204 + assert_equal result.to_a, [2, 3] 186 205 end 187 206 188 207 test "#<=> works as expected" do 189 208 array = Literal::Array(Integer).new(1, 2, 3) 190 209 191 - expect(array <=> [1, 2, 4]) == -1 192 - expect(array <=> [1, 2, 2]) == 1 193 - expect(array <=> [1, 2, 3, 4]) == -1 194 - expect(array <=> [1, 2]) == 1 195 - expect(array <=> [1, 2, 3]) == 0 210 + assert_equal (array <=> [1, 2, 4]), -1 211 + assert_equal (array <=> [1, 2, 2]), 1 212 + assert_equal (array <=> [1, 2, 3, 4]), -1 213 + assert_equal (array <=> [1, 2]), 1 214 + assert_equal (array <=> [1, 2, 3]), 0 196 215 end 197 216 198 217 test "#<=> works with another Literal::Array" do 199 218 array = Literal::Array(Integer).new(1, 2, 3) 200 219 other = Literal::Array(Integer).new(1, 2, 4) 201 220 202 - expect(array <=> other) == -1 221 + assert_equal (array <=> other), -1 203 222 end 204 223 205 224 test "#sort sorts the array" do 206 225 array = Literal::Array(Integer).new(3, 2, 1) 207 226 208 227 result = array.sort 228 + 209 229 assert Literal::Array(Integer) === result 210 - expect(array.sort.to_a) == [1, 2, 3] 230 + assert_equal result.to_a, [1, 2, 3] 211 231 end 212 232 213 233 test "#push appends single value" do 214 234 array = Literal::Array(Integer).new(1, 2, 3) 215 235 216 - expect((array.push(4)).to_a) == [1, 2, 3, 4] 236 + return_value = array.push(4) 237 + 238 + assert_same return_value, array 239 + assert_equal array.to_a, [1, 2, 3, 4] 217 240 end 218 241 219 242 test "#push appends multiple values" do 220 243 array = Literal::Array(Integer).new(1, 2, 3) 221 244 222 - expect((array.push(4, 5)).to_a) == [1, 2, 3, 4, 5] 245 + return_value = array.push(4, 5) 246 + 247 + assert_same return_value, array 248 + assert_equal array.to_a, [1, 2, 3, 4, 5] 223 249 end 224 250 225 251 test "#push raises if any type is wrong" do 226 252 array = Literal::Array(Integer).new(1, 2, 3) 227 253 228 - expect { array.push("4") }.to_raise(Literal::TypeError) 229 - expect { array.push(4, "5") }.to_raise(Literal::TypeError) 254 + assert_raises(Literal::TypeError) do 255 + array.push("4") 256 + end 257 + 258 + assert_raises(Literal::TypeError) do 259 + array.push(4, "5") 260 + end 230 261 end 231 262 232 263 test "#assoc returns the correct element" do 233 264 array = Literal::Array(Array).new([1, 2], [3, 4]) 234 265 235 - expect(array.assoc(1)) == [1, 2] 236 - expect(array.assoc(3)) == [3, 4] 266 + assert_equal array.assoc(1), [1, 2] 267 + assert_equal array.assoc(3), [3, 4] 237 268 end 238 269 239 270 test "#combination yields to the block" do ··· 241 272 results = [] 242 273 243 274 array.combination(2) { |x| results << x } 244 - expect(results) == [[1, 2], [1, 3], [2, 3]] 275 + 276 + assert_equal results, [[1, 2], [1, 3], [2, 3]] 245 277 end 246 278 247 279 test "#combination returns self" do 248 280 array = Literal::Array(Integer).new(1, 2, 3) 281 + return_value = array.combination(0) { nil } 249 282 250 - expect(array.combination(0) { nil }) == array 283 + assert_same return_value, array 251 284 end 252 285 253 286 test "#compact returns a new Literal::Array" do 254 287 array = Literal::Array(Integer).new(1, 2, 3) 255 288 256 - expect(array.compact.to_a) == [1, 2, 3] 257 - assert Literal::Array(Integer) === array.compact 289 + result = array.compact 290 + 291 + refute_same array, result 292 + assert Literal::Array(Integer) === result 293 + assert_equal result.to_a, [1, 2, 3] 258 294 end 259 295 260 296 test "#compact! returns nil" do 261 297 array = Literal::Array(Integer).new(1, 2, 3) 298 + return_value = array.compact! 262 299 263 - expect(array.compact!) == nil 300 + assert_equal return_value, nil 264 301 end 265 302 266 303 test "#delete deletes the element" do 267 304 array = Literal::Array(Integer).new(1, 2, 3) 268 305 269 - array.delete(2) 270 - expect(array.to_a) == [1, 3] 271 - end 306 + return_value = array.delete(2) 272 307 273 - test "#delete returns the deleted element" do 274 - array = Literal::Array(Integer).new(1, 2, 3) 275 - 276 - expect(array.delete(2)) == 2 308 + assert_equal return_value, 2 309 + assert_equal array, Literal::Array(Integer).new(1, 3) 277 310 end 278 311 279 312 test "#delete_at deletes the element at the index" do 280 313 array = Literal::Array(Integer).new(1, 2, 3) 281 314 282 - array.delete_at(1) 283 - expect(array.to_a) == [1, 3] 284 - end 315 + return_value = array.delete_at(1) 285 316 286 - test "#delete_at returns the deleted element" do 287 - array = Literal::Array(Integer).new(1, 2, 3) 288 - 289 - expect(array.delete_at(1)) == 2 317 + assert_equal return_value, 2 318 + assert_equal array, Literal::Array(Integer).new(1, 3) 290 319 end 291 320 292 321 test "#delete_if deletes elements that match the block" do 293 322 array = Literal::Array(Integer).new(1, 2, 3) 294 323 295 - array.delete_if { |i| i < 2 } 296 - expect(array.to_a) == [2, 3] 324 + return_value = array.delete_if { |i| i < 2 } 325 + 326 + assert_same return_value, array 327 + assert_equal array, Literal::Array(Integer).new(2, 3) 297 328 end 298 329 299 330 test "#drop returns a new array with the first n elements removed" do 300 - array = Literal::Array(Integer).new(1, 2, 3) 301 - dropped = array.drop(1) 331 + array = Literal::Array(Integer).new(1, 2, 3) 332 + dropped = array.drop(1) 302 333 303 - expect(dropped.to_a) == [2, 3] 304 - expect(dropped) == Literal::Array(Integer).new(2, 3) 305 - 306 - dropped = array.drop(2) 307 - expect(dropped.to_a) == [3] 308 - expect(dropped) == Literal::Array(Integer).new(3) 334 + refute_same dropped, array 335 + assert Literal::Array(Integer) === dropped 336 + assert_equal dropped, Literal::Array(Integer).new(2, 3) 309 337 end 310 338 311 339 test "#drop_while returns a new array with the first n elements removed" do 312 - array = Literal::Array(Integer).new(1, 2, 3) 313 - dropped = array.drop_while { |i| i < 2 } 340 + array = Literal::Array(Integer).new(1, 2, 3) 341 + dropped = array.drop_while { |i| i < 2 } 314 342 315 - expect(dropped.to_a) == [2, 3] 316 - expect(dropped) == Literal::Array(Integer).new(2, 3) 343 + refute_same dropped, array 344 + assert Literal::Array(Integer) === dropped 345 + assert_equal dropped, Literal::Array(Integer).new(2, 3) 317 346 end 318 347 319 348 test "#insert inserts single element at index offset" do 320 349 array = Literal::Array(Integer).new(1, 2, 3) 321 350 322 - expect((array.insert(1, 4)).to_a) == [1, 4, 2, 3] 351 + return_value = array.insert(1, 4) 352 + 353 + assert_same return_value, array 354 + assert_equal array, Literal::Array(Integer).new(1, 4, 2, 3) 323 355 end 324 356 325 357 test "#insert inserts multiple elements at index offset" do 326 358 array = Literal::Array(Integer).new(1, 2, 3) 327 359 328 - expect((array.insert(1, 4, 5, 6)).to_a) == [1, 4, 5, 6, 2, 3] 360 + return_value = array.insert(1, 4, 5, 6) 361 + 362 + assert_same return_value, array 363 + assert_equal array, Literal::Array(Integer).new(1, 4, 5, 6, 2, 3) 329 364 end 330 365 331 366 test "#insert raises if any type is wrong" do 332 367 array = Literal::Array(Integer).new(1, 2, 3) 333 368 334 - expect { array.insert(1, "4") }.to_raise(Literal::TypeError) 335 - expect { array.insert(1, 4, "5", 6) }.to_raise(Literal::TypeError) 369 + assert_raises(Literal::TypeError) do 370 + array.insert(1, "4") 371 + end 372 + 373 + assert_raises(Literal::TypeError) do 374 + array.insert(1, 4, "5", 6) 375 + end 336 376 end 337 377 338 378 test "#intersect? returns true if the arrays intersect" do ··· 350 390 351 391 intersection = array.intersection(other, [2]) 352 392 353 - assert Literal::Array(Integer) === intersection 354 - expect(intersection.to_a) == [2] 393 + assert_equal intersection, Literal::Array(Integer).new(2) 355 394 end 356 395 357 396 test "#join joins the elements into a string" do 358 397 array = Literal::Array(Integer).new(1, 2, 3) 359 398 360 - expect(array.join(", ")) == "1, 2, 3" 361 - expect(array.join) == "123" 399 + assert_equal array.join, "123" 400 + assert_equal array.join(", "), "1, 2, 3" 362 401 end 363 402 364 403 test "#keep_if keeps elements that match the block" do 365 404 array = Literal::Array(Integer).new(1, 2, 3) 366 405 367 - expect((array.keep_if { |i| i > 1 }).to_a) == [2, 3] 406 + return_value = array.keep_if { |i| i > 1 } 407 + 408 + assert_same return_value, array 409 + assert_equal array, Literal::Array(Integer).new(2, 3) 368 410 end 369 411 370 412 test "#keep_if returns an enumerator if no block is given" do 371 413 array = Literal::Array(Integer).new(1, 2, 3) 372 414 373 - expect(array.keep_if.class.name) == "Enumerator" 415 + return_value = array.keep_if 416 + 417 + assert_equal return_value.class, Enumerator 374 418 end 375 419 376 - test "#replace replaces with passed in array" do 420 + test "#replace replaces with regular Array" do 377 421 array = Literal::Array(Integer).new(1, 2, 3) 378 422 379 - expect((array.replace([4, 5, 6])).to_a) == [4, 5, 6] 423 + return_value = array.replace([4, 5, 6]) 424 + 425 + assert_same return_value, array 426 + assert_equal array, Literal::Array(Integer).new(4, 5, 6) 380 427 end 381 428 382 - test "#replace replaces with passed in Literal::Array" do 429 + test "#replace replaces with Literal::Array" do 383 430 array = Literal::Array(Integer).new(1, 2, 3) 384 431 other = Literal::Array(Integer).new(4, 5, 6) 385 432 386 - expect((array.replace(other)).to_a) == [4, 5, 6] 433 + return_value = array.replace(other) 434 + 435 + assert_same return_value, array 436 + refute_same array, other 437 + assert_equal array, other 387 438 end 388 439 389 440 test "#replace raises if type of any element in array is wrong" do 390 441 array = Literal::Array(Integer).new(1, 2, 3) 391 442 392 - expect { array.replace([1, "4"]) }.to_raise(Literal::TypeError) 393 - expect { array.replace(Literal::Array(String).new("4", "5")) }.to_raise(Literal::TypeError) 443 + assert_raises(Literal::TypeError) do 444 + array.replace([1, "4"]) 445 + end 446 + 447 + assert_raises(Literal::TypeError) do 448 + array.replace( 449 + Literal::Array(String).new 450 + ) 451 + end 394 452 end 395 453 396 454 test "#replace raises with non-array argument" do 397 455 array = Literal::Array(Integer).new(1, 2, 3) 398 456 399 - expect { array.replace("not an array") }.to_raise(ArgumentError) 457 + assert_raises(ArgumentError) do 458 + array.replace("not an array") 459 + end 400 460 end 401 461 402 462 test "#values_at returns the values at the given indexes" do 403 463 array = Literal::Array(Integer).new(1, 2, 3) 404 464 405 - expect(array.values_at(0).to_a) == [1] 406 - expect(array.values_at(1).to_a) == [2] 407 - expect(array.values_at(2).to_a) == [3] 408 - expect(array.values_at(1..2).to_a) == [2, 3] 465 + assert_equal array.values_at(0), Literal::Array(Integer).new(1) 466 + assert_equal array.values_at(1), Literal::Array(Integer).new(2) 467 + assert_equal array.values_at(2), Literal::Array(Integer).new(3) 468 + assert_equal array.values_at(1..2), Literal::Array(Integer).new(2, 3) 409 469 410 - expect { array.values_at(3) }.to_raise(IndexError) 411 - expect { array.values_at(-4) }.to_raise(IndexError) 412 - expect { array.values_at(-4..2) }.to_raise(IndexError) 413 - expect { array.values_at(1..3) }.to_raise(IndexError) 470 + assert_raises IndexError do 471 + array.values_at(3) 472 + end 414 473 415 - nilable_array = Literal::Array(_Nilable(Integer)).new(1, 2, 3) 474 + assert_raises IndexError do 475 + array.values_at(-4) 476 + end 416 477 417 - expect(nilable_array.values_at(-4).to_a) == [nil] 418 - expect(nilable_array.values_at(3).to_a) == [nil] 478 + assert_raises IndexError do 479 + array.values_at(-4..2) 480 + end 481 + 482 + assert_raises IndexError do 483 + array.values_at(1..3) 484 + end 485 + end 486 + 487 + test "#values_at on a nilable array returns the values at the given indexes" do 488 + array = Literal::Array(_Nilable(Integer)).new(1, 2, 3) 489 + 490 + # TODO: We could do some type narrowing here. 491 + assert_equal array.values_at(-4), Literal::Array(_Nilable(Integer)).new(nil) 492 + assert_equal array.values_at(3), Literal::Array(_Nilable(Integer)).new(nil) 419 493 end 420 494 421 495 test "#uniq! removes duplicates" do 422 496 array = Literal::Array(Integer).new(1, 2, 3, 2, 1) 423 497 424 - expect((array.uniq!).to_a) == [1, 2, 3] 498 + return_value = array.uniq! 499 + 500 + assert_same return_value, array 501 + assert_equal array, Literal::Array(Integer).new(1, 2, 3) 425 502 end 426 503 427 504 test "#uniq! returns nil if no duplicates" do 428 505 array = Literal::Array(Integer).new(1, 2, 3) 429 506 430 - expect(array.uniq!) == nil 507 + return_value = array.uniq! 508 + 509 + assert_equal return_value, nil 431 510 end 432 511 433 512 test "#uniq returns a new array with duplicates removed" do 434 513 array = Literal::Array(Integer).new(1, 2, 2, 3, 3, 3) 435 514 436 - expect((array.uniq).to_a) == [1, 2, 3] 437 - expect(array.uniq) == Literal::Array(Integer).new(1, 2, 3) 515 + return_value = array.uniq 516 + 517 + refute_same return_value, array 518 + assert_equal return_value, Literal::Array(Integer).new(1, 2, 3) 438 519 end 439 520 440 521 test "#| returns a union of two Literal::Arrays" do ··· 443 524 444 525 union = array | other 445 526 446 - assert Literal::Array(Integer) === union 447 - expect(union.to_a) == [1, 2, 3, 4] 527 + refute_same union, array 528 + refute_same union, other 529 + 530 + assert_equal union, Literal::Array(Integer).new(1, 2, 3, 4) 448 531 end 449 532 450 533 test "#| returns a union of a Literal::Array and an Array" do ··· 453 536 454 537 union = array | other 455 538 456 - assert Literal::Array(Integer) === union 457 - expect(union.to_a) == [1, 2, 3, 4] 539 + assert_equal union, Literal::Array(Integer).new(1, 2, 3, 4) 458 540 end 459 541 460 542 test "#| raises if the type is wrong" do ··· 462 544 other = Literal::Array(String).new("2", "3") 463 545 other_primitive = ["2", "3"] 464 546 465 - expect { array | other }.to_raise(Literal::TypeError) 466 - expect { array | other_primitive }.to_raise(Literal::TypeError) 547 + assert_raises(Literal::TypeError) do 548 + array | other 549 + end 550 + 551 + assert_raises(Literal::TypeError) do 552 + array | other_primitive 553 + end 467 554 end 468 555 469 556 test "#sum" do 470 - expect(Literal::Array(Integer).new(1, 2, 3).sum) == 6 471 - expect(Literal::Array(String).new("1", "2", "3").sum(&:to_i)) == 6 557 + assert_equal Literal::Array(Integer).new(1, 2, 3).sum, 6 558 + assert_equal Literal::Array(String).new("1", "2", "3").sum(&:to_i), 6 472 559 end 473 560 474 561 test "#select returns a new array with elements that match the block" do 475 562 array = Literal::Array(Integer).new(1, 2, 3) 476 563 477 - expect((array.select { |i| i > 1 }).to_a) == [2, 3] 564 + return_value = array.select { |i| i > 1 } 565 + 566 + refute_same return_value, array 567 + assert_equal return_value, Literal::Array(Integer).new(2, 3) 478 568 end 479 569 480 570 test "#select! removes elements that do not match the block" do 481 571 array = Literal::Array(Integer).new(1, 2, 3) 482 572 483 - expect((array.select! { |i| i > 1 }).to_a) == [2, 3] 573 + return_value = array.select! { |i| i > 1 } 574 + 575 + assert_same return_value, array 576 + assert_equal array, Literal::Array(Integer).new(2, 3) 484 577 end 485 578 486 - test "#select! returns [] if no elements match the block" do 579 + test "#select! empties the array if no elements match the block" do 487 580 array = Literal::Array(Integer).new(1, 2, 3) 488 581 489 - expect(array.select! { |i| i > 4 }) == [] 582 + return_value = array.select! { |i| i > 4 } 583 + 584 + assert_same return_value, array 585 + assert_equal return_value, Literal::Array(Integer).new 490 586 end 491 587 492 588 test "#narrow" do 493 589 array = Literal::Array(Numeric).new(1, 2, 3) 494 590 495 - result = array.narrow(Integer) 496 - assert Literal::Array(Integer) === result 591 + return_value = array.narrow(Integer) 592 + assert Literal::Array(Integer) === return_value 497 593 end 498 594 499 595 test "#narrow with same type" do ··· 505 601 test "#narrow with wrong value" do 506 602 array = Literal::Array(Numeric).new(1, 2, 3.456) 507 603 508 - expect { 604 + assert_raises(Literal::TypeError) do 509 605 array.narrow(Integer) 510 - }.to_raise(Literal::TypeError) 606 + end 511 607 end 512 608 513 609 test "#narrow with wrong type" do 514 610 array = Literal::Array(Integer).new(1, 2, 3) 515 611 516 - expect { 612 + assert_raises(ArgumentError) do 517 613 array.narrow(Numeric) 518 - }.to_raise(ArgumentError) 614 + end 519 615 end 520 616 521 617 test "#flatten! flattens the array" do 522 618 array = Literal::Array(Array).new([1, 2], [3, 4]) 523 619 524 - expect((array.flatten!).to_a) == [1, 2, 3, 4] 620 + return_value = array.flatten! 621 + 622 + assert_same return_value, array 623 + assert_equal return_value, Literal::Array(Integer).new(1, 2, 3, 4) 525 624 end 526 625 527 626 test "#flatten! returns nil if no nested arrays" do 528 627 array = Literal::Array(Integer).new(1, 2, 3) 529 628 530 - expect(array.flatten!) == nil 629 + return_value = array.flatten! 630 + 631 + assert_equal return_value, nil 531 632 end 532 633 533 634 test "#flatten flattens the array" do 534 635 array = Literal::Array(Array).new([1, 2], [3, 4]) 535 636 536 - expect(array.flatten.to_a) == [1, 2, 3, 4] 637 + return_value = array.flatten 638 + 639 + refute_same return_value, array 640 + assert_equal return_value, Literal::Array(Integer).new(1, 2, 3, 4) 537 641 end 538 642 539 643 test "#flatten with level flattens the array" do 540 644 array = Literal::Array(Array).new([1, 2], [3, 4]) 541 645 542 - expect(array.flatten(1).to_a) == [1, 2, 3, 4] 646 + return_value = array.flatten(1) 647 + 648 + refute_same return_value, array 649 + assert_equal return_value, Literal::Array(Integer).new(1, 2, 3, 4) 543 650 end 544 651 545 652 test "#fetch" do 546 653 array = Literal::Array(Integer).new(1, 2, 3) 547 654 548 - expect(array.fetch(0)) == 1 549 - expect(array.fetch(1)) == 2 550 - expect(array.fetch(2)) == 3 551 - expect { array.fetch(3) }.to_raise(IndexError) 655 + assert_equal array.fetch(0), 1 656 + assert_equal array.fetch(1), 2 657 + assert_equal array.fetch(2), 3 658 + 659 + assert_raises(IndexError) { array.fetch(3) } 552 660 end 553 661 554 662 test "#inspect returns a string representation of the array" do 555 663 array = Literal::Array(Integer).new(1, 2, 3) 556 664 557 - expect(array.inspect) == "[1, 2, 3]" 665 + assert_equal array.inspect, "Literal::Array(Integer)[1, 2, 3]" 558 666 end 559 667 560 668 test "#to_s returns a string representation of the array" do 561 669 array = Literal::Array(Integer).new(1, 2, 3) 562 670 563 - expect(array.to_s) == "[1, 2, 3]" 671 + assert_equal array.to_s, "[1, 2, 3]" 564 672 end 565 673 566 674 test "#fetch returns default value if element is missing at index" do 567 675 array = Literal::Array(Integer).new(1, 2, 3) 568 676 569 - expect(array.fetch(4, :missing)) == :missing 677 + assert_equal array.fetch(4, :missing), :missing 570 678 end 571 679 572 680 test "#fetch returns value of block if element is missing at index" do 573 681 array = Literal::Array(Integer).new(1, 2, 3) 574 682 575 - expect(array.fetch(4) { |index| index * 2 }) == 8 683 + assert_equal array.fetch(4) { |index| index * 2 }, 8 576 684 end 577 685 578 686 test "#include? returns true if array contains value" do ··· 585 693 test "#rotate! rotates the array" do 586 694 array = Literal::Array(Integer).new(1, 2, 3) 587 695 588 - result = array.rotate! 696 + return_value = array.rotate! 589 697 590 - assert result.equal?(array) 591 - expect(result.to_a) == [2, 3, 1] 698 + assert_same return_value, array 699 + assert_equal array, Literal::Array(Integer).new(2, 3, 1) 592 700 end 593 701 594 702 test "#rotate rotates the array" do 595 703 array = Literal::Array(Integer).new(1, 2, 3) 596 704 597 - result = array.rotate 598 - assert Literal::Array(Integer) === result 599 - expect(result.to_a) == [2, 3, 1] 600 - expect(array.to_a) == [1, 2, 3] 705 + return_value = array.rotate 706 + 707 + refute_same return_value, array 708 + assert_equal return_value, Literal::Array(Integer).new(2, 3, 1) 601 709 end 602 710 603 711 test "#take takes the first n elements" do 604 712 array = Literal::Array(Integer).new(1, 2, 3, 4, 5) 605 713 606 - result = array.take(2) 607 - assert Literal::Array(Integer) === result 608 - expect(result.to_a) == [1, 2] 609 - expect(array.to_a) == [1, 2, 3, 4, 5] 714 + return_value = array.take(2) 715 + 716 + refute_same return_value, array 717 + assert_equal return_value, Literal::Array(Integer).new(1, 2) 610 718 end 611 719 612 720 test "#shuffle returns a new shuffled array" do 613 721 array = Literal::Array(Integer).new(1, 2, 3, 4, 5) 614 722 random = Random.new(42) 615 723 616 - result = array.shuffle(random:) 617 - assert Literal::Array(Integer) === result 618 - expect(result.to_a) == [2, 5, 3, 1, 4] 724 + return_value = array.shuffle(random:) 725 + 726 + assert_equal return_value, Literal::Array(Integer).new(2, 5, 3, 1, 4) 619 727 end 620 728 621 729 test "#shuffle! shuffles the array" do ··· 623 731 random = Random.new(42) 624 732 625 733 result = array.shuffle!(random:) 626 - assert array.equal?(result) 627 - expect(result.to_a) == [2, 5, 3, 1, 4] 734 + 735 + assert_same result, array 736 + assert_equal result, Literal::Array(Integer).new(2, 5, 3, 1, 4) 628 737 end 629 738 630 739 test "#product with block" do ··· 635 744 636 745 result = a.product(b) { |x, y| yielded << [x, y] } 637 746 638 - assert result.equal?(a) 639 - expect(yielded) == [[1, "a"], [1, "b"], [2, "a"], [2, "b"]] 747 + assert_same result, a 748 + assert_equal yielded, [[1, "a"], [1, "b"], [2, "a"], [2, "b"]] 640 749 end 641 750 642 751 test "#product with another Literal::Array" do ··· 646 755 result = a.product(b) 647 756 648 757 assert Literal::Array(Literal::Tuple(Integer, String)) === result 649 - expect(result.size) == 4 650 - expect(result.first.__values__) == [1, "a"] 758 + 759 + assert_equal result.size, 4 760 + assert_equal result.first, Literal::Tuple(Integer, String).new(1, "a") 651 761 end
+17 -18
test/data.test.rb
··· 6 6 7 7 class Empty < Literal::Data 8 8 end 9 - 10 9 test "properties have readers by default" do 11 10 person = Person.new(name: "John") 12 - expect(person.name) == "John" 11 + assert_equal(person.name, "John") 13 12 end 14 13 15 14 test "data objects are frozen" do 16 15 person = Person.new(name: "John") 17 - expect(person).to_be(:frozen?) 16 + assert_equal(person.frozen?, true) 18 17 end 19 18 20 19 test "immutable attributes are not duplicated" do 21 20 name = "John" 22 21 person = Person.new(name:) 23 22 24 - expect(person.name).to_be(:frozen?) 25 - expect(person.name).to_equal?(name) 23 + assert_equal(person.name.frozen?, true) 24 + assert_equal(person.name, name) 26 25 end 27 26 28 27 test "to_h" do 29 28 person = Person.new(name: "John") 30 - expect(person.to_h) == { name: "John" } 29 + assert_equal(person.to_h, { name: "John" }) 31 30 end 32 31 33 32 test "can be deconstructed" do 34 33 person = Person.new(name: "John") 35 - expect(person.deconstruct) == ["John"] 34 + assert_equal(person.deconstruct, ["John"]) 36 35 end 37 36 38 37 test "can be deconstructed with keys" do 39 38 person = Person.new(name: "John") 40 - expect(person.deconstruct_keys([:name])) == { name: "John" } 39 + assert_equal(person.deconstruct_keys([:name]), { name: "John" }) 41 40 end 42 41 43 42 test "can be used as a hash key" do 44 43 person = Person.new(name: "John") 45 44 person2 = Person.new(name: "Bob") 46 45 hash = { person => "John", person2 => "Bob" } 47 - expect(hash[person]) == "John" 48 - expect(hash[person2]) == "Bob" 49 - expect(hash[Person.new(name: "John")]) == "John" 46 + assert_equal(hash[person], "John") 47 + assert_equal(hash[person2], "Bob") 48 + assert_equal(hash[Person.new(name: "John")], "John") 50 49 end 51 50 52 51 test "empty" do 53 52 empty = Empty.new 54 - expect(empty.to_h) == {} 53 + assert_equal(empty.to_h, {}) 55 54 56 55 other = Empty.new 57 - expect(empty) == other 58 - expect(empty).to_eql?(other) 59 - expect(empty.hash) == other.hash 56 + assert_equal(empty, other) 57 + assert_equal(empty.eql?(other), true) 58 + assert_equal(empty.hash, other.hash) 60 59 61 60 other_empty = Class.new(Literal::Data).new 62 - expect(empty) != other_empty 63 - expect(empty).not_to_eql?(other_empty) 64 - expect(empty.hash) != other_empty.hash 61 + assert_equal(empty != other_empty, true) 62 + assert_equal(empty.eql?(other_empty), false) 63 + assert_equal(empty.hash != other_empty.hash, true) 65 64 end
+25 -30
test/enum.test.rb
··· 31 31 end 32 32 33 33 test do 34 - expect(Color::Red.name) =~ /Color::Red\z/ 35 - expect(Color.where(hex: "#FF0000")) == [Color::Red] 36 - expect(Color.find_by(hex: "#FF0000")) == Color::Red 37 - expect { Color.find_by(lower_hex: "#ff0000") }.to_raise(ArgumentError) 38 - expect(Color.where(lower_hex: "#ff0000")) == [Color::Red] 39 - expect(Color::Red.value) == 1 40 - expect(Color::Red.hex) == "#FF0000" 41 - expect(Color::Red.red?) == true 42 - expect(Color::Red.green?) == false 43 - expect(Color).to_be(:frozen?) 44 - expect(Color::Red).to_be(:frozen?) 45 - expect(Color[1]) == Color::Red 46 - expect(Color.cast(1)) == Color::Red 47 - expect(Color.coerce(1)) == Color::Red 48 - expect(Color.coerce(Color::Red)) == Color::Red 49 - expect(Color.to_set) == Set[Color::Red, Color::Green, Color::Blue] 50 - expect(Color.to_h) == { Color::Red => 1, Color::Green => 2, Color::Blue => 3 } 51 - expect(Color.to_a) == [Color::Red, Color::Green, Color::Blue] if RUBY_VERSION >= "3.2" 52 - expect(Color.values) == [1, 2, 3] if RUBY_VERSION >= "3.2" 53 - expect([3, 2, 1].map(&Color)) == [Color::Blue, Color::Green, Color::Red] 54 - expect([Color::Red, 2].map(&Color)) == [Color::Red, Color::Green] 34 + assert_equal([Color::Red], Color.where(hex: "#FF0000")) 35 + assert_equal(Color::Red, Color.find_by(hex: "#FF0000")) 36 + assert_raises(ArgumentError) { Color.find_by(lower_hex: "#ff0000") } 37 + assert_equal([Color::Red], Color.where(lower_hex: "#ff0000")) 38 + assert_equal(1, Color::Red.value) 39 + assert_equal("#FF0000", Color::Red.hex) 40 + assert_equal(true, Color::Red.red?) 41 + assert_equal(false, Color::Red.green?) 42 + assert_equal(true, Color.frozen?) 43 + assert_equal(true, Color::Red.frozen?) 44 + assert_equal(Color::Red, Color[1]) 45 + assert_equal(Color::Red, Color.cast(1)) 46 + assert_equal(Color::Red, Color.coerce(1)) 47 + assert_equal(Color::Red, Color.coerce(Color::Red)) 48 + assert_equal(Set[Color::Red, Color::Green, Color::Blue], Color.to_set) 49 + assert_equal({ Color::Red => 1, Color::Green => 2, Color::Blue => 3 }, Color.to_h) 50 + assert_equal([Color::Red, Color::Green, Color::Blue], Color.to_a) if RUBY_VERSION >= "3.2" 51 + assert_equal([1, 2, 3], Color.values) if RUBY_VERSION >= "3.2" 52 + assert_equal([Color::Blue, Color::Green, Color::Red], [3, 2, 1].map(&Color)) 53 + assert_equal([Color::Red, Color::Green], [Color::Red, 2].map(&Color)) 55 54 56 - expect(Switch::Off.toggle) == Switch::On 57 - expect(Switch::On.toggle) == Switch::Off 55 + assert_equal(Switch::On, Switch::Off.toggle) 56 + assert_equal(Switch::Off, Switch::On.toggle) 58 57 end 59 58 60 59 test "pattern matching" do 61 - Color::Red => Color 62 - Color::Red => Color[1] 63 - Color::Red => Color[hex: "#FF0000"] 64 - 65 - Color::Red => Color 66 - Color::Red => Color[1] 67 - Color::Red => Color::Red[hex: "#FF0000"] 60 + match { Color::Red => Color } 61 + match { Color::Red => Color[1] } 62 + match { Color::Red => Color[hex: "#FF0000"] } 68 63 end
+11 -11
test/flags.test.rb
··· 29 29 30 30 test "to bit string" do 31 31 flags = Example.new(italic: true) 32 - expect(flags.to_bit_string) == "00000010" 32 + assert_equal("00000010", flags.to_bit_string) 33 33 end 34 34 35 35 test "from bit string" do ··· 56 56 italic: true, 57 57 ) 58 58 59 - expect(flags.to_h) == { 59 + assert_equal(flags.to_h, { 60 60 bold: true, 61 61 italic: true, 62 62 underlined: false, 63 - } 63 + }) 64 64 end 65 65 66 66 test "#map (via Enumerable)" do 67 67 flags = Example.new(bold: true) 68 68 69 - expect(flags.map.to_a) == [ 69 + assert_equal(flags.map.to_a, [ 70 70 [:bold, true], 71 71 [:italic, false], 72 72 [:underlined, false], 73 - ] 73 + ]) 74 74 end 75 75 76 76 test ".from_tokens" do ··· 83 83 84 84 test "#to_tokens" do 85 85 flags = Example.new(bold: true, italic: true) 86 - expect(flags.to_tokens) == [:bold, :italic] 86 + assert_equal([:bold, :italic], flags.to_tokens) 87 87 end 88 88 89 89 test "#to_a" do 90 90 flags = Example.new(bold: true) 91 - expect(flags.to_a) == [false, false, false, false, false, false, false, true] 91 + assert_equal([false, false, false, false, false, false, false, true], flags.to_a) 92 92 end 93 93 94 94 test "#deconstruct_keys with no filter" do 95 95 flags = Example.new(bold: true) 96 96 97 - expect(flags.deconstruct_keys) == { 97 + assert_equal({ 98 98 bold: true, 99 99 italic: false, 100 100 underlined: false, 101 - } 101 + }, flags.deconstruct_keys) 102 102 end 103 103 104 104 test "#deconstruct_keys with filter" do 105 105 flags = Example.new(bold: true) 106 106 107 - expect(flags.deconstruct_keys([:bold, :underlined])) == { 107 + assert_equal({ 108 108 bold: true, 109 109 underlined: false, 110 - } 110 + }, flags.deconstruct_keys([:bold, :underlined])) 111 111 end
-5
test/literal.test.rb
··· 1 - # frozen_string_literal: true 2 - 3 - test do 4 - expect(Literal::VERSION).to_be_a String 5 - end
+111 -104
test/properties.test.rb
··· 1 1 # frozen_string_literal: true 2 2 3 - set_temporary_name("Quickdraw::Context(in #{__FILE__})") if respond_to?(:set_temporary_name) 4 - 5 3 Example = Literal::Object 6 4 7 5 test "positional params are required by default" do ··· 9 7 prop :example, String, :positional 10 8 end 11 9 12 - expect { example.new }.to_raise(ArgumentError) 13 - expect { example.new("Hello") }.not_to_raise 10 + assert_raises(ArgumentError) { example.new } 11 + refute_raises { example.new("Hello") } 14 12 end 15 13 16 14 test "keyword params are required by default" do ··· 18 16 prop :example, String 19 17 end 20 18 21 - expect { example.new }.to_raise(ArgumentError) 22 - expect { example.new(example: "Hello") }.not_to_raise 19 + assert_raises(ArgumentError) { example.new } 20 + refute_raises { example.new(example: "Hello") } 23 21 end 24 22 25 23 test "nilable positional params are optional" do ··· 27 25 prop :example, _Nilable(String), :positional 28 26 end 29 27 30 - expect { example.new }.not_to_raise 31 - expect { example.new("Hello") }.not_to_raise 28 + refute_raises { example.new } 29 + refute_raises { example.new("Hello") } 32 30 end 33 31 34 32 test "nilable keyword params are optional" do ··· 36 34 prop :example, _Nilable(String) 37 35 end 38 36 39 - expect { example.new }.not_to_raise 40 - expect { example.new(example: "Hello") }.not_to_raise 37 + refute_raises { example.new } 38 + refute_raises { example.new(example: "Hello") } 41 39 end 42 40 43 41 test "positional splats are optional" do ··· 45 43 prop :example, _Array(String), :* 46 44 end 47 45 48 - expect { example.new }.not_to_raise 49 - expect { example.new("Hello") }.not_to_raise 50 - expect { example.new("Hello", "World") }.not_to_raise 46 + refute_raises { example.new } 47 + refute_raises { example.new("Hello") } 48 + refute_raises { example.new("Hello", "World") } 51 49 end 52 50 53 51 test "keyword splats are optional" do ··· 55 53 prop :example, _Hash(Symbol, String), :** 56 54 end 57 55 58 - expect { example.new }.not_to_raise 59 - expect { example.new(example: "Hello") }.not_to_raise 60 - expect { example.new(example: "Hello", world: "World") }.not_to_raise 56 + refute_raises { example.new } 57 + refute_raises { example.new(example: "Hello") } 58 + refute_raises { example.new(example: "Hello", world: "World") } 61 59 end 62 60 63 61 test "block params are required by default" do ··· 65 63 prop :example, Proc, :& 66 64 end 67 65 68 - expect { example.new }.to_raise(Literal::TypeError) 69 - expect { example.new { "Hello" } }.not_to_raise 66 + assert_raises(Literal::TypeError) { example.new } 67 + refute_raises { example.new { "Hello" } } 70 68 end 71 69 72 70 test "nilable block params are optional" do ··· 74 72 prop :example, _Nilable(Proc), :& 75 73 end 76 74 77 - expect { example.new }.not_to_raise 78 - expect { example.new { "Hello" } }.not_to_raise 75 + refute_raises { example.new } 76 + refute_raises { example.new { "Hello" } } 79 77 end 80 78 81 79 class Person ··· 105 103 end 106 104 107 105 test "empty initializer" do 108 - expect { Empty.new }.not_to_raise 106 + refute_raises { Empty.new } 109 107 end 110 108 111 109 test do 112 110 person = Person.new("John", age: 30) 113 111 114 - expect(person.name) == "John" 115 - expect(person.age) == 30 112 + assert_equal person.name, "John" 113 + assert_equal person.age, 30 116 114 end 117 115 118 116 test "initializer type check" do 119 - expect { Person.new(1, age: "Joel") }.to_raise(Literal::TypeError) { |error| 120 - expect(error.message) == <<~ERROR 121 - Type mismatch 117 + error = assert_raises(Literal::TypeError) { Person.new(1, age: "Joel") } 122 118 123 - #{Person}#initialize (from #{error.backtrace[1]}) 124 - name 125 - Expected: String 126 - Actual (Integer): 1 119 + assert_equal error.message, <<~ERROR 120 + Type mismatch 121 + 122 + #{Person}#initialize (from #{error.backtrace[1]}) 123 + name 124 + Expected: String 125 + Actual (Integer): 1 127 126 ERROR 128 - } 129 127 end 130 128 131 129 test "initializer keyword check" do 132 130 random = Random.new(1) 133 131 134 - expect(random.begin) == 1 132 + assert_equal random.begin, 1 135 133 end 136 134 137 135 test "default block" do 138 136 object = WithDefaultBlock.new 139 - expect(object.block.call) == "Hello" 137 + assert_equal object.block.call, "Hello" 140 138 141 139 object = WithDefaultBlock.new { "World" } 142 - expect(object.block.call) == "World" 140 + assert_equal object.block.call, "World" 143 141 end 144 142 145 143 test "properties are enumerable" do 146 144 props = Person.literal_properties 147 - expect(props.size) == 2 148 - expect(props.map(&:name)) == [:name, :age] 145 + assert_equal props.size, 2 146 + assert_equal props.map(&:name), [:name, :age] 149 147 150 148 props = Empty.literal_properties 151 - expect(props.size) == 0 149 + assert_equal props.size, 0 152 150 end 153 151 154 152 test "introspection" do 155 153 prop1, prop2 = *Person.literal_properties 156 154 157 - expect(prop1.name) == :name 158 - expect(prop1.type) == String 155 + assert_equal prop1.name, :name 156 + assert_equal prop1.type, String 157 + 159 158 assert(prop1.positional?) { "Expected name to be kind :positional" } 160 159 refute(prop1.keyword?) { "Expected name to not be kind :keyword" } 161 160 refute(prop1.block?) { "Expected name to not be kind :&" } ··· 164 163 assert(prop1.required?) { "Expected name to be required" } 165 164 refute(prop1.optional?) { "Expected name to not be optional" } 166 165 167 - expect(prop2.name) == :age 168 - expect(prop2.type) == Integer 166 + assert_equal prop2.name, :age 167 + assert_equal prop2.type, Integer 168 + 169 169 assert(prop2.keyword?) { "Expected age to be kind :keyword" } 170 170 assert(prop2.required?) { "Expected age to be required" } 171 171 ··· 218 218 test "inheritance" do 219 219 friend = Friend.new("John", age: 30.5) 220 220 221 - expect(friend.name) == "John" 222 - expect(friend.age) > 30 221 + assert_equal friend.name, "John" 222 + assert_equal friend.age, 30.5 223 223 end 224 224 225 225 class WithPredicate ··· 232 232 enabled = WithPredicate.new(enabled: true) 233 233 disabled = WithPredicate.new(enabled: false) 234 234 235 - expect(enabled.enabled?) == true 236 - expect(disabled.enabled?) == false 235 + assert_equal enabled.enabled?, true 236 + assert_equal disabled.enabled?, false 237 237 end 238 238 239 239 class WithWriters < Example ··· 246 246 test "writer type error" do 247 247 instance = WithWriters.new 248 248 249 - expect { instance.example = 0 }.to_raise(Literal::TypeError) { |error| 250 - expect(error.message) == <<~ERROR 251 - Type mismatch 249 + error = assert_raises(Literal::TypeError) do 250 + instance.example = 0 251 + end 252 252 253 - #{WithWriters}#example=(value) (from #{error.backtrace[1]}) 254 - Expected: _Nilable(String) 255 - Actual (Integer): 0 253 + assert_equal error.message, <<~ERROR 254 + Type mismatch 255 + 256 + #{WithWriters}#example=(value) (from #{error.backtrace[1]}) 257 + Expected: _Nilable(String) 258 + Actual (Integer): 0 256 259 ERROR 257 - } 260 + 261 + error = assert_raises(Literal::TypeError) do 262 + instance.a = [1] 263 + end 258 264 259 - expect { instance.a = [1] }.to_raise(Literal::TypeError) { |error| 260 - expect(error.message) == <<~ERROR 265 + assert_equal error.message, <<~ERROR 261 266 Type mismatch 262 267 263 268 #{WithWriters}#a=(value) (from #{error.backtrace[1]}) 264 269 [0] 265 270 Expected: String 266 271 Actual (Integer): 1 267 - ERROR 268 - } 272 + ERROR 269 273 end 270 274 271 275 class Family ··· 276 280 end 277 281 278 282 test "nested properties raise in initializer" do 279 - expect do 283 + error = assert_raises(Literal::TypeError) do 280 284 Family.new( 281 285 [ 282 286 { ··· 292 296 }, 293 297 ], 294 298 ) 295 - end.to_raise(Literal::TypeError) do |error| 296 - expect(error.message) == <<~ERROR 297 - Type mismatch 299 + end 298 300 299 - #{Family}#initialize (from #{error.backtrace[1]}) 300 - members 301 - [0] 302 - [:role] 303 - Expected: Symbol 304 - Actual (Integer): 1 305 - [1] 306 - [:role] 307 - Expected: Symbol 308 - Actual (String): "Father" 309 - [2] 310 - [:person] 311 - Expected: #{Person.inspect} 312 - Actual (NilClass): nil 313 - [:role] 314 - Expected: Symbol 315 - Actual (NilClass): nil 301 + assert_equal error.message, <<~ERROR 302 + Type mismatch 303 + 304 + #{Family}#initialize (from #{error.backtrace[1]}) 305 + members 306 + [0] 307 + [:role] 308 + Expected: Symbol 309 + Actual (Integer): 1 310 + [1] 311 + [:role] 312 + Expected: Symbol 313 + Actual (String): "Father" 314 + [2] 315 + [:person] 316 + Expected: #{Person.inspect} 317 + Actual (NilClass): nil 318 + [:role] 319 + Expected: Symbol 320 + Actual (NilClass): nil 316 321 ERROR 317 - end 322 + 323 + error = assert_raises(Literal::TypeError) { Family.new([1]) } 318 324 319 - expect { Family.new([1]) }.to_raise(Literal::TypeError) { |error| 320 - expect(error.message) == <<~ERROR 321 - Type mismatch 325 + assert_equal error.message, <<~ERROR 326 + Type mismatch 322 327 323 - #{Family}#initialize (from #{error.backtrace[1]}) 324 - members 325 - [0] 326 - Expected: _Map(#{{ person: Person, role: Symbol }}) 327 - Actual (Integer): 1 328 + #{Family}#initialize (from #{error.backtrace[1]}) 329 + members 330 + [0] 331 + Expected: _Map(#{{ person: Person, role: Symbol }}) 332 + Actual (Integer): 1 328 333 ERROR 329 - } 334 + 335 + error = assert_raises(Literal::TypeError) do 336 + Family.new([], last_reunion_year: :two_thousand) 337 + end 330 338 331 - expect { Family.new([], last_reunion_year: :two_thousand) }.to_raise(Literal::TypeError) { |error| 332 - expect(error.message) == <<~ERROR 333 - Type mismatch 339 + assert_equal error.message, <<~ERROR 340 + Type mismatch 334 341 335 - #{Family}#initialize (from #{error.backtrace[1]}) 336 - last_reunion_year: 337 - Expected: _Nilable(Integer) 338 - Actual (Symbol): :two_thousand 339 - ERROR 340 - } 342 + #{Family}#initialize (from #{error.backtrace[1]}) 343 + last_reunion_year: 344 + Expected: _Nilable(Integer) 345 + Actual (Symbol): :two_thousand 346 + ERROR 341 347 end 342 348 343 349 test "nested properties succeed in initializer" do 344 - expect do 350 + refute_raises do 345 351 Family.new( 346 352 [ 347 353 { ··· 354 360 }, 355 361 ], 356 362 ) 357 - end.not_to_raise 358 - expect { Family.new([]) }.not_to_raise 359 - expect { Family.new([], last_reunion_year: 0) }.not_to_raise 363 + end 364 + 365 + refute_raises { Family.new([]) } 366 + refute_raises { Family.new([], last_reunion_year: 0) } 360 367 end 361 368 362 369 test "#to_h" do 363 - person = Person.new("John", age: 30) 364 - expect(person.to_h) == { name: "John", age: 30 } 370 + person = Person.new("John", age: 30) 371 + assert_equal person.to_h, { name: "John", age: 30 } 365 372 366 - empty = Empty.new 367 - expect(empty.to_h) == {} 373 + empty = Empty.new 374 + assert_equal empty.to_h, {} 368 375 end
+12 -12
test/properties/reader.test.rb
··· 11 11 example: "hello", 12 12 ) 13 13 14 - expect(object.public_methods).not_to_include(:example) 15 - expect(object.protected_methods).not_to_include(:example) 16 - expect(object.private_methods).not_to_include(:example) 14 + refute_includes(object.public_methods, :example) 15 + refute_includes(object.protected_methods, :example) 16 + refute_includes(object.private_methods, :example) 17 17 end 18 18 19 19 test "false reader" do ··· 25 25 example: "hello", 26 26 ) 27 27 28 - expect(object.public_methods).not_to_include(:example) 29 - expect(object.protected_methods).not_to_include(:example) 30 - expect(object.private_methods).not_to_include(:example) 28 + refute_includes(object.public_methods, :example) 29 + refute_includes(object.protected_methods, :example) 30 + refute_includes(object.private_methods, :example) 31 31 end 32 32 33 33 test "private reader" do ··· 39 39 example: "hello", 40 40 ) 41 41 42 - expect(object.private_methods).to_include(:example) 43 - expect(object.__send__(:example)) == "hello" 42 + assert_includes(object.private_methods, :example) 43 + assert_equal(object.__send__(:example), "hello") 44 44 end 45 45 46 46 test "protected reader" do ··· 52 52 example: "hello", 53 53 ) 54 54 55 - expect(object.protected_methods).to_include(:example) 56 - expect(object.__send__(:example)) == "hello" 55 + assert_includes(object.protected_methods, :example) 56 + assert_equal(object.__send__(:example), "hello") 57 57 end 58 58 59 59 test "public reader" do ··· 65 65 example: "hello", 66 66 ) 67 67 68 - expect(object.public_methods).to_include(:example) 69 - expect(object.example) == ("hello") 68 + assert_includes(object.public_methods, :example) 69 + assert_equal(object.example, "hello") 70 70 end
+12 -12
test/properties/writer.test.rb
··· 11 11 example: "hello", 12 12 ) 13 13 14 - expect(object.public_methods).not_to_include(:example=) 15 - expect(object.protected_methods).not_to_include(:example=) 16 - expect(object.private_methods).not_to_include(:example=) 14 + refute_includes object.public_methods, :example= 15 + refute_includes object.protected_methods, :example= 16 + refute_includes object.private_methods, :example= 17 17 end 18 18 19 19 test "false writer" do ··· 25 25 example: "hello", 26 26 ) 27 27 28 - expect(object.public_methods).not_to_include(:example) 29 - expect(object.protected_methods).not_to_include(:example) 30 - expect(object.private_methods).not_to_include(:example) 28 + refute_includes object.public_methods, :example 29 + refute_includes object.protected_methods, :example 30 + refute_includes object.private_methods, :example 31 31 end 32 32 33 33 test "private writer" do ··· 39 39 example: "hello", 40 40 ) 41 41 42 - expect(object.private_methods).to_include(:example=) 43 - expect(object.__send__(:example=, "world")) == "world" 42 + assert_includes object.public_methods, :example 43 + assert_equal object.__send__(:example), "hello" 44 44 end 45 45 46 46 test "protected writer" do ··· 52 52 example: "hello", 53 53 ) 54 54 55 - expect(object.protected_methods).to_include(:example=) 56 - expect(object.__send__(:example=, "world")) == "world" 55 + assert_includes object.protected_methods, :example= 56 + assert_equal object.__send__(:example=, "world"), "world" 57 57 end 58 58 59 59 test "public writer" do ··· 65 65 example: "hello", 66 66 ) 67 67 68 - expect(object.public_methods).to_include(:example=) 69 - expect(object.example = "world") == "world" 68 + assert_includes object.public_methods, :example= 69 + assert_equal object.example = "world", "world" 70 70 end
+35 -32
test/struct.test.rb
··· 10 10 11 11 test do 12 12 person = Person.new(name: "Joel") 13 - expect(person.name) == "Joel" 13 + assert_equal person.name, "Joel" 14 14 end 15 15 16 16 test do 17 17 person = Person.new(name: "Joel") 18 18 person.name = "Jill" 19 - expect(person.name) == "Jill" 19 + 20 + assert_equal person.name, "Jill" 20 21 end 21 22 22 23 test do 23 24 person = Person.new(name: "Joel") 24 - expect(person.to_h) == { name: "Joel" } 25 + assert_equal person.to_h, { name: "Joel" } 25 26 end 26 27 27 28 test do 28 29 a = Person.new(name: "Joel") 29 30 b = Person.new(name: "Joel") 30 31 31 - expect(a) == b 32 + assert_equal a, b 32 33 end 33 34 34 35 test do 35 36 a = Person.new(name: "Joel") 36 37 b = Person.new(name: "Jill") 37 38 38 - expect(a) != b 39 + refute_equal a, b 39 40 end 40 41 41 42 test do 42 43 a = Person.new(name: "Joel") 43 44 b = Student.new(name: "Joel", final_grade: 90) 44 45 45 - expect(a) != b 46 + refute_equal a, b 46 47 end 47 48 48 49 # Marshal doesn't work with anonymous classes ··· 54 55 a = RootStruct.new(name: "Joel") 55 56 b = Marshal.load(Marshal.dump(a)) 56 57 57 - expect(b) == a 58 + assert_equal b, a 58 59 end 59 60 60 61 test "marshalling a frozen struct" do ··· 63 64 64 65 b = Marshal.load(Marshal.dump(a)) 65 66 66 - expect(b) == a 67 - expect(b).to_be(:frozen?) 67 + assert_equal b, a 68 + assert b.frozen? 68 69 end 69 70 70 71 test "as_pack/from_pack" do ··· 73 74 74 75 b = RootStruct.from_pack(a.as_pack) 75 76 76 - expect(b) == a 77 - expect(b).to_be(:frozen?) 77 + assert_equal b, a 78 + assert b.frozen? 78 79 end 79 80 80 81 test "can be deconstructed" do 81 82 person = Person.new(name: "Joel") 82 - expect(person.deconstruct) == ["Joel"] 83 + assert_equal person.deconstruct, ["Joel"] 83 84 end 84 85 85 86 test "can be deconstructed with keys" do 86 87 person = Person.new(name: "Joel") 87 - expect(person.deconstruct_keys([:name])) == { name: "Joel" } 88 - expect(person.deconstruct_keys(nil)) == { name: "Joel" } 88 + 89 + assert_equal person.deconstruct_keys([:name]), { name: "Joel" } 90 + assert_equal person.deconstruct_keys(nil), { name: "Joel" } 89 91 end 90 92 91 93 test "can be indexed" do 92 94 person = Person.new(name: "Joel") 93 - expect(person[:name]) == "Joel" 95 + assert_equal person[:name], "Joel" 94 96 end 95 97 96 98 test "returns nil for unknown keys" do 97 99 person = Person.new(name: "Joel") 98 - expect { person[:age] }.to_raise(NameError) 99 - expect(person["name"]) == "Joel" 100 - expect { person[0] }.to_raise(TypeError) 100 + 101 + assert_raises(NameError) { person[:age] } 102 + assert_equal "Joel", person["name"] 103 + assert_raises(TypeError) { person[0] } 101 104 end 102 105 103 106 class WithKeywordPropertyNames < Literal::Struct ··· 108 111 109 112 test do 110 113 with_keyword_property_names = WithKeywordPropertyNames.new(begin: "start", end: "finish") 111 - expect(with_keyword_property_names.to_h) == { :begin => "start", :end => "finish", :module => nil } 112 - expect(with_keyword_property_names) == with_keyword_property_names 113 - expect(with_keyword_property_names) == with_keyword_property_names.dup 114 - expect(with_keyword_property_names.hash) == with_keyword_property_names.dup.hash 115 - expect(with_keyword_property_names[:begin]) == "start" 116 - expect(with_keyword_property_names[:end]) == "finish" 117 - expect(with_keyword_property_names[:module]) == nil 118 - expect(with_keyword_property_names["begin"]) == "start" 119 - expect(with_keyword_property_names["end"]) == "finish" 120 - expect(with_keyword_property_names["module"]) == nil 121 - expect(with_keyword_property_names.begin) == "start" 122 - expect(with_keyword_property_names.end) == "finish" 123 - expect(with_keyword_property_names.module) == nil 114 + assert_equal(with_keyword_property_names.to_h, { :begin => "start", :end => "finish", :module => nil }) 115 + assert_equal(with_keyword_property_names, with_keyword_property_names) 116 + assert_equal(with_keyword_property_names, with_keyword_property_names.dup) 117 + assert_equal(with_keyword_property_names.hash, with_keyword_property_names.dup.hash) 118 + assert_equal(with_keyword_property_names[:begin], "start") 119 + assert_equal(with_keyword_property_names[:end], "finish") 120 + assert_equal(with_keyword_property_names[:module], nil) 121 + assert_equal(with_keyword_property_names["begin"], "start") 122 + assert_equal(with_keyword_property_names["end"], "finish") 123 + assert_equal(with_keyword_property_names["module"], nil) 124 + assert_equal(with_keyword_property_names.begin, "start") 125 + assert_equal(with_keyword_property_names.end, "finish") 126 + assert_equal(with_keyword_property_names.module, nil) 124 127 with_keyword_property_names[:begin] = "start2" 125 128 with_keyword_property_names.end = "finish2" 126 - expect(with_keyword_property_names.to_h) == { :begin => "start2", :end => "finish2", :module => nil } 129 + assert_equal(with_keyword_property_names.to_h, { :begin => "start2", :end => "finish2", :module => nil }) 127 130 end
+9 -7
test/types.test.rb
··· 3 3 include Literal::Types 4 4 5 5 def expect_type_error(expected:, actual:, message:) 6 - expect do 6 + error = assert_raises(Literal::TypeError) do 7 7 Literal.check(expected:, actual:) 8 - end.to_raise(Literal::TypeError) do |error| 9 - expect(error.message) == message 10 8 end 9 + 10 + assert_equal error.message, message 11 11 end 12 12 13 13 test "_Deferred" do ··· 435 435 MSG 436 436 437 437 object_keys_map = _Map(**{ 1 => Integer, "2" => String, :symbol => _Nilable(Symbol) }) 438 - expect(object_keys_map.inspect) == "_Map(#{{ 1 => Integer, '2' => String, :symbol => _Nilable(Symbol) }.inspect})" 439 - expect(object_keys_map) === { 1 => 2, "2" => "string" } 440 - expect(object_keys_map) === { 1 => 2, "2" => "string", :symbol => :foo } 438 + 439 + assert_equal object_keys_map.inspect, "_Map(#{{ 1 => Integer, '2' => String, :symbol => _Nilable(Symbol) }.inspect})" 440 + assert object_keys_map === { 1 => 2, "2" => "string" } 441 + assert object_keys_map === { 1 => 2, "2" => "string", :symbol => :foo } 442 + 441 443 refute object_keys_map === {} 442 444 refute object_keys_map === { 1 => 2 } 443 445 ··· 707 709 _Union(Symbol, Float), 708 710 ) 709 711 710 - expect(type.inspect) == "_Union([String, Integer, Symbol, Float])" 712 + assert_equal type.inspect, "_Union([String, Integer, Symbol, Float])" 711 713 end 712 714 713 715 test "_Void" do