|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Liquid Spec Adapter for Shopify/liquid with ActiveSupport loaded |
| 4 | +# |
| 5 | +# Run with: bundle exec liquid-spec run spec/ruby_liquid_with_active_support.rb |
| 6 | + |
| 7 | +$LOAD_PATH.unshift(File.expand_path('../lib', __dir__)) |
| 8 | +require 'active_support/all' |
| 9 | +require 'liquid' |
| 10 | + |
| 11 | +LiquidSpec.configure do |config| |
| 12 | + # Run core Liquid specs |
| 13 | + config.features = [:core] |
| 14 | +end |
| 15 | + |
| 16 | +# Compile a template string into a Liquid::Template |
| 17 | +LiquidSpec.compile do |source, options| |
| 18 | + Liquid::Template.parse(source, **options) |
| 19 | +end |
| 20 | + |
| 21 | +# Render a compiled template with the given context |
| 22 | +# @param template [Liquid::Template] compiled template |
| 23 | +# @param assigns [Hash] environment variables |
| 24 | +# @param options [Hash] :registers, :strict_errors, :exception_renderer |
| 25 | +LiquidSpec.render do |template, assigns, options| |
| 26 | + registers = Liquid::Registers.new(options[:registers] || {}) |
| 27 | + |
| 28 | + context = Liquid::Context.build( |
| 29 | + static_environments: assigns, |
| 30 | + registers: registers, |
| 31 | + rethrow_errors: options[:strict_errors], |
| 32 | + ) |
| 33 | + |
| 34 | + context.exception_renderer = options[:exception_renderer] if options[:exception_renderer] |
| 35 | + |
| 36 | + template.render(context) |
| 37 | +end |
0 commit comments