cauterize 0.0.1.pre1 → 0.0.1.pre5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. data/.gitignore +5 -0
  2. data/.rspec +1 -1
  3. data/Gemfile +1 -1
  4. data/README.md +0 -2
  5. data/Rakefile +19 -3
  6. data/bin/cauterize +14 -6
  7. data/example/Cauterize +22 -10
  8. data/example/build.sh +15 -2
  9. data/lib/cauterize/base_type.rb +2 -5
  10. data/lib/cauterize/builders.rb +1 -3
  11. data/lib/cauterize/builders/c/buildable.rb +3 -2
  12. data/lib/cauterize/builders/c/builtin.rb +46 -0
  13. data/lib/cauterize/builders/c/enumeration.rb +23 -2
  14. data/lib/cauterize/builders/c/fixed_array.rb +16 -7
  15. data/lib/cauterize/builders/c/group.rb +32 -3
  16. data/lib/cauterize/builders/c/scalar.rb +5 -0
  17. data/lib/cauterize/builders/c/variable_array.rb +1 -1
  18. data/lib/cauterize/builders/cs/buildable.rb +59 -0
  19. data/lib/cauterize/builders/cs/builtin.rb +23 -0
  20. data/lib/cauterize/builders/cs/composite.rb +21 -0
  21. data/lib/cauterize/builders/cs/csarray.rb +32 -0
  22. data/lib/cauterize/builders/cs/enumeration.rb +21 -0
  23. data/lib/cauterize/builders/cs/fixed_array.rb +25 -0
  24. data/lib/cauterize/builders/cs/group.rb +33 -0
  25. data/lib/cauterize/builders/cs/scalar.rb +10 -0
  26. data/lib/cauterize/builders/cs/variable_array.rb +34 -0
  27. data/lib/cauterize/builtin.rb +52 -0
  28. data/lib/cauterize/c_builder.rb +15 -1
  29. data/lib/cauterize/cauterize.rb +44 -13
  30. data/lib/cauterize/composite.rb +3 -3
  31. data/lib/cauterize/cs_builder.rb +53 -0
  32. data/lib/cauterize/enumeration.rb +20 -3
  33. data/lib/cauterize/fixed_array.rb +3 -3
  34. data/lib/cauterize/formatter.rb +7 -3
  35. data/lib/cauterize/group.rb +5 -8
  36. data/lib/cauterize/scalar.rb +16 -7
  37. data/lib/cauterize/variable_array.rb +6 -6
  38. data/lib/cauterize/version.rb +1 -1
  39. data/spec/base_type_spec.rb +133 -125
  40. data/spec/builders/c/buildable_spec.rb +18 -18
  41. data/spec/builders/c/builtin_spec.rb +22 -0
  42. data/spec/builders/c/composite_spec.rb +37 -33
  43. data/spec/builders/c/enumeration_spec.rb +84 -21
  44. data/spec/builders/c/fixed_array_spec.rb +6 -6
  45. data/spec/builders/c/group_spec.rb +97 -90
  46. data/spec/builders/c/scalar_spec.rb +24 -6
  47. data/spec/builders/c/variable_array_spec.rb +37 -37
  48. data/spec/builders/cs/buildable_spec.rb +8 -0
  49. data/spec/builders/cs/composite_spec.rb +32 -0
  50. data/spec/builders/cs/enumeration_spec.rb +33 -0
  51. data/spec/builders/cs/fixed_array_spec.rb +40 -0
  52. data/spec/builders/cs/group_spec.rb +56 -0
  53. data/spec/builders/cs/scalar_spec.rb +7 -0
  54. data/spec/builders/cs/variable_array_spec.rb +46 -0
  55. data/spec/builders_spec.rb +38 -38
  56. data/spec/builtin_spec.rb +46 -0
  57. data/spec/c_builder_spec.rb +116 -102
  58. data/spec/cauterize_spec.rb +8 -1
  59. data/spec/composite_spec.rb +52 -48
  60. data/spec/cs_builder_spec.rb +113 -0
  61. data/spec/enumeration_spec.rb +55 -16
  62. data/spec/fixed_array_spec.rb +7 -9
  63. data/spec/group_spec.rb +81 -76
  64. data/spec/scalar_spec.rb +20 -10
  65. data/spec/spec_helper.rb +103 -94
  66. data/spec/support/shared_examples_for_c_buildables.rb +68 -64
  67. data/spec/variable_array_spec.rb +12 -17
  68. data/{c → support/c}/src/cauterize.c +8 -7
  69. data/support/c/src/cauterize.h +59 -0
  70. data/{c → support/c}/src/cauterize_debug.h +0 -0
  71. data/support/c/src/cauterize_util.h +49 -0
  72. data/{c → support/c}/test/greatest.h +0 -0
  73. data/{c → support/c}/test/test.c +0 -0
  74. data/support/cs/src/CauterizeCompositeFormatter.cs +34 -0
  75. data/support/cs/src/CauterizeContainerFormatter.cs +18 -0
  76. data/support/cs/src/CauterizeEnumFormatter.cs +67 -0
  77. data/support/cs/src/CauterizeException.cs +15 -0
  78. data/support/cs/src/CauterizeFixedArrayFormatter.cs +39 -0
  79. data/support/cs/src/CauterizeFormatter.cs +40 -0
  80. data/support/cs/src/CauterizeGroupFormatter.cs +46 -0
  81. data/support/cs/src/CauterizePrimitiveFormatter.cs +33 -0
  82. data/support/cs/src/CauterizeTypeFormatterFactory.cs +39 -0
  83. data/support/cs/src/CauterizeTypes.cs +107 -0
  84. data/support/cs/src/CauterizeVariableArrayFormatter.cs +49 -0
  85. data/support/cs/src/ICauterizeTypeFormatter.cs +12 -0
  86. data/support/cs/src/OrderAttribute.cs +50 -0
  87. data/support/cs/src/PrimitiveSupport.cs +134 -0
  88. data/support/cs/src/SerializedRepresentationAttribute.cs +24 -0
  89. data/support/cs/test/CauterizeCompositeFormatterTest.cs +59 -0
  90. data/support/cs/test/CauterizeEnumFormatterTest.cs +110 -0
  91. data/support/cs/test/CauterizeFixedArrayFormatterTest.cs +91 -0
  92. data/support/cs/test/CauterizeFormatterTest.cs +40 -0
  93. data/support/cs/test/CauterizeGroupFormatterTest.cs +147 -0
  94. data/support/cs/test/CauterizeIntegrationTest.cs +129 -0
  95. data/support/cs/test/CauterizePrimitiveFormatterTest.cs +98 -0
  96. data/support/cs/test/CauterizeTypeFormatterFactoryTest.cs +73 -0
  97. data/support/cs/test/CauterizeVariableArrayFormatterTest.cs +130 -0
  98. data/support/cs/test/OrderAttributeTest.cs +39 -0
  99. data/support/cs/test/SerializedRepresentationAttributeTest.cs +39 -0
  100. metadata +68 -10
  101. data/c/src/cauterize.h +0 -46
  102. data/c/src/cauterize_util.h +0 -7
@@ -1,132 +1,146 @@
1
1
  require 'tmpdir'
2
2
  require 'fileutils'
3
3
 
4
- describe Cauterize::CBuilder do
5
- before do
6
- @tempdir = Dir.mktmpdir
7
- @h_path = File.join(@tempdir, "testing.h")
8
- @c_path = File.join(@tempdir, "testing.c")
4
+ module Cauterize
5
+ describe Cauterize::CBuilder do
6
+ before do
7
+ @tempdir = Dir.mktmpdir
8
+ @h_path = File.join(@tempdir, "testing.h")
9
+ @c_path = File.join(@tempdir, "testing.c")
9
10
 
10
- @cb = CBuilder.new(@h_path, @c_path, "testing")
11
- end
12
- after { FileUtils.rm_rf @tempdir }
11
+ @cb = CBuilder.new(@h_path, @c_path, "testing")
12
+ end
13
+ after { FileUtils.rm_rf @tempdir }
13
14
 
14
- describe :initialize do
15
- it "saves the h and c paths" do
16
- @cb.h.should == @h_path
17
- @cb.c.should == @c_path
15
+ describe :initialize do
16
+ it "saves the h and c paths" do
17
+ @cb.h.should == @h_path
18
+ @cb.c.should == @c_path
19
+ end
18
20
  end
19
- end
20
21
 
21
- describe :build do
22
- before do
23
- scalar(:uint8_t)
24
- scalar(:uint16_t)
25
- scalar(:uint32_t)
22
+ describe :build do
23
+ before do
24
+ Cauterize.set_version("1.2.3")
26
25
 
27
- fixed_array(:mac_address) do |fa|
28
- fa.array_type :uint8_t
29
- fa.array_size 6
30
- end
26
+ Cauterize.scalar(:small_uint) {|t| t.type_name(:uint8)}
31
27
 
32
- variable_array(:mac_table) do |t|
33
- t.array_type :mac_address
34
- t.array_size 64
35
- t.size_type :uint8_t
36
- end
28
+ Cauterize.fixed_array(:mac_address) do |fa|
29
+ fa.array_type :small_uint
30
+ fa.array_size 6
31
+ end
37
32
 
38
- variable_array(:name) do |va|
39
- va.array_type :uint8_t
40
- va.size_type :uint8_t
41
- va.array_size 32
42
- end
33
+ Cauterize.variable_array(:mac_table) do |t|
34
+ t.array_type :mac_address
35
+ t.array_size 64
36
+ t.size_type :small_uint
37
+ end
43
38
 
44
- enumeration(:gender) do |e|
45
- e.value :male
46
- e.value :female
47
- end
39
+ Cauterize.variable_array(:name) do |va|
40
+ va.array_type :small_uint
41
+ va.size_type :small_uint
42
+ va.array_size 32
43
+ end
48
44
 
49
- composite(:place) do |c|
50
- c.field :name, :name
51
- c.field :elevation, :uint32_t
52
- end
45
+ Cauterize.enumeration(:gender) do |e|
46
+ e.value :male
47
+ e.value :female
48
+ end
53
49
 
54
- composite(:person) do |c|
55
- c.field :first_name, :name
56
- c.field :last_name, :name
57
- c.field :gender, :gender
58
- end
50
+ Cauterize.composite(:place) do |c|
51
+ c.field :name, :name
52
+ c.field :elevation, :uint32
53
+ end
59
54
 
60
- composite(:dog) do |c|
61
- c.field :name, :name
62
- c.field :gender, :gender
63
- c.field :leg_count, :uint8_t
64
- end
55
+ Cauterize.composite(:person) do |c|
56
+ c.field :first_name, :name
57
+ c.field :last_name, :name
58
+ c.field :gender, :gender
59
+ end
65
60
 
66
- group(:creature) do |g|
67
- g.field :person, :person
68
- g.field :dog, :dog
69
- end
61
+ Cauterize.composite(:dog) do |c|
62
+ c.field :name, :name
63
+ c.field :gender, :gender
64
+ c.field :leg_count, :small_uint
65
+ end
70
66
 
71
- @cb.build
72
- @h_text = File.read(@cb.h)
73
- @h_lines = @h_text.lines.to_a
74
- @c_text = File.read(@cb.c)
75
- @c_lines = @c_text.lines.to_a
76
- end
67
+ Cauterize.group(:creature) do |g|
68
+ g.field :person, :person
69
+ g.field :dog, :dog
70
+ end
77
71
 
78
- describe "header generation" do
79
- it "prevents multiple inclusion in headers" do
80
- @h_lines[0].should match /#ifndef TESTING_H_\d+/
81
- @h_lines[1].should match /#define TESTING_H_\d+/
82
- @h_lines.last.should match /#endif \/\* TESTING_H_\d+ \*\//
72
+ @cb.build
73
+ @h_text = File.read(@cb.h)
74
+ @h_lines = @h_text.lines.to_a
75
+ @c_text = File.read(@cb.c)
76
+ @c_lines = @c_text.lines.to_a
83
77
  end
84
78
 
85
- it "includes prototype information for all defined types" do
86
- @h_text.should match "struct name;"
87
- @h_text.should match "struct person;"
88
- @h_text.should match "struct place;"
89
- end
79
+ describe "header generation" do
80
+ it "externs 'c'" do
81
+ @h_lines.should include("extern \"C\" {\n")
82
+ end
90
83
 
91
- it "includes enumeration and structure definitions" do
92
- @h_text.should match /gender/
93
- @h_text.should match /MALE = 0/
94
- @h_text.should match /FEMALE = 1/
84
+ it "creates a VERSION define" do
85
+ @h_lines.should include("#define GEN_VERSION (\"1.2.3\")\n")
86
+ end
87
+
88
+ it "creates a DATE define" do
89
+ @h_lines.any?{|l| l.match /GEN_DATE/}.should be_true
90
+ end
91
+
92
+ it "prevents multiple inclusion in headers" do
93
+ @h_lines[0].should match /#ifndef TESTING_H_\d+/
94
+ @h_lines[1].should match /#define TESTING_H_\d+/
95
+ @h_lines.last.should match /#endif \/\* TESTING_H_\d+ \*\//
96
+ end
97
+
98
+ it "includes prototype information for all defined types" do
99
+ @h_text.should match "struct name;"
100
+ @h_text.should match "struct person;"
101
+ @h_text.should match "struct place;"
102
+ end
103
+
104
+ it "includes enumeration and structure definitions" do
105
+ @h_text.should match /gender/
106
+ @h_text.should match /MALE = 0/
107
+ @h_text.should match /FEMALE = 1/
108
+ end
95
109
  end
96
- end
97
110
 
98
- describe "c body generation" do
99
- it "includes the generated header file" do
100
- @c_text.should match /#include "testing.h"/
111
+ describe "c body generation" do
112
+ it "includes the generated header file" do
113
+ @c_text.should match /#include "testing.h"/
114
+ end
101
115
  end
102
- end
103
116
 
104
- describe "compilation" do
105
- it "can be built" do
106
- caut_dir = "#{File.dirname(__FILE__)}/../c/src"
107
-
108
- res = Dir.chdir @tempdir do
109
- File.open("test_main.c", "wb") do |fh|
110
- syms = BaseType.all_instances.map do |i|
111
- b = Builders.get(:c, i)
112
- [b.packer_sym, b.unpacker_sym]
113
- end.flatten
114
- fh.write(gen_test_main(syms))
117
+ describe "compilation" do
118
+ it "can be built" do
119
+ caut_dir = "#{File.dirname(__FILE__)}/../support/c/src"
120
+
121
+ res = Dir.chdir @tempdir do
122
+ File.open("test_main.c", "wb") do |fh|
123
+ syms = BaseType.all_instances.map do |i|
124
+ b = Builders.get(:c, i)
125
+ [b.packer_sym, b.unpacker_sym]
126
+ end.flatten
127
+ fh.write(gen_test_main(syms))
128
+ end
129
+
130
+ cmd = %W{
131
+ gcc -Wall -Wextra -Werror
132
+ -I#{caut_dir}
133
+ #{@cb.c}
134
+ #{caut_dir}/cauterize.c
135
+ test_main.c
136
+ -o testing.bin 2>&1
137
+ }.join(" ")
138
+
139
+ `#{cmd}`
115
140
  end
116
141
 
117
- cmd = %W{
118
- clang -Wall -Wextra -Werror
119
- -I#{caut_dir}
120
- #{@cb.c}
121
- #{caut_dir}/cauterize.c
122
- test_main.c
123
- -o testing.bin 2>&1
124
- }.join(" ")
125
-
126
- `#{cmd}`
142
+ res.should == ""
127
143
  end
128
-
129
- res.should == ""
130
144
  end
131
145
  end
132
146
  end
@@ -1,8 +1,15 @@
1
1
  describe Cauterize do
2
2
  describe :set_name do
3
3
  it "sets the name of the project" do
4
- set_name("some_name")
4
+ Cauterize.set_name("some_name")
5
5
  Cauterize.get_name.should == "some_name"
6
6
  end
7
7
  end
8
+
9
+ describe :set_version do
10
+ it "sets a version string" do
11
+ Cauterize.set_version("0.1.2")
12
+ Cauterize.get_version.should == "0.1.2"
13
+ end
14
+ end
8
15
  end
@@ -1,62 +1,66 @@
1
- describe Cauterize do
2
- before { reset_for_test }
3
-
4
- describe CompositeField do
5
- describe :initialize do
6
- it "creates a new name -> type mapping" do
7
- a = scalar(:a_type)
8
- b = scalar(:b_type)
9
- f = CompositeField.new(:a_name, :a_type)
10
- f.name.should == :a_name
11
- f.type.should be a
12
- end
13
- end
14
- end
15
-
16
- describe Composite do
17
- describe :initialize do
18
- it "creates a Composite" do
19
- c = Composite.new(:foo)
20
- c.name.should == :foo
1
+ module Cauterize
2
+ describe Cauterize do
3
+ describe CompositeField do
4
+ describe :initialize do
5
+ it "creates a new name -> type mapping" do
6
+ a = Cauterize.scalar(:a_type)
7
+ b = Cauterize.scalar(:b_type)
8
+ f = CompositeField.new(:a_name, :a_type)
9
+ f.name.should == :a_name
10
+ f.type.should be a
11
+ end
21
12
  end
22
13
  end
23
14
 
24
- describe :fields do
25
- it "defines a new field in the composite" do
26
- a = scalar(:foo)
27
- comp = composite(:comp) do |c|
28
- c.field :a_foo, :foo
15
+ describe Composite do
16
+ describe :initialize do
17
+ it "creates a Composite" do
18
+ c = Composite.new(:foo)
19
+ c.name.should == :foo
29
20
  end
30
-
31
- comp.fields.keys[0].should == :a_foo
32
- comp.fields.values[0].name.should == :a_foo
33
- comp.fields.values[0].type.should be a
34
21
  end
35
22
 
36
- it "errors on duplicate field names" do
37
- a = scalar(:foo)
38
- lambda {
39
- composite(:comp) do |c|
23
+ describe :fields do
24
+ it "defines a new field in the composite" do
25
+ a = Cauterize.scalar(:foo)
26
+ comp = Cauterize.composite(:comp) do |c|
40
27
  c.field :a_foo, :foo
41
- c.field :b_foo, :foo
42
- c.field :b_foo, :foo
43
28
  end
44
- }.should raise_error /Field name b_foo already used/
29
+
30
+ comp.fields.keys[0].should == :a_foo
31
+ comp.fields.values[0].name.should == :a_foo
32
+ comp.fields.values[0].type.should be a
33
+ end
34
+
35
+ it "errors on duplicate field names" do
36
+ a = Cauterize.scalar(:foo)
37
+ lambda {
38
+ Cauterize.composite(:comp) do |c|
39
+ c.field :a_foo, :foo
40
+ c.field :b_foo, :foo
41
+ c.field :b_foo, :foo
42
+ end
43
+ }.should raise_error /Field name b_foo already used/
44
+ end
45
45
  end
46
46
  end
47
- end
48
47
 
49
- describe :composite do
50
- it { creates_a_named_object(:composite, Composite) }
51
- it { retrieves_obj_with_identical_name(:composite) }
52
- it { yields_the_object(:composite) }
53
- it { adds_object_to_hash(:composite, :composites) }
54
- end
48
+ describe :composite do
49
+ it { creates_a_named_object(:composite, Composite) }
50
+ it { retrieves_obj_with_identical_name(:composite) }
51
+ it { yields_the_object(:composite) }
52
+ it { adds_object_to_hash(:composite, Cauterize.composites) }
53
+ end
54
+
55
+ describe :composite! do
56
+ it { creates_a_named_object(:composite!, Composite) }
57
+ it { raises_exception_with_identical_name(:composite!) }
58
+ it { yields_the_object(:composite!) }
59
+ it { adds_object_to_hash(:composite!, Cauterize.composites) }
60
+ end
55
61
 
56
- describe :composite! do
57
- it { creates_a_named_object(:composite!, Composite) }
58
- it { raises_exception_with_identical_name(:composite!) }
59
- it { yields_the_object(:composite!) }
60
- it { adds_object_to_hash(:composite!, :composites) }
62
+ describe :composites do
63
+ it { is_hash_of_created_objs(:composite, Cauterize.composites) }
64
+ end
61
65
  end
62
66
  end
@@ -0,0 +1,113 @@
1
+ require 'tmpdir'
2
+ require 'fileutils'
3
+
4
+ module Cauterize
5
+ describe Cauterize::CSBuilder do
6
+ before do
7
+ @tempdir = Dir.mktmpdir
8
+ @cs_path = File.join(@tempdir, "testing.cs")
9
+ @csb = CSBuilder.new(@cs_path, "testing")
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf @tempdir
14
+ end
15
+
16
+ describe :build do
17
+ before do
18
+ Cauterize.set_version("1.2.3")
19
+
20
+ Cauterize.scalar(:uint8_t) { |t| t.type_name(:uint8) }
21
+ Cauterize.scalar(:uint16_t) { |t| t.type_name(:uint16) }
22
+ Cauterize.scalar(:uint32_t) { |t| t.type_name(:uint32) }
23
+
24
+ Cauterize.fixed_array(:mac_address) do |fa|
25
+ fa.array_type :uint8_t
26
+ fa.array_size 6
27
+ end
28
+
29
+ Cauterize.variable_array(:mac_table) do |t|
30
+ t.array_type :mac_address
31
+ t.array_size 64
32
+ t.size_type :uint8_t
33
+ end
34
+
35
+ Cauterize.variable_array(:name) do |va|
36
+ va.array_type :uint8_t
37
+ va.size_type :uint8_t
38
+ va.array_size 32
39
+ end
40
+
41
+ Cauterize.enumeration(:gender) do |e|
42
+ e.value :male
43
+ e.value :female
44
+ end
45
+
46
+ Cauterize.composite(:place) do |c|
47
+ c.field :name, :name
48
+ c.field :elevation, :uint32_t
49
+ end
50
+
51
+ Cauterize.composite(:person) do |c|
52
+ c.field :first_name, :name
53
+ c.field :last_name, :name
54
+ c.field :gender, :gender
55
+ end
56
+
57
+ Cauterize.composite(:dog) do |c|
58
+ c.field :name, :name
59
+ c.field :gender, :gender
60
+ c.field :leg_count, :uint8_t
61
+ end
62
+
63
+ Cauterize.group(:creature) do |g|
64
+ g.field :person, :person
65
+ g.field :dog, :dog
66
+ end
67
+
68
+ @csb.build
69
+ @cs_text = File.read(@csb.cs)
70
+ @cs_lines = @cs_text.lines.to_a
71
+ end
72
+
73
+ it "includes namespaces" do
74
+ @cs_lines.should include("using System;\n")
75
+ @cs_lines.should include("using Cauterize;\n")
76
+ end
77
+
78
+ it "uses a namespace" do
79
+ @cs_lines.should include("namespace Testing\n")
80
+ end
81
+
82
+ it "creates a cauterize info class with version and date" do
83
+ @cs_text.should match /Name = \"Testing\";/
84
+ @cs_text.should match /GeneratedVersion = \"1.2.3\";/
85
+ @cs_text.should match /GeneratedDate = /
86
+ end
87
+
88
+ it "includes enumeration definitions" do
89
+ @cs_text.should match /public enum Gender/
90
+ @cs_text.should match /Male = 0/
91
+ @cs_text.should match /Female = 1/
92
+ end
93
+
94
+ # it "can be built" do
95
+ # caut_dir = "#{File.dirname(__FILE__)}/../cs/src"
96
+
97
+ # res = Dir.chdir @tempdir do
98
+ # File.open("test_main.cs", "wb") do |fh|
99
+ # syms = BaseType.all_instances.map do |i|
100
+ # b = Builders.get(:cs, i)
101
+ # [b.packer_sym, b.unpacker_sym]
102
+ # end.flatten
103
+ # fh.write(gen_test_main(syms))
104
+ # end
105
+
106
+ # `dmcs -lib:#{caut_dir} -target:library test_main.cs 2>&1`
107
+ # end
108
+
109
+ # res.should == ""
110
+ # end
111
+ end
112
+ end
113
+ end