summaryrefslogtreecommitdiff
path: root/FreeRTOS-Plus/Test/CMock/test/unit/cmock_unityhelper_parser_test.rb
blob: 987b0d6974bcfac11921dce696fe6346473c1280 (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
# ==========================================
#   CMock Project - Automatic Mock Generation for C
#   Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
#   [Released under MIT License. Please refer to license.txt for details]
# ==========================================

require File.expand_path(File.dirname(__FILE__)) + "/../test_helper"
require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_unityhelper_parser'

describe CMockUnityHelperParser, "Verify CMockUnityHelperParser Module" do

  before do
    create_mocks :config
  end

  after do
  end

  it "ignore lines that are commented out" do
    source =
      " abcd;\n" +
      "// #define UNITY_TEST_ASSERT_EQUAL_CHICKENS(a,b,line,msg) {...};\n" +
      "or maybe // #define UNITY_TEST_ASSERT_EQUAL_CHICKENS(a,b,line,msg) {...};\n\n"
    @config.expect :plugins, [] #not :array
    @config.expect :treat_as, {}
    @config.expect :load_unity_helper, source
    @parser = CMockUnityHelperParser.new(@config)
    expected = {}

    assert_equal(expected, @parser.c_types)
  end

  it "ignore stuff in block comments" do
    source =
      " abcd; /*\n" +
      "#define UNITY_TEST_ASSERT_EQUAL_CHICKENS(a,b,line,msg) {...};\n" +
      "#define UNITY_TEST_ASSERT_EQUAL_CHICKENS(a,b,line,msg) {...};\n */\n"
    @config.expect :plugins, [] #not :array
    @config.expect :treat_as, {}
    @config.expect :load_unity_helper, source
    @parser = CMockUnityHelperParser.new(@config)
    expected = {}

    assert_equal(expected, @parser.c_types)
  end

  it "notice equal helpers in the proper form and ignore others" do
    source =
      "abcd;\n" +
      "#define UNITY_TEST_ASSERT_EQUAL_TURKEYS_T(a,b,line,msg) {...};\n" +
      "abcd;\n" +
      "#define UNITY_TEST_ASSERT_EQUAL_WRONG_NUM_ARGS(a,b,c,d,e) {...};\n" +
      "#define UNITY_TEST_ASSERT_WRONG_NAME_EQUAL(a,b,c,d) {...};\n" +
      "#define UNITY_TEST_ASSERT_EQUAL_unsigned_funky_rabbits(a,b,c,d) {...};\n" +
      "abcd;\n"
    @config.expect :plugins, [] #not :array
    @config.expect :treat_as, {}
    @config.expect :load_unity_helper, source
    @parser = CMockUnityHelperParser.new(@config)
    expected = {
      'TURKEYS_T' => "UNITY_TEST_ASSERT_EQUAL_TURKEYS_T",
      'unsigned_funky_rabbits' => "UNITY_TEST_ASSERT_EQUAL_unsigned_funky_rabbits"
    }

    assert_equal(expected, @parser.c_types)
  end

  it "notice equal helpers that contain arrays" do
    source =
      "abcd;\n" +
      "#define UNITY_TEST_ASSERT_EQUAL_TURKEYS_ARRAY(a,b,c,d,e) {...};\n" +
      "abcd;\n" +
      "#define UNITY_TEST_ASSERT_EQUAL_WRONG_NUM_ARGS_ARRAY(a,b,c,d,e,f) {...};\n" +
      "#define UNITY_TEST_ASSERT_WRONG_NAME_EQUAL_ARRAY(a,b,c,d,e) {...};\n" +
      "#define UNITY_TEST_ASSERT_EQUAL_unsigned_funky_rabbits_ARRAY(a,b,c,d,e) {...};\n" +
      "abcd;\n"
    @config.expect :plugins, [] #not :array
    @config.expect :treat_as, {}
    @config.expect :load_unity_helper, source
    @parser = CMockUnityHelperParser.new(@config)
    expected = {
      'TURKEYS*' => "UNITY_TEST_ASSERT_EQUAL_TURKEYS_ARRAY",
      'unsigned_funky_rabbits*' => "UNITY_TEST_ASSERT_EQUAL_unsigned_funky_rabbits_ARRAY"
    }

    assert_equal(expected, @parser.c_types)
  end

  it "pull in the standard set of helpers and add them to my list" do
    pairs = {
      "UINT"          => "HEX32",
      "unsigned long" => "HEX64",
    }
    expected = {
      "UINT"          => "UNITY_TEST_ASSERT_EQUAL_HEX32",
      "unsigned_long" => "UNITY_TEST_ASSERT_EQUAL_HEX64",
      "UINT*"         => "UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY",
      "unsigned_long*"=> "UNITY_TEST_ASSERT_EQUAL_HEX64_ARRAY",
    }
    @config.expect :plugins, [] #not :array
    @config.expect :treat_as, pairs
    @config.expect :load_unity_helper, nil
    @parser = CMockUnityHelperParser.new(@config)

    assert_equal(expected, @parser.c_types)
  end

  it "pull in the user specified set of helpers and add them to my list" do
    pairs = {
      "char*"         => "STRING",
      "unsigned  int" => "HEX32",
    }
    expected = {
      "char*"         => "UNITY_TEST_ASSERT_EQUAL_STRING",
      "unsigned_int"  => "UNITY_TEST_ASSERT_EQUAL_HEX32",
      "char**"        => "UNITY_TEST_ASSERT_EQUAL_STRING_ARRAY",
      "unsigned_int*" => "UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY",
    }
    @config.expect :plugins, [] #not :array
    @config.expect :treat_as, pairs
    @config.expect :load_unity_helper, nil
    @parser = CMockUnityHelperParser.new(@config)

    assert_equal(expected, @parser.c_types)
  end

  it "be able to fetch helpers on my list" do
    @config.expect :plugins, [] #not :array
    @config.expect :treat_as, {}
    @config.expect :load_unity_helper, ""
    @parser = CMockUnityHelperParser.new(@config)
    @parser.c_types = {
      'UINT8'     => "UNITY_TEST_ASSERT_EQUAL_UINT8",
      'UINT16*'   => "UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY",
      'SPINACH'   => "UNITY_TEST_ASSERT_EQUAL_SPINACH",
      'LONG_LONG' => "UNITY_TEST_ASSERT_EQUAL_LONG_LONG"
    }

    [["UINT8","UINT8"],
     ["UINT16*","UINT16_ARRAY"],
     ["const SPINACH","SPINACH"],
     ["LONG LONG","LONG_LONG"] ].each do |ctype, exptype|
      assert_equal(["UNITY_TEST_ASSERT_EQUAL_#{exptype}",''], @parser.get_helper(ctype))
    end
  end

  it "return memory comparison when asked to fetch helper of types not on my list" do
    @config.expect :plugins, [] #not :array
    @config.expect :treat_as, {}
    @config.expect :load_unity_helper, ""
    @parser = CMockUnityHelperParser.new(@config)
    @parser.c_types = {
      'UINT8'   => "UNITY_TEST_ASSERT_EQUAL_UINT8",
      'UINT16*' => "UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY",
      'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH",
    }

    ["UINT32","SPINACH_T","SALAD","PINEAPPLE"].each do |ctype|
      @config.expect :memcmp_if_unknown, true
      assert_equal(["UNITY_TEST_ASSERT_EQUAL_MEMORY",'&'], @parser.get_helper(ctype))
    end
  end

  it "return memory array comparison when asked to fetch helper of types not on my list" do
    @config.expect :plugins, [:array]
    @config.expect :treat_as, {}
    @config.expect :load_unity_helper, ""
    @parser = CMockUnityHelperParser.new(@config)
    @parser.c_types = {
      'UINT8'   => "UNITY_TEST_ASSERT_EQUAL_UINT8",
      'UINT16*' => "UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY",
      'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH",
    }

    ["UINT32*","SPINACH_T*"].each do |ctype|
      @config.expect :memcmp_if_unknown, true
      assert_equal(["UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY",''], @parser.get_helper(ctype))
    end
  end

  it "return the array handler if we cannot find the normal handler" do
    @config.expect :plugins, [] #not :array
    @config.expect :treat_as, {}
    @config.expect :load_unity_helper, ""
    @parser = CMockUnityHelperParser.new(@config)
    @parser.c_types = {
      'UINT8'   => "UNITY_TEST_ASSERT_EQUAL_UINT8",
      'UINT16*' => "UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY",
      'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH",
    }

      assert_equal(["UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY",'&'], @parser.get_helper("UINT16"))
  end

  it "return the normal handler if we cannot find the array handler" do
    @config.expect :plugins, [] #not :array
    @config.expect :treat_as, {}
    @config.expect :load_unity_helper, ""
    @parser = CMockUnityHelperParser.new(@config)
    @parser.c_types = {
      'UINT8'   => "UNITY_TEST_ASSERT_EQUAL_UINT8",
      'UINT16'  => "UNITY_TEST_ASSERT_EQUAL_UINT16",
      'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH",
    }

      assert_equal(["UNITY_TEST_ASSERT_EQUAL_UINT8",'*'], @parser.get_helper("UINT8*"))
  end

  it "raise error when asked to fetch helper of type not on my list and not allowed to mem check" do
    @config.expect :plugins, [] #not :array
    @config.expect :treat_as, {}
    @config.expect :load_unity_helper, ""
    @config.expect :memcmp_if_unknown, false
    @parser = CMockUnityHelperParser.new(@config)
    @parser.c_types = {
      'UINT8'   => "UNITY_TEST_ASSERT_EQUAL_UINT8",
      'UINT32*' => "UNITY_TEST_ASSERT_EQUAL_UINT32_ARRAY",
      'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH",
    }

    assert_raises (RuntimeError) { @parser.get_helper("UINT16") }
  end
end