Sha256: d07403b5d2a4aae0c23562cf3b704d4dc4851c2959e3750bb6c8925085a8ebfd

Contents?: true

Size: 1.93 KB

Versions: 18

Compression:

Stored size: 1.93 KB

Contents

# -*- coding: utf-8 -*-
require "test_helper"

class ExpressionTest < Sablon::TestCase
end

class VariableExpressionTest < Sablon::TestCase
  def test_lookup_the_variable_in_the_context
    expr = Sablon::Expression.parse("first_name")
    assert_equal "Jane", expr.evaluate("first_name" => "Jane", "last_name" => "Doe")
  end

  def test_inspect
    expr = Sablon::Expression.parse("first_name")
    assert_equal "«first_name»", expr.inspect
  end
end

class LookupOrMethodCallTest < Sablon::TestCase
  def test_calls_method_on_object
    user = OpenStruct.new(first_name: "Jack")
    expr = Sablon::Expression.parse("user.first_name")
    assert_equal "Jack", expr.evaluate("user" => user)
  end

  def test_calls_perform_lookup_on_hash_with_string_keys
    user = {"first_name" => "Jack"}
    expr = Sablon::Expression.parse("user.first_name")
    assert_equal "Jack", expr.evaluate("user" => user)
  end

  def test_inspect
    expr = Sablon::Expression.parse("user.first_name")
    assert_equal "«user.first_name»", expr.inspect
  end

  def test_calls_chained_methods
    user = OpenStruct.new(first_name: "Jack", address: OpenStruct.new(line_1: "55A"))
    expr = Sablon::Expression.parse("user.address.line_1")
    assert_equal "55A", expr.evaluate("user" => user)
  end

  def test_nested_hash_lookup
    user = {"address" => {"line_1" => "55A"}}
    expr = Sablon::Expression.parse("user.address.line_1")
    assert_equal "55A", expr.evaluate("user" => user)
  end

  def test_mix_hash_lookup_and_method_calls
    user = OpenStruct.new(address: {"country" => OpenStruct.new(name: "Switzerland")})
    expr = Sablon::Expression.parse("user.address.country.name")
    assert_equal "Switzerland", expr.evaluate("user" => user)
  end

  def test_missing_receiver
    user = OpenStruct.new(first_name: "Jack")
    expr = Sablon::Expression.parse("user.address.line_1")
    assert_equal nil, expr.evaluate("user" => user)
    assert_equal nil, expr.evaluate({})
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
sablon-0.0.21 test/expression_test.rb
sablon-0.0.20 test/expression_test.rb
sablon-0.0.19 test/expression_test.rb
sablon-0.0.19.beta5 test/expression_test.rb
sablon-0.0.19.beta4 test/expression_test.rb
sablon-0.0.19.beta3 test/expression_test.rb
sablon-0.0.19.beta2 test/expression_test.rb
sablon-0.0.19.beta1 test/expression_test.rb
sablon-0.0.18 test/expression_test.rb
sablon-0.0.17 test/expression_test.rb
sablon-0.0.16 test/expression_test.rb
sablon-0.0.15 test/expression_test.rb
sablon-0.0.14 test/expression_test.rb
sablon-0.0.13 test/expression_test.rb
sablon-0.0.12 test/expression_test.rb
sablon-0.0.11 test/expression_test.rb
sablon-0.0.10 test/expression_test.rb
sablon-0.0.9 test/expression_test.rb