Add tests (closed #3)

This commit is contained in:
ChillerDragon 2022-11-05 17:04:42 +01:00
parent 00f2c89d93
commit 0be954538c
10 changed files with 148 additions and 51 deletions

23
.github/workflows/ruby_rspec.yml vendored Normal file
View file

@ -0,0 +1,23 @@
name: Ruby Rspec
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
run-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Ruby 3.1
uses: actions/setup-ruby@v1
with:
ruby-version: 3.1.x
- name: Run rspec tests
run: |
gem install bundler
bundle install --jobs 4 --retry 3
bundle exec rspec

View file

@ -1,4 +1,5 @@
source "https://rubygems.org"
# frozen_string_literal: true
gem "huffman_tw"
source 'https://rubygems.org'
gemspec

7
Rakefile Normal file
View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task default: [:spec]

View file

@ -17,8 +17,3 @@ class Array
end
end
def todo_make_this_a_rspec_test()
p (1..10).to_a.groups_of(2) == [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
p (1..10).to_a.groups_of(20) == [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
end

View file

@ -127,43 +127,3 @@ class BigChungusTheChunkGetter
end
end
def todo_make_this_an_rspec_test
# handcrafted fake packet
# two empty motd chunks
data = [
0x40, 0x02, 0x02, 0x02, 0x00,
0x40, 0x02, 0x02, 0x02, 0x00
].pack("C*")
chunks = BigChungusTheChunkGetter.get_chunks(data)
p chunks.size == 2
p chunks[0].msg == NETMSGTYPE_SV_MOTD
p chunks[1].msg == NETMSGTYPE_SV_MOTD
p chunks[0].sys == false
# actual packet server sends
data = [
0x40, 0x02, 0x02, 0x02, 0x00, # motd
0x40, 0x07, 0x03, 0x22, 0x01, 0x00, 0x01, 0x00, 0x01, 0x08, # server settings
0x40, 0x01, 0x04, 0x0b # ready
].pack("C*")
chunks = BigChungusTheChunkGetter.get_chunks(data)
p chunks.size == 3
p chunks[0].msg == NETMSGTYPE_SV_MOTD
p chunks[1].msg == NETMSGTYPE_SV_SERVERSETTINGS
# actual mapchange the server sends
map_change = [
0x40, 0x32, 0x01, 0x05, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x00,
0xee, 0xcb, 0xd0, 0xd7, 0x02, 0x9c, 0x0e, 0x08, 0xa8, 0x15, 0x1a, 0xb3, 0xbb, 0xb1, 0xd4, 0x04,
0x75, 0x68, 0xec, 0xe3, 0x41, 0x6e, 0x83, 0x20, 0xaf, 0x97, 0x0f, 0x49, 0xbe, 0x4f, 0x3c, 0x61,
0x04, 0xf4, 0xbe, 0x60, 0xd2, 0x87, 0x39, 0x91, 0x59, 0xab
].pack("C*")
chunks = BigChungusTheChunkGetter.get_chunks(map_change)
p chunks.size == 1
p chunks[0].sys == true
end
def test2
p NetChunk.create_vital_header({vital: true}, 20, 5) == [64, 20, 5]
end

View file

@ -104,7 +104,3 @@ class Packet
end
end
def todo_add_rspec_test
p PacketFlags.new(control: true).bits == "0001"
end

13
spec/01_array_spec.rb Normal file
View file

@ -0,0 +1,13 @@
require_relative '../lib/array'
describe 'Array', :array do
context 'Simple groups' do
it 'Should do groups of two' do
expect((1..10).to_a.groups_of(2)).to eq([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]])
end
it 'Should create one group if the input is less than the group size' do
expect((1..10).to_a.groups_of(20)).to eq([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
end
end
end

11
spec/02_packet_spec.rb Normal file
View file

@ -0,0 +1,11 @@
require_relative '../lib/packet'
describe 'Packet', :packet do
context 'Set flag bits' do
it 'Should set the control flag bit' do
expect(PacketFlags.new(control: true).bits).to eq("0001")
end
end
end

66
spec/03_chunk_spec.rb Normal file
View file

@ -0,0 +1,66 @@
require_relative '../lib/chunk'
describe 'NetChunk', :net_chunk do
context 'Create vital header' do
it 'Should set the vital flag' do
expect(NetChunk.create_vital_header({vital: true}, 20, 5)).to eq([64, 20, 5])
end
end
end
describe 'BigChungusTheChunkGetter', :chunk_getter do
context 'Single chunk' do
it 'Should count one motd chunk correctly' do
# handcrafted fake packet
# one empty motd chunks
data = [
0x40, 0x02, 0x02, 0x02, 0x00
].pack("C*")
chunks = BigChungusTheChunkGetter.get_chunks(data)
expect(chunks.size).to eq(1)
end
end
context 'Multiple chunks' do
it 'Should parse two motd chunks correctly' do
# handcrafted fake packet
# two empty motd chunks
data = [
0x40, 0x02, 0x02, 0x02, 0x00,
0x40, 0x02, 0x02, 0x02, 0x00
].pack("C*")
chunks = BigChungusTheChunkGetter.get_chunks(data)
expect(chunks.size).to eq(2)
expect(chunks[0].msg).to eq(NETMSGTYPE_SV_MOTD)
expect(chunks[1].msg).to eq(NETMSGTYPE_SV_MOTD)
expect(chunks[0].sys).to eq(false)
end
it 'Should parse motd + server settings' do
# actual packet server sends
data = [
0x40, 0x02, 0x02, 0x02, 0x00, # motd
0x40, 0x07, 0x03, 0x22, 0x01, 0x00, 0x01, 0x00, 0x01, 0x08, # server settings
0x40, 0x01, 0x04, 0x0b # ready
].pack("C*")
chunks = BigChungusTheChunkGetter.get_chunks(data)
expect(chunks.size).to eq(3)
expect(chunks[0].msg).to eq(NETMSGTYPE_SV_MOTD)
expect(chunks[1].msg).to eq(NETMSGTYPE_SV_SERVERSETTINGS)
end
it 'Should parse map change packet' do
# actual mapchange the server sends
map_change = [
0x40, 0x32, 0x01, 0x05, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x00,
0xee, 0xcb, 0xd0, 0xd7, 0x02, 0x9c, 0x0e, 0x08, 0xa8, 0x15, 0x1a, 0xb3, 0xbb, 0xb1, 0xd4, 0x04,
0x75, 0x68, 0xec, 0xe3, 0x41, 0x6e, 0x83, 0x20, 0xaf, 0x97, 0x0f, 0x49, 0xbe, 0x4f, 0x3c, 0x61,
0x04, 0xf4, 0xbe, 0x60, 0xd2, 0x87, 0x39, 0x91, 0x59, 0xab
].pack("C*")
chunks = BigChungusTheChunkGetter.get_chunks(map_change)
expect(chunks.size).to eq(1)
expect(chunks[0].sys).to eq(true)
end
end
end

25
teeworlds-client.gemspec Normal file
View file

@ -0,0 +1,25 @@
# frozen_string_literal: true
require 'rake'
Gem::Specification.new do |s|
s.name = 'teeworlds-client'
s.version = '0.0.1'
s.summary = 'teeworlds 0.7 network protocol (client)'
s.description = <<-DESC
A library wrapping the network protocol of the game teeworlds.
Supported protocol version 0.7 and only the client side.
DESC
s.authors = ['ChillerDragon']
s.email = 'ChillerDragon@gmail.com'
s.files = FileList[
'lib/*.rb'
]
s.required_ruby_version = '>= 3.1.2'
s.add_dependency 'huffman_tw', '~> 0.0.1'
s.add_dependency 'rspec', '~> 3.9.0'
s.homepage = 'https://github.com/ChillerDragon/teeworlds-client'
s.license = 'Unlicense'
s.metadata['rubygems_mfa_required'] = 'true'
end