Skip to content

Commit 46c5377

Browse files
committed
test: add Ruby version matrix testing for PropertyStruct
Add comprehensive Ruby testing infrastructure to verify PropertyStruct compatibility across Ruby versions 2.6 through head. Following the pattern from PR cloudfoundry#707, this adds: - GitHub Actions workflow testing across Ruby 2.6-3.4 and head - RSpec test suite for PropertyStruct - Tests for dynamic attribute access, nested structures, and arrays - Tests for Ruby standard library method pass-through - Tests for OpenStruct API compatibility The Ruby matrix ensures PropertyStruct works correctly across all supported Ruby versions, particularly validating Ruby 3.5+ compatibility where ostruct is being removed. Related to cloudfoundry#708
1 parent d09f9b7 commit 46c5377

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed

.github/workflows/ruby.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: ERB Renderer Ruby Tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test_property_struct:
7+
strategy:
8+
matrix:
9+
os: [ubuntu-latest, macos-latest]
10+
ruby: ['2.6', '2.7', '3.0', '3.1', '3.2', '3.3', '3.4', head]
11+
runs-on: ${{ matrix.os }}
12+
steps:
13+
- uses: actions/checkout@v6
14+
- uses: ruby/setup-ruby@v1
15+
with:
16+
ruby-version: ${{ matrix.ruby }}
17+
- run: bundle install
18+
working-directory: templatescompiler/erbrenderer/
19+
- run: bundle exec rake
20+
working-directory: templatescompiler/erbrenderer/
21+
continue-on-error: ${{ matrix.ruby == 'head' }}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.bundle/
2+
vendor/bundle/
3+
Gemfile.lock
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source "https://rubygems.org"
2+
3+
group :test do
4+
gem "rake"
5+
gem "rspec"
6+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require "rspec/core/rake_task"
2+
3+
RSpec::Core::RakeTask.new(:spec)
4+
5+
task default: [:spec]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
require "spec_helper"
2+
require "erb_renderer"
3+
4+
RSpec.describe "PropertyStruct" do
5+
describe "initialization and attribute access" do
6+
it "provides dynamic attribute access for hash keys" do
7+
ps = PropertyStruct.new(name: "test", value: 42)
8+
expect(ps.name).to eq("test")
9+
expect(ps.value).to eq(42)
10+
end
11+
12+
it "converts string keys to symbols" do
13+
ps = PropertyStruct.new("name" => "test", "value" => 42)
14+
expect(ps.name).to eq("test")
15+
expect(ps.value).to eq(42)
16+
end
17+
18+
it "supports nested attribute access" do
19+
ps = PropertyStruct.new(config: {database: {host: "localhost", port: 5432}})
20+
nested = ps.config
21+
expect(nested).to be_a(PropertyStruct)
22+
expect(nested.database).to be_a(PropertyStruct)
23+
expect(nested.database.host).to eq("localhost")
24+
expect(nested.database.port).to eq(5432)
25+
end
26+
27+
it "handles arrays of hashes" do
28+
ps = PropertyStruct.new(servers: [{name: "web1", ip: "10.0.0.1"}, {name: "web2", ip: "10.0.0.2"}])
29+
servers = ps.servers
30+
expect(servers).to be_an(Array)
31+
expect(servers.length).to eq(2)
32+
expect(servers.first.name).to eq("web1")
33+
expect(servers.last.ip).to eq("10.0.0.2")
34+
end
35+
36+
it "responds to method queries correctly" do
37+
ps = PropertyStruct.new(existing_key: "value")
38+
expect(ps.respond_to?(:existing_key)).to be true
39+
expect(ps.respond_to?(:nonexistent_key)).to be false
40+
end
41+
end
42+
43+
describe "Ruby standard library method pass-through" do
44+
it "supports array operations like map" do
45+
ps = PropertyStruct.new(ports: [8080, 8081, 8082])
46+
expect(ps.ports.map(&:to_s)).to eq(["8080", "8081", "8082"])
47+
end
48+
49+
it "supports string operations" do
50+
ps = PropertyStruct.new(url: "https://example.com")
51+
expect(ps.url.start_with?("https")).to be true
52+
expect(ps.url.split("://")).to eq(["https", "example.com"])
53+
end
54+
55+
it "supports hash operations" do
56+
ps = PropertyStruct.new(config: {a: 1, b: 2, c: 3})
57+
expect(ps.config.keys.sort).to eq([:a, :b, :c])
58+
expect(ps.config.values.sum).to eq(6)
59+
end
60+
61+
it "supports nil and empty checks" do
62+
ps = PropertyStruct.new(empty_string: "", nil_value: nil, filled: "data")
63+
expect(ps.empty_string.empty?).to be true
64+
expect(ps.nil_value.nil?).to be true
65+
expect(ps.filled.nil?).to be false
66+
end
67+
end
68+
69+
describe "compatibility across Ruby versions" do
70+
it "works with ERB rendering" do
71+
template = ERB.new("<%= obj.name.upcase %>: <%= obj.ports.join(',') %>")
72+
ps = PropertyStruct.new(name: "service", ports: [80, 443, 8080])
73+
result = template.result(binding)
74+
expect(result).to eq("SERVICE: 80,443,8080")
75+
end
76+
77+
it "maintains OpenStruct API compatibility" do
78+
# Test that PropertyStruct can be used as a drop-in replacement for OpenStruct
79+
ps = PropertyStruct.new(field1: "value1", field2: "value2")
80+
expect(ps).to respond_to(:field1)
81+
expect(ps).to respond_to(:field2)
82+
expect(ps.field1).to eq("value1")
83+
expect(ps.field2).to eq("value2")
84+
end
85+
end
86+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require "rspec"
2+
require "json"
3+
require "tmpdir"
4+
require "erb"
5+
6+
ERB_RENDERER_ROOT = File.expand_path("..", File.dirname(__FILE__))
7+
8+
$LOAD_PATH.unshift(ERB_RENDERER_ROOT)

0 commit comments

Comments
 (0)