add some tests for RubyAlgebra::Parser

This commit is contained in:
2026-03-23 11:11:14 +03:00
parent b4319a41f4
commit 4a86e653d5
3 changed files with 72 additions and 0 deletions

View File

@@ -1,4 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
require "bundler/gem_tasks" require "bundler/gem_tasks"
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:spec)
task default: %i[] task default: %i[]

View File

@@ -35,6 +35,8 @@ Gem::Specification.new do |spec|
# Uncomment to register a new dependency of your gem # Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0" # spec.add_dependency "example-gem", "~> 1.0"
#
spec.add_development_dependency "rspec", "~> 3.13"
# For more information and examples about making a new gem, check out our # For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html # guide at: https://bundler.io/guides/creating_gem.html

67
spec/parser_spec.rb Normal file
View File

@@ -0,0 +1,67 @@
require "ruby_algebra"
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
end
RSpec.describe RubyAlgebra::Parser do
describe "#parse" do
it "returns nil for an empty string" do
expect(RubyAlgebra::Parser.parse("")).to eq nil
end
it "returns Constant(a) for a" do
[10, 100, 343843, 180.1, 777, 0.52242].each do |a|
expect(RubyAlgebra::Parser.parse(a.to_s)).to eq RubyAlgebra::Constant.new(a)
end
end
it "returns Variable(x) for x" do
["x", "y", "z", "coolVariable"].each do |x|
expect(RubyAlgebra::Parser.parse(x)).to eq RubyAlgebra::Variable.new(x)
end
end
it "returns Addition(Constant(5), Variable(x)) for 5 + x" do
expect(RubyAlgebra::Parser.parse("5 + x")).to eq RubyAlgebra::Addition.new(RubyAlgebra::Constant.new(5), RubyAlgebra::Variable.new("x"))
end
it "returns Subtraction(Variable(z), Variable(h)) for z - h" do
expect(RubyAlgebra::Parser.parse("z - h")).to eq RubyAlgebra::Subtraction.new(RubyAlgebra::Variable.new("z"), RubyAlgebra::Variable.new("h"))
end
it "returns Multiplication(Constant(5), Constant(8)) for 60 * 8" do
expect(RubyAlgebra::Parser.parse("60 * 8")).to eq RubyAlgebra::Multiplication.new(RubyAlgebra::Constant.new(60), RubyAlgebra::Constant.new(8))
end
it "returns Division(Constant(3), Variable(j)) for 3/j" do
expect(RubyAlgebra::Parser.parse("3/j")).to eq RubyAlgebra::Division.new(RubyAlgebra::Constant.new(3), RubyAlgebra::Variable.new("j"))
end
it "returns Division(Constant(3), Variable(j)) for 3/j" do
expect(RubyAlgebra::Parser.parse("3/j")).to eq RubyAlgebra::Division.new(RubyAlgebra::Constant.new(3), RubyAlgebra::Variable.new("j"))
end
it "returns Power(Constant(2), Constant(24)) for 2^24" do
expect(RubyAlgebra::Parser.parse("2^24")).to eq RubyAlgebra::Power.new(RubyAlgebra::Constant.new(2), RubyAlgebra::Constant.new(24))
end
it "returns Multiplication(Constant(-1), Variable(x)) for -x" do
expect(RubyAlgebra::Parser.parse("-x")).to eq RubyAlgebra::Multiplication.new(RubyAlgebra::Constant.new(-1), RubyAlgebra::Variable.new("x"))
end
it "returns Variable(x) for +x" do
expect(RubyAlgebra::Parser.parse("x")).to eq RubyAlgebra::Variable.new("x")
end
it "returns Addition(Addition(Constant(3), Constant(30)), Constant(300)) for 3 + 30 + 300" do
expect(RubyAlgebra::Parser.parse("3 + 30 + 300")).to eq RubyAlgebra::Addition.new(RubyAlgebra::Addition.new(RubyAlgebra::Constant.new(3), RubyAlgebra::Constant.new(30)), RubyAlgebra::Constant.new(300))
end
it "returns Power(Constant(10), Power(Constant(10), Constant(10))) for 10 ^ 10 ^ 10" do
expect(RubyAlgebra::Parser.parse("10 ^ 10 ^ 10")).to eq RubyAlgebra::Power.new(RubyAlgebra::Constant.new(10), RubyAlgebra::Power.new(RubyAlgebra::Constant.new(10), RubyAlgebra::Constant.new(10)))
end
end
end