cauterize 0.0.1.pre13 → 0.0.1.pre14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +33 -33
  3. data/.rspec +1 -1
  4. data/.travisci.yml +4 -4
  5. data/Gemfile +4 -4
  6. data/LICENSE.txt +21 -21
  7. data/README.md +177 -177
  8. data/Rakefile +54 -54
  9. data/bin/cauterize +61 -61
  10. data/cauterize.gemspec +27 -27
  11. data/example/Cauterize +68 -68
  12. data/example/build.sh +25 -25
  13. data/example/c_example_support/empty_main.c +1 -1
  14. data/example/c_example_support/example_project_config.c +11 -11
  15. data/example/c_example_support/example_project_config.h +15 -15
  16. data/example/ruby_ex.rb +14 -14
  17. data/lib/cauterize.rb +117 -117
  18. data/lib/cauterize/base_type.rb +78 -78
  19. data/lib/cauterize/builders.rb +41 -41
  20. data/lib/cauterize/builders/c/buildable.rb +91 -91
  21. data/lib/cauterize/builders/c/builtin.rb +43 -43
  22. data/lib/cauterize/builders/c/composite.rb +57 -57
  23. data/lib/cauterize/builders/c/enumeration.rb +60 -60
  24. data/lib/cauterize/builders/c/fixed_array.rb +74 -74
  25. data/lib/cauterize/builders/c/group.rb +143 -143
  26. data/lib/cauterize/builders/c/scalar.rb +34 -34
  27. data/lib/cauterize/builders/c/variable_array.rb +98 -98
  28. data/lib/cauterize/builders/cs/buildable.rb +59 -59
  29. data/lib/cauterize/builders/cs/builtin.rb +22 -22
  30. data/lib/cauterize/builders/cs/composite.rb +21 -21
  31. data/lib/cauterize/builders/cs/csarray.rb +32 -32
  32. data/lib/cauterize/builders/cs/enumeration.rb +21 -21
  33. data/lib/cauterize/builders/cs/fixed_array.rb +27 -27
  34. data/lib/cauterize/builders/cs/group.rb +32 -32
  35. data/lib/cauterize/builders/cs/scalar.rb +10 -10
  36. data/lib/cauterize/builders/cs/variable_array.rb +40 -34
  37. data/lib/cauterize/builders/doc/buildable.rb +30 -30
  38. data/lib/cauterize/builders/doc/builtin.rb +21 -21
  39. data/lib/cauterize/builders/doc/composite.rb +23 -23
  40. data/lib/cauterize/builders/doc/enumeration.rb +24 -24
  41. data/lib/cauterize/builders/doc/fixed_array.rb +26 -26
  42. data/lib/cauterize/builders/doc/group.rb +29 -29
  43. data/lib/cauterize/builders/doc/scalar.rb +21 -21
  44. data/lib/cauterize/builders/doc/variable_array.rb +26 -26
  45. data/lib/cauterize/builders/ruby/buildable.rb +11 -11
  46. data/lib/cauterize/builders/ruby/builtin.rb +25 -25
  47. data/lib/cauterize/builders/ruby/composite.rb +26 -26
  48. data/lib/cauterize/builders/ruby/enumeration.rb +27 -27
  49. data/lib/cauterize/builders/ruby/fixed_array.rb +20 -20
  50. data/lib/cauterize/builders/ruby/group.rb +35 -35
  51. data/lib/cauterize/builders/ruby/scalar.rb +19 -19
  52. data/lib/cauterize/builders/ruby/variable_array.rb +22 -22
  53. data/lib/cauterize/builtin.rb +62 -62
  54. data/lib/cauterize/c_builder.rb +103 -103
  55. data/lib/cauterize/cauterize.rb +76 -76
  56. data/lib/cauterize/composite.rb +64 -64
  57. data/lib/cauterize/cs_builder.rb +58 -58
  58. data/lib/cauterize/doc_builder.rb +34 -34
  59. data/lib/cauterize/enumeration.rb +98 -98
  60. data/lib/cauterize/fixed_array.rb +50 -50
  61. data/lib/cauterize/formatter.rb +67 -67
  62. data/lib/cauterize/group.rb +81 -81
  63. data/lib/cauterize/representation.rb +32 -32
  64. data/lib/cauterize/ruby_builder.rb +44 -44
  65. data/lib/cauterize/scalar.rb +53 -53
  66. data/lib/cauterize/snake_case.rb +21 -21
  67. data/lib/cauterize/variable_array.rb +55 -55
  68. data/lib/cauterize/version.rb +3 -3
  69. data/spec/base_type_spec.rb +251 -251
  70. data/spec/builders/c/buildable_spec.rb +25 -25
  71. data/spec/builders/c/builtin_spec.rb +22 -22
  72. data/spec/builders/c/composite_spec.rb +50 -50
  73. data/spec/builders/c/enumeration_spec.rb +95 -95
  74. data/spec/builders/c/fixed_array_spec.rb +36 -36
  75. data/spec/builders/c/group_spec.rb +140 -140
  76. data/spec/builders/c/scalar_spec.rb +26 -26
  77. data/spec/builders/c/variable_array_spec.rb +48 -48
  78. data/spec/builders/cs/buildable_spec.rb +8 -8
  79. data/spec/builders/cs/composite_spec.rb +32 -32
  80. data/spec/builders/cs/enumeration_spec.rb +33 -33
  81. data/spec/builders/cs/fixed_array_spec.rb +41 -41
  82. data/spec/builders/cs/group_spec.rb +56 -56
  83. data/spec/builders/cs/scalar_spec.rb +7 -7
  84. data/spec/builders/cs/variable_array_spec.rb +49 -45
  85. data/spec/builders/doc/buildable_spec.rb +25 -25
  86. data/spec/builders_spec.rb +57 -57
  87. data/spec/builtin_spec.rb +43 -43
  88. data/spec/c_builder_spec.rb +176 -176
  89. data/spec/cauterize_spec.rb +15 -15
  90. data/spec/composite_spec.rb +77 -77
  91. data/spec/cs_builder_spec.rb +115 -115
  92. data/spec/doc_builder_spec.rb +260 -260
  93. data/spec/enumeration_spec.rb +145 -145
  94. data/spec/fixed_array_spec.rb +61 -61
  95. data/spec/group_spec.rb +111 -111
  96. data/spec/ruby_builder_spec.rb +83 -83
  97. data/spec/ruby_generated_spec.rb +735 -735
  98. data/spec/scalar_spec.rb +44 -44
  99. data/spec/spec_helper.rb +122 -122
  100. data/spec/support/shared_examples_for_array_buildables.rb +22 -22
  101. data/spec/support/shared_examples_for_c_buildables.rb +103 -103
  102. data/spec/support/shared_examples_for_sane_c_buildables.rb +22 -22
  103. data/spec/support/shared_examples_for_stubbed_functions.rb +18 -18
  104. data/spec/support/spec_sample_model.rb +74 -74
  105. data/spec/test_main.c +13 -13
  106. data/spec/variable_array_spec.rb +73 -73
  107. data/support/c/src/cauterize.c +75 -75
  108. data/support/c/src/cauterize.h +60 -60
  109. data/support/c/src/cauterize_debug.h +29 -29
  110. data/support/c/src/cauterize_util.h +8 -8
  111. data/support/c/test/greatest.h +536 -536
  112. data/support/c/test/test.c +166 -166
  113. data/support/cs/src/CauterizeCompositeFormatter.cs +0 -0
  114. data/support/cs/src/CauterizeContainerFormatter.cs +0 -0
  115. data/support/cs/src/CauterizeEnumFormatter.cs +0 -0
  116. data/support/cs/src/CauterizeException.cs +15 -15
  117. data/support/cs/src/CauterizeFixedArrayFormatter.cs +2 -2
  118. data/support/cs/src/CauterizeFormatter.cs +0 -0
  119. data/support/cs/src/CauterizeGroupFormatter.cs +0 -0
  120. data/support/cs/src/CauterizeTypeFormatterFactory.cs +0 -0
  121. data/support/cs/src/CauterizeTypes.cs +46 -12
  122. data/support/cs/src/CauterizeVariableArrayFormatter.cs +1 -1
  123. data/support/cs/src/ICauterizeTypeFormatter.cs +0 -0
  124. data/support/cs/src/OrderAttribute.cs +0 -0
  125. data/support/cs/src/PrimitiveSupport.cs +12 -0
  126. data/support/cs/test/CauterizeCompositeFormatterTest.cs +0 -0
  127. data/support/cs/test/CauterizeEnumFormatterTest.cs +0 -0
  128. data/support/cs/test/CauterizeFixedArrayFormatterTest.cs +0 -0
  129. data/support/cs/test/CauterizeFormatterTest.cs +0 -0
  130. data/support/cs/test/CauterizeGroupFormatterTest.cs +0 -0
  131. data/support/cs/test/CauterizeIntegrationTest.cs +0 -0
  132. data/support/cs/test/CauterizePrimitiveFormatterTest.cs +0 -0
  133. data/support/cs/test/CauterizeTypeFormatterFactoryTest.cs +0 -0
  134. data/support/cs/test/CauterizeVariableArrayFormatterTest.cs +0 -0
  135. data/support/cs/test/OrderAttributeTest.cs +0 -0
  136. data/support/ruby/src/cauterize_ruby_baseclasses.rb +367 -367
  137. data/support/ruby/src/cauterize_ruby_builtins.rb +148 -148
  138. metadata +20 -20
@@ -1,32 +1,32 @@
1
- class Representation
2
- def self.lengthRepresentation(max_length)
3
- if max_length < 2**8
4
- Cauterize::BaseType.find_type!(:uint8)
5
- elsif max_length < 2**16
6
- Cauterize::BaseType.find_type!(:uint16)
7
- elsif max_length < 2**32
8
- Cauterize::BaseType.find_type!(:uint32)
9
- elsif max_length < 2**64
10
- Cauterize::BaseType.find_type!(:uint64)
11
- else
12
- raise Exception.new("Unable to represent array length (#{max_length}).")
13
- end
14
- end
15
-
16
- def self.enumRepresentation(args)
17
- max_val = args[:max]
18
- min_val = args[:min]
19
-
20
- if -128 <= min_val and max_val <= 127
21
- Cauterize::BaseType.find_type!(:int8)
22
- elsif (-32768 <= min_val and max_val <= 32767)
23
- Cauterize::BaseType.find_type!(:int16)
24
- elsif (-2147483648 <= min_val and max_val <= 2147483647)
25
- Cauterize::BaseType.find_type!(:int32)
26
- elsif (-9223372036854775808 <= min_val and max_val <= 9223372036854775807)
27
- Cauterize::BaseType.find_type!(:int64)
28
- else
29
- raise Exception.new("Unable to represent enumeration (#{min_val} -> #{max_val})")
30
- end
31
- end
32
- end
1
+ class Representation
2
+ def self.lengthRepresentation(max_length)
3
+ if max_length < 2**8
4
+ Cauterize::BaseType.find_type!(:uint8)
5
+ elsif max_length < 2**16
6
+ Cauterize::BaseType.find_type!(:uint16)
7
+ elsif max_length < 2**32
8
+ Cauterize::BaseType.find_type!(:uint32)
9
+ elsif max_length < 2**64
10
+ Cauterize::BaseType.find_type!(:uint64)
11
+ else
12
+ raise Exception.new("Unable to represent array length (#{max_length}).")
13
+ end
14
+ end
15
+
16
+ def self.enumRepresentation(args)
17
+ max_val = args[:max]
18
+ min_val = args[:min]
19
+
20
+ if -128 <= min_val and max_val <= 127
21
+ Cauterize::BaseType.find_type!(:int8)
22
+ elsif (-32768 <= min_val and max_val <= 32767)
23
+ Cauterize::BaseType.find_type!(:int16)
24
+ elsif (-2147483648 <= min_val and max_val <= 2147483647)
25
+ Cauterize::BaseType.find_type!(:int32)
26
+ elsif (-9223372036854775808 <= min_val and max_val <= 9223372036854775807)
27
+ Cauterize::BaseType.find_type!(:int64)
28
+ else
29
+ raise Exception.new("Unable to represent enumeration (#{min_val} -> #{max_val})")
30
+ end
31
+ end
32
+ end
@@ -1,44 +1,44 @@
1
- require 'time'
2
-
3
- module Cauterize
4
- class RubyBuilder
5
- attr_reader :rb
6
- def initialize(rb_file, name="cauterize")
7
- @rb = rb_file
8
- @name = name.camel
9
- end
10
-
11
- def build
12
- build_rb
13
- end
14
-
15
- def build_rb
16
- f = []
17
-
18
- f << "# WARNING: This is generated code. Do not edit this file directly."
19
- f << ""
20
- f << "require_relative './cauterize_ruby_builtins'"
21
- f << ""
22
- f << "module #{Cauterize.get_name.camel}"
23
- f << ""
24
- f << " CAUTERIZE_GEN_VERSION = \"#{Cauterize.get_version}\""
25
- f << " CAUTERIZE_GEN_DATE = \"#{DateTime.now.to_s}\""
26
- f << ""
27
-
28
- f << " CAUTERIZE_MODEL_HASH_LEN = #{BaseType.digest_class.new.length}"
29
- f << " CAUTERIZE_MODEL_HASH = [#{BaseType.model_hash.bytes.to_a.join(", ")}]"
30
- f << ""
31
-
32
- instances = BaseType.all_instances
33
- builders = instances.map {|i| Builders.get(:ruby, i)}
34
- builders.each { |b| b.class_defn(f) }
35
- f << "end"
36
- f << ""
37
-
38
- File.open(@rb, "wb") do |fh|
39
- fh.write(f.join("\n").to_s)
40
- end
41
- end
42
-
43
- end
44
- end
1
+ require 'time'
2
+
3
+ module Cauterize
4
+ class RubyBuilder
5
+ attr_reader :rb
6
+ def initialize(rb_file, name="cauterize")
7
+ @rb = rb_file
8
+ @name = name.camel
9
+ end
10
+
11
+ def build
12
+ build_rb
13
+ end
14
+
15
+ def build_rb
16
+ f = []
17
+
18
+ f << "# WARNING: This is generated code. Do not edit this file directly."
19
+ f << ""
20
+ f << "require_relative './cauterize_ruby_builtins'"
21
+ f << ""
22
+ f << "module #{Cauterize.get_name.camel}"
23
+ f << ""
24
+ f << " CAUTERIZE_GEN_VERSION = \"#{Cauterize.get_version}\""
25
+ f << " CAUTERIZE_GEN_DATE = \"#{DateTime.now.to_s}\""
26
+ f << ""
27
+
28
+ f << " CAUTERIZE_MODEL_HASH_LEN = #{BaseType.digest_class.new.length}"
29
+ f << " CAUTERIZE_MODEL_HASH = [#{BaseType.model_hash.bytes.to_a.join(", ")}]"
30
+ f << ""
31
+
32
+ instances = BaseType.all_instances
33
+ builders = instances.map {|i| Builders.get(:ruby, i)}
34
+ builders.each { |b| b.class_defn(f) }
35
+ f << "end"
36
+ f << ""
37
+
38
+ File.open(@rb, "wb") do |fh|
39
+ fh.write(f.join("\n").to_s)
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -1,53 +1,53 @@
1
- # scalar.rb
2
- #
3
- # scalars are types that, in C, can be updated with assignment. This includes
4
- # native types in C and typedefs around these native types.
5
- #
6
- # scalars are not structs, enumerations, unions, or arrays.
7
-
8
- module Cauterize
9
- module_function
10
-
11
- def scalar(name, desc=nil)
12
- a = Cauterize.scalars[name] || Cauterize.scalars[name] = Scalar.new(name, desc)
13
- yield a if block_given?
14
- return a
15
- end
16
-
17
- def scalar!(name, &blk)
18
- if Cauterize.scalars[name]
19
- raise Exception.new("Scalar with name #{name} already exists.")
20
- else
21
- Cauterize.scalar(name, &blk)
22
- end
23
- end
24
-
25
- def scalars
26
- @scalars ||= {}
27
- end
28
-
29
- class Scalar < BaseType
30
- def initialize(name, desc=nil)
31
- super
32
- end
33
-
34
- def type_name(type_name = nil)
35
- if type_name
36
- t = BaseType.find_type!(type_name)
37
- if t.class != BuiltIn
38
- raise Exception.new("Must specify a BuiltIn type for scalars.")
39
- end
40
-
41
- @type_name = t
42
- else
43
- @type_name
44
- end
45
- end
46
-
47
- protected
48
-
49
- def local_hash(digest)
50
- @type_name.type_hash(digest)
51
- end
52
- end
53
- end
1
+ # scalar.rb
2
+ #
3
+ # scalars are types that, in C, can be updated with assignment. This includes
4
+ # native types in C and typedefs around these native types.
5
+ #
6
+ # scalars are not structs, enumerations, unions, or arrays.
7
+
8
+ module Cauterize
9
+ module_function
10
+
11
+ def scalar(name, desc=nil)
12
+ a = Cauterize.scalars[name] || Cauterize.scalars[name] = Scalar.new(name, desc)
13
+ yield a if block_given?
14
+ return a
15
+ end
16
+
17
+ def scalar!(name, &blk)
18
+ if Cauterize.scalars[name]
19
+ raise Exception.new("Scalar with name #{name} already exists.")
20
+ else
21
+ Cauterize.scalar(name, &blk)
22
+ end
23
+ end
24
+
25
+ def scalars
26
+ @scalars ||= {}
27
+ end
28
+
29
+ class Scalar < BaseType
30
+ def initialize(name, desc=nil)
31
+ super
32
+ end
33
+
34
+ def type_name(type_name = nil)
35
+ if type_name
36
+ t = BaseType.find_type!(type_name)
37
+ if t.class != BuiltIn
38
+ raise Exception.new("Must specify a BuiltIn type for scalars.")
39
+ end
40
+
41
+ @type_name = t
42
+ else
43
+ @type_name
44
+ end
45
+ end
46
+
47
+ protected
48
+
49
+ def local_hash(digest)
50
+ @type_name.type_hash(digest)
51
+ end
52
+ end
53
+ end
@@ -1,21 +1,21 @@
1
- class String
2
- def snake
3
- self.gsub(/::/, '/').
4
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
5
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
6
- tr("-", "_")
7
- end
8
-
9
- def up_snake
10
- snake.upcase
11
- end
12
-
13
- def down_snake
14
- snake.downcase
15
- end
16
-
17
- def camel
18
- return self.capitalize if self !~ /_/ && self =~ /[A-z]+.*/
19
- split('_').map{|e| e.capitalize}.join
20
- end
21
- end
1
+ class String
2
+ def snake
3
+ self.gsub(/::/, '/').
4
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
5
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
6
+ tr("-", "_")
7
+ end
8
+
9
+ def up_snake
10
+ snake.upcase
11
+ end
12
+
13
+ def down_snake
14
+ snake.downcase
15
+ end
16
+
17
+ def camel
18
+ return self.capitalize if self !~ /_/ && self =~ /[A-z]+.*/
19
+ split('_').map{|e| e.capitalize}.join
20
+ end
21
+ end
@@ -1,55 +1,55 @@
1
- module Cauterize
2
- module_function
3
-
4
- def variable_array(name, desc=nil)
5
- a = Cauterize.variable_arrays[name] || Cauterize.variable_arrays[name] = VariableArray.new(name, desc)
6
- yield a if block_given?
7
- return a
8
- end
9
-
10
- def variable_array!(name, &blk)
11
- if Cauterize.variable_arrays[name]
12
- raise Exception.new("VariableArray with name #{name} already exists.")
13
- else
14
- Cauterize.variable_array(name, &blk)
15
- end
16
- end
17
-
18
- def variable_arrays
19
- @variable_arrays ||= {}
20
- end
21
-
22
- class VariableArray < BaseType
23
- def intialize(name, desc=nil)
24
- super
25
- end
26
-
27
- def array_type(t = nil)
28
- if t
29
- @array_type = BaseType.find_type!(t)
30
- else
31
- @array_type
32
- end
33
- end
34
-
35
- def array_size(s = nil)
36
- if s
37
- @array_size = s
38
- else
39
- @array_size
40
- end
41
- end
42
-
43
- def size_type
44
- Representation.lengthRepresentation(@array_size)
45
- end
46
-
47
- protected
48
-
49
- def local_hash(digest)
50
- digest.update(@array_size.to_s)
51
- @array_type.type_hash(digest)
52
- size_type.type_hash(digest)
53
- end
54
- end
55
- end
1
+ module Cauterize
2
+ module_function
3
+
4
+ def variable_array(name, desc=nil)
5
+ a = Cauterize.variable_arrays[name] || Cauterize.variable_arrays[name] = VariableArray.new(name, desc)
6
+ yield a if block_given?
7
+ return a
8
+ end
9
+
10
+ def variable_array!(name, &blk)
11
+ if Cauterize.variable_arrays[name]
12
+ raise Exception.new("VariableArray with name #{name} already exists.")
13
+ else
14
+ Cauterize.variable_array(name, &blk)
15
+ end
16
+ end
17
+
18
+ def variable_arrays
19
+ @variable_arrays ||= {}
20
+ end
21
+
22
+ class VariableArray < BaseType
23
+ def intialize(name, desc=nil)
24
+ super
25
+ end
26
+
27
+ def array_type(t = nil)
28
+ if t
29
+ @array_type = BaseType.find_type!(t)
30
+ else
31
+ @array_type
32
+ end
33
+ end
34
+
35
+ def array_size(s = nil)
36
+ if s
37
+ @array_size = s
38
+ else
39
+ @array_size
40
+ end
41
+ end
42
+
43
+ def size_type
44
+ Representation.lengthRepresentation(@array_size)
45
+ end
46
+
47
+ protected
48
+
49
+ def local_hash(digest)
50
+ digest.update(@array_size.to_s)
51
+ @array_type.type_hash(digest)
52
+ size_type.type_hash(digest)
53
+ end
54
+ end
55
+ end
@@ -1,3 +1,3 @@
1
- module Cauterize
2
- VERSION = "0.0.1.pre13"
3
- end
1
+ module Cauterize
2
+ VERSION = "0.0.1.pre14"
3
+ end
@@ -1,251 +1,251 @@
1
- module Cauterize
2
-
3
- describe Cauterize do
4
- describe BaseType do
5
- describe :description do
6
- it "handles nil description" do
7
- BaseType.new(:foo, nil).description.should be_nil
8
- end
9
-
10
- it "sets the description" do
11
- BaseType.new(:foo, "a desc").description.should == "a desc"
12
- end
13
- end
14
-
15
- describe :register_instance do
16
- it "adds an instance to the instance list" do
17
- orig_len = BaseType.all_instances.length
18
-
19
- class X < BaseType
20
- def initialize(name); end
21
- end
22
- x = X.new(:foo)
23
-
24
- x.instance_exec do
25
- register_instance(x)
26
- end
27
-
28
- BaseType.all_instances.last.should be x
29
- BaseType.all_instances.length.should == orig_len + 1
30
- end
31
- end
32
-
33
- describe :instances do
34
- # Two things are being tested here.
35
- # 1. That instances works.
36
- # 2. That super() is called in each .new
37
- it "is every instance of a BaseType-derived class" do
38
- BaseType.class_variable_set(:@@instances, {})
39
-
40
- b = BuiltIn.new(:uint32)
41
- s = Scalar.new(:eek)
42
- e = Enumeration.new(:emoo)
43
- c = Composite.new(:cooo)
44
- f = FixedArray.new(:moo)
45
- v = VariableArray.new(:quack)
46
- g = Group.new(:goo)
47
-
48
- lst = [b, s, e, c, f, v, g]
49
-
50
- instances = BaseType.all_instances
51
-
52
- # There will be some extras due to automatic creation of enums and
53
- # scalars in Enumeration and Group.
54
- instances.should include(*lst)
55
-
56
- # Check that each of our instances shows up in the list returned from
57
- # all_instances. Do it this way in case there are other types created
58
- # (like the enumeration for Group).
59
- lst.all? do |l|
60
- instances.any? {|i| i.equal? l}
61
- end
62
- end
63
- end
64
-
65
- describe :type_hash do
66
- it "is different for objects with different names" do
67
- f = Cauterize.scalar(:foo) {|t| t.type_name :uint8}.type_hash
68
- g = Cauterize.scalar(:bar) {|t| t.type_name :uint8}.type_hash
69
-
70
- f.should_not == g
71
- end
72
-
73
- it "is different for objects with different types" do
74
- f = Cauterize.scalar(:foo) {|t| t.type_name :uint8}.type_hash
75
- reset_for_test
76
-
77
- g = Cauterize.scalar(:foo) {|t| t.type_name :int8}.type_hash
78
- reset_for_test
79
-
80
- f.should_not == g
81
- end
82
-
83
- it "differes on enumeration value differences" do
84
- f = Cauterize.enumeration(:foo) { |t| t.value :a, 0 }.type_hash
85
- reset_for_test
86
-
87
- g = Cauterize.enumeration(:foo) { |t| t.value :a, 1 }.type_hash
88
- reset_for_test
89
-
90
- f.should_not == g
91
- end
92
-
93
- it "differs on differing length in fixed arrays" do
94
- f = Cauterize.fixed_array(:foo) { |t| t.array_type :int8; t.array_size 1 }.type_hash
95
- reset_for_test
96
-
97
- g = Cauterize.fixed_array(:foo) { |t| t.array_type :int8; t.array_size 2 }.type_hash
98
- reset_for_test
99
-
100
- f.should_not == g
101
- end
102
-
103
- it "differs on differing size type in variable arrays" do
104
- f = Cauterize.variable_array(:foo) { |t| t.array_type :int8; t.array_size 2 }.type_hash
105
- reset_for_test
106
-
107
- g = Cauterize.variable_array(:foo) { |t| t.array_type :int8; t.array_size 2000 }.type_hash
108
- reset_for_test
109
-
110
- f.should_not == g
111
- end
112
-
113
- it "differs on group field reordering" do
114
- f = Cauterize.group(:foo) do |t|
115
- t.field :a, :int8
116
- t.field :b, :int8
117
- end.type_hash
118
- reset_for_test
119
-
120
- g = Cauterize.group(:foo) do |t|
121
- t.field :b, :int8
122
- t.field :a, :int8
123
- end.type_hash
124
- reset_for_test
125
-
126
- f.should_not == g
127
- end
128
-
129
- it "differs on composite field reordering" do
130
- f = Cauterize.composite(:foo) do |t|
131
- t.field :a, :int8
132
- t.field :b, :int8
133
- end.type_hash
134
- reset_for_test
135
-
136
- g = Cauterize.composite(:foo) do |t|
137
- t.field :b, :int8
138
- t.field :a, :int8
139
- end.type_hash
140
- reset_for_test
141
-
142
- f.should_not == g
143
- end
144
-
145
- it "differs on changes in indirect fields" do
146
- Cauterize.scalar(:a_thing) { |t| t.type_name :int8 }
147
- f = Cauterize.composite(:foo) do |t|
148
- t.field :a, :a_thing
149
- end.type_hash
150
- reset_for_test
151
-
152
- Cauterize.scalar(:a_thing) { |t| t.type_name :uint8 }
153
- g = Cauterize.composite(:foo) do |t|
154
- t.field :a, :a_thing
155
- end.type_hash
156
- reset_for_test
157
-
158
- f.should_not == g
159
- end
160
- end
161
-
162
- describe :model_hash do
163
- it "returns a SHA1 of the model" do
164
- Cauterize.scalar(:foo) {|t| t.type_name :uint8}
165
-
166
- h = BaseType.model_hash
167
- h.should_not be_nil
168
- h.length.should == 20 # length of sha1
169
- end
170
-
171
- it "is supported by all the types" do
172
- gen_a_model
173
- h = BaseType.model_hash
174
- h.should_not be_nil
175
- h.length.should == 20 # length of sha1
176
- end
177
-
178
- it "differs on different models" do
179
- gen_a_model
180
- a = BaseType.model_hash
181
- reset_for_test
182
-
183
- gen_b_model
184
- b = BaseType.model_hash
185
- reset_for_test
186
-
187
- a.should_not == b
188
- end
189
- end
190
-
191
- describe :find_type do
192
- it "returns the instance with the provided name" do
193
- f = Cauterize.scalar(:foo)
194
- b = Cauterize.scalar(:bar)
195
-
196
- BaseType.find_type(:bar).should be b
197
- BaseType.find_type(:foo).should be f
198
- end
199
-
200
- it "is nil on an unknown name" do
201
- BaseType.find_type(:xxxxxxxxxxxxxxxxxx).should be_nil
202
- end
203
- end
204
-
205
- describe :find_type! do
206
- it "returns the instance with the provided name" do
207
- f = Cauterize.scalar(:foo)
208
- b = Cauterize.scalar(:bar)
209
-
210
- BaseType.find_type!(:bar).should be b
211
- BaseType.find_type!(:foo).should be f
212
- end
213
-
214
- it "errors on an unknown name" do
215
- lambda { BaseType.find_type!(:foo) }.should raise_error /name foo does not/
216
- end
217
- end
218
-
219
- describe "is_[type]?" do
220
- it "supports scalar" do
221
- Cauterize.scalar(:foo).is_scalar?.should be_true
222
- end
223
-
224
- it "supports enumeration" do
225
- Cauterize.enumeration(:foo).is_enumeration?.should be_true
226
- end
227
-
228
- it "supports composite" do
229
- Cauterize.composite(:foo).is_composite?.should be_true
230
- end
231
-
232
- it "supports fixed_array" do
233
- Cauterize.fixed_array(:foo).is_fixed_array?.should be_true
234
- end
235
-
236
- it "supports variable_array" do
237
- Cauterize.variable_array(:foo).is_variable_array?.should be_true
238
- end
239
-
240
- it "supports group" do
241
- Cauterize.group(:foo).is_group?.should be_true
242
- end
243
-
244
- it "supports builtin" do
245
- # This one is special because of how buitins are declared
246
- Cauterize.builtins[:int32].is_built_in?.should be_true
247
- end
248
- end
249
- end
250
- end
251
- end
1
+ module Cauterize
2
+
3
+ describe Cauterize do
4
+ describe BaseType do
5
+ describe :description do
6
+ it "handles nil description" do
7
+ BaseType.new(:foo, nil).description.should be_nil
8
+ end
9
+
10
+ it "sets the description" do
11
+ BaseType.new(:foo, "a desc").description.should == "a desc"
12
+ end
13
+ end
14
+
15
+ describe :register_instance do
16
+ it "adds an instance to the instance list" do
17
+ orig_len = BaseType.all_instances.length
18
+
19
+ class X < BaseType
20
+ def initialize(name); end
21
+ end
22
+ x = X.new(:foo)
23
+
24
+ x.instance_exec do
25
+ register_instance(x)
26
+ end
27
+
28
+ BaseType.all_instances.last.should be x
29
+ BaseType.all_instances.length.should == orig_len + 1
30
+ end
31
+ end
32
+
33
+ describe :instances do
34
+ # Two things are being tested here.
35
+ # 1. That instances works.
36
+ # 2. That super() is called in each .new
37
+ it "is every instance of a BaseType-derived class" do
38
+ BaseType.class_variable_set(:@@instances, {})
39
+
40
+ b = BuiltIn.new(:uint32)
41
+ s = Scalar.new(:eek)
42
+ e = Enumeration.new(:emoo)
43
+ c = Composite.new(:cooo)
44
+ f = FixedArray.new(:moo)
45
+ v = VariableArray.new(:quack)
46
+ g = Group.new(:goo)
47
+
48
+ lst = [b, s, e, c, f, v, g]
49
+
50
+ instances = BaseType.all_instances
51
+
52
+ # There will be some extras due to automatic creation of enums and
53
+ # scalars in Enumeration and Group.
54
+ instances.should include(*lst)
55
+
56
+ # Check that each of our instances shows up in the list returned from
57
+ # all_instances. Do it this way in case there are other types created
58
+ # (like the enumeration for Group).
59
+ lst.all? do |l|
60
+ instances.any? {|i| i.equal? l}
61
+ end
62
+ end
63
+ end
64
+
65
+ describe :type_hash do
66
+ it "is different for objects with different names" do
67
+ f = Cauterize.scalar(:foo) {|t| t.type_name :uint8}.type_hash
68
+ g = Cauterize.scalar(:bar) {|t| t.type_name :uint8}.type_hash
69
+
70
+ f.should_not == g
71
+ end
72
+
73
+ it "is different for objects with different types" do
74
+ f = Cauterize.scalar(:foo) {|t| t.type_name :uint8}.type_hash
75
+ reset_for_test
76
+
77
+ g = Cauterize.scalar(:foo) {|t| t.type_name :int8}.type_hash
78
+ reset_for_test
79
+
80
+ f.should_not == g
81
+ end
82
+
83
+ it "differes on enumeration value differences" do
84
+ f = Cauterize.enumeration(:foo) { |t| t.value :a, 0 }.type_hash
85
+ reset_for_test
86
+
87
+ g = Cauterize.enumeration(:foo) { |t| t.value :a, 1 }.type_hash
88
+ reset_for_test
89
+
90
+ f.should_not == g
91
+ end
92
+
93
+ it "differs on differing length in fixed arrays" do
94
+ f = Cauterize.fixed_array(:foo) { |t| t.array_type :int8; t.array_size 1 }.type_hash
95
+ reset_for_test
96
+
97
+ g = Cauterize.fixed_array(:foo) { |t| t.array_type :int8; t.array_size 2 }.type_hash
98
+ reset_for_test
99
+
100
+ f.should_not == g
101
+ end
102
+
103
+ it "differs on differing size type in variable arrays" do
104
+ f = Cauterize.variable_array(:foo) { |t| t.array_type :int8; t.array_size 2 }.type_hash
105
+ reset_for_test
106
+
107
+ g = Cauterize.variable_array(:foo) { |t| t.array_type :int8; t.array_size 2000 }.type_hash
108
+ reset_for_test
109
+
110
+ f.should_not == g
111
+ end
112
+
113
+ it "differs on group field reordering" do
114
+ f = Cauterize.group(:foo) do |t|
115
+ t.field :a, :int8
116
+ t.field :b, :int8
117
+ end.type_hash
118
+ reset_for_test
119
+
120
+ g = Cauterize.group(:foo) do |t|
121
+ t.field :b, :int8
122
+ t.field :a, :int8
123
+ end.type_hash
124
+ reset_for_test
125
+
126
+ f.should_not == g
127
+ end
128
+
129
+ it "differs on composite field reordering" do
130
+ f = Cauterize.composite(:foo) do |t|
131
+ t.field :a, :int8
132
+ t.field :b, :int8
133
+ end.type_hash
134
+ reset_for_test
135
+
136
+ g = Cauterize.composite(:foo) do |t|
137
+ t.field :b, :int8
138
+ t.field :a, :int8
139
+ end.type_hash
140
+ reset_for_test
141
+
142
+ f.should_not == g
143
+ end
144
+
145
+ it "differs on changes in indirect fields" do
146
+ Cauterize.scalar(:a_thing) { |t| t.type_name :int8 }
147
+ f = Cauterize.composite(:foo) do |t|
148
+ t.field :a, :a_thing
149
+ end.type_hash
150
+ reset_for_test
151
+
152
+ Cauterize.scalar(:a_thing) { |t| t.type_name :uint8 }
153
+ g = Cauterize.composite(:foo) do |t|
154
+ t.field :a, :a_thing
155
+ end.type_hash
156
+ reset_for_test
157
+
158
+ f.should_not == g
159
+ end
160
+ end
161
+
162
+ describe :model_hash do
163
+ it "returns a SHA1 of the model" do
164
+ Cauterize.scalar(:foo) {|t| t.type_name :uint8}
165
+
166
+ h = BaseType.model_hash
167
+ h.should_not be_nil
168
+ h.length.should == 20 # length of sha1
169
+ end
170
+
171
+ it "is supported by all the types" do
172
+ gen_a_model
173
+ h = BaseType.model_hash
174
+ h.should_not be_nil
175
+ h.length.should == 20 # length of sha1
176
+ end
177
+
178
+ it "differs on different models" do
179
+ gen_a_model
180
+ a = BaseType.model_hash
181
+ reset_for_test
182
+
183
+ gen_b_model
184
+ b = BaseType.model_hash
185
+ reset_for_test
186
+
187
+ a.should_not == b
188
+ end
189
+ end
190
+
191
+ describe :find_type do
192
+ it "returns the instance with the provided name" do
193
+ f = Cauterize.scalar(:foo)
194
+ b = Cauterize.scalar(:bar)
195
+
196
+ BaseType.find_type(:bar).should be b
197
+ BaseType.find_type(:foo).should be f
198
+ end
199
+
200
+ it "is nil on an unknown name" do
201
+ BaseType.find_type(:xxxxxxxxxxxxxxxxxx).should be_nil
202
+ end
203
+ end
204
+
205
+ describe :find_type! do
206
+ it "returns the instance with the provided name" do
207
+ f = Cauterize.scalar(:foo)
208
+ b = Cauterize.scalar(:bar)
209
+
210
+ BaseType.find_type!(:bar).should be b
211
+ BaseType.find_type!(:foo).should be f
212
+ end
213
+
214
+ it "errors on an unknown name" do
215
+ lambda { BaseType.find_type!(:foo) }.should raise_error /name foo does not/
216
+ end
217
+ end
218
+
219
+ describe "is_[type]?" do
220
+ it "supports scalar" do
221
+ Cauterize.scalar(:foo).is_scalar?.should be_true
222
+ end
223
+
224
+ it "supports enumeration" do
225
+ Cauterize.enumeration(:foo).is_enumeration?.should be_true
226
+ end
227
+
228
+ it "supports composite" do
229
+ Cauterize.composite(:foo).is_composite?.should be_true
230
+ end
231
+
232
+ it "supports fixed_array" do
233
+ Cauterize.fixed_array(:foo).is_fixed_array?.should be_true
234
+ end
235
+
236
+ it "supports variable_array" do
237
+ Cauterize.variable_array(:foo).is_variable_array?.should be_true
238
+ end
239
+
240
+ it "supports group" do
241
+ Cauterize.group(:foo).is_group?.should be_true
242
+ end
243
+
244
+ it "supports builtin" do
245
+ # This one is special because of how buitins are declared
246
+ Cauterize.builtins[:int32].is_built_in?.should be_true
247
+ end
248
+ end
249
+ end
250
+ end
251
+ end