summaryrefslogtreecommitdiffstats
path: root/tinyusb/test/vendor/ceedling/plugins/fake_function_framework/spec/fff_mock_header_generator_spec.rb
blob: e6ac11dd0d088a4187929ac32752bd0ca346afe3 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
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
109
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
require 'stringio'
require 'fff_mock_generator.rb'
require 'header_generator.rb'

# Test the contents of the .h file created for the mock.
describe "FffMockGenerator.create_mock_header" do

  context "when there is nothing to mock," do
    let(:mock_header) {
      parsed_header = {}
      FffMockGenerator.create_mock_header("display", "mock_display", parsed_header)
    }
    it "then the generated header file starts with an opening include guard" do
      expect(mock_header).to start_with(
          "#ifndef mock_display_H\n" +
          "#define mock_display_H")
    end
    it "then the generated file ends with a closing include guard" do
      expect(mock_header).to end_with(
          "#endif // mock_display_H\n")
    end
    it "then the generated file includes the fff header" do
      expect(mock_header).to include(
          %{#include "fff.h"\n})
    end
    it "then the generated file has a prototype for the init function" do
      expect(mock_header).to include(
          "void mock_display_Init(void);")
    end
    it "then the generated file has a prototype for the verify function" do
      expect(mock_header).to include(
          "void mock_display_Verify(void);")
    end
    it "then the generated file has a prototype for the destroy function" do
      expect(mock_header).to include(
          "void mock_display_Destroy(void);")
    end
  end

  context "when there is a function with no args and a void return," do
      let(:mock_header) {
        parsed_header = create_cmock_style_parsed_header(
          [{:name => 'display_turnOffStatusLed', :return_type => 'void'}])
        FffMockGenerator.create_mock_header("display", "mock_display", parsed_header)
      }
      it "then the generated header file starts with an opening include guard" do
        expect(mock_header).to start_with(
          "#ifndef mock_display_H\n" +
          "#define mock_display_H")
      end
      it "then the generated header file contains a fake function declaration" do
        expect(mock_header).to include(
          "DECLARE_FAKE_VOID_FUNC0(display_turnOffStatusLed);"
        )
      end
      it "then the generated file ends with a closing include guard" do
          expect(mock_header).to end_with(
              "#endif // mock_display_H\n")
      end
  end

  context "when there is a function with no args and a bool return," do
    let(:mock_header) {
      parsed_header = create_cmock_style_parsed_header(
        [{:name => 'display_isError', :return_type => 'bool'}])
      FffMockGenerator.create_mock_header("display", "mock_display", parsed_header)
    }
    it "then the generated file contains the fake function declaration" do
      expect(mock_header).to include(
        "DECLARE_FAKE_VALUE_FUNC0(bool, display_isError);"
      )
    end
  end

  context "when there is a function with no args and an int return," do
    let(:mock_header) {
      parsed_header = create_cmock_style_parsed_header(
        [{:name => 'display_isError', :return_type => 'int'}])
      FffMockGenerator.create_mock_header("display", "mock_display", parsed_header)
    }
    it "then the generated file contains the fake function declaration" do
      expect(mock_header).to include(
        "DECLARE_FAKE_VALUE_FUNC0(int, display_isError);"
      )
    end
  end

  context "when there is a function with args and a void return," do
    let(:mock_header) {
      parsed_header = create_cmock_style_parsed_header(
        [{:name => 'display_setVolume', :return_type => 'void', :args => ['int']}])
      FffMockGenerator.create_mock_header("display", "mock_display", parsed_header)
    }
    it "then the generated file contains the fake function declaration" do
      expect(mock_header).to include(
        "DECLARE_FAKE_VOID_FUNC1(display_setVolume, int);"
      )
    end
  end

  context "when there is a function with args and a value return," do
    let(:mock_header) {
      parsed_header = create_cmock_style_parsed_header(
        [{:name => 'a_function', :return_type => 'int', :args => ['char *']}])
      FffMockGenerator.create_mock_header("display", "mock_display", parsed_header)
    }
    it "then the generated file contains the fake function declaration" do
      expect(mock_header).to include(
        "FAKE_VALUE_FUNC1(int, a_function, char *);"
      )
    end
  end

  context "when there is a function with many args and a void return," do
    let(:mock_header) {
      parsed_header = create_cmock_style_parsed_header(
        [{:name => 'a_function', :return_type => 'void',
          :args => ['int', 'char *', 'int', 'int', 'bool', 'applesauce']}])
      FffMockGenerator.create_mock_header("display", "mock_display", parsed_header)
    }
    it "then the generated file contains the fake function declaration" do
      expect(mock_header).to include(
        "DECLARE_FAKE_VOID_FUNC6(a_function, int, char *, int, int, bool, applesauce);"
      )
    end
  end

  context "when there are multiple functions," do
    let(:mock_header) {
      parsed_header = create_cmock_style_parsed_header(
        [ {:name => 'a_function', :return_type => 'int', :args => ['char *']},
          {:name => 'another_function', :return_type => 'void'},
          {:name => 'three', :return_type => 'bool', :args => ['float', 'int']}
        ])
      FffMockGenerator.create_mock_header("display", "mock_display", parsed_header)
    }
    it "then the generated file contains the first fake function declaration" do
      expect(mock_header).to include(
        "DECLARE_FAKE_VALUE_FUNC1(int, a_function, char *);"
      )
    end
    it "then the generated file contains the second fake function declaration" do
      expect(mock_header).to include(
        "DECLARE_FAKE_VOID_FUNC0(another_function);"
      )
    end
    it "then the generated file contains the third fake function declaration" do
      expect(mock_header).to include(
        "DECLARE_FAKE_VALUE_FUNC2(bool, three, float, int);"
      )
    end
  end

  context "when there is a typedef," do
    let(:mock_header) {
      parsed_header = create_cmock_style_parsed_header(
        nil, ["typedef void (*displayCompleteCallback) (void);"])
      FffMockGenerator.create_mock_header("display", "mock_display", parsed_header)
    }
    it "then the generated file contains the typedef" do
      expect(mock_header).to include(
        "typedef void (*displayCompleteCallback) (void);"
      )
    end
  end

  context "when there is a void function with variable arguments" do
    let(:mock_header){
      parsed_header = {}
      parsed_header[:functions] = [{
        :name => "function_with_var_args",
        :return => {:type => "void"},
        :var_arg => "...",
        :args => [{:type => 'char *'}]
      }]
      FffMockGenerator.create_mock_header("display", "mock_display", parsed_header)
    }
    it "then the generated file contains the vararg declaration" do
      expect(mock_header).to include(
        "DECLARE_FAKE_VOID_FUNC2_VARARG(function_with_var_args, char *, ...)"
      )
    end
  end

  context "when there is a function with a return value and variable arguments" do
    let(:mock_header){
      parsed_header = {}
      parsed_header[:functions] = [{
        :name => "function_with_var_args",
        :return => {:type => "int"},
        :var_arg => "...",
        :args => [{:type => 'char *'}]
      }]
      FffMockGenerator.create_mock_header("display", "mock_display", parsed_header)
    }
    it "then the generated file contains the vararg declaration" do
      expect(mock_header).to include(
        "DECLARE_FAKE_VALUE_FUNC2_VARARG(int, function_with_var_args, char *, ...)"
      )
    end
  end

  context "when there is a void function with variable arguments and " +
          "additional arguments" do
    let(:mock_header){
      parsed_header = {}
      parsed_header[:functions] = [{
        :name => "function_with_var_args",
        :return => {:type => "void"},
        :var_arg => "...",
        :args => [{:type => 'char *'}, {:type => 'int'}]
      }]
      FffMockGenerator.create_mock_header("display", "mock_display", parsed_header)
    }
    it "then the generated file contains the vararg declaration" do
      expect(mock_header).to include(
        "DECLARE_FAKE_VOID_FUNC3_VARARG(function_with_var_args, char *, int, ...)"
      )
    end
  end

  context "when there is a function with a pointer to a const value" do
    let(:mock_header){
      parsed_header = {}
      parsed_header[:functions] = [{
        :name => "const_test_function",
        :return => {:type => "void"},
        :args => [{:type => "char *", :name => "a", :ptr? => false, :const? => true},
                  {:type => "char *", :name => "b", :ptr? => false, :const? => false}]
      }]
      FffMockGenerator.create_mock_header("display", "mock_display", parsed_header)
    }
    it "then the generated file contains the correct const argument in the declaration" do
      expect(mock_header).to include(
        "DECLARE_FAKE_VOID_FUNC2(const_test_function, const char *, char *)"
      )
    end
  end

  context "when there is a function that returns a const pointer" do
    let(:mock_header){
      parsed_header = {}
      parsed_header[:functions] = [{
        :name => "return_const_pointer_test_function",
        :modifier => "const",
        :return => {:type => "char *" },
        :args => [{:type => "int", :name => "a"}]
      }]
      FffMockGenerator.create_mock_header("display", "mock_display", parsed_header)
    }
    it "then the generated file contains the correct const return value in the declaration" do
      expect(mock_header).to include(
        "DECLARE_FAKE_VALUE_FUNC1(const char *, return_const_pointer_test_function, int)"
      )
    end
  end
  
  context "when there is a function that returns a const int" do
    let(:mock_header){
      parsed_header = {}
      parsed_header[:functions] = [{
        :name => "return_const_int_test_function",
        :modifier => "const",
        :return => {:type => "int" },
        :args => []
      }]
      FffMockGenerator.create_mock_header("display", "mock_display", parsed_header)
    }
    it "then the generated file contains the correct const return value in the declaration" do
      expect(mock_header).to include(
        "DECLARE_FAKE_VALUE_FUNC0(const int, return_const_int_test_function)"
      )
    end
  end

  context "when there are pre-includes" do
    let(:mock_header) {
      parsed_header = {}
      FffMockGenerator.create_mock_header("display", "mock_display", parsed_header,
        [%{"another_header.h"}])
    }
    it "then they are included before the other files" do
      expect(mock_header).to include(
        %{#include "another_header.h"\n} +
        %{#include "fff.h"}
      )
    end
  end

  context "when there are post-includes" do
    let(:mock_header) {
      parsed_header = {}
      FffMockGenerator.create_mock_header("display", "mock_display", parsed_header,
        nil, [%{"another_header.h"}])
    }
    it "then they are included after the other files" do
      expect(mock_header).to include(
        %{#include "display.h"\n} +
        %{#include "another_header.h"\n}
      )
    end
  end

end