astruct 2.9.0 → 2.11.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -24,8 +24,8 @@
24
24
  # /.config
25
25
  /.yardoc
26
26
  # /InstalledFiles
27
- # /_yardoc
28
- # /coverage/
27
+ /_yardoc
28
+ /coverage/
29
29
  /doc/
30
30
  # /lib/bundler/man/
31
31
  # /rdoc/
data/.rvmrc CHANGED
@@ -6,10 +6,11 @@
6
6
  # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
7
  # Only full ruby name is supported here, for short names use:
8
8
  # echo "rvm use 1.9.3" > .rvmrc
9
- environment_id="ruby-1.9.3-p194@astruct"
9
+ environment_id="ruby-1.9.3-p286@astruct"
10
10
 
11
11
  # Uncomment the following lines if you want to verify rvm version per project
12
- # rvmrc_rvm_version="1.13.8 (master)" # 1.10.1 seams as a safe start
12
+ # rvmrc_rvm_version="1.16.17 (stable)" # 1.10.1 seams as a safe start
13
+
13
14
  # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
15
  # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
16
  # return 1
data/Rakefile CHANGED
@@ -27,5 +27,17 @@ YARD::Rake::YardocTask.new do |config|
27
27
  config.files = Dir['lib/**/*.rb']
28
28
  end
29
29
 
30
+ desc "Running all the benchmarks and writing results to file"
31
+ task :bench do
32
+ Dir[File.join(File.dirname(__FILE__), "bench", "*")].each do |benchmark|
33
+ header = "\nPLATFORM: #{RUBY_DESCRIPTION}\nTIMESTAMP: #{Time.now}\n---\n"
34
+ results = `bundle exec ruby #{benchmark}`
35
+ document = header + results
36
+ File.open benchmark, "a" do |file|
37
+ file.write document.gsub /^/, "# "
38
+ end
39
+ end
40
+ end
41
+
30
42
  desc 'Default: run tests, and generate docs'
31
43
  task :default => [ :test, :yard ]
data/astruct.gemspec CHANGED
@@ -1,4 +1,4 @@
1
- # -*- encoding: utf-8 -*-
1
+ # encoding: utf-8
2
2
  require File.expand_path('../lib/astruct/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
@@ -15,8 +15,9 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = AltStruct::VERSION
17
17
 
18
- gem.add_development_dependency 'rake', '~> 0.9'
18
+ gem.add_development_dependency 'rake'
19
19
  gem.add_development_dependency 'yard'
20
20
  gem.add_development_dependency 'kramdown'
21
21
  gem.add_development_dependency 'benchmark-ips'
22
+ gem.add_development_dependency 'simplecov'
22
23
  end
@@ -2,14 +2,14 @@ require 'benchmark/ips'
2
2
  require 'astruct'
3
3
  require 'ostruct'
4
4
 
5
- DATA = (1..10_000).map { |i| { :"item#{i}" => i } }.inject :merge!
6
- DATA2 = (10_000..20_000).map { |i| { :"item#{i}" => i } }.inject :merge!
5
+ DATA = (1..10_000).map { |i| { "key#{i}" => "value#{i}" } }.inject :merge!
6
+ DATA2 = (10_000..20_000).map { |i| { "key#{i}" => "value#{i}" } }.inject :merge!
7
7
 
8
8
  Benchmark.ips do |x|
9
9
  x.report "OStruct new with data then load with more data" do
10
10
  class OProfile < OpenStruct; end
11
11
  op = OProfile.new DATA.dup
12
- op.marshal_load DATA2.dup
12
+ op.marshal_load op.marshal_dump.merge DATA2.dup
13
13
  end
14
14
 
15
15
  x.report "AStruct new with data then load with more data" do
@@ -18,9 +18,17 @@ Benchmark.ips do |x|
18
18
  ap.load DATA2.dup
19
19
  end
20
20
  end
21
+ #
22
+ # PLATFORM: ruby 1.9.3p286 (2012-10-12 revision 37165) [x86_64-darwin12.2.0]
23
+ # TIMESTAMP: 2012-11-06 11:17:33 -0800
24
+ # ---
21
25
  # Calculating -------------------------------------
22
- # OStruct new with data then load with more data 1 i/100ms
23
- # AStruct new with data then load with more data 1 i/100ms
26
+ # OStruct new with data then load with more data
27
+ # 1 i/100ms
28
+ # AStruct new with data then load with more data
29
+ # 1 i/100ms
24
30
  # -------------------------------------------------
25
- # OStruct new with data then load with more data 3.2 (±0.0%) i/s - 16 in 5.032341s
26
- # AStruct new with data then load with more data 4.423.0%) i/s - 21 in 5.033214s
31
+ # OStruct new with data then load with more data
32
+ # 1.80.0%) i/s - 9 in 5.186949s
33
+ # AStruct new with data then load with more data
34
+ # 1.9 (±0.0%) i/s - 10 in 5.353544s
@@ -2,24 +2,32 @@ require 'benchmark/ips'
2
2
  require 'astruct'
3
3
  require 'ostruct'
4
4
 
5
- DATA = (1..10_000).map { |i| { :"item#{i}" => i } }.inject :merge!
5
+ DATA = (1..10_000).map { |i| { "key#{i}" => "value#{i}" } }.inject :merge!
6
6
 
7
7
  Benchmark.ips do |x|
8
8
  x.report "OStruct new with data then delete" do
9
9
  class OProfile < OpenStruct; end
10
10
  op = OProfile.new DATA.dup
11
- op.delete_field :item1
11
+ op.delete_field :key1
12
12
  end
13
13
 
14
14
  x.report "AStruct new with data then delete" do
15
15
  class AProfile < AltStruct; end
16
16
  ap = AProfile.new DATA.dup
17
- ap.delete :item1
17
+ ap.delete :key1
18
18
  end
19
19
  end
20
+ #
21
+ # PLATFORM: ruby 1.9.3p286 (2012-10-12 revision 37165) [x86_64-darwin12.2.0]
22
+ # TIMESTAMP: 2012-11-06 11:17:50 -0800
23
+ # ---
20
24
  # Calculating -------------------------------------
21
- # OStruct new with data then delete 1 i/100ms
22
- # AStruct new with data then delete 1 i/100ms
25
+ # OStruct new with data then delete
26
+ # 1 i/100ms
27
+ # AStruct new with data then delete
28
+ # 1 i/100ms
23
29
  # -------------------------------------------------
24
- # OStruct new with data then delete 8.9 (±11.3%) i/s - 44 in 5.014542s
25
- # AStruct new with data then delete 10.229.5%) i/s - 48 in 5.014586s
30
+ # OStruct new with data then delete
31
+ # 4.422.5%) i/s - 22 in 5.028472s
32
+ # AStruct new with data then delete
33
+ # 5.4 (±18.6%) i/s - 26 in 5.028498s
@@ -2,7 +2,7 @@ require 'benchmark/ips'
2
2
  require 'astruct'
3
3
  require 'ostruct'
4
4
 
5
- DATA = (1..10_000).map { |i| { :"item#{i}" => i } }.inject :merge!
5
+ DATA = (1..10_000).map { |i| { "key#{i}" => "value#{i}" } }.inject :merge!
6
6
 
7
7
  Benchmark.ips do |x|
8
8
  x.report "OStruct dump with data" do
@@ -17,9 +17,17 @@ Benchmark.ips do |x|
17
17
  ap.dump
18
18
  end
19
19
  end
20
+ #
21
+ # PLATFORM: ruby 1.9.3p286 (2012-10-12 revision 37165) [x86_64-darwin12.2.0]
22
+ # TIMESTAMP: 2012-11-06 11:18:06 -0800
23
+ # ---
20
24
  # Calculating -------------------------------------
21
- # OStruct dump with data 1 i/100ms
22
- # AStruct dump with data 1 i/100ms
25
+ # OStruct dump with data
26
+ # 1 i/100ms
27
+ # AStruct dump with data
28
+ # 1 i/100ms
23
29
  # -------------------------------------------------
24
- # OStruct dump with data 9.0 (±22.2%) i/s - 44 in 5.055799s
25
- # AStruct dump with data 10.229.4%) i/s - 48 in 5.073042s
30
+ # OStruct dump with data
31
+ # 4.621.6%) i/s - 23 in 5.176739s
32
+ # AStruct dump with data
33
+ # 5.4 (±37.3%) i/s - 26 in 5.200824s
@@ -2,7 +2,7 @@ require 'benchmark/ips'
2
2
  require 'astruct'
3
3
  require 'ostruct'
4
4
 
5
- DATA = (1..10_000).map { |i| { :"item#{i}" => i } }.inject :merge!
5
+ DATA = (1..10_000).map { |i| { "key#{i}" => "value#{i}" } }.inject :merge!
6
6
 
7
7
  Benchmark.ips do |x|
8
8
  x.report "OStruct new with data then assign new data" do
@@ -21,9 +21,17 @@ Benchmark.ips do |x|
21
21
  ap.example3 = "green"
22
22
  end
23
23
  end
24
+ #
25
+ # PLATFORM: ruby 1.9.3p286 (2012-10-12 revision 37165) [x86_64-darwin12.2.0]
26
+ # TIMESTAMP: 2012-11-06 11:18:22 -0800
27
+ # ---
24
28
  # Calculating -------------------------------------
25
- # OStruct new with data then assign new data 1 i/100ms
26
- # AStructt new with data then assign new data 1 i/100ms
29
+ # OStruct new with data then assign new data
30
+ # 1 i/100ms
31
+ # AStructt new with data then assign new data
32
+ # 1 i/100ms
27
33
  # -------------------------------------------------
28
- # OStruct new with data then assign new data 8.5 (±11.8%) i/s - 42 in 5.078029s
29
- # AStructt new with data then assign new data 9.432.0%) i/s - 43 in 5.005849s
34
+ # OStruct new with data then assign new data
35
+ # 4.223.8%) i/s - 21 in 5.200944s
36
+ # AStructt new with data then assign new data
37
+ # 5.1 (±38.9%) i/s - 24 in 5.001440s
@@ -2,7 +2,7 @@ require 'benchmark/ips'
2
2
  require 'astruct'
3
3
  require 'ostruct'
4
4
 
5
- DATA = (1..10_000).map { |i| { :"item#{i}" => i } }.inject :merge!
5
+ DATA = (1..10_000).map { |i| { "key#{i}" => "value#{i}" } }.inject :merge!
6
6
 
7
7
  Benchmark.ips do |x|
8
8
  x.report "OStruct inspect with data" do
@@ -17,9 +17,17 @@ Benchmark.ips do |x|
17
17
  ap.inspect
18
18
  end
19
19
  end
20
+ #
21
+ # PLATFORM: ruby 1.9.3p286 (2012-10-12 revision 37165) [x86_64-darwin12.2.0]
22
+ # TIMESTAMP: 2012-11-06 11:18:38 -0800
23
+ # ---
20
24
  # Calculating -------------------------------------
21
- # OStruct inspect with data 1 i/100ms
22
- # AStruct inspect with data 1 i/100ms
25
+ # OStruct inspect with data
26
+ # 1 i/100ms
27
+ # AStruct inspect with data
28
+ # 1 i/100ms
23
29
  # -------------------------------------------------
24
- # OStruct inspect with data 8.2 (±24.4%) i/s - 40 in 5.085258s
25
- # AStruct inspect with data 9.121.9%) i/s - 45 in 5.063373s
30
+ # OStruct inspect with data
31
+ # 4.422.5%) i/s - 22 in 5.063692s
32
+ # AStruct inspect with data
33
+ # 4.9 (±20.2%) i/s - 25 in 5.229125s
@@ -2,7 +2,7 @@ require 'benchmark/ips'
2
2
  require 'astruct'
3
3
  require 'ostruct'
4
4
 
5
- DATA = (1..10_000).map { |i| { :"item#{i}" => i } }.inject :merge!
5
+ DATA = (1..10_000).map { |i| { "key#{i}" => "value#{i}" } }.inject :merge!
6
6
 
7
7
  Benchmark.ips do |x|
8
8
  x.report "OStruct load with data" do
@@ -17,9 +17,17 @@ Benchmark.ips do |x|
17
17
  ap.load DATA.dup
18
18
  end
19
19
  end
20
+ #
21
+ # PLATFORM: ruby 1.9.3p286 (2012-10-12 revision 37165) [x86_64-darwin12.2.0]
22
+ # TIMESTAMP: 2012-11-06 11:18:54 -0800
23
+ # ---
20
24
  # Calculating -------------------------------------
21
- # OStruct load with data 1 i/100ms
22
- # AStruct load with data 1 i/100ms
25
+ # OStruct load with data
26
+ # 1 i/100ms
27
+ # AStruct load with data
28
+ # 1 i/100ms
23
29
  # -------------------------------------------------
24
- # OStruct load with data 10.1 (±19.9%) i/s - 49 in 5.017475s
25
- # AStruct load with data 10.3 (±29.1%) i/s - 50 in 5.090294s
30
+ # OStruct load with data
31
+ # 5.3 (±37.6%) i/s - 26 in 5.143205s
32
+ # AStruct load with data
33
+ # 5.3 (±18.8%) i/s - 26 in 5.086193s
@@ -2,7 +2,7 @@ require 'benchmark/ips'
2
2
  require 'astruct'
3
3
  require 'ostruct'
4
4
 
5
- DATA = (1..10_000).map { |i| { :"item#{i}" => i } }.inject :merge!
5
+ DATA = (1..10_000).map { |i| { "key#{i}" => "value#{i}" } }.inject :merge!
6
6
 
7
7
  Benchmark.ips do |x|
8
8
  x.report "OStruct new with data then 3 deep and inspect" do
@@ -21,9 +21,17 @@ Benchmark.ips do |x|
21
21
  ap.inspect
22
22
  end
23
23
  end
24
+ #
25
+ # PLATFORM: ruby 1.9.3p286 (2012-10-12 revision 37165) [x86_64-darwin12.2.0]
26
+ # TIMESTAMP: 2012-11-06 11:19:10 -0800
27
+ # ---
24
28
  # Calculating -------------------------------------
25
- # OStruct new with data then 3 deep and inspect 1 i/100ms
26
- # AStruct new with data then 3 deep and inspect 1 i/100ms
29
+ # OStruct new with data then 3 deep and inspect
30
+ # 1 i/100ms
31
+ # AStruct new with data then 3 deep and inspect
32
+ # 1 i/100ms
27
33
  # -------------------------------------------------
28
- # OStruct new with data then 3 deep and inspect 2.0 (±0.0%) i/s - 10 in 5.184739s
29
- # AStruct new with data then 3 deep and inspect 3.1 31.9%) i/s - 15 in 5.029310s
34
+ # OStruct new with data then 3 deep and inspect
35
+ # 1.3 (±0.0%) i/s - 7 in 5.560086s
36
+ # AStruct new with data then 3 deep and inspect
37
+ # 1.6 (±0.0%) i/s - 9 in 5.569932s
@@ -2,7 +2,7 @@ require 'benchmark/ips'
2
2
  require 'astruct'
3
3
  require 'ostruct'
4
4
 
5
- DATA = (1..10_000).map { |i| { :"item#{i}" => i } }.inject :merge!
5
+ DATA = (1..10_000).map { |i| { "key#{i}" => "value#{i}" } }.inject :merge!
6
6
 
7
7
  Benchmark.ips do |x|
8
8
  x.report "OStruct new with data" do
@@ -15,9 +15,17 @@ Benchmark.ips do |x|
15
15
  AProfile.new DATA.dup
16
16
  end
17
17
  end
18
+ #
19
+ # PLATFORM: ruby 1.9.3p286 (2012-10-12 revision 37165) [x86_64-darwin12.2.0]
20
+ # TIMESTAMP: 2012-11-06 11:19:28 -0800
21
+ # ---
18
22
  # Calculating -------------------------------------
19
- # OStruct new with data 1 i/100ms
20
- # AStruct new with data 1 i/100ms
23
+ # OStruct new with data
24
+ # 1 i/100ms
25
+ # AStruct new with data
26
+ # 1 i/100ms
21
27
  # -------------------------------------------------
22
- # OStruct new with data 8.6 (±23.2%) i/s - 42 in 5.041086s
23
- # AStruct new with data 9.730.8%) i/s - 47 in 5.101214s
28
+ # OStruct new with data
29
+ # 4.323.4%) i/s - 21 in 5.130880s
30
+ # AStruct new with data
31
+ # 5.2 (±38.7%) i/s - 25 in 5.211888s
@@ -0,0 +1,34 @@
1
+ require 'benchmark/ips'
2
+ require 'astruct'
3
+ require 'ostruct'
4
+
5
+ DATA = (1..10_000).map { |i| { "key#{i}" => "value#{i}" } }.inject :merge!
6
+ UPDATE = DATA.merge (1..5_000).map { |i| { "key#{i}" => "value#{i + rand(1..3)}" } }.inject :merge!
7
+
8
+ Benchmark.ips do |x|
9
+ x.report "OStruct partial updates with data" do
10
+ class OProfile < OpenStruct; end
11
+ op = OProfile.new DATA.dup
12
+ op.marshal_load op.marshal_dump.merge UPDATE.dup
13
+ end
14
+
15
+ x.report "AStruct partial updates with data" do
16
+ class AProfile < AltStruct; end
17
+ ap = AProfile.new DATA.dup
18
+ ap.load UPDATE.dup
19
+ end
20
+ end
21
+ #
22
+ # PLATFORM: ruby 1.9.3p286 (2012-10-12 revision 37165) [x86_64-darwin12.2.0]
23
+ # TIMESTAMP: 2012-11-06 11:19:44 -0800
24
+ # ---
25
+ # Calculating -------------------------------------
26
+ # OStruct partial updates with data
27
+ # 1 i/100ms
28
+ # AStruct partial updates with data
29
+ # 1 i/100ms
30
+ # -------------------------------------------------
31
+ # OStruct partial updates with data
32
+ # 3.4 (±29.5%) i/s - 17 in 5.211254s
33
+ # AStruct partial updates with data
34
+ # 3.0 (±0.0%) i/s - 15 in 5.116449s
@@ -22,6 +22,7 @@ class AltStruct
22
22
  def initialize(pairs = {})
23
23
  @table ||= {}
24
24
  for key, value in pairs
25
+ key = key.to_sym
25
26
  __new_field__ key, value
26
27
  end unless pairs.empty?
27
28
  end
@@ -33,6 +34,8 @@ class AltStruct
33
34
  # Hash#merge!
34
35
  def __load__(pairs)
35
36
  for key, value in pairs
37
+ key = key.to_sym
38
+ next if table[key] == value
36
39
  __new_field__ key, value
37
40
  end unless pairs.empty?
38
41
  end
@@ -105,7 +108,7 @@ class AltStruct
105
108
 
106
109
  def __dump_inspect__
107
110
  __create_id_list__
108
- unless __id_exists_in_id_list?
111
+ unless __id_exists_in_id_list__?
109
112
  __add_id_to_id_list__
110
113
  string = __dump__.any? ? " #{__dump_string__.join ', '}" : ""
111
114
  else
@@ -123,7 +126,7 @@ class AltStruct
123
126
  end
124
127
 
125
128
  def __new_field__(key, value)
126
- table.merge! __define_accessor__ key.to_sym, value
129
+ table.merge! __define_accessor__ key, value
127
130
  end
128
131
 
129
132
  def __dump_specific__(keys)
@@ -144,12 +147,16 @@ class AltStruct
144
147
  Thread.current[ThreadKey] ||= []
145
148
  end
146
149
 
147
- def __id_exists_in_id_list?
150
+ def __id_exists_in_id_list__?
148
151
  Thread.current[ThreadKey].include?(__object_id__)
149
152
  end
150
153
 
151
154
  def __remove_last_id_from_id_list__
152
155
  Thread.current[ThreadKey].pop
153
156
  end
157
+
158
+ def __field_exists_and_has_same_value__?(key, value)
159
+ table[key] == value
160
+ end
154
161
  end
155
162
  end
@@ -1,4 +1,4 @@
1
1
  class AltStruct
2
2
  # This is the AltStruct version
3
- VERSION = "2.9.0"
3
+ VERSION = "2.11.0"
4
4
  end
data/test/helper.rb CHANGED
@@ -1 +1,3 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
1
3
  require 'astruct'
@@ -1,5 +1,5 @@
1
+ require 'helper'
1
2
  require 'minitest/autorun'
2
- require_relative '../helper'
3
3
 
4
4
  class TestAltStruct < MiniTest::Unit::TestCase
5
5
  def setup
@@ -1,227 +1,227 @@
1
- require 'minitest/autorun'
2
- require_relative '../helper'
3
-
4
- class TestOpenStruct < MiniTest::Unit::TestCase
5
- def setup
6
- @empty = OpenStruct.new
7
- @example = OpenStruct.new name: "Kurtis", age: 24
8
- end
9
-
10
- def test_equality_with_two_empty_ostructs
11
- empty2 = OpenStruct.new
12
- assert_equal @empty, empty2
13
- end
14
-
15
- def test_equality_vs_ostruct_with_fields
16
- refute_equal @example, @empty
17
- end
18
-
19
- def test_equality_vs_object_with_fake_table
20
- empty_object = Object.new
21
- empty_object.instance_eval { @table = { name: "Kurtis", age: 24 } }
22
- expected = @example
23
- actual = empty_object
24
- refute_equal expected, actual
25
- end
26
-
27
- def test_inspect_with_no_fields
28
- expected = "#<OpenStruct>"
29
- actual = @empty.inspect
30
- assert_equal expected, actual
31
- end
32
-
33
- def test_inspect_with_fields
34
- @empty.example1 = 1
35
- @empty.example2 = 2
36
- expected = "#<OpenStruct example1=1, example2=2>"
37
- actual = @empty.inspect
38
- assert_equal expected, actual
39
- end
40
-
41
- def test_inspect_with_sub_struct_duplicate
42
- @empty.struct2 = OpenStruct.new
43
- @empty.struct2.struct3 = @empty
44
- expected = '#<OpenStruct struct2=#<OpenStruct struct3=#<OpenStruct ...>>>'
45
- actual = @empty.inspect
46
- assert_equal expected, actual
47
- end
48
-
49
- def test_inspect_with_sub_struct
50
- @example.friends = OpenStruct.new name: "Jason", age: 24
51
- @example.friends.friends = OpenStruct.new name: "John", age: 15
52
- @example.friends.friends.friends = OpenStruct.new name: "Ally", age: 32
53
- expected = '#<OpenStruct name="Kurtis", age=24, friends=#<OpenStruct name="Jason", age=24, friends=#<OpenStruct name="John", age=15, friends=#<OpenStruct name="Ally", age=32>>>>'
54
- actual = @example.inspect
55
- assert_equal expected, actual
56
- end
57
-
58
- def test_inspect_with_twice_inspected_struct
59
- @example.inspect
60
- @example.inspect
61
- expected = '#<OpenStruct name="Kurtis", age=24>'
62
- actual = @example.inspect
63
- assert_equal expected, actual
64
- end
65
-
66
- def test_inspect_with_empty_sub_struct
67
- @empty.struct2 = OpenStruct.new
68
- expected = '#<OpenStruct struct2=#<OpenStruct>>'
69
- actual = @empty.inspect
70
- assert_equal expected, actual
71
- end
72
-
73
- def test_freeze_stops_new_assignments
74
- @example.freeze
75
- assert_raises(RuntimeError) { @example.age = 24 }
76
- end
77
-
78
- def test_freeze_still_returns_values
79
- @example.freeze
80
- expecteds = "Kurtis"
81
- actual = @example.name
82
- assert_equal expecteds, actual
83
- end
84
-
85
- def test_freeze_stops_reassignments
86
- @example.freeze
87
- assert_raises(RuntimeError) { @example.name = "Jazzy" }
88
- end
89
-
90
- # def test_freeze_stops_reassign_even_if_frozen_redefined
91
- # @example.freeze
92
- # def @example.frozen?; nil end
93
- # @example.freeze
94
- # message = '[ruby-core:22559]'
95
- # assert_raises(RuntimeError, message) { @example.name = "Jazzy" }
96
- # # assert_raises(TypeError, message) { @example.name = "Jazzy" }
97
- # end
98
-
99
- def test_ostruct_doesn_respond_to_non_existant_keys_getter
100
- refute_respond_to @empty, :akey
101
- end
102
-
103
- def test_ostruct_doesn_respond_to_non_existant_keys_setter
104
- refute_respond_to @empty, :akey=
105
- end
106
-
107
- def test_delete_field_removes_getter_method
108
- bug = '[ruby-core:33010]'
109
- @example.delete_field :name
110
- refute_respond_to @example, :name, bug
111
- end
112
-
113
- def test_delete_field_removes_setter_method
114
- bug = '[ruby-core:33010]'
115
- @example.delete_field :name
116
- refute_respond_to @example, :name=, bug
117
- end
118
-
119
- def test_delete_field_removes_table_key_value
120
- @example.delete_field :name
121
- expected = nil
122
- actual = @example.table[:name]
123
- assert_equal expected, actual
124
- end
125
-
126
- def test_delete_field_returns_value_of_deleted_key
127
- expected = "Kurtis"
128
- actual = @example.delete_field :name
129
- assert_equal expected, actual
130
- end
131
-
132
- def test_method_missing_handles_square_bracket_equals
133
- assert_raises(ArgumentError) { @empty[:foo] = :bar }
134
- end
135
-
136
- def test_method_missing_handles_square_brackets
137
- assert_raises(NoMethodError) { @empty[:foo] }
138
- end
139
-
140
- def test_to_hash_returns_hash
141
- expected = { name: "John Smith", age: 70, pension: 300 }
142
- actual = OpenStruct.new(expected).to_hash
143
- assert_equal expected, actual
144
- end
145
-
146
- def test_to_hash_modified_modifies_ostruct
147
- @example.to_hash[:age] = 70
148
- expected = 70
149
- actual = @example.age
150
- assert_equal expected, actual
151
- end
152
-
153
- def test_empty_example_has_empty_table
154
- expected = {}
155
- actual = @empty.table
156
- assert_equal expected, actual
157
- end
158
-
159
- def test_example_has_name_method
160
- assert_respond_to @example, :name
161
- end
162
-
163
- def test_example_has_given_name
164
- expected = "Kurtis"
165
- actual = @example.name
166
- assert_equal expected, actual
167
- end
168
-
169
- def test_example_takes_name
170
- assert_send [@example, :name=, "Dave"]
171
- end
172
-
173
- def test_example_has_taken_name
174
- @example.name = "Dave"
175
- expected = "Dave"
176
- actual = @example.name
177
- assert_equal expected, actual
178
- end
179
-
180
- def test_load_takes_a_hash
181
- assert_send [@example, :load, { nickname: "Kurt" }]
182
- end
183
-
184
- def test_load_sets_methods
185
- @example.load nickname: "Kurt"
186
- assert_respond_to @example, :nickname
187
- end
188
-
189
- def test_ostruct_has_getter_methods_with_non_alpha_numeric_characters
190
- @example.load "Length (In Inchs)" => 72
191
- assert_send [@example, :"Length (In Inchs)"]
192
- end
193
-
194
- def test_ostruct_has_getter_methods_with_non_alpha_numeric_characters
195
- @example.load "Length (In Inchs)" => 72
196
- assert_send [@example, :"Length (In Inchs)=", 73]
197
- end
198
-
199
- def test_load_sets_correct_value
200
- @example.load nickname: "Kurt"
201
- expected = "Kurt"
202
- actual = @example.nickname
203
- assert_equal expected, actual
204
- end
205
-
206
- def test_dump_contains_values
207
- expected = { name: "Kurtis", age: 24 }
208
- actual = @example.marshal_dump
209
- assert_equal expected, actual
210
- end
211
-
212
- def test_selective_dump_contains_selective_values
213
- expected = { age: 24 }
214
- actual = @example.dump :age
215
- assert_equal expected, actual
216
- end
217
-
218
- def test_inspect_has_values_delimited_by_comma
219
- expected = /name="Kurtis", age=24/
220
- actual = @example.inspect
221
- assert_match expected, actual
222
- end
223
-
224
- def test_other_object_isnt_affected
225
- refute_respond_to @empty, :name
226
- end
227
- end
1
+ # require 'helper'
2
+ # require 'minitest/autorun'
3
+
4
+ # class TestOpenStruct < MiniTest::Unit::TestCase
5
+ # def setup
6
+ # @empty = OpenStruct.new
7
+ # @example = OpenStruct.new name: "Kurtis", age: 24
8
+ # end
9
+
10
+ # def test_equality_with_two_empty_ostructs
11
+ # empty2 = OpenStruct.new
12
+ # assert_equal @empty, empty2
13
+ # end
14
+
15
+ # def test_equality_vs_ostruct_with_fields
16
+ # refute_equal @example, @empty
17
+ # end
18
+
19
+ # def test_equality_vs_object_with_fake_table
20
+ # empty_object = Object.new
21
+ # empty_object.instance_eval { @table = { name: "Kurtis", age: 24 } }
22
+ # expected = @example
23
+ # actual = empty_object
24
+ # refute_equal expected, actual
25
+ # end
26
+
27
+ # def test_inspect_with_no_fields
28
+ # expected = "#<OpenStruct>"
29
+ # actual = @empty.inspect
30
+ # assert_equal expected, actual
31
+ # end
32
+
33
+ # def test_inspect_with_fields
34
+ # @empty.example1 = 1
35
+ # @empty.example2 = 2
36
+ # expected = "#<OpenStruct example1=1, example2=2>"
37
+ # actual = @empty.inspect
38
+ # assert_equal expected, actual
39
+ # end
40
+
41
+ # def test_inspect_with_sub_struct_duplicate
42
+ # @empty.struct2 = OpenStruct.new
43
+ # @empty.struct2.struct3 = @empty
44
+ # expected = '#<OpenStruct struct2=#<OpenStruct struct3=#<OpenStruct ...>>>'
45
+ # actual = @empty.inspect
46
+ # assert_equal expected, actual
47
+ # end
48
+
49
+ # def test_inspect_with_sub_struct
50
+ # @example.friends = OpenStruct.new name: "Jason", age: 24
51
+ # @example.friends.friends = OpenStruct.new name: "John", age: 15
52
+ # @example.friends.friends.friends = OpenStruct.new name: "Ally", age: 32
53
+ # expected = '#<OpenStruct name="Kurtis", age=24, friends=#<OpenStruct name="Jason", age=24, friends=#<OpenStruct name="John", age=15, friends=#<OpenStruct name="Ally", age=32>>>>'
54
+ # actual = @example.inspect
55
+ # assert_equal expected, actual
56
+ # end
57
+
58
+ # def test_inspect_with_twice_inspected_struct
59
+ # @example.inspect
60
+ # @example.inspect
61
+ # expected = '#<OpenStruct name="Kurtis", age=24>'
62
+ # actual = @example.inspect
63
+ # assert_equal expected, actual
64
+ # end
65
+
66
+ # def test_inspect_with_empty_sub_struct
67
+ # @empty.struct2 = OpenStruct.new
68
+ # expected = '#<OpenStruct struct2=#<OpenStruct>>'
69
+ # actual = @empty.inspect
70
+ # assert_equal expected, actual
71
+ # end
72
+
73
+ # def test_freeze_stops_new_assignments
74
+ # @example.freeze
75
+ # assert_raises(RuntimeError) { @example.age = 24 }
76
+ # end
77
+
78
+ # def test_freeze_still_returns_values
79
+ # @example.freeze
80
+ # expecteds = "Kurtis"
81
+ # actual = @example.name
82
+ # assert_equal expecteds, actual
83
+ # end
84
+
85
+ # def test_freeze_stops_reassignments
86
+ # @example.freeze
87
+ # assert_raises(RuntimeError) { @example.name = "Jazzy" }
88
+ # end
89
+
90
+ # # def test_freeze_stops_reassign_even_if_frozen_redefined
91
+ # # @example.freeze
92
+ # # def @example.frozen?; nil end
93
+ # # @example.freeze
94
+ # # message = '[ruby-core:22559]'
95
+ # # assert_raises(RuntimeError, message) { @example.name = "Jazzy" }
96
+ # # # assert_raises(TypeError, message) { @example.name = "Jazzy" }
97
+ # # end
98
+
99
+ # def test_ostruct_doesn_respond_to_non_existant_keys_getter
100
+ # refute_respond_to @empty, :akey
101
+ # end
102
+
103
+ # def test_ostruct_doesn_respond_to_non_existant_keys_setter
104
+ # refute_respond_to @empty, :akey=
105
+ # end
106
+
107
+ # def test_delete_field_removes_getter_method
108
+ # bug = '[ruby-core:33010]'
109
+ # @example.delete_field :name
110
+ # refute_respond_to @example, :name, bug
111
+ # end
112
+
113
+ # def test_delete_field_removes_setter_method
114
+ # bug = '[ruby-core:33010]'
115
+ # @example.delete_field :name
116
+ # refute_respond_to @example, :name=, bug
117
+ # end
118
+
119
+ # def test_delete_field_removes_table_key_value
120
+ # @example.delete_field :name
121
+ # expected = nil
122
+ # actual = @example.table[:name]
123
+ # assert_equal expected, actual
124
+ # end
125
+
126
+ # def test_delete_field_returns_value_of_deleted_key
127
+ # expected = "Kurtis"
128
+ # actual = @example.delete_field :name
129
+ # assert_equal expected, actual
130
+ # end
131
+
132
+ # def test_method_missing_handles_square_bracket_equals
133
+ # assert_raises(ArgumentError) { @empty[:foo] = :bar }
134
+ # end
135
+
136
+ # def test_method_missing_handles_square_brackets
137
+ # assert_raises(NoMethodError) { @empty[:foo] }
138
+ # end
139
+
140
+ # def test_to_hash_returns_hash
141
+ # expected = { name: "John Smith", age: 70, pension: 300 }
142
+ # actual = OpenStruct.new(expected).to_hash
143
+ # assert_equal expected, actual
144
+ # end
145
+
146
+ # def test_to_hash_modified_modifies_ostruct
147
+ # @example.to_hash[:age] = 70
148
+ # expected = 70
149
+ # actual = @example.age
150
+ # assert_equal expected, actual
151
+ # end
152
+
153
+ # def test_empty_example_has_empty_table
154
+ # expected = {}
155
+ # actual = @empty.table
156
+ # assert_equal expected, actual
157
+ # end
158
+
159
+ # def test_example_has_name_method
160
+ # assert_respond_to @example, :name
161
+ # end
162
+
163
+ # def test_example_has_given_name
164
+ # expected = "Kurtis"
165
+ # actual = @example.name
166
+ # assert_equal expected, actual
167
+ # end
168
+
169
+ # def test_example_takes_name
170
+ # assert_send [@example, :name=, "Dave"]
171
+ # end
172
+
173
+ # def test_example_has_taken_name
174
+ # @example.name = "Dave"
175
+ # expected = "Dave"
176
+ # actual = @example.name
177
+ # assert_equal expected, actual
178
+ # end
179
+
180
+ # def test_load_takes_a_hash
181
+ # assert_send [@example, :load, { nickname: "Kurt" }]
182
+ # end
183
+
184
+ # def test_load_sets_methods
185
+ # @example.load nickname: "Kurt"
186
+ # assert_respond_to @example, :nickname
187
+ # end
188
+
189
+ # def test_ostruct_has_getter_methods_with_non_alpha_numeric_characters
190
+ # @example.load "Length (In Inchs)" => 72
191
+ # assert_send [@example, :"Length (In Inchs)"]
192
+ # end
193
+
194
+ # def test_ostruct_has_getter_methods_with_non_alpha_numeric_characters
195
+ # @example.load "Length (In Inchs)" => 72
196
+ # assert_send [@example, :"Length (In Inchs)=", 73]
197
+ # end
198
+
199
+ # def test_load_sets_correct_value
200
+ # @example.load nickname: "Kurt"
201
+ # expected = "Kurt"
202
+ # actual = @example.nickname
203
+ # assert_equal expected, actual
204
+ # end
205
+
206
+ # def test_dump_contains_values
207
+ # expected = { name: "Kurtis", age: 24 }
208
+ # actual = @example.marshal_dump
209
+ # assert_equal expected, actual
210
+ # end
211
+
212
+ # def test_selective_dump_contains_selective_values
213
+ # expected = { age: 24 }
214
+ # actual = @example.dump :age
215
+ # assert_equal expected, actual
216
+ # end
217
+
218
+ # def test_inspect_has_values_delimited_by_comma
219
+ # expected = /name="Kurtis", age=24/
220
+ # actual = @example.inspect
221
+ # assert_match expected, actual
222
+ # end
223
+
224
+ # def test_other_object_isnt_affected
225
+ # refute_respond_to @empty, :name
226
+ # end
227
+ # end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: astruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.9.0
4
+ version: 2.11.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-25 00:00:00.000000000 Z
12
+ date: 2012-11-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ~>
19
+ - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: '0.9'
21
+ version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ~>
27
+ - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: '0.9'
29
+ version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: yard
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -75,6 +75,22 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
78
94
  description: An alternative to OpenStruct
79
95
  email:
80
96
  - kurtisrainboltgreene@gmail.com
@@ -99,6 +115,7 @@ files:
99
115
  - bench/load_vs_ostruct.rb
100
116
  - bench/nested_vs_ostruct.rb
101
117
  - bench/new_vs_ostruct.rb
118
+ - bench/some_updates_vs_ostruct.rb
102
119
  - lib/astruct.rb
103
120
  - lib/astruct/module.rb
104
121
  - lib/astruct/version.rb
@@ -119,7 +136,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
119
136
  version: '0'
120
137
  segments:
121
138
  - 0
122
- hash: -2502922354495279839
139
+ hash: -3183751075223347111
123
140
  required_rubygems_version: !ruby/object:Gem::Requirement
124
141
  none: false
125
142
  requirements:
@@ -128,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
145
  version: '0'
129
146
  segments:
130
147
  - 0
131
- hash: -2502922354495279839
148
+ hash: -3183751075223347111
132
149
  requirements: []
133
150
  rubyforge_project:
134
151
  rubygems_version: 1.8.24