Skip to content

Commit

Permalink
Added a test case for the switch module
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfmanjm committed Sep 13, 2015
1 parent 80bbeba commit 2097978
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ if TESTING
TESTMODULES= %w(tools/temperatureswitch) unless defined? EXCLUDE_MODULES
puts "Modules under test: #{TESTMODULES}"
excludes << %w(Kernel.cpp main.cpp) # we replace these with mock versions in testframework
testmodules= FileList['src/testframework/*.{c,cpp}', 'src/testframework/easyunit/*.{c,cpp}', 'src/modules/communication/SerialConsole.cpp', 'src/modules/communication/utils/Gcode.cpp'].include(TESTMODULES.collect { |e| "src/modules/#{e}/**/*.{c,cpp}"}).include(TESTMODULES.collect { |e| "src/testframework/unittests/#{e}/*.{c,cpp}"})
SRC = FileList['src/libs/**/*.{c,cpp}'].exclude(/#{excludes.join('|')}/) + testmodules
frameworkfiles= FileList['src/testframework/*.{c,cpp}', 'src/testframework/easyunit/*.{c,cpp}']
extrafiles= FileList['src/modules/communication/SerialConsole.cpp', 'src/modules/communication/utils/Gcode.cpp', 'src/modules/robot/Conveyor.cpp', 'src/modules/robot/Block.cpp']
testmodules= FileList['src/libs/**/*.{c,cpp}'].include(TESTMODULES.collect { |e| "src/modules/#{e}/**/*.{c,cpp}"}).include(TESTMODULES.collect { |e| "src/testframework/unittests/#{e}/*.{c,cpp}"}).exclude(/#{excludes.join('|')}/)
SRC = frameworkfiles + extrafiles + testmodules
else
excludes << %w(testframework)
SRC = FileList['src/**/*.{c,cpp}'].exclude(/#{excludes.join('|')}/)
Expand Down
1 change: 0 additions & 1 deletion src/modules/tools/switch/Switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ void Switch::on_get_public_data(void *argument)
pad->state = this->switch_state;
pad->value = this->switch_value;

pdr->set_data_ptr(&pad);
pdr->set_taken();
pdr->clear_returned_data();
}
Expand Down
12 changes: 12 additions & 0 deletions src/testframework/Test_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ This is aprt of the Smoothie test framework, it generates a Mockable Kernl so ke

Kernel* Kernel::instance;


// define some dummies here to satisfy linker
// Conveyor::Conveyor(){ /*dummy*/ }
// void Conveyor::wait_for_empty_queue(){ /*dummy*/ }
//template class HeapRing<Block>;


// The kernel is the central point in Smoothie : it stores modules, and handles event calls
Kernel::Kernel(){
instance= this; // setup the Singleton instance of the kernel
Expand All @@ -57,6 +64,11 @@ Kernel::Kernel(){

this->current_path = "/";

this->slow_ticker = new SlowTicker();

// dummies (woul dbe noce to refactor to not have to create a conveyor)
this->conveyor= new Conveyor();

// Configure UART depending on MRI config
// Match up the SerialConsole to MRI UART. This makes it easy to use only one UART for both debug and actual commands.
NVIC_SetPriorityGrouping(0);
Expand Down
90 changes: 90 additions & 0 deletions src/testframework/unittests/tools/switch/TEST_Switch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "Kernel.h"
#include "checksumm.h"
#include "utils.h"
#include "Test_kernel.h"
#include "PublicDataRequest.h"
#include "PublicData.h"
#include "SwitchPublicAccess.h"
#include "Gcode.h"
#include "Switch.h"
#include "mri.h"

#include <stdio.h>
#include <memory>

#include "easyunit/test.h"

// this declares any global variables the test needs
DECLARE(Switch)
Switch *ts;
uint16_t switch_id;
END_DECLARE

// called before each test
SETUP(Switch)
{
// create the module we want to test
switch_id= get_checksum("fan");
ts = new Switch(switch_id);
//printf("...Setup Switch\n");
}

// called after each test
TEARDOWN(Switch)
{
// delete the module
delete ts;

// have kernel reset to a clean state
test_kernel_teardown();

//printf("...Teardown Switch\n");
}

// define various configs here, these are in the same formate they would appeat in the config file (comments removed for clarity)
const static char switch_config[]= "\
switch.fan.enable true \n\
switch.fan.input_on_command M106 \n\
switch.fan.input_off_command M107 \n\
switch.fan.output_pin 2.6 \n\
switch.fan.output_type digital \n\
";

static bool get_switch_state(Switch *ts, struct pad_switch& s)
{
// inquire if the switch is on or off (simulate an ON_GET_PUBLIC_DATA)
PublicDataRequest pdr(switch_checksum, fan_checksum, 0);
pdr.set_data_ptr(&s); // the caller may have put a placeholder for the returned data here
ts->on_get_public_data(&pdr);
return(pdr.is_taken() && !pdr.has_returned_data());
}

TESTF(Switch,set_on_off_with_gcode)
{
// load config with required settings for this test
test_kernel_setup_config(switch_config, &switch_config[sizeof(switch_config)]);

// make module load the config
ts->on_config_reload(nullptr);

// make sure switch starts off
struct pad_switch s;
ASSERT_TRUE(get_switch_state(ts, s));
ASSERT_EQUALS(s.name, switch_id);
ASSERT_TRUE(!s.state);

// if we want to enter the debugger here
//__debugbreak();

// now turn it on
Gcode gc1("M106", (StreamOutput *)THEKERNEL->serial);
ts->on_gcode_received(&gc1);
ASSERT_TRUE(get_switch_state(ts, s));
ASSERT_TRUE(s.state);

// now turn it off
Gcode gc2("M107", (StreamOutput *)THEKERNEL->serial);
ts->on_gcode_received(&gc2);
ASSERT_TRUE(get_switch_state(ts, s));
ASSERT_TRUE(!s.state);
}

0 comments on commit 2097978

Please sign in to comment.