80 lines
2.2 KiB
Ruby
80 lines
2.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'spec_helper'
|
|
|
|
RSpec.describe WAG::Function do
|
|
let(:label) { Faker::Name.first_name }
|
|
let(:function) { described_class.new(label) }
|
|
subject { function }
|
|
|
|
describe 'When the function is given a label' do
|
|
let(:label) { Faker::Name.first_name }
|
|
|
|
it 'correctly sets the label' do
|
|
expect(subject.label.value).to eq "$#{label}".to_sym
|
|
end
|
|
end
|
|
|
|
describe '#param' do
|
|
let(:type) { :i32 }
|
|
subject { super().param(label, type) }
|
|
|
|
it 'adds the param to the function' do
|
|
expect { subject }
|
|
.to change { function.params.size }
|
|
.from(0)
|
|
.to(1)
|
|
end
|
|
|
|
it 'correctly sets the param' do
|
|
subject
|
|
expect(function.params.first).to be_a(WAG::Param)
|
|
expect(function.params.first.label.value).to eq("$#{label}".to_sym)
|
|
expect(function.params.first.type).to be_a(WAG::Type::I32)
|
|
end
|
|
end
|
|
|
|
describe '#result' do
|
|
subject { super().result(:i32) }
|
|
|
|
it 'correctly sets the result' do
|
|
subject
|
|
expect(function.result).to be_a(WAG::Result)
|
|
expect(function.result.types.size).to eq(1)
|
|
expect(function.result.types.first).to be_a(WAG::Type::I32)
|
|
end
|
|
end
|
|
|
|
describe '#to_sexpr' do
|
|
subject { super().to_sexpr }
|
|
|
|
context 'When the function is empty' do
|
|
let(:label) { nil }
|
|
it { is_expected.to eq [:func] }
|
|
end
|
|
|
|
context 'When the function has a label' do
|
|
it { is_expected.to eq [:func, "$#{label}".to_sym] }
|
|
|
|
context 'And the function takes a parameter' do
|
|
before { function.param(:p0, :i32) }
|
|
it { is_expected.to eq [:func, "$#{label}".to_sym, %i[param $p0 i32]] }
|
|
|
|
context 'And the function takes a second parameter' do
|
|
before { function.param(:p1, :f32) }
|
|
it { is_expected.to eq [:func, "$#{label}".to_sym, %i[param $p0 i32], %i[param $p1 f32]] }
|
|
end
|
|
end
|
|
|
|
context 'When the function has a return type' do
|
|
before { function.result(:i64) }
|
|
it { is_expected.to eq [:func, "$#{label}".to_sym, %i[result i64]] }
|
|
end
|
|
|
|
context 'When the function has a body' do
|
|
before { function.i64.const(13) }
|
|
it { is_expected.to eq [:func, "$#{label}".to_sym, [:"i64.const", 13]] }
|
|
end
|
|
end
|
|
end
|
|
end
|