diff --git a/Gemfile b/Gemfile index 47ffd876..c71645a8 100644 --- a/Gemfile +++ b/Gemfile @@ -34,4 +34,5 @@ end group :spec do # Minimum required: 3d1b492 (run `bundle update liquid-spec` to get newer) gem 'liquid-spec', github: 'Shopify/liquid-spec', ref: '3d1b492dce27cd78b4d5a46ca8cf260ae1349f29' + gem 'activesupport' end diff --git a/spec/ruby_liquid_with_active_support.rb b/spec/ruby_liquid_with_active_support.rb new file mode 100644 index 00000000..2b3d817f --- /dev/null +++ b/spec/ruby_liquid_with_active_support.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +# Liquid Spec Adapter for Shopify/liquid with ActiveSupport loaded +# +# Run with: bundle exec liquid-spec run spec/ruby_liquid_with_active_support.rb + +$LOAD_PATH.unshift(File.expand_path('../lib', __dir__)) +require 'active_support/all' +require 'liquid' + +LiquidSpec.configure do |config| + # Run core Liquid specs + config.features = [:core] +end + +# Compile a template string into a Liquid::Template +LiquidSpec.compile do |source, options| + Liquid::Template.parse(source, **options) +end + +# Render a compiled template with the given context +# @param template [Liquid::Template] compiled template +# @param assigns [Hash] environment variables +# @param options [Hash] :registers, :strict_errors, :exception_renderer +LiquidSpec.render do |template, assigns, options| + registers = Liquid::Registers.new(options[:registers] || {}) + + context = Liquid::Context.build( + static_environments: assigns, + registers: registers, + rethrow_errors: options[:strict_errors], + ) + + context.exception_renderer = options[:exception_renderer] if options[:exception_renderer] + + template.render(context) +end