Skip to content

Commit

Permalink
Fix ruby gem and add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
nudded committed Oct 16, 2024
1 parent 787fcaf commit 005c49c
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
tmp/
*.bundle
.rspec_status
4 changes: 2 additions & 2 deletions expression-ruby/ext/lago_expression/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions expression-ruby/lib/lago-expression.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
require 'bigdecimal/util'

require_relative 'lago_expression/lago_expression'


module Lago
VERSION = '0.0.1'.freeze
end
14 changes: 14 additions & 0 deletions expression-ruby/spec/lago-expression/expression_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -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
50 changes: 50 additions & 0 deletions expression-ruby/spec/lago-expression/expression_spec.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions expression-ruby/spec/lago-expression/version_spec.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions expression-ruby/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 005c49c

Please sign in to comment.