Sha256: a52f7f9cfe8a8866d6cb4b0b2c1df1c662b06b3fecab1ba1753865067122420a

Contents?: true

Size: 1.69 KB

Versions: 5

Compression:

Stored size: 1.69 KB

Contents

require '../test_helper'
require 'test/unit'
require 'grizzled/string/template'

include Grizzled::String::Template

module TemplateTestDriver

  def do_safe_expansion(template_class, resolver, test_data)
    u = template_class.new(resolver, :safe => true)
    test_data.each do |string, expected, has_missing|
      assert_equal(expected, u.substitute(string))
    end
  end

  def do_unsafe_expansion(template_class, resolver, test_data)
    u = template_class.new(resolver, :safe => false)
    test_data.each do |string, expected, has_missing|
      if has_missing
        assert_raise(VariableNotFoundException) do
          u.substitute(string)
        end
      end
    end
  end
end

class TestUnixShellStringTemplate < Test::Unit::TestCase

  include TemplateTestDriver

  RESOLVER  = {"a" => "alpha", "foo" => "FOOBAR"}
  TEST_DATA = 
    [
     ['${a} $foo ${b?bdef} $b ${foo}\$x', 'alpha FOOBAR bdef  FOOBAR$x', true],
     ['\$a $foo $b', '$a FOOBAR ', true],
     ['$a', 'alpha', false]
    ]

  def test_safe_expansion
    do_safe_expansion(UnixShellStringTemplate, RESOLVER, TEST_DATA)
  end

  def test_unsafe_expansion
    do_unsafe_expansion(UnixShellStringTemplate, RESOLVER, TEST_DATA)
  end
end

class TestWindowsCmdStringTemplate < Test::Unit::TestCase

  include TemplateTestDriver

  RESOLVER  = {"a" => "alpha", "foo" => "FOOBAR"}
  TEST_DATA = 
    [
     ['%a% %foo% %b% %foo%\%x', 'alpha FOOBAR  FOOBAR%x', true],
     ['\%a% %foo% %b%', '%a% FOOBAR ', true],
     ['%a%', 'alpha', false]
    ]

  def test_safe_expansion
    do_safe_expansion(WindowsCmdStringTemplate, RESOLVER, TEST_DATA)
  end

  def test_unsafe_expansion
    do_unsafe_expansion(WindowsCmdStringTemplate, RESOLVER, TEST_DATA)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
grizzled-ruby-0.1.9 test/string/tc_template.rb
grizzled-ruby-0.1.8 test/string/tc_template.rb
grizzled-ruby-0.1.7 test/string/tc_template.rb
grizzled-ruby-0.1.4 test/string/tc_template.rb
grizzled-ruby-0.1.3 test/string/tc_template.rb