raindrops.git  about / heads / tags
real-time stats for preforking Rack servers
blob abf0c59c781f73269e95050a9e515e01d9b0a49e 1152 bytes (raw)
$ git show HEAD:test/test_struct.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 
# frozen_string_literal: false
require 'test/unit'
require 'raindrops'

class TestRaindrops < Test::Unit::TestCase

  def test_struct_new
    @rw = Raindrops::Struct.new(:r, :w)
    assert @rw.kind_of?(Class)
  end

  TMP = Raindrops::Struct.new(:r, :w)

  def test_init_basic
    tmp = TMP.new
    assert_equal 0, tmp.r
    assert_equal 1, tmp.incr_r
    assert_equal 1, tmp.r
    assert_equal({ :r => 1, :w => 0 }, tmp.to_hash)

    assert_equal 1, tmp[0]
    assert_equal 0, tmp[1]
    assert_equal [ :r, :w ], TMP::MEMBERS
  end

  def test_init
    tmp = TMP.new(5, 6)
    assert_equal({ :r => 5, :w => 6 }, tmp.to_hash)
  end

  def test_dup
    a = TMP.new(5, 6)
    b = a.dup
    assert_equal({ :r => 5, :w => 6 }, b.to_hash)
    assert_nothing_raised { 4.times { b.decr_r } }
    assert_equal({ :r => 1, :w => 6 }, b.to_hash)
    assert_equal({ :r => 5, :w => 6 }, a.to_hash)
  end

  class Foo < Raindrops::Struct.new(:a, :b, :c, :d)
    def to_ary
      @raindrops.to_ary
    end

    def hello
      "world"
    end
  end

  def test_subclass
    assert_equal [0, 0, 0, 0], Foo.new.to_ary
    assert_equal "world", Foo.new.hello
  end

end

git clone https://yhbt.net/raindrops.git