Class: RushCheck::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/rushcheck/config.rb

Overview

Config is a class which has configurations of tests. This class is needed for several test functions.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_test = 100, max_fail = 1000, size = Proc.new {|x| x / 2 + 3}, every = Proc.new {|n, args| s = n.to_s; s + ("\b" * s.length)}) ⇒ Config

Returns a new instance of Config.



36
37
38
39
40
41
# File 'lib/rushcheck/config.rb', line 36

def initialize(max_test = 100, 
               max_fail = 1000,
               size = Proc.new {|x| x / 2 + 3},
               every = Proc.new {|n, args| s = n.to_s; s + ("\b" * s.length)})
  @max_test, @max_fail, @size, @every = max_test, max_fail, size, every
end

Instance Attribute Details

#everyObject (readonly)

Returns the value of attribute every.



12
13
14
# File 'lib/rushcheck/config.rb', line 12

def every
  @every
end

#max_failObject (readonly)

Returns the value of attribute max_fail.



12
13
14
# File 'lib/rushcheck/config.rb', line 12

def max_fail
  @max_fail
end

#max_testObject (readonly)

Returns the value of attribute max_test.



12
13
14
# File 'lib/rushcheck/config.rb', line 12

def max_test
  @max_test
end

#sizeObject (readonly)

Returns the value of attribute size.



12
13
14
# File 'lib/rushcheck/config.rb', line 12

def size
  @size
end

Class Method Details

.batch(n, v) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/rushcheck/config.rb', line 28

def self.batch(n, v)
  new(n, n * 10, 
      Proc.new{|x| (x / 2 + 3).to_i},
      Proc.new do |n, args|
        v ? n.to_s + ":\n" + args.join("\n") + "\n" : ""
      end)
end

.silentObject



22
23
24
25
26
# File 'lib/rushcheck/config.rb', line 22

def self.silent
  new(100, 1000, 
      Proc.new{|x| x / 2 + 3},
      Proc.new { |n, args| ''})
end

.verboseObject



14
15
16
17
18
19
20
# File 'lib/rushcheck/config.rb', line 14

def self.verbose 
  new(100, 1000, 
      Proc.new{|x| x / 2 + 3},
      Proc.new do |n, args|
        n.to_s + ":\n" + args.join("\n") + "\n"
      end)
end

Instance Method Details

#done(mesg, ntest, stamps) ⇒ Object

print results of tests.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rushcheck/config.rb', line 44

def done(mesg, ntest, stamps)
  print mesg + ' ' + ntest.to_s + ' tests'

  bag = stamps.compact.find_all {|x| ! x.empty?}.sort.
    inject(Hash.new) do |r, m|
    r[m] = r[m].nil? ? 1 : r[m] + 1
    r
  end.sort_by {|k, v| v}.reverse.map do |pair|
    percentage = ((100 * pair[1]) / ntest).to_s + '%'
    ([percentage] + pair[0]).join(', ')
  end

  mes = case bag.length
        when 0
          ".\n"
        when 1
          '(' + bag[0] + ").\n"
        else
          ".\n" + bag.join(".\n") + ".\n"
        end

  print mes
end

#test_batch(gen, rnd, nt = 1, nf = 0, stamps = []) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rushcheck/config.rb', line 110

def test_batch(gen, rnd, nt=1, nf=0, stamps=[])
  ntest, nfail = nt, nf
  while true
    if ntest > max_test
      tests_result = RushCheck::TestOk.new("OK, passed", ntest, stamps)
      break
    end
    if nfail > max_fail
      test_result = RushCheck::TestExausted.new('Arguments exhausted after ', ntest, stamps)
      break
    end
    
    rnd_l, rnd_r = rnd.split
    result = gen.generate(size.call(ntest), rnd_r)
    message = every.call(ntest, result.arguments)
    print message # don't puts

    case result.ok
    when nil
      nfail += 1
      redo
    when true
      stamps.push(result.stamp)
      ntest += 1
      redo
    when false
      tests_result = RushCheck::TestFailed.new(result.arguments, ntest)
      break
    else
      raise(RuntimeError, "RushCheck: illegal result")
    end
  end    

  test_result
end

#tests(gen, rnd, nt = 1, nf = 0, stamps = []) ⇒ Object

execute tests



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rushcheck/config.rb', line 69

def tests(gen, rnd, nt=1, nf=0, stamps=[])
  ntest, nfail = nt, nf
  while true
    if ntest > max_test
      done('OK, passed', max_test, stamps)
      tests_result = true
      break
    end
    if nfail > max_fail
      done('Arguments exhausted after ', ntest, stamps)
      tests_result = nil
      break
    end

    rnd_l, rnd_r = rnd.split
    result = gen.generate(size.call(ntest), rnd_r)
    message = every.call(ntest, result.arguments)
    print message # don't puts

    case result.ok
    when nil
      nfail += 1
      redo
    when true
      stamps.push(result.stamp)
      ntest += 1
      redo
    when false
      error = "Falsifiable, after " + ntest.to_s + " tests:\n" + 
        result.arguments.join("\n")
      puts error
      tests_result = false
      break
    else
      raise(RuntimeError, "RushCheck: illegal result")
    end
  end
  
  tests_result
end