-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/target | ||
tmp/ | ||
*.bundle | ||
.rspec_status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
expression-ruby/spec/lago-expression/expression_parser_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |