Skip to content

Instantly share code, notes, and snippets.

View barneywilliams's full-sized avatar

Greg Williams barneywilliams

  • Bytewise
  • Grand Rapids, MI, USA
View GitHub Profile
@barneywilliams
barneywilliams / client.cpp
Created September 20, 2012 14:43
JSONSocket - Qt JSON receipt and parsing
void Client::ProcessRequest(void)
{
QByteArray request = tcpSocket->readAll();
QScriptEngine engine;
// Validate that the request was parsed and non-empty
QScriptValue data = engine.evaluate("(" + QString(request) + ")");
...
}
@barneywilliams
barneywilliams / client.cpp
Created September 20, 2012 14:40
JSONSocket - Qt JSON parsing and validation
QString Client::ProcessCommand(QScriptValue req)
{
QScriptValue cmd = req.property("command");
QString response;
// Basic sanity check
if (!cmd.isValid())
{
response = CommandErrorResponse("invalid", "No command specified");
}
@barneywilliams
barneywilliams / client.cpp
Created September 20, 2012 14:37
JSONSocket - Qt QTcpSocket creation and wiring up
tcpSocket = new QTcpSocket(this);
...
Q_ASSERT(connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(ProcessRequest())));
@barneywilliams
barneywilliams / json_server.rb
Created September 20, 2012 14:32
JSONSocket - Ruby server class snippet
def initialize
@server = TCPServer.new 19024
end
def start
@server_thread = Thread.new do
@client_thread = Thread.start(@server.accept) do |client|
puts "server: Accepted client connection!"
send_request(client, 'greeting', :message => 'Hello client!')
receive_response(client)
@barneywilliams
barneywilliams / JSONSocket - Qt JSON parsing and validation
Created September 20, 2012 14:28
JSONSocket - Ruby server class snippet
QString Client::ProcessCommand(QScriptValue req)
{
QScriptValue cmd = req.property("command");
QString response;
// Basic sanity check
if (!cmd.isValid())
{
response = CommandErrorResponse("invalid", "No command specified");
}
@barneywilliams
barneywilliams / CalculatorSteps.cpp
Created May 24, 2012 13:10
C++ step definitions and the corresponding test fixture
#include <gtest/gtest.h>
#include <cucumber-cpp/defs.hpp>
#include <Calculator.h>
struct CalcCtx {
Calculator calc;
double result;
};
@barneywilliams
barneywilliams / division.feature
Created May 23, 2012 06:01
Cucumber-Cpp Example Trace
Scenario: Regular numbers # division.feature:6
> ["begin_scenario"] # Cucumber sends this message to the Wire server to start the scenario
< ["success"] # Each message sent to the Wire server is acknowledged, and may include response data as well
> ["step_matches", # Cucumber sends the first step of the scenario
{
"name_to_match":"I have entered 3 into the calculator"
}
]
< ["success", # The Wire server responds with the regular expression that matches the specified step, including the extracted parameter and location of the step definition
[
@barneywilliams
barneywilliams / division.feature
Created May 23, 2012 05:52
Cucumber-Cpp Feature
Feature: Division
In order to avoid silly mistakes
Cashiers must be able to calculate a fraction
Scenario: Regular numbers
Given I have entered 3 into the calculator
And I have entered 2 into the calculator
When I press divide
Then the result should be 1.5 on the screen
@barneywilliams
barneywilliams / division.feature
Created May 23, 2012 05:45
Cucumber-Cpp Example
Feature: Division
In order to avoid silly mistakes
Cashiers must be able to calculate a fraction
Scenario: Regular numbers # division.feature:6
Given I have entered 3 into the calculator # CalculatorSteps.cpp:11
And I have entered 2 into the calculator # CalculatorSteps.cpp:11
When I press divide # CalculatorSteps.cpp:22
Then the result should be 1.5 on the screen # CalculatorSteps.cpp:27
@barneywilliams
barneywilliams / division.feature
Created May 23, 2012 05:37
Cucumber-Cpp Example w/Trace
Feature: Division
In order to avoid silly mistakes
Cashiers must be able to calculate a fraction
Scenario: Regular numbers # examples/Calc/CalcFeatures/features/division.feature:6
> ["begin_scenario"]
< ["success"]
> ["step_matches",{"name_to_match":"I have entered 3 into the calculator"}]
< ["success",[{"regexp":"^I have entered (\\d+) into the calculator$","args":[{"val":"3","pos":15}],"id":"1","source":"GTestCalculatorSteps.cpp:11"}]]
> ["invoke",{"args":["3"],"id":"1"}]