diff --git a/.gitignore b/.gitignore index 45e2eb4..1e24dcf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target tmp/ *.bundle +.rspec_status diff --git a/expression-ruby/ext/lago_expression/src/lib.rs b/expression-ruby/ext/lago_expression/src/lib.rs index 8669b93..a614cf0 100644 --- a/expression-ruby/ext/lago_expression/src/lib.rs +++ b/expression-ruby/ext/lago_expression/src/lib.rs @@ -3,10 +3,10 @@ use std::collections::HashMap; use expression_core::{Event, Expression, ExpressionParser, ExpressionValue}; use magnus::{function, method, value::ReprValue, Error, IntoValue, Module, Object, Ruby}; -#[magnus::wrap(class = "Expression", free_immediately, size)] +#[magnus::wrap(class = "Lago::Expression", free_immediately, size)] struct ExpressionWrapper(Expression); -#[magnus::wrap(class = "Event", free_immediately, size)] +#[magnus::wrap(class = "Lago::Event", free_immediately, size)] struct EventWrapper(Event); impl EventWrapper { diff --git a/expression-ruby/lib/lago-expression.rb b/expression-ruby/lib/lago-expression.rb index 39d2f8d..e3374b8 100644 --- a/expression-ruby/lib/lago-expression.rb +++ b/expression-ruby/lib/lago-expression.rb @@ -1,3 +1,8 @@ require 'bigdecimal/util' require_relative 'lago_expression/lago_expression' + + +module Lago + VERSION = '0.0.1'.freeze +end diff --git a/expression-ruby/spec/lago-expression/expression_parser_spec.rb b/expression-ruby/spec/lago-expression/expression_parser_spec.rb new file mode 100644 index 0000000..6f40daa --- /dev/null +++ b/expression-ruby/spec/lago-expression/expression_parser_spec.rb @@ -0,0 +1,14 @@ +require 'spec_helper' + + +RSpec.describe Lago::ExpressionParser do + describe '.parse' do + it "returns an expression when it's valid" do + expect(described_class.parse("1+2")).not_to be_nil + end + + it "returns nil when it's not valid" do + expect(described_class.parse("1+")).to be_nil + end + end +end diff --git a/expression-ruby/spec/lago-expression/expression_spec.rb b/expression-ruby/spec/lago-expression/expression_spec.rb new file mode 100644 index 0000000..40b539b --- /dev/null +++ b/expression-ruby/spec/lago-expression/expression_spec.rb @@ -0,0 +1,50 @@ +require 'spec_helper' + + +RSpec.describe Lago::Expression do + let(:event) { Lago::Event.new("code", 1234, {"property_1" => "1.23", "property_2" => "test", "property_3" => "12.34"}) } + + describe '#evaluate' do + context "with a simple math expression" do + let(:expression) { Lago::ExpressionParser.parse("1 + 3") } + + it "returns a bigdecimal" do + expect(expression.evaluate(event)).to eq(4.to_d) + expect(expression.evaluate(event)).to be_a(BigDecimal) + end + end + + context "with a simple string expression" do + let(:expression) { Lago::ExpressionParser.parse("'test'") } + + it "returns a string" do + expect(expression.evaluate(event)).to eq('test') + expect(expression.evaluate(event)).to be_a(String) + end + end + + context "with a math expression with a decimal value from the event" do + let(:expression) { Lago::ExpressionParser.parse("(123 - event.properties.property_1) / 10") } + + it "returns the calculated value" do + expect(expression.evaluate(event)).to eq(12.177) + end + end + + context "with a concat function" do + let(:expression) { Lago::ExpressionParser.parse("concat(event.properties.property_2, '-', 'suffix')") } + + it "concats the string" do + expect(expression.evaluate(event)).to eq('test-suffix') + end + end + + context "with rounding function" do + let(:expression) { Lago::ExpressionParser.parse("round(event.properties.property_3, -1)") } + + it "rounds the property" do + expect(expression.evaluate(event)).to eq(10) + end + end + end +end diff --git a/expression-ruby/spec/lago-expression/version_spec.rb b/expression-ruby/spec/lago-expression/version_spec.rb new file mode 100644 index 0000000..56e154d --- /dev/null +++ b/expression-ruby/spec/lago-expression/version_spec.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true +# +require 'spec_helper' + +RSpec.describe Lago do + it 'has a version number' do + expect(Lago::VERSION).not_to be nil + end +end diff --git a/expression-ruby/spec/spec_helper.rb b/expression-ruby/spec/spec_helper.rb new file mode 100644 index 0000000..1e6dacd --- /dev/null +++ b/expression-ruby/spec/spec_helper.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'lago-expression' + +RSpec.configure do |config| + + # Enable flags like --only-failures and --next-failure + config.example_status_persistence_file_path = '.rspec_status' + + # Disable RSpec exposing methods globally on `Module` and `main` + config.disable_monkey_patching! + + config.expect_with :rspec do |c| + c.syntax = :expect + end +end