File: ActionTest.cpp

package info (click to toggle)
ola 0.10.8.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,656 kB
  • sloc: cpp: 132,274; python: 14,082; javascript: 6,774; sh: 4,616; ansic: 2,189; java: 518; xml: 253; makefile: 183
file content (199 lines) | stat: -rw-r--r-- 5,374 bytes parent folder | download | duplicates (6)
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
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * ActionTest.cpp
 * Test fixture for the Action classes
 * Copyright (C) 2011 Simon Newton
 */

#include <cppunit/extensions/HelperMacros.h>
#include <ola/Logging.h>
#include <sstream>
#include <string>
#include <vector>

#include "tools/ola_trigger/Action.h"
#include "ola/testing/TestUtils.h"


using std::vector;
using std::string;


class ActionTest: public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(ActionTest);
  CPPUNIT_TEST(testVariableAssignment);
  CPPUNIT_TEST(testVariableAssignmentInterpolation);
  CPPUNIT_TEST(testCommandAction);
  CPPUNIT_TEST_SUITE_END();

 public:
    void testVariableAssignment();
    void testVariableAssignmentInterpolation();
    void testCommandAction();

    void setUp() {
      ola::InitLogging(ola::OLA_LOG_INFO, ola::OLA_LOG_STDERR);
    }
};


CPPUNIT_TEST_SUITE_REGISTRATION(ActionTest);


/**
 * A Mock CommandAction which doesn't fork and exec
 */
class MockCommandAction: CommandAction {
 public:
    MockCommandAction(const string &command,
                      const vector<string> &args)
      : CommandAction(command, args) {
    }

    void Execute(Context *context, uint8_t slot_value);
    void CheckArgs(const ola::testing::SourceLine &source_line,
                   const char* args[]);

 private:
    vector<string> m_interpolated_args;
};


/**
 * Build the list of args and save it.
 */
void MockCommandAction::Execute(Context *context, uint8_t) {
  m_interpolated_args.clear();

  char **args = BuildArgList(context);
  char **ptr = args;
  while (*ptr)
    m_interpolated_args.push_back(string(*ptr++));
  FreeArgList(args);
}


/**
 * Check what we got matches what we expected
 */
void MockCommandAction::CheckArgs(const ola::testing::SourceLine &source_line,
                                  const char* args[]) {
  const char **ptr = args;
  vector<string>::const_iterator iter = m_interpolated_args.begin();
  while (*ptr && iter != m_interpolated_args.end()) {
    ola::testing::_AssertEquals(source_line, string(*ptr++), *iter++);
  }

  std::ostringstream str;
  if (iter != m_interpolated_args.end()) {
    str << ", got extra args: ";
    while (iter != m_interpolated_args.end()) {
      str << *iter;
      iter++;
      if (iter != m_interpolated_args.end()) {
        str << ", ";
      }
    }
    ola::testing::_Fail(source_line, str.str());
  } else if (*ptr) {
    str << ", missing args: ";
    while (*ptr) {
      str << *ptr++;
      if (*ptr) {
        str << ", ";
      }
    }
    ola::testing::_Fail(source_line, str.str());
  }
  m_interpolated_args.clear();
}


/*
 * Check that the VariableAssignmentActions work.
 */
void ActionTest::testVariableAssignment() {
  const string VARIABLE_ONE = "one";
  const string FOO_VALUE = "foo";
  const string BAR_VALUE = "bar";

  Context context;
  string value;
  OLA_ASSERT_FALSE(context.Lookup(VARIABLE_ONE, &value));

  // trigger the action
  VariableAssignmentAction action(VARIABLE_ONE, FOO_VALUE);
  action.Execute(&context, 0);

  // check the context has updated
  OLA_ASSERT(context.Lookup(VARIABLE_ONE, &value));
  OLA_ASSERT_EQ(FOO_VALUE, value);

  // another action
  VariableAssignmentAction action2(VARIABLE_ONE, BAR_VALUE);
  action2.Execute(&context, 0);

  OLA_ASSERT(context.Lookup(VARIABLE_ONE, &value));
  OLA_ASSERT_EQ(BAR_VALUE, value);
}


/**
 * Check that variable interpolation works with the VariableAssignmentAction
 */
void ActionTest::testVariableAssignmentInterpolation() {
  const string VARIABLE_NAME = "var1";
  const string ASSIGNMENT_STRING = "${slot_offset} = ${slot_value}";

  Context context;
  context.Update(Context::SLOT_OFFSET_VARIABLE, "1");
  context.Update(Context::SLOT_VALUE_VARIABLE, "100");
  string value;

  VariableAssignmentAction action(VARIABLE_NAME, ASSIGNMENT_STRING);
  action.Execute(&context, 1);

  OLA_ASSERT(context.Lookup(VARIABLE_NAME, &value));
  OLA_ASSERT_EQ(string("1 = 100"), value);
}


/**
 * Test the command action
 */
void ActionTest::testCommandAction() {
  Context context;
  vector<string> args;
  args.push_back("one");
  args.push_back("two");
  MockCommandAction action("echo", args);
  action.Execute(&context, 0);

  const char *expected_args[] = {"echo", "one", "two", NULL};
  action.CheckArgs(OLA_SOURCELINE(), expected_args);

  // now check interpolated variables
  args.push_back("_${slot_offset}_");
  args.push_back("_${slot_value}_");
  MockCommandAction action2("echo", args);

  context.SetSlotOffset(1);
  context.SetSlotValue(100);
  action2.Execute(&context, 0);

  const char *expected_args2[] = {"echo", "one", "two", "_1_", "_100_", NULL};
  action2.CheckArgs(OLA_SOURCELINE(), expected_args2);
}