Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
run: bundle install
- name: Generate ruby
run: bundle exec rake generate_ruby:all
- name: Run loading tests
run: bundle exec rspec spec/tucana_spec.rb
- name: Run tests
run: bundle exec rspec

Expand Down
36 changes: 36 additions & 0 deletions build/ruby/lib/tucana/shared/shared.data_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module Tucana
module Shared
UnexpectedRuleType = Class.new(Tucana::Error)

DataTypeRule.class_eval do
def variant
self.config
end

def create(variant, config)
case variant
when :contains_key
self.contains_key = DataTypeContainsKeyRuleConfig.new(config)
when :contains_type
self.contains_type = DataTypeContainsTypeRuleConfig.new(config)
when :item_of_collection
self.item_of_collection = DataTypeItemOfCollectionRuleConfig.new(config)
when :number_range
self.number_range = DataTypeNumberRangeRuleConfig.new(config)
when :regex
self.regex = DataTypeRegexRuleConfig.new(config)
else
raise UnexpectedRuleType, "Unknown rule type #{variant}"
end

self
end

def self.create(variant, config)
self.new.create(variant, config)
end
end
end
end
55 changes: 55 additions & 0 deletions build/ruby/spec/tucana/shared/shared.data_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

Tucana.load_protocol(:shared)

RSpec.describe Tucana::Shared::DataTypeRule do
describe "#create" do
context "with :contains_key variant" do
it "sets the contains_key field" do
config = { key: "test_key", data_type_identifier: "test_type" }
rule = described_class.create(:contains_key, config)
expect(rule.contains_key).to be_a(Tucana::Shared::DataTypeContainsKeyRuleConfig)
end
end

context "with :contains_type variant" do
it "sets the contains_type field" do
config = { data_type_identifier: "test_type" }
rule = described_class.create(:contains_type, config)
expect(rule.contains_type).to be_a(Tucana::Shared::DataTypeContainsTypeRuleConfig)
end
end

context "with :item_of_collection variant" do
it "sets the item_of_collection field" do
config = { items: [Tucana::Shared::Value.from_ruby('item1'), Tucana::Shared::Value.from_ruby('item2')] }
rule = described_class.create(:item_of_collection, config)
expect(rule.item_of_collection).to be_a(Tucana::Shared::DataTypeItemOfCollectionRuleConfig)
end
end

context "with :number_range variant" do
it "sets the number_range field" do
config = { from: 1, to: 10 }
rule = described_class.create(:number_range, config)
expect(rule.number_range).to be_a(Tucana::Shared::DataTypeNumberRangeRuleConfig)
end
end

context "with :regex variant" do
it "sets the regex field" do
config = { pattern: "\\d+" }
rule = described_class.create(:regex, config)
expect(rule.regex).to be_a(Tucana::Shared::DataTypeRegexRuleConfig)
end
end

context "with unknown variant" do
it "raises UnexpectedRuleType error" do
expect {
described_class.create(:unknown, {})
}.to raise_error(Tucana::Shared::UnexpectedRuleType, "Unknown rule type unknown")
end
end
end
end