Bug #11953 ยป nth_prime_test.rb
1 
#!/usr/bin/env ruby


2 
gem 'minitest', '>= 5.0.0' 
3 
require 'minitest/autorun' 
4  
5 
require 'prime' 
6 
ERROR_MESSAGE = <<MSG 
7 
Using Ruby's Prime class is probably the best way to do this in a

8 
'real' application; but this is an exercise, not a real application,

9 
so you're expected to implement this yourself.

10 
MSG

11  
12 
class Prime 
13 
[:each, :new, :prime?, :take].each do m 
14 
define_method(m) { *_ fail ERROR_MESSAGE } 
15 
end

16 
end

17  
18 
class Integer 
19 
[:prime?, :each_prime].each do m 
20 
define_method(m) { *_ fail ERROR_MESSAGE } 
21 
end

22 
end

23  
24 
require_relative 'nth_prime' 
25  
26 
class TestPrimes < Minitest::Test 
27 
def test_first 
28 
assert_equal 2, Prime.nth(1) 
29 
end

30  
31 
def test_second 
32 
skip

33 
assert_equal 3, Prime.nth(2) 
34 
end

35  
36 
def test_sixth_prime 
37 
skip

38 
assert_equal 13, Prime.nth(6) 
39 
end

40  
41 
def test_big_prime 
42 
skip

43 
assert_equal 104_743, Prime.nth(10_001) 
44 
end

45  
46 
def test_weird_case 
47 
skip

48 
assert_raises ArgumentError do 
49 
Prime.nth(0) 
50 
end

51 
end

52 
end
