From 0be954538ce02993076ac8864e3be3e27966266f Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Sat, 5 Nov 2022 17:04:42 +0100 Subject: [PATCH] Add tests (closed #3) --- .github/workflows/ruby_rspec.yml | 23 +++++++++++ Gemfile | 5 ++- Rakefile | 7 ++++ lib/array.rb | 5 --- lib/chunk.rb | 40 ------------------- lib/packet.rb | 4 -- spec/01_array_spec.rb | 13 +++++++ spec/02_packet_spec.rb | 11 ++++++ spec/03_chunk_spec.rb | 66 ++++++++++++++++++++++++++++++++ teeworlds-client.gemspec | 25 ++++++++++++ 10 files changed, 148 insertions(+), 51 deletions(-) create mode 100644 .github/workflows/ruby_rspec.yml create mode 100644 Rakefile create mode 100644 spec/01_array_spec.rb create mode 100644 spec/02_packet_spec.rb create mode 100644 spec/03_chunk_spec.rb create mode 100644 teeworlds-client.gemspec diff --git a/.github/workflows/ruby_rspec.yml b/.github/workflows/ruby_rspec.yml new file mode 100644 index 0000000..ec157c1 --- /dev/null +++ b/.github/workflows/ruby_rspec.yml @@ -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 diff --git a/Gemfile b/Gemfile index b181324..7f4f5e9 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,5 @@ -source "https://rubygems.org" +# frozen_string_literal: true -gem "huffman_tw" +source 'https://rubygems.org' +gemspec diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..a3fab08 --- /dev/null +++ b/Rakefile @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new(:spec) + +task default: [:spec] diff --git a/lib/array.rb b/lib/array.rb index ff2a239..bc1b712 100644 --- a/lib/array.rb +++ b/lib/array.rb @@ -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 - diff --git a/lib/chunk.rb b/lib/chunk.rb index a25fe28..076e835 100644 --- a/lib/chunk.rb +++ b/lib/chunk.rb @@ -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 - diff --git a/lib/packet.rb b/lib/packet.rb index 6a314af..a253496 100644 --- a/lib/packet.rb +++ b/lib/packet.rb @@ -104,7 +104,3 @@ class Packet end end -def todo_add_rspec_test - p PacketFlags.new(control: true).bits == "0001" -end - diff --git a/spec/01_array_spec.rb b/spec/01_array_spec.rb new file mode 100644 index 0000000..34218f1 --- /dev/null +++ b/spec/01_array_spec.rb @@ -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 + diff --git a/spec/02_packet_spec.rb b/spec/02_packet_spec.rb new file mode 100644 index 0000000..276ccae --- /dev/null +++ b/spec/02_packet_spec.rb @@ -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 + diff --git a/spec/03_chunk_spec.rb b/spec/03_chunk_spec.rb new file mode 100644 index 0000000..665e665 --- /dev/null +++ b/spec/03_chunk_spec.rb @@ -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 + diff --git a/teeworlds-client.gemspec b/teeworlds-client.gemspec new file mode 100644 index 0000000..569375b --- /dev/null +++ b/teeworlds-client.gemspec @@ -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