From c952f33d50379e0c76c28b8308284704c6c41f2b Mon Sep 17 00:00:00 2001 From: Saf Date: Sun, 30 Aug 2020 16:11:54 -0700 Subject: [PATCH] Setup RSpec rake task and wrote spec for MemorySpace (#2) * Setup RSpec rake task and wrote spec for MemorySpace * Removed mintest rake task --- Rakefile | 11 +-- n65.gemspec | 1 + spec/.rubocop.yml | 4 ++ spec/lib/n65/memory_space_spec.rb | 107 ++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 spec/.rubocop.yml create mode 100644 spec/lib/n65/memory_space_spec.rb diff --git a/Rakefile b/Rakefile index df3ca09..7b5e6bf 100644 --- a/Rakefile +++ b/Rakefile @@ -1,13 +1,16 @@ # frozen_string_literal: true require 'bundler/gem_tasks' -require 'rake/testtask' -Rake::TestTask.new do |t| - t.pattern = 'test/test*.rb' +begin + require 'rspec/core/rake_task' + RSpec::Core::RakeTask.new(:spec) + task(default: :spec) +rescue LoadError + warn("Couldn't load RSpec gem") end -## Check the syntax of all ruby files +# Check the syntax of all Ruby files task :syntax do sh 'find . -name *.rb -type f -exec ruby -c {} \; -exec echo {} \;' end diff --git a/n65.gemspec b/n65.gemspec index 6c7f8a1..c8ca351 100644 --- a/n65.gemspec +++ b/n65.gemspec @@ -25,4 +25,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'minitest' spec.add_development_dependency 'rake' spec.add_development_dependency 'rubocop' + spec.add_development_dependency 'rspec' end diff --git a/spec/.rubocop.yml b/spec/.rubocop.yml new file mode 100644 index 0000000..dd94258 --- /dev/null +++ b/spec/.rubocop.yml @@ -0,0 +1,4 @@ +inherit_from: ../.rubocop.yml + +Metrics/BlockLength: + Enabled: false diff --git a/spec/lib/n65/memory_space_spec.rb b/spec/lib/n65/memory_space_spec.rb new file mode 100644 index 0000000..d9fc814 --- /dev/null +++ b/spec/lib/n65/memory_space_spec.rb @@ -0,0 +1,107 @@ +# frozen_string_literal: true + +require_relative '../../../lib/n65/memory_space' + +RSpec.describe(N65::MemorySpace) do + describe '.new' do + context 'when provided a size and type' do + let(:bank) { described_class.new(size, type) } + let(:size) { 0x100 } + let(:type) { :prog } + + it 'returns a zeroed bank' do + expect(bank.emit_bytes.all?(&:zero?)).to eq(true) + end + + it 'returns the requested sized bank' do + expect(bank.emit_bytes.size).to be(size) + end + end + end + + describe '.create_prog_rom' do + context 'when creating a new prog rom' do + let(:bank) { described_class.create_prog_rom } + + it 'returns a zeroed bank' do + expect(bank.emit_bytes.all?(&:zero?)).to eq(true) + end + + it 'returns the correct sized bank' do + expect(bank.emit_bytes.size).to eq(0x4000) + end + end + end + + describe '.create_char_rom' do + context 'when creating a new prog rom' do + let(:bank) { described_class.create_char_rom } + + it 'returns a zeroed bank' do + expect(bank.emit_bytes.all?(&:zero?)).to eq(true) + end + + it 'returns the correct sized bank' do + expect(bank.emit_bytes.size).to eq(0x2000) + end + end + end + + describe '#read' do + let(:address) { 0xC100 } + let(:mirroed_address) { 0x8100 } + let(:bank) { described_class.create_prog_rom } + let(:data) { 'hi there'.bytes } + + before { bank.write(address, data) } + + context 'when reading from the bank' do + it 'can read back from the 0xC000 base address' do + expect(bank.read(address, data.size)).to eq(data) + end + + it 'can read back from the 0x8000 mirrored address' do + expect(bank.read(mirroed_address, data.size)).to eq(data) + end + end + + context 'when attempting to read out of bounds' do + it 'throws an error' do + expect { bank.read(0x0, 0x10) }.to raise_error(described_class::AccessOutsideProgRom) + end + + it 'throws an error' do + expect { bank.read(0xffff, 0x10) }.to raise_error(described_class::AccessOutsideProgRom) + end + end + end + + describe '#write' do + let(:address) { 0xC100 } + let(:mirroed_address) { 0x8100 } + let(:bank) { described_class.create_prog_rom } + let(:data) { 'hi there'.bytes } + + before { bank.write(mirroed_address, data) } + + context 'when reading from the bank' do + it 'can read back from the 0xC000 base address' do + expect(bank.read(address, data.size)).to eq(data) + end + + it 'can read back from the 0x8000 mirrored address' do + expect(bank.read(mirroed_address, data.size)).to eq(data) + end + end + + context 'when attempting to write out of bounds' do + it 'throws an error' do + expect { bank.write(0x0, data) }.to raise_error(described_class::AccessOutsideProgRom) + end + + it 'throws an error' do + expect { bank.write(0xffff, data) }.to raise_error(described_class::AccessOutsideProgRom) + end + end + end +end